| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_UDP_UDP_SOCKET_LIBEVENT_H_ | 5 #ifndef NET_UDP_UDP_SOCKET_LIBEVENT_H_ |
| 6 #define NET_UDP_UDP_SOCKET_LIBEVENT_H_ | 6 #define NET_UDP_UDP_SOCKET_LIBEVENT_H_ |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 // |callback| is the user callback function to call on complete. | 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. | 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| | 89 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| |
| 90 // alive until the callback is called. | 90 // alive until the callback is called. |
| 91 int SendTo(IOBuffer* buf, | 91 int SendTo(IOBuffer* buf, |
| 92 int buf_len, | 92 int buf_len, |
| 93 const IPEndPoint& address, | 93 const IPEndPoint& address, |
| 94 const CompletionCallback& callback); | 94 const CompletionCallback& callback); |
| 95 | 95 |
| 96 // Set the receive buffer size (in bytes) for the socket. | 96 // Set the receive buffer size (in bytes) for the socket. |
| 97 // Returns a net error code. |
| 97 int SetReceiveBufferSize(int32 size); | 98 int SetReceiveBufferSize(int32 size); |
| 98 | 99 |
| 99 // Set the send buffer size (in bytes) for the socket. | 100 // Set the send buffer size (in bytes) for the socket. |
| 101 // Returns a net error code. |
| 100 int SetSendBufferSize(int32 size); | 102 int SetSendBufferSize(int32 size); |
| 101 | 103 |
| 102 // Returns true if the socket is already connected or bound. | 104 // Returns true if the socket is already connected or bound. |
| 103 bool is_connected() const { return socket_ != kInvalidSocket; } | 105 bool is_connected() const { return socket_ != kInvalidSocket; } |
| 104 | 106 |
| 105 const BoundNetLog& NetLog() const { return net_log_; } | 107 const BoundNetLog& NetLog() const { return net_log_; } |
| 106 | 108 |
| 107 // Sets corresponding flags in |socket_options_| to allow the socket | 109 // Sets corresponding flags in |socket_options_| to allow the socket |
| 108 // to share the local address to which the socket will be bound with | 110 // to share the local address to which the socket will be bound with |
| 109 // other processes. Should be called before Bind(). | 111 // other processes. Should be called before Bind(). |
| 110 void AllowAddressReuse(); | 112 void AllowAddressReuse(); |
| 111 | 113 |
| 112 // Sets corresponding flags in |socket_options_| to allow sending | 114 // Sets corresponding flags in |socket_options_| to allow or disallow |
| 113 // and receiving packets to and from broadcast addresses. Should be | 115 // sending and receiving packets to and from broadcast addresses. |
| 114 // called before Bind(). | 116 // Returns a net error code. |
| 115 void AllowBroadcast(); | 117 int SetBroadcast(bool broadcast); |
| 116 | 118 |
| 117 // Join the multicast group. | 119 // Join the multicast group. |
| 118 // |group_address| is the group address to join, could be either | 120 // |group_address| is the group address to join, could be either |
| 119 // an IPv4 or IPv6 address. | 121 // an IPv4 or IPv6 address. |
| 120 // Return a network error code. | 122 // Return a network error code. |
| 121 int JoinGroup(const IPAddressNumber& group_address) const; | 123 int JoinGroup(const IPAddressNumber& group_address) const; |
| 122 | 124 |
| 123 // Leave the multicast group. | 125 // Leave the multicast group. |
| 124 // |group_address| is the group address to leave, could be either | 126 // |group_address| is the group address to leave, could be either |
| 125 // an IPv4 or IPv6 address. If the socket hasn't joined the group, | 127 // an IPv4 or IPv6 address. If the socket hasn't joined the group, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 // Return a network error code. | 165 // Return a network error code. |
| 164 int SetDiffServCodePoint(DiffServCodePoint dscp); | 166 int SetDiffServCodePoint(DiffServCodePoint dscp); |
| 165 | 167 |
| 166 // Resets the thread to be used for thread-safety checks. | 168 // Resets the thread to be used for thread-safety checks. |
| 167 void DetachFromThread(); | 169 void DetachFromThread(); |
| 168 | 170 |
| 169 private: | 171 private: |
| 170 enum SocketOptions { | 172 enum SocketOptions { |
| 171 SOCKET_OPTION_REUSE_ADDRESS = 1 << 0, | 173 SOCKET_OPTION_REUSE_ADDRESS = 1 << 0, |
| 172 SOCKET_OPTION_BROADCAST = 1 << 1, | 174 SOCKET_OPTION_BROADCAST = 1 << 1, |
| 173 SOCKET_OPTION_MULTICAST_LOOP = 1 << 2 | 175 SOCKET_OPTION_MULTICAST_LOOP = 1 << 2, |
| 176 SOCKET_OPTION_RCVBUF_SIZE = 1 << 3, |
| 177 SOCKET_OPTION_SNDBUF_SIZE = 1 << 4 |
| 174 }; | 178 }; |
| 175 | 179 |
| 176 class ReadWatcher : public base::MessageLoopForIO::Watcher { | 180 class ReadWatcher : public base::MessageLoopForIO::Watcher { |
| 177 public: | 181 public: |
| 178 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} | 182 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} |
| 179 | 183 |
| 180 // MessageLoopForIO::Watcher methods | 184 // MessageLoopForIO::Watcher methods |
| 181 | 185 |
| 182 void OnFileCanReadWithoutBlocking(int /* fd */) override; | 186 void OnFileCanReadWithoutBlocking(int /* fd */) override; |
| 183 | 187 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 int SendToOrWrite(IOBuffer* buf, | 231 int SendToOrWrite(IOBuffer* buf, |
| 228 int buf_len, | 232 int buf_len, |
| 229 const IPEndPoint* address, | 233 const IPEndPoint* address, |
| 230 const CompletionCallback& callback); | 234 const CompletionCallback& callback); |
| 231 | 235 |
| 232 int InternalConnect(const IPEndPoint& address); | 236 int InternalConnect(const IPEndPoint& address); |
| 233 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); | 237 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); |
| 234 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); | 238 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); |
| 235 | 239 |
| 236 // Applies |socket_options_| to |socket_|. Should be called before | 240 // Applies |socket_options_| to |socket_|. Should be called before |
| 237 // Bind(). | 241 // DoBind(). |
| 238 int SetSocketOptions(); | 242 int SetSocketOptions(); |
| 243 int AllowAddressReuseInternal(); |
| 244 int SetBroadcastInternal(bool broadcast); |
| 245 int SetReceiveBufferSizeInternal(int32 size); |
| 246 int SetSendBufferSizeInternal(int32 size); |
| 247 |
| 239 int DoBind(const IPEndPoint& address); | 248 int DoBind(const IPEndPoint& address); |
| 240 // Binds to a random port on |address|. | 249 // Binds to a random port on |address|. |
| 241 int RandomBind(const IPAddressNumber& address); | 250 int RandomBind(const IPAddressNumber& address); |
| 242 | 251 |
| 243 int socket_; | 252 int socket_; |
| 244 int addr_family_; | 253 int addr_family_; |
| 245 | 254 |
| 246 // Bitwise-or'd combination of SocketOptions. Specifies the set of | 255 // Bitwise-or'd combination of SocketOptions. Specifies the set of |
| 247 // options that should be applied to |socket_| before Bind(). | 256 // options that should be applied to |socket_| before Bind(). |
| 248 int socket_options_; | 257 int socket_options_; |
| 249 | 258 |
| 259 // Locally cached value of buffer size. These are automatically applied |
| 260 // when SetSocketOptions() is called. |
| 261 int32 rcvbuf_size_; |
| 262 int32 sndbuf_size_; |
| 263 |
| 250 // Multicast interface. | 264 // Multicast interface. |
| 251 uint32 multicast_interface_; | 265 uint32 multicast_interface_; |
| 252 | 266 |
| 253 // Multicast socket options cached for SetSocketOption. | 267 // Multicast socket options cached for SetSocketOption. |
| 254 // Cannot be used after Bind(). | 268 // Cannot be used after Bind(). |
| 255 int multicast_time_to_live_; | 269 int multicast_time_to_live_; |
| 256 | 270 |
| 257 // How to do source port binding, used only when UDPSocket is part of | 271 // How to do source port binding, used only when UDPSocket is part of |
| 258 // UDPClientSocket, since UDPServerSocket provides Bind. | 272 // UDPClientSocket, since UDPServerSocket provides Bind. |
| 259 DatagramSocket::BindType bind_type_; | 273 DatagramSocket::BindType bind_type_; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 CompletionCallback write_callback_; | 305 CompletionCallback write_callback_; |
| 292 | 306 |
| 293 BoundNetLog net_log_; | 307 BoundNetLog net_log_; |
| 294 | 308 |
| 295 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); | 309 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); |
| 296 }; | 310 }; |
| 297 | 311 |
| 298 } // namespace net | 312 } // namespace net |
| 299 | 313 |
| 300 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ | 314 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ |
| OLD | NEW |