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" | 8 #include "build/build_config.h" |
9 | 9 |
10 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
11 #include <ws2tcpip.h> | 11 #include <ws2tcpip.h> |
12 #include "base/object_watcher.h" | 12 #include "base/object_watcher.h" |
13 #elif defined(OS_POSIX) | 13 #elif defined(OS_POSIX) |
14 struct event; // From libevent | 14 struct event; // From libevent |
| 15 #include <sys/socket.h> // for struct sockaddr |
15 #define SOCKET int | 16 #define SOCKET int |
16 #include "base/message_pump_libevent.h" | 17 #include "base/message_pump_libevent.h" |
17 #endif | 18 #endif |
18 | 19 |
19 #include "base/scoped_ptr.h" | 20 #include "base/scoped_ptr.h" |
20 #include "net/base/address_list.h" | 21 #include "net/base/address_list.h" |
21 #include "net/base/client_socket.h" | 22 #include "net/base/client_socket.h" |
22 #include "net/base/completion_callback.h" | 23 #include "net/base/completion_callback.h" |
23 | 24 |
24 namespace net { | 25 namespace net { |
25 | 26 |
26 // A client socket that uses TCP as the transport layer. | 27 // A client socket that uses TCP as the transport layer. |
27 // | 28 // |
28 // NOTE: The implementation supports half duplex only. Read and Write calls | 29 // NOTE: The windows implementation supports half duplex only. |
29 // must not be in progress at the same time. | 30 // Read and Write calls must not be in progress at the same time. |
| 31 // The libevent implementation supports full duplex because that |
| 32 // made it slightly easier to implement ssl. |
30 class TCPClientSocket : public ClientSocket, | 33 class TCPClientSocket : public ClientSocket, |
31 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
32 public base::ObjectWatcher::Delegate | 35 public base::ObjectWatcher::Delegate |
33 #elif defined(OS_POSIX) | 36 #elif defined(OS_POSIX) |
34 public base::MessagePumpLibevent::Watcher | 37 public base::MessagePumpLibevent::Watcher |
35 #endif | 38 #endif |
36 { | 39 { |
37 public: | 40 public: |
38 // The IP address(es) and port number to connect to. The TCP socket will try | 41 // 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 | 42 // each IP address in the list until it succeeds in establishing a |
40 // connection. | 43 // connection. |
41 explicit TCPClientSocket(const AddressList& addresses); | 44 explicit TCPClientSocket(const AddressList& addresses); |
42 | 45 |
43 ~TCPClientSocket(); | 46 ~TCPClientSocket(); |
44 | 47 |
45 // ClientSocket methods: | 48 // ClientSocket methods: |
46 virtual int Connect(CompletionCallback* callback); | 49 virtual int Connect(CompletionCallback* callback); |
47 virtual int ReconnectIgnoringLastError(CompletionCallback* callback); | 50 virtual int ReconnectIgnoringLastError(CompletionCallback* callback); |
48 virtual void Disconnect(); | 51 virtual void Disconnect(); |
49 virtual bool IsConnected() const; | 52 virtual bool IsConnected() const; |
50 | 53 |
51 // Socket methods: | 54 // Socket methods: |
52 // Multiple outstanding requests are not supported. In particular, full | 55 // Multiple outstanding requests are not supported. |
53 // duplex mode (reading and writing at the same time) is not supported. | 56 // Full duplex mode (reading and writing at the same time) is not supported |
| 57 // on Windows (but is supported on Linux and Mac for ease of implementation |
| 58 // of SSLClientSocket) |
54 virtual int Read(char* buf, int buf_len, CompletionCallback* callback); | 59 virtual int Read(char* buf, int buf_len, CompletionCallback* callback); |
55 virtual int Write(const char* buf, int buf_len, CompletionCallback* callback); | 60 virtual int Write(const char* buf, int buf_len, CompletionCallback* callback); |
56 | 61 |
| 62 #if defined(OS_POSIX) |
| 63 // Identical to posix system call of same name |
| 64 // Needed by ssl_client_socket_nss |
| 65 virtual int GetPeerName(struct sockaddr *name, socklen_t *namelen); |
| 66 #endif |
| 67 |
57 private: | 68 private: |
58 SOCKET socket_; | 69 SOCKET socket_; |
59 | 70 |
60 // The list of addresses we should try in order to establish a connection. | 71 // The list of addresses we should try in order to establish a connection. |
61 AddressList addresses_; | 72 AddressList addresses_; |
62 | 73 |
63 // Where we are in above list, or NULL if all addrinfos have been tried. | 74 // Where we are in above list, or NULL if all addrinfos have been tried. |
64 const struct addrinfo* current_ai_; | 75 const struct addrinfo* current_ai_; |
65 | 76 |
| 77 #if defined(OS_WIN) |
66 enum WaitState { | 78 enum WaitState { |
67 NOT_WAITING, | 79 NOT_WAITING, |
68 WAITING_CONNECT, | 80 WAITING_CONNECT, |
69 WAITING_READ, | 81 WAITING_READ, |
70 WAITING_WRITE | 82 WAITING_WRITE |
71 }; | 83 }; |
72 WaitState wait_state_; | 84 WaitState wait_state_; |
73 | 85 |
74 #if defined(OS_WIN) | |
75 // base::ObjectWatcher::Delegate methods: | 86 // base::ObjectWatcher::Delegate methods: |
76 virtual void OnObjectSignaled(HANDLE object); | 87 virtual void OnObjectSignaled(HANDLE object); |
77 | 88 |
78 OVERLAPPED overlapped_; | 89 OVERLAPPED overlapped_; |
79 WSABUF buffer_; | 90 WSABUF buffer_; |
80 | 91 |
81 base::ObjectWatcher watcher_; | 92 base::ObjectWatcher watcher_; |
| 93 |
| 94 void DidCompleteIO(); |
82 #elif defined(OS_POSIX) | 95 #elif defined(OS_POSIX) |
| 96 // Whether we're currently waiting for connect() to complete |
| 97 bool waiting_connect_; |
| 98 |
83 // The socket's libevent wrapper | 99 // The socket's libevent wrapper |
84 scoped_ptr<event> event_; | 100 scoped_ptr<event> event_; |
85 | 101 |
86 // Called by MessagePumpLibevent when the socket is ready to do I/O | 102 // Called by MessagePumpLibevent when the socket is ready to do I/O |
87 void OnSocketReady(short flags); | 103 void OnSocketReady(short flags); |
88 | 104 |
89 // The buffer used by OnSocketReady to retry Read and Write requests | 105 // The buffer used by OnSocketReady to retry Read requests |
90 char* buf_; | 106 char* buf_; |
91 int buf_len_; | 107 int buf_len_; |
| 108 |
| 109 // The buffer used by OnSocketReady to retry Write requests |
| 110 const char* write_buf_; |
| 111 int write_buf_len_; |
| 112 |
| 113 // External callback; called when write is complete. |
| 114 CompletionCallback* write_callback_; |
| 115 |
| 116 void DoWriteCallback(int rv); |
| 117 void DidCompleteRead(); |
| 118 void DidCompleteWrite(); |
92 #endif | 119 #endif |
93 | 120 |
94 // External callback; called when read or write is complete. | 121 // External callback; called when read (and on Windows, write) is complete. |
95 CompletionCallback* callback_; | 122 CompletionCallback* callback_; |
96 | 123 |
97 int CreateSocket(const struct addrinfo* ai); | 124 int CreateSocket(const struct addrinfo* ai); |
98 void DoCallback(int rv); | 125 void DoCallback(int rv); |
99 void DidCompleteConnect(); | 126 void DidCompleteConnect(); |
100 void DidCompleteIO(); | |
101 }; | 127 }; |
102 | 128 |
103 } // namespace net | 129 } // namespace net |
104 | 130 |
105 #endif // NET_BASE_TCP_CLIENT_SOCKET_H_ | 131 #endif // NET_BASE_TCP_CLIENT_SOCKET_H_ |
106 | 132 |
OLD | NEW |