Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: net/socket/tcp_server_socket_libevent.cc

Issue 6820057: Added TCPServerSocket. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: windows, fixes for mac Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "net/socket/tcp_server_socket_libevent.h"
6
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <netdb.h>
10 #include <sys/socket.h>
11
12 #include "base/eintr_wrapper.h"
13 #include "net/base/ip_endpoint.h"
14 #include "net/base/net_errors.h"
15 #include "net/base/net_util.h"
16 #if defined(OS_POSIX)
17 #include <netinet/in.h>
18 #endif
19 #if defined(USE_SYSTEM_LIBEVENT)
20 #include <event.h>
21 #else
22 #include "third_party/libevent/event.h"
23 #endif
24
25 namespace net {
26
27 namespace {
28
29 const int kInvalidSocket = -1;
30
31 } // namespace
32
33 TCPServerSocketLibevent::TCPServerSocketLibevent(
34 net::NetLog* net_log,
35 const net::NetLog::Source& source)
36 : socket_(kInvalidSocket),
37 accept_socket_(NULL),
38 accept_address_(NULL),
39 accept_callback_(NULL),
40 net_log_(net_log),
41 net_log_source_(source) {
42 }
43
44 TCPServerSocketLibevent::~TCPServerSocketLibevent() {
45 if (socket_ != kInvalidSocket) {
46 close(socket_);
willchan no longer on Chromium 2011/04/12 16:45:00 HANDLE_EINTR
Sergey Ulanov 2011/04/12 19:20:46 Done.
47 socket_ = kInvalidSocket;
48 }
49 }
50
51 int TCPServerSocketLibevent::Bind(const IPEndPoint& address, int backlog) {
52 DCHECK(CalledOnValidThread());
53 DCHECK_GT(backlog, 0);
54 DCHECK_EQ(socket_, kInvalidSocket);
55
56 socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
57 if (socket_ < 0) {
58 PLOG(ERROR) << "socket() returned an error";
59 return MapSystemError(errno);
60 }
61
62 if (SetNonBlocking(socket_)) {
63 int result = MapSystemError(errno);
64 close(socket_);
65 return result;
66 }
67
68 struct sockaddr_storage addr_storage;
69 size_t addr_len = sizeof(addr_storage);
70 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage);
71 if (!address.ToSockAddr(addr, &addr_len))
72 return ERR_INVALID_ARGUMENT;
73
74 int result = bind(socket_, addr, addr_len);
75 if (result < 0) {
76 PLOG(ERROR) << "bind() returned an error";
77 result = MapSystemError(errno);
78 close(socket_);
79 socket_ = kInvalidSocket;
80 return result;
81 }
82
83 result = listen(socket_, backlog);
84 if (result < 0) {
85 PLOG(ERROR) << "listen() returned an error";
86 result = MapSystemError(errno);
87 close(socket_);
88 socket_ = kInvalidSocket;
89 return result;
90 }
91
92 return OK;
93 }
94
95 int TCPServerSocketLibevent::GetLocalAddress(IPEndPoint* address) const {
96 DCHECK(CalledOnValidThread());
97 DCHECK(address);
98
99 struct sockaddr_storage addr_storage;
100 socklen_t addr_len = sizeof(addr_storage);
101 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage);
102 if (getsockname(socket_, addr, &addr_len) < 0)
103 return MapSystemError(errno);
104 if (!address->FromSockAddr(addr, addr_len))
105 return ERR_FAILED;
106
107 return OK;
108 }
109
110 int TCPServerSocketLibevent::Accept(
111 TCPClientSocket** socket, net::IPEndPoint* address,
112 CompletionCallback* callback) {
113 DCHECK(CalledOnValidThread());
114 DCHECK(socket);
115 DCHECK(address);
116 DCHECK(callback);
117 DCHECK(!accept_callback_);
118
119 int result = AcceptInternal(socket, address);
120
121 if (result == ERR_IO_PENDING) {
122 if (!MessageLoopForIO::current()->WatchFileDescriptor(
123 socket_, true, MessageLoopForIO::WATCH_READ,
124 &accept_socket_watcher_, this)) {
125 PLOG(ERROR) << "WatchFileDescriptor failed on read";
126 return MapSystemError(errno);
127 }
128
129 accept_socket_ = socket;
130 accept_address_ = address;
131 accept_callback_ = callback;
132 }
133
134 return result;
135 }
136
137 int TCPServerSocketLibevent::AcceptInternal(
138 TCPClientSocket** socket, net::IPEndPoint* address) {
139 struct sockaddr_storage addr_storage;
140 socklen_t addr_len = sizeof(addr_storage);
141 struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage);
142
143 int result = HANDLE_EINTR(accept(socket_, addr, &addr_len));
144 if (result < 0)
145 return MapSystemError(errno);
146
147 if (!address->FromSockAddr(addr, addr_len)) {
148 NOTREACHED();
149 close(result);
150 return ERR_FAILED;
151 }
152 *socket = new TCPClientSocket(AddressList(), net_log_, net_log_source_);
153 (*socket)->AdoptSocket(result);
154 return OK;
155 }
156
157 void TCPServerSocketLibevent::OnFileCanReadWithoutBlocking(int fd) {
158 DCHECK(CalledOnValidThread());
159
160 int result = AcceptInternal(accept_socket_, accept_address_);
161 if (result == 0) {
162 CompletionCallback* c = accept_callback_;
163 accept_callback_ = NULL;
164 accept_socket_ = NULL;
165 accept_address_ = NULL;
166 c->Run(0);
167 } else {
168 PLOG(ERROR) << "accept() returned error after read event was signalled";
169 }
170 }
171
172 void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) {
173 NOTREACHED();
174 }
175
176 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698