diff options
Diffstat (limited to 'net/socket/tcp_client_socket_libevent.h')
-rw-r--r-- | net/socket/tcp_client_socket_libevent.h | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/net/socket/tcp_client_socket_libevent.h b/net/socket/tcp_client_socket_libevent.h index 699ddc7..6186159 100644 --- a/net/socket/tcp_client_socket_libevent.h +++ b/net/socket/tcp_client_socket_libevent.h @@ -47,6 +47,13 @@ class TCPClientSocketLibevent : public ClientSocket { virtual bool SetSendBufferSize(int32 size); private: + // State machine for connecting the socket. + enum ConnectState { + CONNECT_STATE_CONNECT, + CONNECT_STATE_CONNECT_COMPLETE, + CONNECT_STATE_NONE, + }; + class ReadWatcher : public MessageLoopForIO::Watcher { public: explicit ReadWatcher(TCPClientSocketLibevent* socket) : socket_(socket) {} @@ -75,7 +82,7 @@ class TCPClientSocketLibevent : public ClientSocket { virtual void OnFileCanReadWithoutBlocking(int /* fd */) {} virtual void OnFileCanWriteWithoutBlocking(int /* fd */) { - if (socket_->waiting_connect_) { + if (socket_->waiting_connect()) { socket_->DidCompleteConnect(); } else if (socket_->write_callback_) { socket_->DidCompleteWrite(); @@ -88,8 +95,15 @@ class TCPClientSocketLibevent : public ClientSocket { DISALLOW_COPY_AND_ASSIGN(WriteWatcher); }; - // Performs the actual connect(). Returns a net error code. + // State machine used by Connect(). + int DoConnectLoop(int result); int DoConnect(); + int DoConnectComplete(int result); + + // Helper used by Disconnect(), which disconnects minus the logging and + // resetting of current_ai_. + void DoDisconnect(); + void DoReadCallback(int rv); void DoWriteCallback(int rv); @@ -97,6 +111,12 @@ class TCPClientSocketLibevent : public ClientSocket { void DidCompleteWrite(); void DidCompleteConnect(); + // Returns true if a Connect() is in progress. + bool waiting_connect() const { + return next_connect_state_ != CONNECT_STATE_NONE; + } + + // Returns the OS error code (or 0 on success). int CreateSocket(const struct addrinfo* ai); int socket_; @@ -107,9 +127,6 @@ class TCPClientSocketLibevent : public ClientSocket { // Where we are in above list, or NULL if all addrinfos have been tried. const struct addrinfo* current_ai_; - // Whether we're currently waiting for connect() to complete - bool waiting_connect_; - // The socket's libevent wrappers MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; @@ -132,6 +149,9 @@ class TCPClientSocketLibevent : public ClientSocket { // External callback; called when write is complete. CompletionCallback* write_callback_; + // The next state for the Connect() state machine. + ConnectState next_connect_state_; + BoundNetLog net_log_; DISALLOW_COPY_AND_ASSIGN(TCPClientSocketLibevent); |