| 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 #include <sys/socket.h> // for struct sockaddr |
| 16 #define SOCKET int | 16 #define SOCKET int |
| 17 #include "base/message_loop.h" | 17 #include "base/message_pump_libevent.h" |
| 18 #endif | 18 #endif |
| 19 | 19 |
| 20 #include "base/scoped_ptr.h" | 20 #include "base/scoped_ptr.h" |
| 21 #include "net/base/address_list.h" | 21 #include "net/base/address_list.h" |
| 22 #include "net/base/client_socket.h" | 22 #include "net/base/client_socket.h" |
| 23 #include "net/base/completion_callback.h" | 23 #include "net/base/completion_callback.h" |
| 24 | 24 |
| 25 namespace net { | 25 namespace net { |
| 26 | 26 |
| 27 // A client socket that uses TCP as the transport layer. | 27 // A client socket that uses TCP as the transport layer. |
| 28 // | 28 // |
| 29 // NOTE: The windows implementation supports half duplex only. | 29 // NOTE: The windows implementation supports half duplex only. |
| 30 // Read and Write calls 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 | 31 // The libevent implementation supports full duplex because that |
| 32 // made it slightly easier to implement ssl. | 32 // made it slightly easier to implement ssl. |
| 33 class TCPClientSocket : public ClientSocket, | 33 class TCPClientSocket : public ClientSocket, |
| 34 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
| 35 public base::ObjectWatcher::Delegate | 35 public base::ObjectWatcher::Delegate |
| 36 #elif defined(OS_POSIX) | 36 #elif defined(OS_POSIX) |
| 37 public MessageLoopForIO::Watcher | 37 public base::MessagePumpLibevent::Watcher |
| 38 #endif | 38 #endif |
| 39 { | 39 { |
| 40 public: | 40 public: |
| 41 // 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 |
| 42 // 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 |
| 43 // connection. | 43 // connection. |
| 44 explicit TCPClientSocket(const AddressList& addresses); | 44 explicit TCPClientSocket(const AddressList& addresses); |
| 45 | 45 |
| 46 ~TCPClientSocket(); | 46 ~TCPClientSocket(); |
| 47 | 47 |
| 48 // ClientSocket methods: | 48 // ClientSocket methods: |
| 49 virtual int Connect(CompletionCallback* callback); | 49 virtual int Connect(CompletionCallback* callback); |
| 50 virtual int ReconnectIgnoringLastError(CompletionCallback* callback); | 50 virtual int ReconnectIgnoringLastError(CompletionCallback* callback); |
| 51 virtual void Disconnect(); | 51 virtual void Disconnect(); |
| 52 virtual bool IsConnected() const; | 52 virtual bool IsConnected() const; |
| 53 | 53 |
| 54 // Socket methods: | 54 // Socket methods: |
| 55 // Multiple outstanding requests are not supported. | 55 // Multiple outstanding requests are not supported. |
| 56 // Full 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 | 57 // on Windows (but is supported on Linux and Mac for ease of implementation |
| 58 // of SSLClientSocket) | 58 // of SSLClientSocket) |
| 59 virtual int Read(char* buf, int buf_len, CompletionCallback* callback); | 59 virtual int Read(char* buf, int buf_len, CompletionCallback* callback); |
| 60 virtual int Write(const char* buf, int buf_len, CompletionCallback* callback); | 60 virtual int Write(const char* buf, int buf_len, CompletionCallback* callback); |
| 61 | 61 |
| 62 #if defined(OS_POSIX) | 62 #if defined(OS_POSIX) |
| 63 // Identical to posix system call of same name | 63 // Identical to posix system call of same name |
| 64 // Needed by ssl_client_socket_nss | 64 // Needed by ssl_client_socket_nss |
| 65 virtual int GetPeerName(struct sockaddr *name, socklen_t *namelen); | 65 virtual int GetPeerName(struct sockaddr *name, socklen_t *namelen); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 90 WSABUF buffer_; | 90 WSABUF buffer_; |
| 91 | 91 |
| 92 base::ObjectWatcher watcher_; | 92 base::ObjectWatcher watcher_; |
| 93 | 93 |
| 94 void DidCompleteIO(); | 94 void DidCompleteIO(); |
| 95 #elif defined(OS_POSIX) | 95 #elif defined(OS_POSIX) |
| 96 // Whether we're currently waiting for connect() to complete | 96 // Whether we're currently waiting for connect() to complete |
| 97 bool waiting_connect_; | 97 bool waiting_connect_; |
| 98 | 98 |
| 99 // The socket's libevent wrapper | 99 // The socket's libevent wrapper |
| 100 MessageLoopForIO::FileDescriptorWatcher socket_watcher_; | 100 scoped_ptr<event> event_; |
| 101 | 101 |
| 102 // 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 |
| 103 void OnFileCanReadWithoutBlocking(int fd); | 103 void OnSocketReady(short flags); |
| 104 void OnFileCanWriteWithoutBlocking(int fd); | |
| 105 | 104 |
| 106 // The buffer used by OnSocketReady to retry Read requests | 105 // The buffer used by OnSocketReady to retry Read requests |
| 107 char* buf_; | 106 char* buf_; |
| 108 int buf_len_; | 107 int buf_len_; |
| 109 | 108 |
| 110 // The buffer used by OnSocketReady to retry Write requests | 109 // The buffer used by OnSocketReady to retry Write requests |
| 111 const char* write_buf_; | 110 const char* write_buf_; |
| 112 int write_buf_len_; | 111 int write_buf_len_; |
| 113 | 112 |
| 114 // External callback; called when write is complete. | 113 // External callback; called when write is complete. |
| 115 CompletionCallback* write_callback_; | 114 CompletionCallback* write_callback_; |
| 116 | 115 |
| 117 void DoWriteCallback(int rv); | 116 void DoWriteCallback(int rv); |
| 118 void DidCompleteRead(); | 117 void DidCompleteRead(); |
| 119 void DidCompleteWrite(); | 118 void DidCompleteWrite(); |
| 120 #endif | 119 #endif |
| 121 | 120 |
| 122 // External callback; called when read (and on Windows, write) is complete. | 121 // External callback; called when read (and on Windows, write) is complete. |
| 123 CompletionCallback* callback_; | 122 CompletionCallback* callback_; |
| 124 | 123 |
| 125 int CreateSocket(const struct addrinfo* ai); | 124 int CreateSocket(const struct addrinfo* ai); |
| 126 void DoCallback(int rv); | 125 void DoCallback(int rv); |
| 127 void DidCompleteConnect(); | 126 void DidCompleteConnect(); |
| 128 }; | 127 }; |
| 129 | 128 |
| 130 } // namespace net | 129 } // namespace net |
| 131 | 130 |
| 132 #endif // NET_BASE_TCP_CLIENT_SOCKET_H_ | 131 #endif // NET_BASE_TCP_CLIENT_SOCKET_H_ |
| 133 | 132 |
| OLD | NEW |