| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2009 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. | |
| 4 | |
| 5 #ifndef NET_BASE_TCP_CLIENT_SOCKET_LIBEVENT_H_ | |
| 6 #define NET_BASE_TCP_CLIENT_SOCKET_LIBEVENT_H_ | |
| 7 | |
| 8 #include <sys/socket.h> // for struct sockaddr | |
| 9 | |
| 10 #include "base/message_loop.h" | |
| 11 #include "base/ref_counted.h" | |
| 12 #include "base/scoped_ptr.h" | |
| 13 #include "net/base/address_list.h" | |
| 14 #include "net/base/client_socket.h" | |
| 15 #include "net/base/completion_callback.h" | |
| 16 | |
| 17 struct event; // From libevent | |
| 18 | |
| 19 namespace net { | |
| 20 | |
| 21 // A client socket that uses TCP as the transport layer. | |
| 22 class TCPClientSocketLibevent : public ClientSocket { | |
| 23 public: | |
| 24 // The IP address(es) and port number to connect to. The TCP socket will try | |
| 25 // each IP address in the list until it succeeds in establishing a | |
| 26 // connection. | |
| 27 explicit TCPClientSocketLibevent(const AddressList& addresses); | |
| 28 | |
| 29 virtual ~TCPClientSocketLibevent(); | |
| 30 | |
| 31 // ClientSocket methods: | |
| 32 virtual int Connect(CompletionCallback* callback); | |
| 33 virtual void Disconnect(); | |
| 34 virtual bool IsConnected() const; | |
| 35 virtual bool IsConnectedAndIdle() const; | |
| 36 | |
| 37 // Socket methods: | |
| 38 // Multiple outstanding requests are not supported. | |
| 39 // Full duplex mode (reading and writing at the same time) is supported | |
| 40 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); | |
| 41 virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); | |
| 42 | |
| 43 // Identical to posix system call of same name | |
| 44 // Needed by ssl_client_socket_nss | |
| 45 virtual int GetPeerName(struct sockaddr *name, socklen_t *namelen); | |
| 46 | |
| 47 private: | |
| 48 class ReadWatcher : public MessageLoopForIO::Watcher { | |
| 49 public: | |
| 50 explicit ReadWatcher(TCPClientSocketLibevent* socket) : socket_(socket) {} | |
| 51 | |
| 52 // MessageLoopForIO::Watcher methods | |
| 53 | |
| 54 virtual void OnFileCanReadWithoutBlocking(int /* fd */) { | |
| 55 if (socket_->read_callback_) | |
| 56 socket_->DidCompleteRead(); | |
| 57 } | |
| 58 | |
| 59 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) {} | |
| 60 | |
| 61 private: | |
| 62 TCPClientSocketLibevent* const socket_; | |
| 63 | |
| 64 DISALLOW_COPY_AND_ASSIGN(ReadWatcher); | |
| 65 }; | |
| 66 | |
| 67 class WriteWatcher : public MessageLoopForIO::Watcher { | |
| 68 public: | |
| 69 explicit WriteWatcher(TCPClientSocketLibevent* socket) : socket_(socket) {} | |
| 70 | |
| 71 // MessageLoopForIO::Watcher methods | |
| 72 | |
| 73 virtual void OnFileCanReadWithoutBlocking(int /* fd */) {} | |
| 74 | |
| 75 virtual void OnFileCanWriteWithoutBlocking(int /* fd */) { | |
| 76 if (socket_->waiting_connect_) { | |
| 77 socket_->DidCompleteConnect(); | |
| 78 } else if (socket_->write_callback_) { | |
| 79 socket_->DidCompleteWrite(); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 private: | |
| 84 TCPClientSocketLibevent* const socket_; | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(WriteWatcher); | |
| 87 }; | |
| 88 | |
| 89 void DoReadCallback(int rv); | |
| 90 void DoWriteCallback(int rv); | |
| 91 void DidCompleteRead(); | |
| 92 void DidCompleteWrite(); | |
| 93 void DidCompleteConnect(); | |
| 94 | |
| 95 int CreateSocket(const struct addrinfo* ai); | |
| 96 | |
| 97 int socket_; | |
| 98 | |
| 99 // The list of addresses we should try in order to establish a connection. | |
| 100 AddressList addresses_; | |
| 101 | |
| 102 // Where we are in above list, or NULL if all addrinfos have been tried. | |
| 103 const struct addrinfo* current_ai_; | |
| 104 | |
| 105 // Whether we're currently waiting for connect() to complete | |
| 106 bool waiting_connect_; | |
| 107 | |
| 108 // The socket's libevent wrappers | |
| 109 MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; | |
| 110 MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; | |
| 111 | |
| 112 // The corresponding watchers for reads and writes. | |
| 113 ReadWatcher read_watcher_; | |
| 114 WriteWatcher write_watcher_; | |
| 115 | |
| 116 // The buffer used by OnSocketReady to retry Read requests | |
| 117 scoped_refptr<IOBuffer> read_buf_; | |
| 118 int read_buf_len_; | |
| 119 | |
| 120 // The buffer used by OnSocketReady to retry Write requests | |
| 121 scoped_refptr<IOBuffer> write_buf_; | |
| 122 int write_buf_len_; | |
| 123 | |
| 124 // External callback; called when read is complete. | |
| 125 CompletionCallback* read_callback_; | |
| 126 | |
| 127 // External callback; called when write is complete. | |
| 128 CompletionCallback* write_callback_; | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(TCPClientSocketLibevent); | |
| 131 }; | |
| 132 | |
| 133 } // namespace net | |
| 134 | |
| 135 #endif // NET_BASE_TCP_CLIENT_SOCKET_LIBEVENT_H_ | |
| OLD | NEW |