Index: net/socket/tcp_server_socket_libevent.cc |
diff --git a/net/socket/tcp_server_socket_libevent.cc b/net/socket/tcp_server_socket_libevent.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..cb311c7fc9117e42e455322049a96c64868007d1 |
--- /dev/null |
+++ b/net/socket/tcp_server_socket_libevent.cc |
@@ -0,0 +1,176 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "net/socket/tcp_server_socket_libevent.h" |
+ |
+#include <errno.h> |
+#include <fcntl.h> |
+#include <netdb.h> |
+#include <sys/socket.h> |
+ |
+#include "base/eintr_wrapper.h" |
+#include "net/base/ip_endpoint.h" |
+#include "net/base/net_errors.h" |
+#include "net/base/net_util.h" |
+#if defined(OS_POSIX) |
+#include <netinet/in.h> |
+#endif |
+#if defined(USE_SYSTEM_LIBEVENT) |
+#include <event.h> |
+#else |
+#include "third_party/libevent/event.h" |
+#endif |
+ |
+namespace net { |
+ |
+namespace { |
+ |
+const int kInvalidSocket = -1; |
+ |
+} // namespace |
+ |
+TCPServerSocketLibevent::TCPServerSocketLibevent( |
+ net::NetLog* net_log, |
+ const net::NetLog::Source& source) |
+ : socket_(kInvalidSocket), |
+ accept_socket_(NULL), |
+ accept_address_(NULL), |
+ accept_callback_(NULL), |
+ net_log_(net_log), |
+ net_log_source_(source) { |
+} |
+ |
+TCPServerSocketLibevent::~TCPServerSocketLibevent() { |
+ if (socket_ != kInvalidSocket) { |
+ close(socket_); |
willchan no longer on Chromium
2011/04/12 16:45:00
HANDLE_EINTR
Sergey Ulanov
2011/04/12 19:20:46
Done.
|
+ socket_ = kInvalidSocket; |
+ } |
+} |
+ |
+int TCPServerSocketLibevent::Bind(const IPEndPoint& address, int backlog) { |
+ DCHECK(CalledOnValidThread()); |
+ DCHECK_GT(backlog, 0); |
+ DCHECK_EQ(socket_, kInvalidSocket); |
+ |
+ socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
+ if (socket_ < 0) { |
+ PLOG(ERROR) << "socket() returned an error"; |
+ return MapSystemError(errno); |
+ } |
+ |
+ if (SetNonBlocking(socket_)) { |
+ int result = MapSystemError(errno); |
+ close(socket_); |
+ return result; |
+ } |
+ |
+ struct sockaddr_storage addr_storage; |
+ size_t addr_len = sizeof(addr_storage); |
+ struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); |
+ if (!address.ToSockAddr(addr, &addr_len)) |
+ return ERR_INVALID_ARGUMENT; |
+ |
+ int result = bind(socket_, addr, addr_len); |
+ if (result < 0) { |
+ PLOG(ERROR) << "bind() returned an error"; |
+ result = MapSystemError(errno); |
+ close(socket_); |
+ socket_ = kInvalidSocket; |
+ return result; |
+ } |
+ |
+ result = listen(socket_, backlog); |
+ if (result < 0) { |
+ PLOG(ERROR) << "listen() returned an error"; |
+ result = MapSystemError(errno); |
+ close(socket_); |
+ socket_ = kInvalidSocket; |
+ return result; |
+ } |
+ |
+ return OK; |
+} |
+ |
+int TCPServerSocketLibevent::GetLocalAddress(IPEndPoint* address) const { |
+ DCHECK(CalledOnValidThread()); |
+ DCHECK(address); |
+ |
+ struct sockaddr_storage addr_storage; |
+ socklen_t addr_len = sizeof(addr_storage); |
+ struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); |
+ if (getsockname(socket_, addr, &addr_len) < 0) |
+ return MapSystemError(errno); |
+ if (!address->FromSockAddr(addr, addr_len)) |
+ return ERR_FAILED; |
+ |
+ return OK; |
+} |
+ |
+int TCPServerSocketLibevent::Accept( |
+ TCPClientSocket** socket, net::IPEndPoint* address, |
+ CompletionCallback* callback) { |
+ DCHECK(CalledOnValidThread()); |
+ DCHECK(socket); |
+ DCHECK(address); |
+ DCHECK(callback); |
+ DCHECK(!accept_callback_); |
+ |
+ int result = AcceptInternal(socket, address); |
+ |
+ if (result == ERR_IO_PENDING) { |
+ if (!MessageLoopForIO::current()->WatchFileDescriptor( |
+ socket_, true, MessageLoopForIO::WATCH_READ, |
+ &accept_socket_watcher_, this)) { |
+ PLOG(ERROR) << "WatchFileDescriptor failed on read"; |
+ return MapSystemError(errno); |
+ } |
+ |
+ accept_socket_ = socket; |
+ accept_address_ = address; |
+ accept_callback_ = callback; |
+ } |
+ |
+ return result; |
+} |
+ |
+int TCPServerSocketLibevent::AcceptInternal( |
+ TCPClientSocket** socket, net::IPEndPoint* address) { |
+ struct sockaddr_storage addr_storage; |
+ socklen_t addr_len = sizeof(addr_storage); |
+ struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); |
+ |
+ int result = HANDLE_EINTR(accept(socket_, addr, &addr_len)); |
+ if (result < 0) |
+ return MapSystemError(errno); |
+ |
+ if (!address->FromSockAddr(addr, addr_len)) { |
+ NOTREACHED(); |
+ close(result); |
+ return ERR_FAILED; |
+ } |
+ *socket = new TCPClientSocket(AddressList(), net_log_, net_log_source_); |
+ (*socket)->AdoptSocket(result); |
+ return OK; |
+} |
+ |
+void TCPServerSocketLibevent::OnFileCanReadWithoutBlocking(int fd) { |
+ DCHECK(CalledOnValidThread()); |
+ |
+ int result = AcceptInternal(accept_socket_, accept_address_); |
+ if (result == 0) { |
+ CompletionCallback* c = accept_callback_; |
+ accept_callback_ = NULL; |
+ accept_socket_ = NULL; |
+ accept_address_ = NULL; |
+ c->Run(0); |
+ } else { |
+ PLOG(ERROR) << "accept() returned error after read event was signalled"; |
+ } |
+} |
+ |
+void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { |
+ NOTREACHED(); |
+} |
+ |
+} // namespace net |