| 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> |
| 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/eintr_wrapper.h" | 18 #include "base/eintr_wrapper.h" |
| 19 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
| 20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 21 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
| 22 #include "net/socket/socket_net_log_params.h" |
| 22 #include "net/socket/tcp_client_socket.h" | 23 #include "net/socket/tcp_client_socket.h" |
| 23 | 24 |
| 24 namespace net { | 25 namespace net { |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 const int kInvalidSocket = -1; | 29 const int kInvalidSocket = -1; |
| 29 | 30 |
| 30 } // namespace | 31 } // namespace |
| 31 | 32 |
| 32 TCPServerSocketLibevent::TCPServerSocketLibevent( | 33 TCPServerSocketLibevent::TCPServerSocketLibevent( |
| 33 net::NetLog* net_log, | 34 net::NetLog* net_log, |
| 34 const net::NetLog::Source& source) | 35 const net::NetLog::Source& source) |
| 35 : socket_(kInvalidSocket), | 36 : socket_(kInvalidSocket), |
| 36 accept_socket_(NULL), | 37 accept_socket_(NULL), |
| 37 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { | 38 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { |
| 38 scoped_refptr<NetLog::EventParameters> params; | 39 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, |
| 39 if (source.is_valid()) | 40 source.ToEventParametersCallback()); |
| 40 params = new NetLogSourceParameter("source_dependency", source); | |
| 41 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); | |
| 42 } | 41 } |
| 43 | 42 |
| 44 TCPServerSocketLibevent::~TCPServerSocketLibevent() { | 43 TCPServerSocketLibevent::~TCPServerSocketLibevent() { |
| 45 if (socket_ != kInvalidSocket) | 44 if (socket_ != kInvalidSocket) |
| 46 Close(); | 45 Close(); |
| 47 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE, NULL); | 46 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); |
| 48 } | 47 } |
| 49 | 48 |
| 50 int TCPServerSocketLibevent::Listen(const IPEndPoint& address, int backlog) { | 49 int TCPServerSocketLibevent::Listen(const IPEndPoint& address, int backlog) { |
| 51 DCHECK(CalledOnValidThread()); | 50 DCHECK(CalledOnValidThread()); |
| 52 DCHECK_GT(backlog, 0); | 51 DCHECK_GT(backlog, 0); |
| 53 DCHECK_EQ(socket_, kInvalidSocket); | 52 DCHECK_EQ(socket_, kInvalidSocket); |
| 54 | 53 |
| 55 socket_ = socket(address.GetFamily(), SOCK_STREAM, IPPROTO_TCP); | 54 socket_ = socket(address.GetFamily(), SOCK_STREAM, IPPROTO_TCP); |
| 56 if (socket_ < 0) { | 55 if (socket_ < 0) { |
| 57 PLOG(ERROR) << "socket() returned an error"; | 56 PLOG(ERROR) << "socket() returned an error"; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 return OK; | 99 return OK; |
| 101 } | 100 } |
| 102 | 101 |
| 103 int TCPServerSocketLibevent::Accept( | 102 int TCPServerSocketLibevent::Accept( |
| 104 scoped_ptr<StreamSocket>* socket, const CompletionCallback& callback) { | 103 scoped_ptr<StreamSocket>* socket, const CompletionCallback& callback) { |
| 105 DCHECK(CalledOnValidThread()); | 104 DCHECK(CalledOnValidThread()); |
| 106 DCHECK(socket); | 105 DCHECK(socket); |
| 107 DCHECK(!callback.is_null()); | 106 DCHECK(!callback.is_null()); |
| 108 DCHECK(accept_callback_.is_null()); | 107 DCHECK(accept_callback_.is_null()); |
| 109 | 108 |
| 110 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT, NULL); | 109 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT); |
| 111 | 110 |
| 112 int result = AcceptInternal(socket); | 111 int result = AcceptInternal(socket); |
| 113 | 112 |
| 114 if (result == ERR_IO_PENDING) { | 113 if (result == ERR_IO_PENDING) { |
| 115 if (!MessageLoopForIO::current()->WatchFileDescriptor( | 114 if (!MessageLoopForIO::current()->WatchFileDescriptor( |
| 116 socket_, true, MessageLoopForIO::WATCH_READ, | 115 socket_, true, MessageLoopForIO::WATCH_READ, |
| 117 &accept_socket_watcher_, this)) { | 116 &accept_socket_watcher_, this)) { |
| 118 PLOG(ERROR) << "WatchFileDescriptor failed on read"; | 117 PLOG(ERROR) << "WatchFileDescriptor failed on read"; |
| 119 return MapSystemError(errno); | 118 return MapSystemError(errno); |
| 120 } | 119 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 net_log_.net_log(), net_log_.source())); | 151 net_log_.net_log(), net_log_.source())); |
| 153 int adopt_result = tcp_socket->AdoptSocket(new_socket); | 152 int adopt_result = tcp_socket->AdoptSocket(new_socket); |
| 154 if (adopt_result != OK) { | 153 if (adopt_result != OK) { |
| 155 if (HANDLE_EINTR(close(new_socket)) < 0) | 154 if (HANDLE_EINTR(close(new_socket)) < 0) |
| 156 PLOG(ERROR) << "close"; | 155 PLOG(ERROR) << "close"; |
| 157 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); | 156 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); |
| 158 return adopt_result; | 157 return adopt_result; |
| 159 } | 158 } |
| 160 socket->reset(tcp_socket.release()); | 159 socket->reset(tcp_socket.release()); |
| 161 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, | 160 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, |
| 162 make_scoped_refptr(new NetLogStringParameter( | 161 CreateNetLogIPEndPointCallback(&address)); |
| 163 "address", address.ToString()))); | |
| 164 return OK; | 162 return OK; |
| 165 } | 163 } |
| 166 | 164 |
| 167 void TCPServerSocketLibevent::Close() { | 165 void TCPServerSocketLibevent::Close() { |
| 168 if (socket_ != kInvalidSocket) { | 166 if (socket_ != kInvalidSocket) { |
| 169 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor(); | 167 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor(); |
| 170 DCHECK(ok); | 168 DCHECK(ok); |
| 171 if (HANDLE_EINTR(close(socket_)) < 0) | 169 if (HANDLE_EINTR(close(socket_)) < 0) |
| 172 PLOG(ERROR) << "close"; | 170 PLOG(ERROR) << "close"; |
| 173 socket_ = kInvalidSocket; | 171 socket_ = kInvalidSocket; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 186 accept_callback_.Reset(); | 184 accept_callback_.Reset(); |
| 187 callback.Run(result); | 185 callback.Run(result); |
| 188 } | 186 } |
| 189 } | 187 } |
| 190 | 188 |
| 191 void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { | 189 void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { |
| 192 NOTREACHED(); | 190 NOTREACHED(); |
| 193 } | 191 } |
| 194 | 192 |
| 195 } // namespace net | 193 } // namespace net |
| OLD | NEW |