OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket/tcp_server_socket_libevent.h" | 5 #include "net/socket/tcp_server_socket_libevent.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <netdb.h> | 9 #include <netdb.h> |
10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 const int kInvalidSocket = -1; | 29 const int kInvalidSocket = -1; |
30 | 30 |
31 } // namespace | 31 } // namespace |
32 | 32 |
33 TCPServerSocketLibevent::TCPServerSocketLibevent( | 33 TCPServerSocketLibevent::TCPServerSocketLibevent( |
34 net::NetLog* net_log, | 34 net::NetLog* net_log, |
35 const net::NetLog::Source& source) | 35 const net::NetLog::Source& source) |
36 : socket_(kInvalidSocket), | 36 : socket_(kInvalidSocket), |
37 accept_socket_(NULL), | 37 accept_socket_(NULL), |
| 38 reuse_address_(false), |
38 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { | 39 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { |
39 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, | 40 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, |
40 source.ToEventParametersCallback()); | 41 source.ToEventParametersCallback()); |
41 } | 42 } |
42 | 43 |
43 TCPServerSocketLibevent::~TCPServerSocketLibevent() { | 44 TCPServerSocketLibevent::~TCPServerSocketLibevent() { |
44 if (socket_ != kInvalidSocket) | 45 if (socket_ != kInvalidSocket) |
45 Close(); | 46 Close(); |
46 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); | 47 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); |
47 } | 48 } |
48 | 49 |
| 50 void TCPServerSocketLibevent::AllowAddressReuse() { |
| 51 DCHECK(CalledOnValidThread()); |
| 52 DCHECK_EQ(socket_, kInvalidSocket); |
| 53 |
| 54 reuse_address_ = true; |
| 55 } |
| 56 |
49 int TCPServerSocketLibevent::Listen(const IPEndPoint& address, int backlog) { | 57 int TCPServerSocketLibevent::Listen(const IPEndPoint& address, int backlog) { |
50 DCHECK(CalledOnValidThread()); | 58 DCHECK(CalledOnValidThread()); |
51 DCHECK_GT(backlog, 0); | 59 DCHECK_GT(backlog, 0); |
52 DCHECK_EQ(socket_, kInvalidSocket); | 60 DCHECK_EQ(socket_, kInvalidSocket); |
53 | 61 |
54 socket_ = socket(address.GetFamily(), SOCK_STREAM, IPPROTO_TCP); | 62 socket_ = socket(address.GetFamily(), SOCK_STREAM, IPPROTO_TCP); |
55 if (socket_ < 0) { | 63 if (socket_ < 0) { |
56 PLOG(ERROR) << "socket() returned an error"; | 64 PLOG(ERROR) << "socket() returned an error"; |
57 return MapSystemError(errno); | 65 return MapSystemError(errno); |
58 } | 66 } |
59 | 67 |
60 if (SetNonBlocking(socket_)) { | 68 if (SetNonBlocking(socket_)) { |
61 int result = MapSystemError(errno); | 69 int result = MapSystemError(errno); |
62 Close(); | 70 Close(); |
63 return result; | 71 return result; |
64 } | 72 } |
65 | 73 |
| 74 int result = SetSocketOptions(); |
| 75 if (result != OK) |
| 76 return result; |
| 77 |
66 SockaddrStorage storage; | 78 SockaddrStorage storage; |
67 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) | 79 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
68 return ERR_INVALID_ARGUMENT; | 80 return ERR_INVALID_ARGUMENT; |
69 | 81 |
70 int result = bind(socket_, storage.addr, storage.addr_len); | 82 result = bind(socket_, storage.addr, storage.addr_len); |
71 if (result < 0) { | 83 if (result < 0) { |
72 PLOG(ERROR) << "bind() returned an error"; | 84 PLOG(ERROR) << "bind() returned an error"; |
73 result = MapSystemError(errno); | 85 result = MapSystemError(errno); |
74 Close(); | 86 Close(); |
75 return result; | 87 return result; |
76 } | 88 } |
77 | 89 |
78 result = listen(socket_, backlog); | 90 result = listen(socket_, backlog); |
79 if (result < 0) { | 91 if (result < 0) { |
80 PLOG(ERROR) << "listen() returned an error"; | 92 PLOG(ERROR) << "listen() returned an error"; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 return MapSystemError(errno); | 130 return MapSystemError(errno); |
119 } | 131 } |
120 | 132 |
121 accept_socket_ = socket; | 133 accept_socket_ = socket; |
122 accept_callback_ = callback; | 134 accept_callback_ = callback; |
123 } | 135 } |
124 | 136 |
125 return result; | 137 return result; |
126 } | 138 } |
127 | 139 |
| 140 int TCPServerSocketLibevent::SetSocketOptions() { |
| 141 int true_value = 1; |
| 142 if (reuse_address_) { |
| 143 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &true_value, |
| 144 sizeof(true_value)); |
| 145 if (rv < 0) |
| 146 return MapSystemError(errno); |
| 147 } |
| 148 return OK; |
| 149 } |
| 150 |
128 int TCPServerSocketLibevent::AcceptInternal( | 151 int TCPServerSocketLibevent::AcceptInternal( |
129 scoped_ptr<StreamSocket>* socket) { | 152 scoped_ptr<StreamSocket>* socket) { |
130 SockaddrStorage storage; | 153 SockaddrStorage storage; |
131 int new_socket = HANDLE_EINTR(accept(socket_, | 154 int new_socket = HANDLE_EINTR(accept(socket_, |
132 storage.addr, | 155 storage.addr, |
133 &storage.addr_len)); | 156 &storage.addr_len)); |
134 if (new_socket < 0) { | 157 if (new_socket < 0) { |
135 int net_error = MapSystemError(errno); | 158 int net_error = MapSystemError(errno); |
136 if (net_error != ERR_IO_PENDING) | 159 if (net_error != ERR_IO_PENDING) |
137 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); | 160 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 accept_callback_.Reset(); | 207 accept_callback_.Reset(); |
185 callback.Run(result); | 208 callback.Run(result); |
186 } | 209 } |
187 } | 210 } |
188 | 211 |
189 void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { | 212 void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { |
190 NOTREACHED(); | 213 NOTREACHED(); |
191 } | 214 } |
192 | 215 |
193 } // namespace net | 216 } // namespace net |
OLD | NEW |