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

Unified Diff: net/base/listen_socket.cc

Issue 6382003: Reorder the methods in net/url_request/. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Compiling net_unittests != compiling the rest of chrome Created 9 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/base/listen_socket.h ('k') | net/url_request/https_prober.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/listen_socket.cc
diff --git a/net/base/listen_socket.cc b/net/base/listen_socket.cc
index 445d57d141a514e678ebc95cd778da9e5b56ff1e..274837a7c59075f2fed6b26121807076b7b7c500 100644
--- a/net/base/listen_socket.cc
+++ b/net/base/listen_socket.cc
@@ -44,6 +44,44 @@ const SOCKET ListenSocket::kInvalidSocket = -1;
const int ListenSocket::kSocketError = -1;
#endif
+ListenSocket* ListenSocket::Listen(std::string ip, int port,
+ ListenSocketDelegate* del) {
+ SOCKET s = Listen(ip, port);
+ if (s == kInvalidSocket) {
+ // TODO(erikkay): error handling
+ } else {
+ ListenSocket* sock = new ListenSocket(s, del);
+ sock->Listen();
+ return sock;
+ }
+ return NULL;
+}
+
+void ListenSocket::Send(const char* bytes, int len, bool append_linefeed) {
+ SendInternal(bytes, len);
+ if (append_linefeed) {
+ SendInternal("\r\n", 2);
+ }
+}
+
+void ListenSocket::Send(const std::string& str, bool append_linefeed) {
+ Send(str.data(), static_cast<int>(str.length()), append_linefeed);
+}
+
+void ListenSocket::PauseReads() {
+ DCHECK(!reads_paused_);
+ reads_paused_ = true;
+}
+
+void ListenSocket::ResumeReads() {
+ DCHECK(reads_paused_);
+ reads_paused_ = false;
+ if (has_pending_reads_) {
+ has_pending_reads_ = false;
+ Read();
+ }
+}
+
ListenSocket::ListenSocket(SOCKET s, ListenSocketDelegate *del)
: socket_(s),
socket_delegate_(del),
@@ -86,17 +124,45 @@ SOCKET ListenSocket::Listen(std::string ip, int port) {
return s;
}
-ListenSocket* ListenSocket::Listen(std::string ip, int port,
- ListenSocketDelegate* del) {
- SOCKET s = Listen(ip, port);
- if (s == kInvalidSocket) {
- // TODO(erikkay): error handling
- } else {
- ListenSocket* sock = new ListenSocket(s, del);
- sock->Listen();
- return sock;
+SOCKET ListenSocket::Accept(SOCKET s) {
+ sockaddr_in from;
+ socklen_t from_len = sizeof(from);
+ SOCKET conn =
+ HANDLE_EINTR(accept(s, reinterpret_cast<sockaddr*>(&from), &from_len));
+ if (conn != kInvalidSocket) {
+ net::SetNonBlocking(conn);
+ }
+ return conn;
+}
+
+void ListenSocket::SendInternal(const char* bytes, int len) {
+ char* send_buf = const_cast<char *>(bytes);
+ int len_left = len;
+ while (true) {
+ int sent = HANDLE_EINTR(send(socket_, send_buf, len_left, 0));
+ if (sent == len_left) { // A shortcut to avoid extraneous checks.
+ break;
+ }
+ if (sent == kSocketError) {
+#if defined(OS_WIN)
+ if (WSAGetLastError() != WSAEWOULDBLOCK) {
+ LOG(ERROR) << "send failed: WSAGetLastError()==" << WSAGetLastError();
+#elif defined(OS_POSIX)
+ if (errno != EWOULDBLOCK && errno != EAGAIN) {
+ LOG(ERROR) << "send failed: errno==" << errno;
+#endif
+ break;
+ }
+ // Otherwise we would block, and now we have to wait for a retry.
+ // Fall through to PlatformThread::YieldCurrentThread()
+ } else {
+ // sent != len_left according to the shortcut above.
+ // Shift the buffer start and send the remainder after a short while.
+ send_buf += sent;
+ len_left -= sent;
+ }
+ base::PlatformThread::YieldCurrentThread();
}
- return NULL;
}
void ListenSocket::Listen() {
@@ -108,17 +174,6 @@ void ListenSocket::Listen() {
#endif
}
-SOCKET ListenSocket::Accept(SOCKET s) {
- sockaddr_in from;
- socklen_t from_len = sizeof(from);
- SOCKET conn =
- HANDLE_EINTR(accept(s, reinterpret_cast<sockaddr*>(&from), &from_len));
- if (conn != kInvalidSocket) {
- net::SetNonBlocking(conn);
- }
- return conn;
-}
-
void ListenSocket::Accept() {
SOCKET conn = Accept(socket_);
if (conn != kInvalidSocket) {
@@ -166,17 +221,6 @@ void ListenSocket::Read() {
} while (len == kReadBufSize);
}
-void ListenSocket::CloseSocket(SOCKET s) {
- if (s && s != kInvalidSocket) {
- UnwatchSocket();
-#if defined(OS_WIN)
- closesocket(s);
-#elif defined(OS_POSIX)
- close(s);
-#endif
- }
-}
-
void ListenSocket::Close() {
#if defined(OS_POSIX)
if (wait_state_ == WAITING_CLOSE)
@@ -186,12 +230,15 @@ void ListenSocket::Close() {
socket_delegate_->DidClose(this);
}
-void ListenSocket::UnwatchSocket() {
+void ListenSocket::CloseSocket(SOCKET s) {
+ if (s && s != kInvalidSocket) {
+ UnwatchSocket();
#if defined(OS_WIN)
- watcher_.StopWatching();
+ closesocket(s);
#elif defined(OS_POSIX)
- watcher_.StopWatchingFileDescriptor();
+ close(s);
#endif
+ }
}
void ListenSocket::WatchSocket(WaitState state) {
@@ -206,59 +253,12 @@ void ListenSocket::WatchSocket(WaitState state) {
#endif
}
-void ListenSocket::SendInternal(const char* bytes, int len) {
- char* send_buf = const_cast<char *>(bytes);
- int len_left = len;
- while (true) {
- int sent = HANDLE_EINTR(send(socket_, send_buf, len_left, 0));
- if (sent == len_left) { // A shortcut to avoid extraneous checks.
- break;
- }
- if (sent == kSocketError) {
+void ListenSocket::UnwatchSocket() {
#if defined(OS_WIN)
- if (WSAGetLastError() != WSAEWOULDBLOCK) {
- LOG(ERROR) << "send failed: WSAGetLastError()==" << WSAGetLastError();
+ watcher_.StopWatching();
#elif defined(OS_POSIX)
- if (errno != EWOULDBLOCK && errno != EAGAIN) {
- LOG(ERROR) << "send failed: errno==" << errno;
+ watcher_.StopWatchingFileDescriptor();
#endif
- break;
- }
- // Otherwise we would block, and now we have to wait for a retry.
- // Fall through to PlatformThread::YieldCurrentThread()
- } else {
- // sent != len_left according to the shortcut above.
- // Shift the buffer start and send the remainder after a short while.
- send_buf += sent;
- len_left -= sent;
- }
- base::PlatformThread::YieldCurrentThread();
- }
-}
-
-void ListenSocket::Send(const char* bytes, int len, bool append_linefeed) {
- SendInternal(bytes, len);
- if (append_linefeed) {
- SendInternal("\r\n", 2);
- }
-}
-
-void ListenSocket::Send(const std::string& str, bool append_linefeed) {
- Send(str.data(), static_cast<int>(str.length()), append_linefeed);
-}
-
-void ListenSocket::PauseReads() {
- DCHECK(!reads_paused_);
- reads_paused_ = true;
-}
-
-void ListenSocket::ResumeReads() {
- DCHECK(reads_paused_);
- reads_paused_ = false;
- if (has_pending_reads_) {
- has_pending_reads_ = false;
- Read();
- }
}
// TODO(ibrar): We can add these functions into OS dependent files
« no previous file with comments | « net/base/listen_socket.h ('k') | net/url_request/https_prober.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698