license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame^] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #ifndef NET_BASE_TCP_CLIENT_SOCKET_H_ |
| 6 | #define NET_BASE_TCP_CLIENT_SOCKET_H_ |
| 7 | |
| 8 | #include <ws2tcpip.h> |
| 9 | |
[email protected] | 3fa4bad | 2008-08-04 18:38:04 | [diff] [blame] | 10 | #include "base/object_watcher.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 11 | #include "net/base/address_list.h" |
| 12 | #include "net/base/client_socket.h" |
| 13 | |
| 14 | namespace net { |
| 15 | |
[email protected] | 836dff3 | 2008-08-05 23:15:36 | [diff] [blame] | 16 | // A client socket that uses TCP as the transport layer. |
| 17 | // |
| 18 | // NOTE: The implementation supports half duplex only. Read and Write calls |
| 19 | // must not be in progress at the same time. |
[email protected] | 3fa4bad | 2008-08-04 18:38:04 | [diff] [blame] | 20 | class TCPClientSocket : public ClientSocket, |
| 21 | public base::ObjectWatcher::Delegate { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 22 | public: |
| 23 | // The IP address(es) and port number to connect to. The TCP socket will try |
| 24 | // each IP address in the list until it succeeds in establishing a |
| 25 | // connection. |
[email protected] | 836dff3 | 2008-08-05 23:15:36 | [diff] [blame] | 26 | explicit TCPClientSocket(const AddressList& addresses); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 27 | |
| 28 | ~TCPClientSocket(); |
| 29 | |
| 30 | // ClientSocket methods: |
| 31 | virtual int Connect(CompletionCallback* callback); |
| 32 | virtual int ReconnectIgnoringLastError(CompletionCallback* callback); |
| 33 | virtual void Disconnect(); |
| 34 | virtual bool IsConnected() const; |
| 35 | |
| 36 | // Socket methods: |
| 37 | virtual int Read(char* buf, int buf_len, CompletionCallback* callback); |
| 38 | virtual int Write(const char* buf, int buf_len, CompletionCallback* callback); |
| 39 | |
| 40 | private: |
| 41 | int CreateSocket(const struct addrinfo* ai); |
| 42 | void DoCallback(int rv); |
| 43 | void DidCompleteConnect(); |
| 44 | void DidCompleteIO(); |
| 45 | |
[email protected] | 5656af6 | 2008-08-14 22:55:04 | [diff] [blame] | 46 | // base::ObjectWatcher::Delegate methods: |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 47 | virtual void OnObjectSignaled(HANDLE object); |
| 48 | |
| 49 | SOCKET socket_; |
| 50 | OVERLAPPED overlapped_; |
| 51 | WSABUF buffer_; |
| 52 | |
[email protected] | 3fa4bad | 2008-08-04 18:38:04 | [diff] [blame] | 53 | base::ObjectWatcher watcher_; |
| 54 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 55 | CompletionCallback* callback_; |
| 56 | |
[email protected] | 836dff3 | 2008-08-05 23:15:36 | [diff] [blame] | 57 | // The list of addresses we should try in order to establish a connection. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 58 | AddressList addresses_; |
| 59 | |
[email protected] | 836dff3 | 2008-08-05 23:15:36 | [diff] [blame] | 60 | // The addrinfo that we are attempting to use or NULL if all addrinfos have |
| 61 | // been tried. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 62 | const struct addrinfo* current_ai_; |
| 63 | |
| 64 | enum WaitState { |
| 65 | NOT_WAITING, |
| 66 | WAITING_CONNECT, |
| 67 | WAITING_READ, |
| 68 | WAITING_WRITE |
| 69 | }; |
| 70 | WaitState wait_state_; |
| 71 | }; |
| 72 | |
| 73 | } // namespace net |
| 74 | |
| 75 | #endif // NET_BASE_TCP_CLIENT_SOCKET_H_ |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame^] | 76 | |