Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(286)

Side by Side Diff: net/socket/tcp_socket_libevent.cc

Issue 22861033: Move server socket functionality from TCPServerSocket into TCPSocket. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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_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>
11 11
12 #include "build/build_config.h" 12 #include "build/build_config.h"
13 13
14 #if defined(OS_POSIX) 14 #if defined(OS_POSIX)
15 #include <netinet/in.h> 15 #include <netinet/in.h>
16 #endif 16 #endif
17 17
18 #include "base/logging.h"
18 #include "base/posix/eintr_wrapper.h" 19 #include "base/posix/eintr_wrapper.h"
19 #include "net/base/ip_endpoint.h" 20 #include "net/base/ip_endpoint.h"
20 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
21 #include "net/base/net_util.h" 22 #include "net/base/net_util.h"
22 #include "net/socket/socket_net_log_params.h" 23 #include "net/socket/socket_net_log_params.h"
23 #include "net/socket/tcp_client_socket.h"
24 24
25 namespace net { 25 namespace net {
26 26
27 namespace { 27 namespace {
28 28
29 const int kInvalidSocket = -1; 29 const int kInvalidSocket = -1;
30 30
31 } // namespace 31 } // namespace
32 32
33 TCPServerSocketLibevent::TCPServerSocketLibevent( 33 TCPSocketLibevent::TCPSocketLibevent(NetLog* net_log,
34 net::NetLog* net_log, 34 const NetLog::Source& source)
35 const net::NetLog::Source& source)
36 : socket_(kInvalidSocket), 35 : socket_(kInvalidSocket),
37 accept_socket_(NULL), 36 accept_socket_(NULL),
37 accept_address_(NULL),
38 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { 38 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) {
39 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, 39 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE,
40 source.ToEventParametersCallback()); 40 source.ToEventParametersCallback());
41 } 41 }
42 42
43 TCPServerSocketLibevent::~TCPServerSocketLibevent() { 43 TCPSocketLibevent::~TCPSocketLibevent() {
44 if (socket_ != kInvalidSocket) 44 if (socket_ != kInvalidSocket)
45 Close(); 45 Close();
46 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); 46 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE);
47 } 47 }
48 48
49 int TCPServerSocketLibevent::Listen(const IPEndPoint& address, int backlog) { 49 int TCPSocketLibevent::Create(AddressFamily family) {
50 DCHECK(CalledOnValidThread()); 50 DCHECK(CalledOnValidThread());
51 DCHECK_GT(backlog, 0);
52 DCHECK_EQ(socket_, kInvalidSocket); 51 DCHECK_EQ(socket_, kInvalidSocket);
53 52
54 socket_ = socket(address.GetSockAddrFamily(), SOCK_STREAM, IPPROTO_TCP); 53 int address_family = AF_UNSPEC;
54 switch (family) {
55 case ADDRESS_FAMILY_IPV4:
56 address_family = AF_INET;
57 break;
58 case ADDRESS_FAMILY_IPV6:
59 address_family = AF_INET6;
60 break;
61 default:
akalin 2013/08/29 22:08:33 suggest removing the default case so that in case
yzshen1 2013/08/29 23:04:18 Done.
62 NOTREACHED();
63 }
64 socket_ = socket(address_family, SOCK_STREAM, IPPROTO_TCP);
55 if (socket_ < 0) { 65 if (socket_ < 0) {
56 PLOG(ERROR) << "socket() returned an error"; 66 PLOG(ERROR) << "socket() returned an error";
57 return MapSystemError(errno); 67 return MapSystemError(errno);
58 } 68 }
59 69
60 if (SetNonBlocking(socket_)) { 70 if (SetNonBlocking(socket_)) {
61 int result = MapSystemError(errno); 71 int result = MapSystemError(errno);
62 Close(); 72 Close();
63 return result; 73 return result;
64 } 74 }
65 75
66 int result = SetSocketOptions(); 76 return OK;
67 if (result != OK) { 77 }
68 Close();
69 return result;
70 }
71 78
72 SockaddrStorage storage; 79 int TCPSocketLibevent::Adopt(int socket) {
73 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) { 80 DCHECK(CalledOnValidThread());
74 Close(); 81 DCHECK_EQ(socket_, kInvalidSocket);
75 return ERR_ADDRESS_INVALID;
76 }
77 82
78 result = bind(socket_, storage.addr, storage.addr_len); 83 socket_ = socket;
79 if (result < 0) {
80 PLOG(ERROR) << "bind() returned an error";
81 result = MapSystemError(errno);
82 Close();
83 return result;
84 }
85 84
86 result = listen(socket_, backlog); 85 if (SetNonBlocking(socket_)) {
87 if (result < 0) { 86 int result = MapSystemError(errno);
88 PLOG(ERROR) << "listen() returned an error";
89 result = MapSystemError(errno);
90 Close(); 87 Close();
91 return result; 88 return result;
92 } 89 }
93 90
94 return OK; 91 return OK;
95 } 92 }
96 93
97 int TCPServerSocketLibevent::GetLocalAddress(IPEndPoint* address) const { 94 int TCPSocketLibevent::Release() {
95 DCHECK(CalledOnValidThread());
96 DCHECK(accept_callback_.is_null());
97
98 int result = socket_;
99 socket_ = kInvalidSocket;
100 return result;
101 }
102
103 int TCPSocketLibevent::Bind(const IPEndPoint& address) {
104 DCHECK(CalledOnValidThread());
105 DCHECK_NE(socket_, kInvalidSocket);
106
107 SockaddrStorage storage;
108 if (!address.ToSockAddr(storage.addr, &storage.addr_len))
109 return ERR_ADDRESS_INVALID;
110
111 int result = bind(socket_, storage.addr, storage.addr_len);
112 if (result < 0) {
113 PLOG(ERROR) << "bind() returned an error";
114 return MapSystemError(errno);
115 }
116
117 return OK;
118 }
119
120 int TCPSocketLibevent::GetLocalAddress(IPEndPoint* address) const {
98 DCHECK(CalledOnValidThread()); 121 DCHECK(CalledOnValidThread());
99 DCHECK(address); 122 DCHECK(address);
100 123
101 SockaddrStorage storage; 124 SockaddrStorage storage;
102 if (getsockname(socket_, storage.addr, &storage.addr_len) < 0) 125 if (getsockname(socket_, storage.addr, &storage.addr_len) < 0)
103 return MapSystemError(errno); 126 return MapSystemError(errno);
104 if (!address->FromSockAddr(storage.addr, storage.addr_len)) 127 if (!address->FromSockAddr(storage.addr, storage.addr_len))
105 return ERR_FAILED; 128 return ERR_FAILED;
106 129
107 return OK; 130 return OK;
108 } 131 }
109 132
110 int TCPServerSocketLibevent::Accept( 133 int TCPSocketLibevent::Listen(int backlog) {
111 scoped_ptr<StreamSocket>* socket, const CompletionCallback& callback) { 134 DCHECK(CalledOnValidThread());
135 DCHECK_GT(backlog, 0);
136 DCHECK_NE(socket_, kInvalidSocket);
137
138 int result = listen(socket_, backlog);
139 if (result < 0) {
140 PLOG(ERROR) << "listen() returned an error";
141 return MapSystemError(errno);
142 }
143
144 return OK;
145 }
146
147 int TCPSocketLibevent::Accept(scoped_ptr<TCPSocketLibevent>* socket,
148 IPEndPoint* address,
149 const CompletionCallback& callback) {
112 DCHECK(CalledOnValidThread()); 150 DCHECK(CalledOnValidThread());
113 DCHECK(socket); 151 DCHECK(socket);
114 DCHECK(!callback.is_null()); 152 DCHECK(!callback.is_null());
115 DCHECK(accept_callback_.is_null()); 153 DCHECK(accept_callback_.is_null());
116 154
117 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT); 155 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT);
118 156
119 int result = AcceptInternal(socket); 157 int result = AcceptInternal(socket, address);
120 158
121 if (result == ERR_IO_PENDING) { 159 if (result == ERR_IO_PENDING) {
122 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( 160 if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
123 socket_, true, base::MessageLoopForIO::WATCH_READ, 161 socket_, true, base::MessageLoopForIO::WATCH_READ,
124 &accept_socket_watcher_, this)) { 162 &accept_socket_watcher_, this)) {
125 PLOG(ERROR) << "WatchFileDescriptor failed on read"; 163 PLOG(ERROR) << "WatchFileDescriptor failed on read";
126 return MapSystemError(errno); 164 return MapSystemError(errno);
127 } 165 }
128 166
129 accept_socket_ = socket; 167 accept_socket_ = socket;
168 accept_address_ = address;
130 accept_callback_ = callback; 169 accept_callback_ = callback;
131 } 170 }
132 171
133 return result; 172 return result;
134 } 173 }
135 174
136 int TCPServerSocketLibevent::SetSocketOptions() { 175 int TCPSocketLibevent::SetDefaultOptionsForServer() {
176 return SetAddressReuse(true);
177 }
178
179 int TCPSocketLibevent::SetAddressReuse(bool allow) {
137 // SO_REUSEADDR is useful for server sockets to bind to a recently unbound 180 // SO_REUSEADDR is useful for server sockets to bind to a recently unbound
138 // port. When a socket is closed, the end point changes its state to TIME_WAIT 181 // port. When a socket is closed, the end point changes its state to TIME_WAIT
139 // and wait for 2 MSL (maximum segment lifetime) to ensure the remote peer 182 // and wait for 2 MSL (maximum segment lifetime) to ensure the remote peer
140 // acknowledges its closure. For server sockets, it is usually safe to 183 // acknowledges its closure. For server sockets, it is usually safe to
141 // bind to a TIME_WAIT end point immediately, which is a widely adopted 184 // bind to a TIME_WAIT end point immediately, which is a widely adopted
142 // behavior. 185 // behavior.
143 // 186 //
144 // Note that on *nix, SO_REUSEADDR does not enable the TCP socket to bind to 187 // Note that on *nix, SO_REUSEADDR does not enable the TCP socket to bind to
145 // an end point that is already bound by another socket. To do that one must 188 // an end point that is already bound by another socket. To do that one must
146 // set SO_REUSEPORT instead. This option is not provided on Linux prior 189 // set SO_REUSEPORT instead. This option is not provided on Linux prior
147 // to 3.9. 190 // to 3.9.
148 // 191 //
149 // SO_REUSEPORT is provided in MacOS X and iOS. 192 // SO_REUSEPORT is provided in MacOS X and iOS.
150 int true_value = 1; 193 int boolean_value = allow ? 1 : 0;
151 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &true_value, 194 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &boolean_value,
152 sizeof(true_value)); 195 sizeof(boolean_value));
153 if (rv < 0) 196 if (rv < 0)
154 return MapSystemError(errno); 197 return MapSystemError(errno);
155 return OK; 198 return OK;
156 } 199 }
157 200
158 int TCPServerSocketLibevent::AcceptInternal( 201 void TCPSocketLibevent::Close() {
159 scoped_ptr<StreamSocket>* socket) { 202 if (socket_ != kInvalidSocket) {
203 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor();
204 DCHECK(ok);
205 if (HANDLE_EINTR(close(socket_)) < 0)
206 PLOG(ERROR) << "close";
207 socket_ = kInvalidSocket;
208 }
209 }
210
211 int TCPSocketLibevent::AcceptInternal(scoped_ptr<TCPSocketLibevent>* socket,
212 IPEndPoint* address) {
160 SockaddrStorage storage; 213 SockaddrStorage storage;
161 int new_socket = HANDLE_EINTR(accept(socket_, 214 int new_socket = HANDLE_EINTR(accept(socket_,
162 storage.addr, 215 storage.addr,
163 &storage.addr_len)); 216 &storage.addr_len));
164 if (new_socket < 0) { 217 if (new_socket < 0) {
165 int net_error = MapSystemError(errno); 218 int net_error = MapSystemError(errno);
166 if (net_error != ERR_IO_PENDING) 219 if (net_error != ERR_IO_PENDING)
167 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); 220 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error);
168 return net_error; 221 return net_error;
169 } 222 }
170 223
171 IPEndPoint address; 224 IPEndPoint ip_end_point;
172 if (!address.FromSockAddr(storage.addr, storage.addr_len)) { 225 if (!ip_end_point.FromSockAddr(storage.addr, storage.addr_len)) {
173 NOTREACHED(); 226 NOTREACHED();
174 if (HANDLE_EINTR(close(new_socket)) < 0) 227 if (HANDLE_EINTR(close(new_socket)) < 0)
175 PLOG(ERROR) << "close"; 228 PLOG(ERROR) << "close";
176 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED); 229 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED);
177 return ERR_FAILED; 230 return ERR_FAILED;
178 } 231 }
179 scoped_ptr<TCPClientSocket> tcp_socket(new TCPClientSocket( 232 scoped_ptr<TCPSocketLibevent> tcp_socket(new TCPSocketLibevent(
180 AddressList(address),
181 net_log_.net_log(), net_log_.source())); 233 net_log_.net_log(), net_log_.source()));
182 int adopt_result = tcp_socket->AdoptSocket(new_socket); 234 int adopt_result = tcp_socket->Adopt(new_socket);
183 if (adopt_result != OK) { 235 if (adopt_result != OK) {
184 if (HANDLE_EINTR(close(new_socket)) < 0)
185 PLOG(ERROR) << "close";
186 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); 236 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result);
187 return adopt_result; 237 return adopt_result;
188 } 238 }
189 socket->reset(tcp_socket.release()); 239 *socket = tcp_socket.Pass();
240 if (address)
akalin 2013/08/29 22:08:33 is this check necessary? can't you mandate that |a
yzshen1 2013/08/29 23:04:18 I was trying to follow POSIX accept(); its output
241 *address = ip_end_point;
190 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, 242 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT,
191 CreateNetLogIPEndPointCallback(&address)); 243 CreateNetLogIPEndPointCallback(&ip_end_point));
192 return OK; 244 return OK;
193 } 245 }
194 246
195 void TCPServerSocketLibevent::Close() { 247 void TCPSocketLibevent::OnFileCanReadWithoutBlocking(int fd) {
196 if (socket_ != kInvalidSocket) {
197 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor();
198 DCHECK(ok);
199 if (HANDLE_EINTR(close(socket_)) < 0)
200 PLOG(ERROR) << "close";
201 socket_ = kInvalidSocket;
202 }
203 }
204
205 void TCPServerSocketLibevent::OnFileCanReadWithoutBlocking(int fd) {
206 DCHECK(CalledOnValidThread()); 248 DCHECK(CalledOnValidThread());
207 249
208 int result = AcceptInternal(accept_socket_); 250 int result = AcceptInternal(accept_socket_, accept_address_);
209 if (result != ERR_IO_PENDING) { 251 if (result != ERR_IO_PENDING) {
210 accept_socket_ = NULL; 252 accept_socket_ = NULL;
253 accept_address_ = NULL;
211 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor(); 254 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor();
212 DCHECK(ok); 255 DCHECK(ok);
213 CompletionCallback callback = accept_callback_; 256 CompletionCallback callback = accept_callback_;
214 accept_callback_.Reset(); 257 accept_callback_.Reset();
215 callback.Run(result); 258 callback.Run(result);
216 } 259 }
217 } 260 }
218 261
219 void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { 262 void TCPSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) {
220 NOTREACHED(); 263 NOTREACHED();
221 } 264 }
222 265
223 } // namespace net 266 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698