[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 1 | // Copyright (c) 2011 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_UDP_UDP_SOCKET_LIBEVENT_H_ |
| 6 | #define NET_UDP_UDP_SOCKET_LIBEVENT_H_ |
| 7 | #pragma once |
| 8 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 9 | #include "base/memory/ref_counted.h" |
| 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 11 | #include "base/message_loop.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 12 | #include "base/threading/non_thread_safe.h" |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame^] | 13 | #include "net/base/address_list_net_log_param.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 14 | #include "net/base/completion_callback.h" |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 15 | #include "net/base/rand_callback.h" |
| 16 | #include "net/base/io_buffer.h" |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 17 | #include "net/base/ip_endpoint.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 18 | #include "net/base/net_log.h" |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 19 | #include "net/udp/datagram_socket.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 20 | |
| 21 | namespace net { |
| 22 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 23 | class UDPSocketLibevent : public base::NonThreadSafe { |
| 24 | public: |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 25 | UDPSocketLibevent(DatagramSocket::BindType bind_type, |
| 26 | const RandIntCallback& rand_int_cb, |
| 27 | net::NetLog* net_log, |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 28 | const net::NetLog::Source& source); |
| 29 | virtual ~UDPSocketLibevent(); |
| 30 | |
| 31 | // Connect the socket to connect with a certain |address|. |
| 32 | // Returns a net error code. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 33 | int Connect(const IPEndPoint& address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 34 | |
| 35 | // Bind the address/port for this socket to |address|. This is generally |
| 36 | // only used on a server. |
| 37 | // Returns a net error code. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 38 | int Bind(const IPEndPoint& address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 39 | |
| 40 | // Close the socket. |
| 41 | void Close(); |
| 42 | |
| 43 | // Copy the remote udp address into |address| and return a network error code. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 44 | int GetPeerAddress(IPEndPoint* address) const; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 45 | |
| 46 | // Copy the local udp address into |address| and return a network error code. |
| 47 | // (similar to getsockname) |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 48 | int GetLocalAddress(IPEndPoint* address) const; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 49 | |
| 50 | // IO: |
| 51 | // Multiple outstanding read requests are not supported. |
| 52 | // Full duplex mode (reading and writing at the same time) is supported |
| 53 | |
| 54 | // Read from the socket. |
| 55 | // Only usable from the client-side of a UDP socket, after the socket |
| 56 | // has been connected. |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 57 | int Read(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 58 | |
| 59 | // Write to the socket. |
| 60 | // Only usable from the client-side of a UDP socket, after the socket |
| 61 | // has been connected. |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 62 | int Write(IOBuffer* buf, int buf_len, OldCompletionCallback* callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 63 | |
| 64 | // Read from a socket and receive sender address information. |
| 65 | // |buf| is the buffer to read data into. |
| 66 | // |buf_len| is the maximum amount of data to read. |
| 67 | // |address| is a buffer provided by the caller for receiving the sender |
| 68 | // address information about the received data. This buffer must be kept |
| 69 | // alive by the caller until the callback is placed. |
| 70 | // |address_length| is a ptr to the length of the |address| buffer. This |
| 71 | // is an input parameter containing the maximum size |address| can hold |
| 72 | // and also an output parameter for the size of |address| upon completion. |
| 73 | // |callback| the callback on completion of the Recv. |
| 74 | // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. |
| 75 | // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|, |
| 76 | // and |address_length| alive until the callback is called. |
| 77 | int RecvFrom(IOBuffer* buf, |
| 78 | int buf_len, |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 79 | IPEndPoint* address, |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 80 | OldCompletionCallback* callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 81 | |
| 82 | // Send to a socket with a particular destination. |
| 83 | // |buf| is the buffer to send |
| 84 | // |buf_len| is the number of bytes to send |
| 85 | // |address| is the recipient address. |
| 86 | // |address_length| is the size of the recipient address |
| 87 | // |callback| is the user callback function to call on complete. |
| 88 | // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. |
| 89 | // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| |
| 90 | // alive until the callback is called. |
| 91 | int SendTo(IOBuffer* buf, |
| 92 | int buf_len, |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 93 | const IPEndPoint& address, |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 94 | OldCompletionCallback* callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 95 | |
[email protected] | df31da4 | 2011-10-18 01:44:40 | [diff] [blame] | 96 | // Set the receive buffer size (in bytes) for the socket. |
| 97 | bool SetReceiveBufferSize(int32 size); |
| 98 | |
| 99 | // Set the send buffer size (in bytes) for the socket. |
| 100 | bool SetSendBufferSize(int32 size); |
| 101 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 102 | // Returns true if the socket is already connected or bound. |
| 103 | bool is_connected() const { return socket_ != kInvalidSocket; } |
| 104 | |
[email protected] | eaf10dc | 2011-07-18 21:47:35 | [diff] [blame] | 105 | const BoundNetLog& NetLog() const { return net_log_; } |
| 106 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 107 | private: |
| 108 | static const int kInvalidSocket = -1; |
| 109 | |
| 110 | class ReadWatcher : public MessageLoopForIO::Watcher { |
| 111 | public: |
| 112 | explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} |
| 113 | |
| 114 | // MessageLoopForIO::Watcher methods |
| 115 | |
| 116 | virtual void OnFileCanReadWithoutBlocking(int /* fd */) { |
| 117 | if (socket_->read_callback_) |
| 118 | socket_->DidCompleteRead(); |
| 119 | } |
| 120 | |
| 121 | virtual void OnFileCanWriteWithoutBlocking(int /* fd */) {} |
| 122 | |
| 123 | private: |
| 124 | UDPSocketLibevent* const socket_; |
| 125 | |
| 126 | DISALLOW_COPY_AND_ASSIGN(ReadWatcher); |
| 127 | }; |
| 128 | |
| 129 | class WriteWatcher : public MessageLoopForIO::Watcher { |
| 130 | public: |
| 131 | explicit WriteWatcher(UDPSocketLibevent* socket) : socket_(socket) {} |
| 132 | |
| 133 | // MessageLoopForIO::Watcher methods |
| 134 | |
| 135 | virtual void OnFileCanReadWithoutBlocking(int /* fd */) {} |
| 136 | |
| 137 | virtual void OnFileCanWriteWithoutBlocking(int /* fd */) { |
| 138 | if (socket_->write_callback_) |
| 139 | socket_->DidCompleteWrite(); |
| 140 | } |
| 141 | |
| 142 | private: |
| 143 | UDPSocketLibevent* const socket_; |
| 144 | |
| 145 | DISALLOW_COPY_AND_ASSIGN(WriteWatcher); |
| 146 | }; |
| 147 | |
| 148 | void DoReadCallback(int rv); |
| 149 | void DoWriteCallback(int rv); |
| 150 | void DidCompleteRead(); |
| 151 | void DidCompleteWrite(); |
| 152 | |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame^] | 153 | // Handles stats and logging. |result| is the number of bytes transferred, on |
| 154 | // success, or the net error code on failure. On success, LogRead takes in a |
| 155 | // sockaddr and its length, which are mandatory, while LogWrite takes in an |
| 156 | // optional IPEndPoint. |
| 157 | void LogRead(int result, const char* bytes, socklen_t addr_len, |
| 158 | const sockaddr* addr) const; |
| 159 | void LogWrite(int result, const char* bytes, const IPEndPoint* address) const; |
| 160 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 161 | // Returns the OS error code (or 0 on success). |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 162 | int CreateSocket(const IPEndPoint& address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 163 | |
[email protected] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 164 | // Same as SendTo(), except that address is passed by pointer |
| 165 | // instead of by reference. It is called from Write() with |address| |
| 166 | // set to NULL. |
| 167 | int SendToOrWrite(IOBuffer* buf, |
| 168 | int buf_len, |
| 169 | const IPEndPoint* address, |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 170 | OldCompletionCallback* callback); |
[email protected] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 171 | |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame^] | 172 | int InternalConnect(const IPEndPoint& address); |
[email protected] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 173 | int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); |
| 174 | int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 175 | |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 176 | int DoBind(const IPEndPoint& address); |
| 177 | int RandomBind(const IPEndPoint& address); |
| 178 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 179 | int socket_; |
| 180 | |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 181 | // How to do source port binding, used only when UDPSocket is part of |
| 182 | // UDPClientSocket, since UDPServerSocket provides Bind. |
| 183 | DatagramSocket::BindType bind_type_; |
| 184 | |
| 185 | // PRNG function for generating port numbers. |
| 186 | RandIntCallback rand_int_cb_; |
| 187 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 188 | // These are mutable since they're just cached copies to make |
| 189 | // GetPeerAddress/GetLocalAddress smarter. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 190 | mutable scoped_ptr<IPEndPoint> local_address_; |
| 191 | mutable scoped_ptr<IPEndPoint> remote_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 192 | |
| 193 | // The socket's libevent wrappers |
| 194 | MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; |
| 195 | MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; |
| 196 | |
| 197 | // The corresponding watchers for reads and writes. |
| 198 | ReadWatcher read_watcher_; |
| 199 | WriteWatcher write_watcher_; |
| 200 | |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 201 | // The buffer used by InternalRead() to retry Read requests |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 202 | scoped_refptr<IOBuffer> read_buf_; |
| 203 | int read_buf_len_; |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 204 | IPEndPoint* recv_from_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 205 | |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 206 | // The buffer used by InternalWrite() to retry Write requests |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 207 | scoped_refptr<IOBuffer> write_buf_; |
| 208 | int write_buf_len_; |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 209 | scoped_ptr<IPEndPoint> send_to_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 210 | |
| 211 | // External callback; called when read is complete. |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 212 | OldCompletionCallback* read_callback_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 213 | |
| 214 | // External callback; called when write is complete. |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 215 | OldCompletionCallback* write_callback_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 216 | |
| 217 | BoundNetLog net_log_; |
| 218 | |
| 219 | DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); |
| 220 | }; |
| 221 | |
| 222 | } // namespace net |
| 223 | |
| 224 | #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ |