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
3 // found in the LICENSE file.
5 #include "net/socket/stream_listen_socket.h"
8 // winsock2.h must be included first in order to ensure it is included before
11 #elif defined(OS_POSIX)
12 #include <arpa/inet.h>
14 #include <netinet/in.h>
15 #include <sys/socket.h>
16 #include <sys/types.h>
17 #include "net/base/net_errors.h"
20 #include "base/logging.h"
21 #include "base/memory/ref_counted.h"
22 #include "base/memory/scoped_ptr.h"
23 #include "base/posix/eintr_wrapper.h"
24 #include "base/sys_byteorder.h"
25 #include "base/threading/platform_thread.h"
26 #include "build/build_config.h"
27 #include "net/base/ip_endpoint.h"
28 #include "net/base/net_errors.h"
29 #include "net/base/net_util.h"
30 #include "net/socket/socket_descriptor.h"
35 typedef int socklen_t;
36 #endif // defined(OS_WIN)
42 const int kReadBufSize = 4096;
47 const int StreamListenSocket::kSocketError = SOCKET_ERROR;
48 #elif defined(OS_POSIX)
49 const int StreamListenSocket::kSocketError = -1;
52 StreamListenSocket::StreamListenSocket(SocketDescriptor s,
53 StreamListenSocket::Delegate* del)
54 : socket_delegate_(del),
57 has_pending_reads_(false) {
59 socket_event_ = WSACreateEvent();
60 // TODO(ibrar): error handling in case of socket_event_ == WSA_INVALID_EVENT.
61 WatchSocket(NOT_WAITING);
62 #elif defined(OS_POSIX)
63 wait_state_ = NOT_WAITING;
67 StreamListenSocket::~StreamListenSocket() {
71 WSACloseEvent(socket_event_);
72 socket_event_ = WSA_INVALID_EVENT;
77 void StreamListenSocket::Send(const char* bytes, int len,
78 bool append_linefeed) {
79 SendInternal(bytes, len);
81 SendInternal("\r\n", 2);
84 void StreamListenSocket::Send(const string& str, bool append_linefeed) {
85 Send(str.data(), static_cast<int>(str.length()), append_linefeed);
88 int StreamListenSocket::GetLocalAddress(IPEndPoint* address) {
89 SockaddrStorage storage;
90 if (getsockname(socket_, storage.addr, &storage.addr_len)) {
92 int err = WSAGetLastError();
96 return MapSystemError(err);
98 if (!address->FromSockAddr(storage.addr, storage.addr_len))
99 return ERR_ADDRESS_INVALID;
103 int StreamListenSocket::GetPeerAddress(IPEndPoint* address) {
104 SockaddrStorage storage;
105 if (getpeername(socket_, storage.addr, &storage.addr_len)) {
107 int err = WSAGetLastError();
111 return MapSystemError(err);
114 if (!address->FromSockAddr(storage.addr, storage.addr_len))
115 return ERR_ADDRESS_INVALID;
120 SocketDescriptor StreamListenSocket::AcceptSocket() {
121 SocketDescriptor conn = HANDLE_EINTR(accept(socket_, NULL, NULL));
122 if (conn == kInvalidSocket)
123 LOG(ERROR) << "Error accepting connection.";
125 SetNonBlocking(conn);
129 void StreamListenSocket::SendInternal(const char* bytes, int len) {
130 char* send_buf = const_cast<char *>(bytes);
133 int sent = HANDLE_EINTR(send(socket_, send_buf, len_left, 0));
134 if (sent == len_left) { // A shortcut to avoid extraneous checks.
137 if (sent == kSocketError) {
139 if (WSAGetLastError() != WSAEWOULDBLOCK) {
140 LOG(ERROR) << "send failed: WSAGetLastError()==" << WSAGetLastError();
141 #elif defined(OS_POSIX)
142 if (errno != EWOULDBLOCK && errno != EAGAIN) {
143 LOG(ERROR) << "send failed: errno==" << errno;
147 // Otherwise we would block, and now we have to wait for a retry.
148 // Fall through to PlatformThread::YieldCurrentThread()
150 // sent != len_left according to the shortcut above.
151 // Shift the buffer start and send the remainder after a short while.
155 base::PlatformThread::YieldCurrentThread();
159 void StreamListenSocket::Listen() {
160 int backlog = 10; // TODO(erikkay): maybe don't allow any backlog?
161 if (listen(socket_, backlog) == -1) {
162 // TODO(erikkay): error handling.
163 LOG(ERROR) << "Could not listen on socket.";
166 #if defined(OS_POSIX)
167 WatchSocket(WAITING_ACCEPT);
171 void StreamListenSocket::Read() {
172 char buf[kReadBufSize + 1]; // +1 for null termination.
175 len = HANDLE_EINTR(recv(socket_, buf, kReadBufSize, 0));
176 if (len == kSocketError) {
178 int err = WSAGetLastError();
179 if (err == WSAEWOULDBLOCK) {
180 #elif defined(OS_POSIX)
181 if (errno == EWOULDBLOCK || errno == EAGAIN) {
185 // TODO(ibrar): some error handling required here.
188 } else if (len == 0) {
189 // In Windows, Close() is called by OnObjectSignaled. In POSIX, we need
191 #if defined(OS_POSIX)
195 // TODO(ibrar): maybe change DidRead to take a length instead.
197 DCHECK_LE(len, kReadBufSize);
198 buf[len] = 0; // Already create a buffer with +1 length.
199 socket_delegate_->DidRead(this, buf, len);
201 } while (len == kReadBufSize);
204 void StreamListenSocket::Close() {
205 #if defined(OS_POSIX)
206 if (wait_state_ == NOT_WAITING)
208 wait_state_ = NOT_WAITING;
211 socket_delegate_->DidClose(this);
214 void StreamListenSocket::CloseSocket() {
215 if (socket_ != kInvalidSocket) {
218 closesocket(socket_);
219 #elif defined(OS_POSIX)
225 void StreamListenSocket::WatchSocket(WaitState state) {
227 WSAEventSelect(socket_, socket_event_, FD_ACCEPT | FD_CLOSE | FD_READ);
228 watcher_.StartWatching(socket_event_, this);
229 #elif defined(OS_POSIX)
230 // Implicitly calls StartWatchingFileDescriptor().
231 base::MessageLoopForIO::current()->WatchFileDescriptor(
232 socket_, true, base::MessageLoopForIO::WATCH_READ, &watcher_, this);
237 void StreamListenSocket::UnwatchSocket() {
239 watcher_.StopWatching();
240 #elif defined(OS_POSIX)
241 watcher_.StopWatchingFileDescriptor();
245 // TODO(ibrar): We can add these functions into OS dependent files.
247 // MessageLoop watcher callback.
248 void StreamListenSocket::OnObjectSignaled(HANDLE object) {
250 if (kSocketError == WSAEnumNetworkEvents(socket_, socket_event_, &ev)) {
255 if (ev.lNetworkEvents & FD_CLOSE) {
257 // Close might have deleted this object. We should return immediately.
261 // The object was reset by WSAEnumNetworkEvents. Watch for the next signal.
262 watcher_.StartWatching(object, this);
264 if (ev.lNetworkEvents == 0) {
265 // Occasionally the event is set even though there is no new data.
266 // The net seems to think that this is ignorable.
269 if (ev.lNetworkEvents & FD_ACCEPT) {
272 if (ev.lNetworkEvents & FD_READ) {
274 has_pending_reads_ = true;
277 // Read() might call Close() internally and 'this' can be invalid here
282 #elif defined(OS_POSIX)
283 void StreamListenSocket::OnFileCanReadWithoutBlocking(int fd) {
284 switch (wait_state_) {
290 has_pending_reads_ = true;
296 // Close() is called by Read() in the Linux case.
302 void StreamListenSocket::OnFileCanWriteWithoutBlocking(int fd) {
303 // MessagePumpLibevent callback, we don't listen for write events
304 // so we shouldn't ever reach here.
310 void StreamListenSocket::PauseReads() {
311 DCHECK(!reads_paused_);
312 reads_paused_ = true;
315 void StreamListenSocket::ResumeReads() {
316 DCHECK(reads_paused_);
317 reads_paused_ = false;
318 if (has_pending_reads_) {
319 has_pending_reads_ = false;