| OLD | NEW |
| 1 // Copyright (c) 2011 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> |
| 11 | 11 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 PLOG(ERROR) << "socket() returned an error"; | 57 PLOG(ERROR) << "socket() returned an error"; |
| 58 return MapSystemError(errno); | 58 return MapSystemError(errno); |
| 59 } | 59 } |
| 60 | 60 |
| 61 if (SetNonBlocking(socket_)) { | 61 if (SetNonBlocking(socket_)) { |
| 62 int result = MapSystemError(errno); | 62 int result = MapSystemError(errno); |
| 63 Close(); | 63 Close(); |
| 64 return result; | 64 return result; |
| 65 } | 65 } |
| 66 | 66 |
| 67 struct sockaddr_storage addr_storage; | 67 SockaddrStorage storage; |
| 68 size_t addr_len = sizeof(addr_storage); | 68 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
| 69 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); | |
| 70 if (!address.ToSockAddr(addr, &addr_len)) | |
| 71 return ERR_INVALID_ARGUMENT; | 69 return ERR_INVALID_ARGUMENT; |
| 72 | 70 |
| 73 int result = bind(socket_, addr, addr_len); | 71 int result = bind(socket_, storage.addr, storage.addr_len); |
| 74 if (result < 0) { | 72 if (result < 0) { |
| 75 PLOG(ERROR) << "bind() returned an error"; | 73 PLOG(ERROR) << "bind() returned an error"; |
| 76 result = MapSystemError(errno); | 74 result = MapSystemError(errno); |
| 77 Close(); | 75 Close(); |
| 78 return result; | 76 return result; |
| 79 } | 77 } |
| 80 | 78 |
| 81 result = listen(socket_, backlog); | 79 result = listen(socket_, backlog); |
| 82 if (result < 0) { | 80 if (result < 0) { |
| 83 PLOG(ERROR) << "listen() returned an error"; | 81 PLOG(ERROR) << "listen() returned an error"; |
| 84 result = MapSystemError(errno); | 82 result = MapSystemError(errno); |
| 85 Close(); | 83 Close(); |
| 86 return result; | 84 return result; |
| 87 } | 85 } |
| 88 | 86 |
| 89 return OK; | 87 return OK; |
| 90 } | 88 } |
| 91 | 89 |
| 92 int TCPServerSocketLibevent::GetLocalAddress(IPEndPoint* address) const { | 90 int TCPServerSocketLibevent::GetLocalAddress(IPEndPoint* address) const { |
| 93 DCHECK(CalledOnValidThread()); | 91 DCHECK(CalledOnValidThread()); |
| 94 DCHECK(address); | 92 DCHECK(address); |
| 95 | 93 |
| 96 struct sockaddr_storage addr_storage; | 94 SockaddrStorage storage; |
| 97 socklen_t addr_len = sizeof(addr_storage); | 95 if (getsockname(socket_, storage.addr, &storage.addr_len) < 0) |
| 98 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); | |
| 99 if (getsockname(socket_, addr, &addr_len) < 0) | |
| 100 return MapSystemError(errno); | 96 return MapSystemError(errno); |
| 101 if (!address->FromSockAddr(addr, addr_len)) | 97 if (!address->FromSockAddr(storage.addr, storage.addr_len)) |
| 102 return ERR_FAILED; | 98 return ERR_FAILED; |
| 103 | 99 |
| 104 return OK; | 100 return OK; |
| 105 } | 101 } |
| 106 | 102 |
| 107 int TCPServerSocketLibevent::Accept( | 103 int TCPServerSocketLibevent::Accept( |
| 108 scoped_ptr<StreamSocket>* socket, const CompletionCallback& callback) { | 104 scoped_ptr<StreamSocket>* socket, const CompletionCallback& callback) { |
| 109 DCHECK(CalledOnValidThread()); | 105 DCHECK(CalledOnValidThread()); |
| 110 DCHECK(socket); | 106 DCHECK(socket); |
| 111 DCHECK(!callback.is_null()); | 107 DCHECK(!callback.is_null()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 125 | 121 |
| 126 accept_socket_ = socket; | 122 accept_socket_ = socket; |
| 127 accept_callback_ = callback; | 123 accept_callback_ = callback; |
| 128 } | 124 } |
| 129 | 125 |
| 130 return result; | 126 return result; |
| 131 } | 127 } |
| 132 | 128 |
| 133 int TCPServerSocketLibevent::AcceptInternal( | 129 int TCPServerSocketLibevent::AcceptInternal( |
| 134 scoped_ptr<StreamSocket>* socket) { | 130 scoped_ptr<StreamSocket>* socket) { |
| 135 struct sockaddr_storage addr_storage; | 131 SockaddrStorage storage; |
| 136 socklen_t addr_len = sizeof(addr_storage); | 132 int new_socket = HANDLE_EINTR(accept(socket_, |
| 137 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); | 133 storage.addr, |
| 138 | 134 &storage.addr_len)); |
| 139 int new_socket = HANDLE_EINTR(accept(socket_, addr, &addr_len)); | |
| 140 if (new_socket < 0) { | 135 if (new_socket < 0) { |
| 141 int net_error = MapSystemError(errno); | 136 int net_error = MapSystemError(errno); |
| 142 if (net_error != ERR_IO_PENDING) | 137 if (net_error != ERR_IO_PENDING) |
| 143 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); | 138 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); |
| 144 return net_error; | 139 return net_error; |
| 145 } | 140 } |
| 146 | 141 |
| 147 IPEndPoint address; | 142 IPEndPoint address; |
| 148 if (!address.FromSockAddr(addr, addr_len)) { | 143 if (!address.FromSockAddr(storage.addr, storage.addr_len)) { |
| 149 NOTREACHED(); | 144 NOTREACHED(); |
| 150 if (HANDLE_EINTR(close(new_socket)) < 0) | 145 if (HANDLE_EINTR(close(new_socket)) < 0) |
| 151 PLOG(ERROR) << "close"; | 146 PLOG(ERROR) << "close"; |
| 152 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED); | 147 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED); |
| 153 return ERR_FAILED; | 148 return ERR_FAILED; |
| 154 } | 149 } |
| 155 scoped_ptr<TCPClientSocket> tcp_socket(new TCPClientSocket( | 150 scoped_ptr<TCPClientSocket> tcp_socket(new TCPClientSocket( |
| 156 AddressList::CreateFromIPAddress(address.address(), address.port()), | 151 AddressList(address), |
| 157 net_log_.net_log(), net_log_.source())); | 152 net_log_.net_log(), net_log_.source())); |
| 158 int adopt_result = tcp_socket->AdoptSocket(new_socket); | 153 int adopt_result = tcp_socket->AdoptSocket(new_socket); |
| 159 if (adopt_result != OK) { | 154 if (adopt_result != OK) { |
| 160 if (HANDLE_EINTR(close(new_socket)) < 0) | 155 if (HANDLE_EINTR(close(new_socket)) < 0) |
| 161 PLOG(ERROR) << "close"; | 156 PLOG(ERROR) << "close"; |
| 162 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); | 157 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); |
| 163 return adopt_result; | 158 return adopt_result; |
| 164 } | 159 } |
| 165 socket->reset(tcp_socket.release()); | 160 socket->reset(tcp_socket.release()); |
| 166 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, | 161 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 191 accept_callback_.Reset(); | 186 accept_callback_.Reset(); |
| 192 callback.Run(result); | 187 callback.Run(result); |
| 193 } | 188 } |
| 194 } | 189 } |
| 195 | 190 |
| 196 void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { | 191 void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { |
| 197 NOTREACHED(); | 192 NOTREACHED(); |
| 198 } | 193 } |
| 199 | 194 |
| 200 } // namespace net | 195 } // namespace net |
| OLD | NEW |