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 #if defined(OS_WIN) |
8 #include <ws2tcpip.h> | 9 #include <ws2tcpip.h> |
9 | |
10 #include "base/object_watcher.h" | 10 #include "base/object_watcher.h" |
| 11 #endif |
11 #include "net/base/address_list.h" | 12 #include "net/base/address_list.h" |
12 #include "net/base/client_socket.h" | 13 #include "net/base/client_socket.h" |
13 | 14 |
| 15 #if defined(OS_LINUX) |
| 16 // From libevent. (Why did they pick such a generic name?) |
| 17 struct event; |
| 18 |
| 19 // The callback we pass to libevent for everything |
| 20 extern "C" void TCPClientSocket_libevent_cb(int socket, short flags, void *conte
xt); |
| 21 #endif |
| 22 |
14 namespace net { | 23 namespace net { |
15 | 24 |
16 // A client socket that uses TCP as the transport layer. | 25 // A client socket that uses TCP as the transport layer. |
17 // | 26 // |
18 // NOTE: The implementation supports half duplex only. Read and Write calls | 27 // NOTE: The implementation supports half duplex only. Read and Write calls |
19 // must not be in progress at the same time. | 28 // must not be in progress at the same time. |
20 class TCPClientSocket : public ClientSocket, | 29 class TCPClientSocket : public ClientSocket |
21 public base::ObjectWatcher::Delegate { | 30 #ifdef OS_WIN |
| 31 , public base::ObjectWatcher::Delegate |
| 32 #endif |
| 33 { |
22 public: | 34 public: |
23 // The IP address(es) and port number to connect to. The TCP socket will try | 35 // 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 | 36 // each IP address in the list until it succeeds in establishing a |
25 // connection. | 37 // connection. |
26 explicit TCPClientSocket(const AddressList& addresses); | 38 explicit TCPClientSocket(const AddressList& addresses); |
27 | 39 |
28 ~TCPClientSocket(); | 40 ~TCPClientSocket(); |
29 | 41 |
30 // ClientSocket methods: | 42 // ClientSocket methods: |
31 virtual int Connect(CompletionCallback* callback); | 43 virtual int Connect(CompletionCallback* callback); |
32 virtual int ReconnectIgnoringLastError(CompletionCallback* callback); | 44 virtual int ReconnectIgnoringLastError(CompletionCallback* callback); |
33 virtual void Disconnect(); | 45 virtual void Disconnect(); |
34 virtual bool IsConnected() const; | 46 virtual bool IsConnected() const; |
35 | 47 |
36 // Socket methods: | 48 // Socket methods: |
| 49 // Try to transfer buf_len bytes to/from socket. |
| 50 // If a result is available immediately, return it; otherwise call back later
with the result. |
| 51 // If any bytes were transferred, the result is the byte count. |
| 52 // If there was an error, the result is a negative error code; see net/base/ne
t_error_list.h |
| 53 // TODO: what would a zero return value indicate? |
37 virtual int Read(char* buf, int buf_len, CompletionCallback* callback); | 54 virtual int Read(char* buf, int buf_len, CompletionCallback* callback); |
38 virtual int Write(const char* buf, int buf_len, CompletionCallback* callback); | 55 virtual int Write(const char* buf, int buf_len, CompletionCallback* callback); |
39 | 56 |
40 private: | 57 private: |
41 int CreateSocket(const struct addrinfo* ai); | 58 int CreateSocket(const struct addrinfo* ai); |
42 void DoCallback(int rv); | 59 void DoCallback(int rv); |
43 void DidCompleteConnect(); | 60 void DidCompleteConnect(); |
44 void DidCompleteIO(); | 61 void DidCompleteIO(); |
45 | 62 |
| 63 #ifdef OS_WIN |
46 // base::ObjectWatcher::Delegate methods: | 64 // base::ObjectWatcher::Delegate methods: |
47 virtual void OnObjectSignaled(HANDLE object); | 65 virtual void OnObjectSignaled(HANDLE object); |
48 | 66 |
49 SOCKET socket_; | 67 SOCKET socket_; |
50 OVERLAPPED overlapped_; | 68 OVERLAPPED overlapped_; |
51 WSABUF buffer_; | 69 WSABUF buffer_; |
52 | 70 |
53 base::ObjectWatcher watcher_; | 71 base::ObjectWatcher watcher_; |
| 72 #else |
| 73 friend void ::TCPClientSocket_libevent_cb(int socket, short flags, void *conte
xt); |
| 74 public: // FIXME: friend isn't working, so make these public |
| 75 void OnLibeventNotification(short flags); |
| 76 int socket_; |
| 77 private: |
| 78 ::event* event_; |
| 79 char *buf_; |
| 80 int buf_len_; |
| 81 |
| 82 #endif |
54 | 83 |
55 CompletionCallback* callback_; | 84 CompletionCallback* callback_; |
56 | 85 |
57 // The list of addresses we should try in order to establish a connection. | 86 // The list of addresses we should try in order to establish a connection. |
58 AddressList addresses_; | 87 AddressList addresses_; |
59 | 88 |
60 // The addrinfo that we are attempting to use or NULL if all addrinfos have | 89 // The addrinfo that we are attempting to use or NULL if all addrinfos have |
61 // been tried. | 90 // been tried. |
62 const struct addrinfo* current_ai_; | 91 const struct addrinfo* current_ai_; |
63 | 92 |
64 enum WaitState { | 93 enum WaitState { |
65 NOT_WAITING, | 94 NOT_WAITING, |
66 WAITING_CONNECT, | 95 WAITING_CONNECT, |
67 WAITING_READ, | 96 WAITING_READ, |
68 WAITING_WRITE | 97 WAITING_WRITE |
69 }; | 98 }; |
70 WaitState wait_state_; | 99 WaitState wait_state_; |
71 }; | 100 }; |
72 | 101 |
73 } // namespace net | 102 } // namespace net |
74 | 103 |
75 #endif // NET_BASE_TCP_CLIENT_SOCKET_H_ | 104 #endif // NET_BASE_TCP_CLIENT_SOCKET_H_ |
76 | 105 |
OLD | NEW |