OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_BASE_TCP_CLIENT_SOCKET_H_ | 5 #ifndef NET_BASE_TCP_CLIENT_SOCKET_H_ |
6 #define NET_BASE_TCP_CLIENT_SOCKET_H_ | 6 #define NET_BASE_TCP_CLIENT_SOCKET_H_ |
7 | 7 |
| 8 #include "build/build_config.h" |
| 9 |
| 10 #if defined(OS_WIN) |
8 #include <ws2tcpip.h> | 11 #include <ws2tcpip.h> |
| 12 #include "base/object_watcher.h" |
| 13 #elif defined(OS_POSIX) |
| 14 struct event; // From libevent |
| 15 #define SOCKET int |
| 16 #include "base/message_pump_libevent.h" |
| 17 #endif |
9 | 18 |
10 #include "base/object_watcher.h" | 19 #include "base/completion_callback.h" |
| 20 #include "base/scoped_ptr.h" |
11 #include "net/base/address_list.h" | 21 #include "net/base/address_list.h" |
12 #include "net/base/client_socket.h" | 22 #include "net/base/client_socket.h" |
13 | 23 |
14 namespace net { | 24 namespace net { |
15 | 25 |
16 // A client socket that uses TCP as the transport layer. | 26 // A client socket that uses TCP as the transport layer. |
17 // | 27 // |
18 // NOTE: The implementation supports half duplex only. Read and Write calls | 28 // NOTE: The implementation supports half duplex only. Read and Write calls |
19 // must not be in progress at the same time. | 29 // must not be in progress at the same time. |
20 class TCPClientSocket : public ClientSocket, | 30 class TCPClientSocket : public ClientSocket, |
21 public base::ObjectWatcher::Delegate { | 31 #if defined(OS_WIN) |
| 32 public base::ObjectWatcher::Delegate |
| 33 #elif defined(OS_POSIX) |
| 34 public base::MessagePumpLibevent::Watcher |
| 35 #endif |
| 36 { |
22 public: | 37 public: |
23 // The IP address(es) and port number to connect to. The TCP socket will try | 38 // 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 | 39 // each IP address in the list until it succeeds in establishing a |
25 // connection. | 40 // connection. |
26 explicit TCPClientSocket(const AddressList& addresses); | 41 explicit TCPClientSocket(const AddressList& addresses); |
27 | 42 |
28 ~TCPClientSocket(); | 43 ~TCPClientSocket(); |
29 | 44 |
30 // ClientSocket methods: | 45 // ClientSocket methods: |
31 virtual int Connect(CompletionCallback* callback); | 46 virtual int Connect(CompletionCallback* callback); |
32 virtual int ReconnectIgnoringLastError(CompletionCallback* callback); | 47 virtual int ReconnectIgnoringLastError(CompletionCallback* callback); |
33 virtual void Disconnect(); | 48 virtual void Disconnect(); |
34 virtual bool IsConnected() const; | 49 virtual bool IsConnected() const; |
35 | 50 |
36 // Socket methods: | 51 // Socket methods: |
| 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? |
37 virtual int Read(char* buf, int buf_len, CompletionCallback* callback); | 59 virtual int Read(char* buf, int buf_len, CompletionCallback* callback); |
38 virtual int Write(const char* buf, int buf_len, CompletionCallback* callback); | 60 virtual int Write(const char* buf, int buf_len, CompletionCallback* callback); |
39 | 61 |
40 private: | 62 private: |
41 int CreateSocket(const struct addrinfo* ai); | |
42 void DoCallback(int rv); | |
43 void DidCompleteConnect(); | |
44 void DidCompleteIO(); | |
45 | |
46 // base::ObjectWatcher::Delegate methods: | |
47 virtual void OnObjectSignaled(HANDLE object); | |
48 | |
49 SOCKET socket_; | 63 SOCKET socket_; |
50 OVERLAPPED overlapped_; | |
51 WSABUF buffer_; | |
52 | |
53 base::ObjectWatcher watcher_; | |
54 | |
55 CompletionCallback* callback_; | |
56 | 64 |
57 // The list of addresses we should try in order to establish a connection. | 65 // The list of addresses we should try in order to establish a connection. |
58 AddressList addresses_; | 66 AddressList addresses_; |
59 | 67 |
60 // The addrinfo that we are attempting to use or NULL if all addrinfos have | 68 // Where we are in above list, or NULL if all addrinfos have been tried. |
61 // been tried. | |
62 const struct addrinfo* current_ai_; | 69 const struct addrinfo* current_ai_; |
63 | 70 |
64 enum WaitState { | 71 enum WaitState { |
65 NOT_WAITING, | 72 NOT_WAITING, |
66 WAITING_CONNECT, | 73 WAITING_CONNECT, |
67 WAITING_READ, | 74 WAITING_READ, |
68 WAITING_WRITE | 75 WAITING_WRITE |
69 }; | 76 }; |
70 WaitState wait_state_; | 77 WaitState wait_state_; |
| 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(); |
71 }; | 106 }; |
72 | 107 |
73 } // namespace net | 108 } // namespace net |
74 | 109 |
75 #endif // NET_BASE_TCP_CLIENT_SOCKET_H_ | 110 #endif // NET_BASE_TCP_CLIENT_SOCKET_H_ |
76 | 111 |
OLD | NEW |