| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/stream_listen_socket.h" | 5 #include "net/socket/stream_listen_socket.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 // winsock2.h must be included first in order to ensure it is included before | 8 // winsock2.h must be included first in order to ensure it is included before |
| 9 // windows.h. | 9 // windows.h. |
| 10 #include <winsock2.h> | 10 #include <winsock2.h> |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 StreamListenSocket::~StreamListenSocket() { | 67 StreamListenSocket::~StreamListenSocket() { |
| 68 CloseSocket(); | 68 CloseSocket(); |
| 69 #if defined(OS_WIN) | 69 #if defined(OS_WIN) |
| 70 if (socket_event_) { | 70 if (socket_event_) { |
| 71 WSACloseEvent(socket_event_); | 71 WSACloseEvent(socket_event_); |
| 72 socket_event_ = WSA_INVALID_EVENT; | 72 socket_event_ = WSA_INVALID_EVENT; |
| 73 } | 73 } |
| 74 #endif | 74 #endif |
| 75 } | 75 } |
| 76 | 76 |
| 77 void StreamListenSocket::Send(const char* bytes, int len, | 77 void StreamListenSocket::Send(const char* bytes, |
| 78 int len, |
| 78 bool append_linefeed) { | 79 bool append_linefeed) { |
| 79 SendInternal(bytes, len); | 80 SendInternal(bytes, len); |
| 80 if (append_linefeed) | 81 if (append_linefeed) |
| 81 SendInternal("\r\n", 2); | 82 SendInternal("\r\n", 2); |
| 82 } | 83 } |
| 83 | 84 |
| 84 void StreamListenSocket::Send(const string& str, bool append_linefeed) { | 85 void StreamListenSocket::Send(const string& str, bool append_linefeed) { |
| 85 Send(str.data(), static_cast<int>(str.length()), append_linefeed); | 86 Send(str.data(), static_cast<int>(str.length()), append_linefeed); |
| 86 } | 87 } |
| 87 | 88 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 SocketDescriptor StreamListenSocket::AcceptSocket() { | 121 SocketDescriptor StreamListenSocket::AcceptSocket() { |
| 121 SocketDescriptor conn = HANDLE_EINTR(accept(socket_, NULL, NULL)); | 122 SocketDescriptor conn = HANDLE_EINTR(accept(socket_, NULL, NULL)); |
| 122 if (conn == kInvalidSocket) | 123 if (conn == kInvalidSocket) |
| 123 LOG(ERROR) << "Error accepting connection."; | 124 LOG(ERROR) << "Error accepting connection."; |
| 124 else | 125 else |
| 125 SetNonBlocking(conn); | 126 SetNonBlocking(conn); |
| 126 return conn; | 127 return conn; |
| 127 } | 128 } |
| 128 | 129 |
| 129 void StreamListenSocket::SendInternal(const char* bytes, int len) { | 130 void StreamListenSocket::SendInternal(const char* bytes, int len) { |
| 130 char* send_buf = const_cast<char *>(bytes); | 131 char* send_buf = const_cast<char*>(bytes); |
| 131 int len_left = len; | 132 int len_left = len; |
| 132 while (true) { | 133 while (true) { |
| 133 int sent = HANDLE_EINTR(send(socket_, send_buf, len_left, 0)); | 134 int sent = HANDLE_EINTR(send(socket_, send_buf, len_left, 0)); |
| 134 if (sent == len_left) { // A shortcut to avoid extraneous checks. | 135 if (sent == len_left) { // A shortcut to avoid extraneous checks. |
| 135 break; | 136 break; |
| 136 } | 137 } |
| 137 if (sent == kSocketError) { | 138 if (sent == kSocketError) { |
| 138 #if defined(OS_WIN) | 139 #if defined(OS_WIN) |
| 139 if (WSAGetLastError() != WSAEWOULDBLOCK) { | 140 if (WSAGetLastError() != WSAEWOULDBLOCK) { |
| 140 LOG(ERROR) << "send failed: WSAGetLastError()==" << WSAGetLastError(); | 141 LOG(ERROR) << "send failed: WSAGetLastError()==" << WSAGetLastError(); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 if (err == WSAEWOULDBLOCK) { | 180 if (err == WSAEWOULDBLOCK) { |
| 180 #elif defined(OS_POSIX) | 181 #elif defined(OS_POSIX) |
| 181 if (errno == EWOULDBLOCK || errno == EAGAIN) { | 182 if (errno == EWOULDBLOCK || errno == EAGAIN) { |
| 182 #endif | 183 #endif |
| 183 break; | 184 break; |
| 184 } else { | 185 } else { |
| 185 // TODO(ibrar): some error handling required here. | 186 // TODO(ibrar): some error handling required here. |
| 186 break; | 187 break; |
| 187 } | 188 } |
| 188 } else if (len == 0) { | 189 } else if (len == 0) { |
| 189 // In Windows, Close() is called by OnObjectSignaled. In POSIX, we need | 190 // In Windows, Close() is called by OnObjectSignaled. In POSIX, we need |
| 190 // to call it here. | 191 // to call it here. |
| 191 #if defined(OS_POSIX) | 192 #if defined(OS_POSIX) |
| 192 Close(); | 193 Close(); |
| 193 #endif | 194 #endif |
| 194 } else { | 195 } else { |
| 195 // TODO(ibrar): maybe change DidRead to take a length instead. | 196 // TODO(ibrar): maybe change DidRead to take a length instead. |
| 196 DCHECK_GT(len, 0); | 197 DCHECK_GT(len, 0); |
| 197 DCHECK_LE(len, kReadBufSize); | 198 DCHECK_LE(len, kReadBufSize); |
| 198 buf[len] = 0; // Already create a buffer with +1 length. | 199 buf[len] = 0; // Already create a buffer with +1 length. |
| 199 socket_delegate_->DidRead(this, buf, len); | 200 socket_delegate_->DidRead(this, buf, len); |
| 200 } | 201 } |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 void StreamListenSocket::ResumeReads() { | 316 void StreamListenSocket::ResumeReads() { |
| 316 DCHECK(reads_paused_); | 317 DCHECK(reads_paused_); |
| 317 reads_paused_ = false; | 318 reads_paused_ = false; |
| 318 if (has_pending_reads_) { | 319 if (has_pending_reads_) { |
| 319 has_pending_reads_ = false; | 320 has_pending_reads_ = false; |
| 320 Read(); | 321 Read(); |
| 321 } | 322 } |
| 322 } | 323 } |
| 323 | 324 |
| 324 } // namespace net | 325 } // namespace net |
| OLD | NEW |