diff options
author | erg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-20 22:07:43 +0000 |
---|---|---|
committer | erg@google.com <erg@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-20 22:07:43 +0000 |
commit | 5394e4206cdf407e84ee59ef46ba9001254efd6d (patch) | |
tree | 272fe4989521b4c09bdd7d47b10e4d8f92452f6c /net/base | |
parent | 378a843690f450ee36f0c4e6e2453d847be1750b (diff) | |
download | chromium_src-5394e4206cdf407e84ee59ef46ba9001254efd6d.zip chromium_src-5394e4206cdf407e84ee59ef46ba9001254efd6d.tar.gz chromium_src-5394e4206cdf407e84ee59ef46ba9001254efd6d.tar.bz2 |
Reorder the methods in net/url_request/.
BUG=68682
TEST=compiles
Review URL: http://codereview.chromium.org/6382003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@72013 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/listen_socket.cc | 170 | ||||
-rw-r--r-- | net/base/listen_socket.h | 19 |
2 files changed, 95 insertions, 94 deletions
diff --git a/net/base/listen_socket.cc b/net/base/listen_socket.cc index 445d57d..274837a 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 diff --git a/net/base/listen_socket.h b/net/base/listen_socket.h index 641ae51..086705e 100644 --- a/net/base/listen_socket.h +++ b/net/base/listen_socket.h @@ -76,6 +76,13 @@ class ListenSocket : public base::RefCountedThreadSafe<ListenSocket>, protected: friend class base::RefCountedThreadSafe<ListenSocket>; + enum WaitState { + NOT_WAITING = 0, + WAITING_ACCEPT = 1, + WAITING_READ = 3, + WAITING_CLOSE = 4 + }; + static const SOCKET kInvalidSocket; static const int kSocketError; @@ -93,12 +100,6 @@ class ListenSocket : public base::RefCountedThreadSafe<ListenSocket>, virtual void Close(); virtual void CloseSocket(SOCKET s); - enum WaitState { - NOT_WAITING = 0, - WAITING_ACCEPT = 1, - WAITING_READ = 3, - WAITING_CLOSE = 4 - }; // Pass any value in case of Windows, because in Windows // we are not using state. void WatchSocket(WaitState state); @@ -110,12 +111,12 @@ class ListenSocket : public base::RefCountedThreadSafe<ListenSocket>, base::win::ObjectWatcher watcher_; HANDLE socket_event_; #elif defined(OS_POSIX) - WaitState wait_state_; - // The socket's libevent wrapper - MessageLoopForIO::FileDescriptorWatcher watcher_; // Called by MessagePumpLibevent when the socket is ready to do I/O virtual void OnFileCanReadWithoutBlocking(int fd); virtual void OnFileCanWriteWithoutBlocking(int fd); + WaitState wait_state_; + // The socket's libevent wrapper + MessageLoopForIO::FileDescriptorWatcher watcher_; #endif SOCKET socket_; |