| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 const int kInvalidSocket = -1; | 28 const int kInvalidSocket = -1; |
| 29 | 29 |
| 30 } // namespace | 30 } // namespace |
| 31 | 31 |
| 32 TCPServerSocketLibevent::TCPServerSocketLibevent( | 32 TCPServerSocketLibevent::TCPServerSocketLibevent( |
| 33 net::NetLog* net_log, | 33 net::NetLog* net_log, |
| 34 const net::NetLog::Source& source) | 34 const net::NetLog::Source& source) |
| 35 : socket_(kInvalidSocket), | 35 : socket_(kInvalidSocket), |
| 36 accept_socket_(NULL), | 36 accept_socket_(NULL), |
| 37 accept_callback_(NULL), | |
| 38 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { | 37 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { |
| 39 scoped_refptr<NetLog::EventParameters> params; | 38 scoped_refptr<NetLog::EventParameters> params; |
| 40 if (source.is_valid()) | 39 if (source.is_valid()) |
| 41 params = new NetLogSourceParameter("source_dependency", source); | 40 params = new NetLogSourceParameter("source_dependency", source); |
| 42 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); | 41 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); |
| 43 } | 42 } |
| 44 | 43 |
| 45 TCPServerSocketLibevent::~TCPServerSocketLibevent() { | 44 TCPServerSocketLibevent::~TCPServerSocketLibevent() { |
| 46 if (socket_ != kInvalidSocket) | 45 if (socket_ != kInvalidSocket) |
| 47 Close(); | 46 Close(); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); | 98 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); |
| 100 if (getsockname(socket_, addr, &addr_len) < 0) | 99 if (getsockname(socket_, addr, &addr_len) < 0) |
| 101 return MapSystemError(errno); | 100 return MapSystemError(errno); |
| 102 if (!address->FromSockAddr(addr, addr_len)) | 101 if (!address->FromSockAddr(addr, addr_len)) |
| 103 return ERR_FAILED; | 102 return ERR_FAILED; |
| 104 | 103 |
| 105 return OK; | 104 return OK; |
| 106 } | 105 } |
| 107 | 106 |
| 108 int TCPServerSocketLibevent::Accept( | 107 int TCPServerSocketLibevent::Accept( |
| 109 scoped_ptr<StreamSocket>* socket, OldCompletionCallback* callback) { | 108 scoped_ptr<StreamSocket>* socket, const CompletionCallback& callback) { |
| 110 DCHECK(CalledOnValidThread()); | 109 DCHECK(CalledOnValidThread()); |
| 111 DCHECK(socket); | 110 DCHECK(socket); |
| 112 DCHECK(callback); | 111 DCHECK(!callback.is_null()); |
| 113 DCHECK(!accept_callback_); | 112 DCHECK(accept_callback_.is_null()); |
| 114 | 113 |
| 115 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT, NULL); | 114 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT, NULL); |
| 116 | 115 |
| 117 int result = AcceptInternal(socket); | 116 int result = AcceptInternal(socket); |
| 118 | 117 |
| 119 if (result == ERR_IO_PENDING) { | 118 if (result == ERR_IO_PENDING) { |
| 120 if (!MessageLoopForIO::current()->WatchFileDescriptor( | 119 if (!MessageLoopForIO::current()->WatchFileDescriptor( |
| 121 socket_, true, MessageLoopForIO::WATCH_READ, | 120 socket_, true, MessageLoopForIO::WATCH_READ, |
| 122 &accept_socket_watcher_, this)) { | 121 &accept_socket_watcher_, this)) { |
| 123 PLOG(ERROR) << "WatchFileDescriptor failed on read"; | 122 PLOG(ERROR) << "WatchFileDescriptor failed on read"; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 PLOG(ERROR) << "close"; | 177 PLOG(ERROR) << "close"; |
| 179 socket_ = kInvalidSocket; | 178 socket_ = kInvalidSocket; |
| 180 } | 179 } |
| 181 } | 180 } |
| 182 | 181 |
| 183 void TCPServerSocketLibevent::OnFileCanReadWithoutBlocking(int fd) { | 182 void TCPServerSocketLibevent::OnFileCanReadWithoutBlocking(int fd) { |
| 184 DCHECK(CalledOnValidThread()); | 183 DCHECK(CalledOnValidThread()); |
| 185 | 184 |
| 186 int result = AcceptInternal(accept_socket_); | 185 int result = AcceptInternal(accept_socket_); |
| 187 if (result != ERR_IO_PENDING) { | 186 if (result != ERR_IO_PENDING) { |
| 188 OldCompletionCallback* c = accept_callback_; | |
| 189 accept_callback_ = NULL; | |
| 190 accept_socket_ = NULL; | 187 accept_socket_ = NULL; |
| 191 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor(); | 188 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor(); |
| 192 DCHECK(ok); | 189 DCHECK(ok); |
| 193 c->Run(result); | 190 accept_callback_.Run(result); |
| 191 accept_callback_.Reset(); |
| 194 } | 192 } |
| 195 } | 193 } |
| 196 | 194 |
| 197 void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { | 195 void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { |
| 198 NOTREACHED(); | 196 NOTREACHED(); |
| 199 } | 197 } |
| 200 | 198 |
| 201 } // namespace net | 199 } // namespace net |
| OLD | NEW |