Chromium Code Reviews| 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 |
| 49 int TCPServerSocketLibevent::Listen(const IPEndPoint& address, int backlog) { | 50 int TCPServerSocketLibevent::Listen(const IPEndPoint& address, int backlog) { |
| 50 DCHECK(CalledOnValidThread()); | 51 DCHECK(CalledOnValidThread()); |
| 51 DCHECK_GT(backlog, 0); | 52 DCHECK_GT(backlog, 0); |
| 52 DCHECK_EQ(socket_, kInvalidSocket); | 53 DCHECK_EQ(socket_, kInvalidSocket); |
| 53 | 54 |
| 54 socket_ = socket(address.GetFamily(), SOCK_STREAM, IPPROTO_TCP); | 55 socket_ = socket(address.GetFamily(), SOCK_STREAM, IPPROTO_TCP); |
| 55 if (socket_ < 0) { | 56 if (socket_ < 0) { |
| 56 PLOG(ERROR) << "socket() returned an error"; | 57 PLOG(ERROR) << "socket() returned an error"; |
| 57 return MapSystemError(errno); | 58 return MapSystemError(errno); |
| 58 } | 59 } |
| 59 | 60 |
| 60 if (SetNonBlocking(socket_)) { | 61 if (SetNonBlocking(socket_)) { |
| 61 int result = MapSystemError(errno); | 62 int result = MapSystemError(errno); |
| 62 Close(); | 63 Close(); |
| 63 return result; | 64 return result; |
| 64 } | 65 } |
| 65 | 66 |
| 67 int result = SetSocketOptions(); | |
| 68 if (result < 0) { | |
|
wtc
2012/09/13 18:31:55
result < 0 => result != OK
Note: although SetSock
justinlin
2012/09/14 03:54:40
Done. Nice catch.
| |
| 69 return result; | |
| 70 } | |
| 71 | |
| 66 SockaddrStorage storage; | 72 SockaddrStorage storage; |
| 67 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) | 73 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
| 68 return ERR_INVALID_ARGUMENT; | 74 return ERR_INVALID_ARGUMENT; |
| 69 | 75 |
| 70 int result = bind(socket_, storage.addr, storage.addr_len); | 76 result = bind(socket_, storage.addr, storage.addr_len); |
| 71 if (result < 0) { | 77 if (result < 0) { |
| 72 PLOG(ERROR) << "bind() returned an error"; | 78 PLOG(ERROR) << "bind() returned an error"; |
| 73 result = MapSystemError(errno); | 79 result = MapSystemError(errno); |
| 74 Close(); | 80 Close(); |
| 75 return result; | 81 return result; |
| 76 } | 82 } |
| 77 | 83 |
| 78 result = listen(socket_, backlog); | 84 result = listen(socket_, backlog); |
| 79 if (result < 0) { | 85 if (result < 0) { |
| 80 PLOG(ERROR) << "listen() returned an error"; | 86 PLOG(ERROR) << "listen() returned an error"; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 return MapSystemError(errno); | 124 return MapSystemError(errno); |
| 119 } | 125 } |
| 120 | 126 |
| 121 accept_socket_ = socket; | 127 accept_socket_ = socket; |
| 122 accept_callback_ = callback; | 128 accept_callback_ = callback; |
| 123 } | 129 } |
| 124 | 130 |
| 125 return result; | 131 return result; |
| 126 } | 132 } |
| 127 | 133 |
| 134 void TCPServerSocketLibevent::AllowAddressReuse() { | |
| 135 DCHECK(CalledOnValidThread()); | |
| 136 DCHECK_EQ(socket_, kInvalidSocket); | |
| 137 | |
| 138 reuse_address_ = true; | |
| 139 } | |
| 140 | |
| 141 int TCPServerSocketLibevent::SetSocketOptions() { | |
| 142 int true_value = 1; | |
| 143 if (reuse_address_) { | |
| 144 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &true_value, | |
| 145 sizeof(true_value)); | |
| 146 if (rv < 0) | |
| 147 return MapSystemError(errno); | |
|
wtc
2012/09/13 18:31:55
Why didn't you copy the following code from tcp_cl
Sergey Ulanov
2012/09/13 22:08:45
I actually don't think we really need this. Yuri a
wtc
2012/09/14 02:36:12
Thank you. Then we should not copy the SO_REUSEPOR
| |
| 148 } | |
| 149 return OK; | |
| 150 } | |
| 151 | |
| 128 int TCPServerSocketLibevent::AcceptInternal( | 152 int TCPServerSocketLibevent::AcceptInternal( |
| 129 scoped_ptr<StreamSocket>* socket) { | 153 scoped_ptr<StreamSocket>* socket) { |
| 130 SockaddrStorage storage; | 154 SockaddrStorage storage; |
| 131 int new_socket = HANDLE_EINTR(accept(socket_, | 155 int new_socket = HANDLE_EINTR(accept(socket_, |
| 132 storage.addr, | 156 storage.addr, |
| 133 &storage.addr_len)); | 157 &storage.addr_len)); |
| 134 if (new_socket < 0) { | 158 if (new_socket < 0) { |
| 135 int net_error = MapSystemError(errno); | 159 int net_error = MapSystemError(errno); |
| 136 if (net_error != ERR_IO_PENDING) | 160 if (net_error != ERR_IO_PENDING) |
| 137 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); | 161 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(); | 208 accept_callback_.Reset(); |
| 185 callback.Run(result); | 209 callback.Run(result); |
| 186 } | 210 } |
| 187 } | 211 } |
| 188 | 212 |
| 189 void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { | 213 void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { |
| 190 NOTREACHED(); | 214 NOTREACHED(); |
| 191 } | 215 } |
| 192 | 216 |
| 193 } // namespace net | 217 } // namespace net |
| OLD | NEW |