Chromium Code Reviews| 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..abe6cc4341055fe442ca474d477d2e27c2c40928 |
| --- /dev/null |
| +++ b/net/socket/tcp_server_socket_libevent.cc |
| @@ -0,0 +1,179 @@ |
| +// 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) { |
|
willchan no longer on Chromium
2011/04/13 16:08:51
You should probably be creating new NetLog events
Sergey Ulanov
2011/04/13 21:20:19
Done.
|
| +} |
| + |
| +TCPServerSocketLibevent::~TCPServerSocketLibevent() { |
| + if (socket_ != kInvalidSocket) |
| + Close(); |
| +} |
| + |
| +int TCPServerSocketLibevent::Listen(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(); |
| + 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(); |
| + return result; |
| + } |
| + |
| + result = listen(socket_, backlog); |
| + if (result < 0) { |
| + PLOG(ERROR) << "listen() returned an error"; |
| + result = MapSystemError(errno); |
| + Close(); |
| + 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(); |
| + HANDLE_EINTR(close(result)); |
| + return ERR_FAILED; |
| + } |
| + *socket = new TCPClientSocket(AddressList(), net_log_, net_log_source_); |
|
willchan no longer on Chromium
2011/04/13 16:08:51
You should probably be constructing a AddressList
Sergey Ulanov
2011/04/13 21:20:19
Done.
|
| + (*socket)->AdoptSocket(result); |
| + return OK; |
| +} |
| + |
| +void TCPServerSocketLibevent::Close() { |
| + if (socket_ != kInvalidSocket) { |
| + HANDLE_EINTR(close(socket_)); |
| + socket_ = kInvalidSocket; |
| + } |
| +} |
| + |
| +void TCPServerSocketLibevent::OnFileCanReadWithoutBlocking(int fd) { |
| + DCHECK(CalledOnValidThread()); |
| + |
| + int result = AcceptInternal(accept_socket_, accept_address_); |
| + if (result == 0) { |
|
willchan no longer on Chromium
2011/04/13 16:08:51
result == OK
Sergey Ulanov
2011/04/13 21:20:19
Actually this code was incorrect: Callback should
|
| + CompletionCallback* c = accept_callback_; |
| + accept_callback_ = NULL; |
| + accept_socket_ = NULL; |
| + accept_address_ = NULL; |
| + c->Run(0); |
|
willchan no longer on Chromium
2011/04/13 16:08:51
Run(OK)
Sergey Ulanov
2011/04/13 21:20:19
Done.
|
| + } else { |
| + PLOG(ERROR) << "accept() returned error after read event was signalled"; |
| + } |
| +} |
| + |
| +void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { |
| + NOTREACHED(); |
| +} |
| + |
| +} // namespace net |