initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #ifndef NET_BASE_TCP_CLIENT_SOCKET_H_ |
| 31 | #define NET_BASE_TCP_CLIENT_SOCKET_H_ |
| 32 | |
| 33 | #include <ws2tcpip.h> |
| 34 | |
[email protected] | 3fa4bad | 2008-08-04 18:38:04 | [diff] [blame] | 35 | #include "base/object_watcher.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 36 | #include "net/base/address_list.h" |
| 37 | #include "net/base/client_socket.h" |
| 38 | |
| 39 | namespace net { |
| 40 | |
[email protected] | 836dff3 | 2008-08-05 23:15:36 | [diff] [blame^] | 41 | // A client socket that uses TCP as the transport layer. |
| 42 | // |
| 43 | // NOTE: The implementation supports half duplex only. Read and Write calls |
| 44 | // must not be in progress at the same time. |
[email protected] | 3fa4bad | 2008-08-04 18:38:04 | [diff] [blame] | 45 | class TCPClientSocket : public ClientSocket, |
| 46 | public base::ObjectWatcher::Delegate { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 47 | public: |
| 48 | // The IP address(es) and port number to connect to. The TCP socket will try |
| 49 | // each IP address in the list until it succeeds in establishing a |
| 50 | // connection. |
[email protected] | 836dff3 | 2008-08-05 23:15:36 | [diff] [blame^] | 51 | explicit TCPClientSocket(const AddressList& addresses); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 52 | |
| 53 | ~TCPClientSocket(); |
| 54 | |
| 55 | // ClientSocket methods: |
| 56 | virtual int Connect(CompletionCallback* callback); |
| 57 | virtual int ReconnectIgnoringLastError(CompletionCallback* callback); |
| 58 | virtual void Disconnect(); |
| 59 | virtual bool IsConnected() const; |
| 60 | |
| 61 | // Socket methods: |
| 62 | virtual int Read(char* buf, int buf_len, CompletionCallback* callback); |
| 63 | virtual int Write(const char* buf, int buf_len, CompletionCallback* callback); |
| 64 | |
| 65 | private: |
| 66 | int CreateSocket(const struct addrinfo* ai); |
| 67 | void DoCallback(int rv); |
| 68 | void DidCompleteConnect(); |
| 69 | void DidCompleteIO(); |
| 70 | |
[email protected] | 836dff3 | 2008-08-05 23:15:36 | [diff] [blame^] | 71 | // MessageLoop::Watcher methods: |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 72 | virtual void OnObjectSignaled(HANDLE object); |
| 73 | |
| 74 | SOCKET socket_; |
| 75 | OVERLAPPED overlapped_; |
| 76 | WSABUF buffer_; |
| 77 | |
[email protected] | 3fa4bad | 2008-08-04 18:38:04 | [diff] [blame] | 78 | base::ObjectWatcher watcher_; |
| 79 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 80 | CompletionCallback* callback_; |
| 81 | |
[email protected] | 836dff3 | 2008-08-05 23:15:36 | [diff] [blame^] | 82 | // The list of addresses we should try in order to establish a connection. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 83 | AddressList addresses_; |
| 84 | |
[email protected] | 836dff3 | 2008-08-05 23:15:36 | [diff] [blame^] | 85 | // The addrinfo that we are attempting to use or NULL if all addrinfos have |
| 86 | // been tried. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 87 | const struct addrinfo* current_ai_; |
| 88 | |
| 89 | enum WaitState { |
| 90 | NOT_WAITING, |
| 91 | WAITING_CONNECT, |
| 92 | WAITING_READ, |
| 93 | WAITING_WRITE |
| 94 | }; |
| 95 | WaitState wait_state_; |
| 96 | }; |
| 97 | |
| 98 | } // namespace net |
| 99 | |
| 100 | #endif // NET_BASE_TCP_CLIENT_SOCKET_H_ |