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 | |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame^] | 8 | #include "build/build_config.h" |
[email protected] | dab8619 | 2008-09-18 00:10:38 | [diff] [blame] | 9 | |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame^] | 10 | #if defined(OS_WIN) |
| 11 | #include <ws2tcpip.h> |
[email protected] | 35aa85a | 2008-09-18 00:23:24 | [diff] [blame] | 12 | #include "base/object_watcher.h" |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame^] | 13 | #elif defined(OS_POSIX) |
| 14 | struct event; // From libevent |
| 15 | #define SOCKET int |
| 16 | #include "base/message_pump_libevent.h" |
| 17 | #endif |
| 18 | |
| 19 | #include "base/scoped_ptr.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 20 | #include "net/base/address_list.h" |
| 21 | #include "net/base/client_socket.h" |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame^] | 22 | #include "net/base/completion_callback.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 23 | |
| 24 | namespace net { |
| 25 | |
[email protected] | 836dff3 | 2008-08-05 23:15:36 | [diff] [blame] | 26 | // A client socket that uses TCP as the transport layer. |
| 27 | // |
| 28 | // NOTE: The implementation supports half duplex only. Read and Write calls |
| 29 | // must not be in progress at the same time. |
[email protected] | 3fa4bad | 2008-08-04 18:38:04 | [diff] [blame] | 30 | class TCPClientSocket : public ClientSocket, |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame^] | 31 | #if defined(OS_WIN) |
| 32 | public base::ObjectWatcher::Delegate |
| 33 | #elif defined(OS_POSIX) |
| 34 | public base::MessagePumpLibevent::Watcher |
| 35 | #endif |
| 36 | { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 37 | public: |
| 38 | // The IP address(es) and port number to connect to. The TCP socket will try |
| 39 | // each IP address in the list until it succeeds in establishing a |
| 40 | // connection. |
[email protected] | 836dff3 | 2008-08-05 23:15:36 | [diff] [blame] | 41 | explicit TCPClientSocket(const AddressList& addresses); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 42 | |
| 43 | ~TCPClientSocket(); |
| 44 | |
| 45 | // ClientSocket methods: |
| 46 | virtual int Connect(CompletionCallback* callback); |
| 47 | virtual int ReconnectIgnoringLastError(CompletionCallback* callback); |
| 48 | virtual void Disconnect(); |
| 49 | virtual bool IsConnected() const; |
| 50 | |
| 51 | // Socket methods: |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame^] | 52 | // Try to transfer buf_len bytes to/from socket. |
| 53 | // If a result is available now, return it; else call back later with one. |
| 54 | // Do not call again until a result is returned! |
| 55 | // If any bytes were transferred, the result is the byte count. |
| 56 | // On error, result is a negative error code; see net/base/net_error_list.h |
| 57 | // TODO: what would a zero return value indicate? |
| 58 | // TODO: support multiple outstanding requests? |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 59 | virtual int Read(char* buf, int buf_len, CompletionCallback* callback); |
| 60 | virtual int Write(const char* buf, int buf_len, CompletionCallback* callback); |
| 61 | |
| 62 | private: |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 63 | SOCKET socket_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 64 | |
[email protected] | 836dff3 | 2008-08-05 23:15:36 | [diff] [blame] | 65 | // The list of addresses we should try in order to establish a connection. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 66 | AddressList addresses_; |
| 67 | |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame^] | 68 | // Where we are in above list, or NULL if all addrinfos have been tried. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 69 | const struct addrinfo* current_ai_; |
| 70 | |
| 71 | enum WaitState { |
| 72 | NOT_WAITING, |
| 73 | WAITING_CONNECT, |
| 74 | WAITING_READ, |
| 75 | WAITING_WRITE |
| 76 | }; |
| 77 | WaitState wait_state_; |
[email protected] | 36987e9 | 2008-09-18 18:46:26 | [diff] [blame^] | 78 | |
| 79 | #if defined(OS_WIN) |
| 80 | // base::ObjectWatcher::Delegate methods: |
| 81 | virtual void OnObjectSignaled(HANDLE object); |
| 82 | |
| 83 | OVERLAPPED overlapped_; |
| 84 | WSABUF buffer_; |
| 85 | |
| 86 | base::ObjectWatcher watcher_; |
| 87 | #elif defined(OS_POSIX) |
| 88 | // The socket's libevent wrapper |
| 89 | scoped_ptr<event> event_; |
| 90 | |
| 91 | // Called by MessagePumpLibevent when the socket is ready to do I/O |
| 92 | void OnSocketReady(short flags); |
| 93 | |
| 94 | // The buffer used by OnSocketReady to retry Read and Write requests |
| 95 | char* buf_; |
| 96 | int buf_len_; |
| 97 | #endif |
| 98 | |
| 99 | // External callback; called when read or write is complete. |
| 100 | CompletionCallback* callback_; |
| 101 | |
| 102 | int CreateSocket(const struct addrinfo* ai); |
| 103 | void DoCallback(int rv); |
| 104 | void DidCompleteConnect(); |
| 105 | void DidCompleteIO(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | } // namespace net |
| 109 | |
| 110 | #endif // NET_BASE_TCP_CLIENT_SOCKET_H_ |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 111 | |