Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: net/udp/udp_socket_libevent.h

Issue 721273002: Remove timing limitation to set Broadcast, ReceiveBuffer, and SendBuffer options from UDPSocket. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/udp/udp_socket.h ('k') | net/udp/udp_socket_libevent.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "base/threading/non_thread_safe.h" 11 #include "base/threading/non_thread_safe.h"
12 #include "net/base/address_family.h"
12 #include "net/base/completion_callback.h" 13 #include "net/base/completion_callback.h"
13 #include "net/base/io_buffer.h" 14 #include "net/base/io_buffer.h"
14 #include "net/base/ip_endpoint.h" 15 #include "net/base/ip_endpoint.h"
15 #include "net/base/net_export.h" 16 #include "net/base/net_export.h"
16 #include "net/base/net_log.h" 17 #include "net/base/net_log.h"
17 #include "net/base/rand_callback.h" 18 #include "net/base/rand_callback.h"
18 #include "net/socket/socket_descriptor.h" 19 #include "net/socket/socket_descriptor.h"
19 #include "net/udp/datagram_socket.h" 20 #include "net/udp/datagram_socket.h"
20 21
21 namespace net { 22 namespace net {
22 23
23 class NET_EXPORT UDPSocketLibevent : public base::NonThreadSafe { 24 class NET_EXPORT UDPSocketLibevent : public base::NonThreadSafe {
24 public: 25 public:
25 UDPSocketLibevent(DatagramSocket::BindType bind_type, 26 UDPSocketLibevent(DatagramSocket::BindType bind_type,
26 const RandIntCallback& rand_int_cb, 27 const RandIntCallback& rand_int_cb,
27 net::NetLog* net_log, 28 net::NetLog* net_log,
28 const net::NetLog::Source& source); 29 const net::NetLog::Source& source);
29 virtual ~UDPSocketLibevent(); 30 virtual ~UDPSocketLibevent();
30 31
32 // Opens the socket.
33 // Returns a net error code.
34 int Open(AddressFamily address_family);
35
31 // Connect the socket to connect with a certain |address|. 36 // Connect the socket to connect with a certain |address|.
37 // Should be called after Open().
32 // Returns a net error code. 38 // Returns a net error code.
33 int Connect(const IPEndPoint& address); 39 int Connect(const IPEndPoint& address);
34 40
35 // Bind the address/port for this socket to |address|. This is generally 41 // Bind the address/port for this socket to |address|. This is generally
36 // only used on a server. 42 // only used on a server. Should be called after Open().
37 // Returns a net error code. 43 // Returns a net error code.
38 int Bind(const IPEndPoint& address); 44 int Bind(const IPEndPoint& address);
39 45
40 // Close the socket. 46 // Close the socket.
41 void Close(); 47 void Close();
42 48
43 // Copy the remote udp address into |address| and return a network error code. 49 // Copy the remote udp address into |address| and return a net error code.
44 int GetPeerAddress(IPEndPoint* address) const; 50 int GetPeerAddress(IPEndPoint* address) const;
45 51
46 // Copy the local udp address into |address| and return a network error code. 52 // Copy the local udp address into |address| and return a net error code.
47 // (similar to getsockname) 53 // (similar to getsockname)
48 int GetLocalAddress(IPEndPoint* address) const; 54 int GetLocalAddress(IPEndPoint* address) const;
49 55
50 // IO: 56 // IO:
51 // Multiple outstanding read requests are not supported. 57 // Multiple outstanding read requests are not supported.
52 // Full duplex mode (reading and writing at the same time) is supported 58 // Full duplex mode (reading and writing at the same time) is supported
53 59
54 // Read from the socket. 60 // Read from the socket.
55 // Only usable from the client-side of a UDP socket, after the socket 61 // Only usable from the client-side of a UDP socket, after the socket
56 // has been connected. 62 // has been connected.
(...skipping 30 matching lines...) Expand all
87 // |callback| is the user callback function to call on complete. 93 // |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. 94 // 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| 95 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address|
90 // alive until the callback is called. 96 // alive until the callback is called.
91 int SendTo(IOBuffer* buf, 97 int SendTo(IOBuffer* buf,
92 int buf_len, 98 int buf_len,
93 const IPEndPoint& address, 99 const IPEndPoint& address,
94 const CompletionCallback& callback); 100 const CompletionCallback& callback);
95 101
96 // Set the receive buffer size (in bytes) for the socket. 102 // Set the receive buffer size (in bytes) for the socket.
103 // Returns a net error code.
97 int SetReceiveBufferSize(int32 size); 104 int SetReceiveBufferSize(int32 size);
98 105
99 // Set the send buffer size (in bytes) for the socket. 106 // Set the send buffer size (in bytes) for the socket.
107 // Returns a net error code.
100 int SetSendBufferSize(int32 size); 108 int SetSendBufferSize(int32 size);
101 109
102 // Returns true if the socket is already connected or bound. 110 // Returns true if the socket is already connected or bound.
103 bool is_connected() const { return socket_ != kInvalidSocket; } 111 bool is_connected() const { return is_connected_; }
104 112
105 const BoundNetLog& NetLog() const { return net_log_; } 113 const BoundNetLog& NetLog() const { return net_log_; }
106 114
107 // Sets corresponding flags in |socket_options_| to allow the socket 115 // Sets corresponding flags in |socket_options_| to allow the socket
108 // to share the local address to which the socket will be bound with 116 // to share the local address to which the socket will be bound with
109 // other processes. Should be called before Bind(). 117 // other processes. Should be called before Bind().
rvargas (doing something else) 2014/12/03 21:00:16 nit: returns a ...
hidehiko 2014/12/04 04:03:36 Done. Walked though the udp_socket_libevent.h and
rvargas (doing something else) 2014/12/04 19:51:37 I'm sorry I was not clear with this one. Now there
hidehiko 2014/12/08 05:07:40 Oops, I misunderstood your comments. Fixed the com
110 void AllowAddressReuse(); 118 int AllowAddressReuse();
111 119
112 // Sets corresponding flags in |socket_options_| to allow sending 120 // Sets corresponding flags in |socket_options_| to allow or disallow sending
113 // and receiving packets to and from broadcast addresses. Should be 121 // and receiving packets to and from broadcast addresses.
114 // called before Bind(). 122 // Returns a net error code.
115 void AllowBroadcast(); 123 int SetBroadcast(bool broadcast);
116 124
117 // Join the multicast group. 125 // Join the multicast group.
118 // |group_address| is the group address to join, could be either 126 // |group_address| is the group address to join, could be either
119 // an IPv4 or IPv6 address. 127 // an IPv4 or IPv6 address.
120 // Return a network error code. 128 // Return a net error code.
rvargas (doing something else) 2014/12/03 21:00:16 nit: return -> returns
hidehiko 2014/12/04 04:03:36 Done.
121 int JoinGroup(const IPAddressNumber& group_address) const; 129 int JoinGroup(const IPAddressNumber& group_address) const;
122 130
123 // Leave the multicast group. 131 // Leave the multicast group.
124 // |group_address| is the group address to leave, could be either 132 // |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, 133 // an IPv4 or IPv6 address. If the socket hasn't joined the group,
126 // it will be ignored. 134 // it will be ignored.
127 // It's optional to leave the multicast group before destroying 135 // It's optional to leave the multicast group before destroying
128 // the socket. It will be done by the OS. 136 // the socket. It will be done by the OS.
129 // Return a network error code. 137 // Return a net error code.
rvargas (doing something else) 2014/12/03 21:00:16 etc
hidehiko 2014/12/04 04:03:36 Done.
130 int LeaveGroup(const IPAddressNumber& group_address) const; 138 int LeaveGroup(const IPAddressNumber& group_address) const;
131 139
132 // Set interface to use for multicast. If |interface_index| set to 0, default 140 // Set interface to use for multicast. If |interface_index| set to 0, default
133 // interface is used. 141 // interface is used.
134 // Should be called before Bind(). 142 // Should be called before Bind().
135 // Returns a network error code. 143 // Returns a net error code.
136 int SetMulticastInterface(uint32 interface_index); 144 int SetMulticastInterface(uint32 interface_index);
137 145
138 // Set the time-to-live option for UDP packets sent to the multicast 146 // Set the time-to-live option for UDP packets sent to the multicast
139 // group address. The default value of this option is 1. 147 // group address. The default value of this option is 1.
140 // Cannot be negative or more than 255. 148 // Cannot be negative or more than 255.
141 // Should be called before Bind(). 149 // Should be called before Bind().
142 // Return a network error code. 150 // Return a net error code.
143 int SetMulticastTimeToLive(int time_to_live); 151 int SetMulticastTimeToLive(int time_to_live);
144 152
145 // Set the loopback flag for UDP socket. If this flag is true, the host 153 // Set the loopback flag for UDP socket. If this flag is true, the host
146 // will receive packets sent to the joined group from itself. 154 // will receive packets sent to the joined group from itself.
147 // The default value of this option is true. 155 // The default value of this option is true.
148 // Should be called before Bind(). 156 // Should be called before Bind().
149 // Return a network error code. 157 // Return a net error code.
150 // 158 //
151 // Note: the behavior of |SetMulticastLoopbackMode| is slightly 159 // Note: the behavior of |SetMulticastLoopbackMode| is slightly
152 // different between Windows and Unix-like systems. The inconsistency only 160 // different between Windows and Unix-like systems. The inconsistency only
153 // happens when there are more than one applications on the same host 161 // happens when there are more than one applications on the same host
154 // joined to the same multicast group while having different settings on 162 // joined to the same multicast group while having different settings on
155 // multicast loopback mode. On Windows, the applications with loopback off 163 // multicast loopback mode. On Windows, the applications with loopback off
156 // will not RECEIVE the loopback packets; while on Unix-like systems, the 164 // will not RECEIVE the loopback packets; while on Unix-like systems, the
157 // applications with loopback off will not SEND the loopback packets to 165 // applications with loopback off will not SEND the loopback packets to
158 // other applications on the same host. See MSDN: http://goo.gl/6vqbj 166 // other applications on the same host. See MSDN: http://goo.gl/6vqbj
159 int SetMulticastLoopbackMode(bool loopback); 167 int SetMulticastLoopbackMode(bool loopback);
160 168
161 // Set the differentiated services flags on outgoing packets. May not 169 // Set the differentiated services flags on outgoing packets. May not
162 // do anything on some platforms. 170 // do anything on some platforms.
163 // Return a network error code. 171 // Return a net error code.
164 int SetDiffServCodePoint(DiffServCodePoint dscp); 172 int SetDiffServCodePoint(DiffServCodePoint dscp);
165 173
166 // Resets the thread to be used for thread-safety checks. 174 // Resets the thread to be used for thread-safety checks.
167 void DetachFromThread(); 175 void DetachFromThread();
168 176
169 private: 177 private:
170 enum SocketOptions { 178 enum SocketOptions {
171 SOCKET_OPTION_REUSE_ADDRESS = 1 << 0, 179 SOCKET_OPTION_REUSE_ADDRESS = 1 << 0,
rvargas (doing something else) 2014/12/03 21:00:16 should not need this
hidehiko 2014/12/04 04:03:36 Oops, overlooked. Removed.
172 SOCKET_OPTION_BROADCAST = 1 << 1, 180 SOCKET_OPTION_MULTICAST_LOOP = 1 << 1
173 SOCKET_OPTION_MULTICAST_LOOP = 1 << 2
174 }; 181 };
175 182
176 class ReadWatcher : public base::MessageLoopForIO::Watcher { 183 class ReadWatcher : public base::MessageLoopForIO::Watcher {
177 public: 184 public:
178 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} 185 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {}
179 186
180 // MessageLoopForIO::Watcher methods 187 // MessageLoopForIO::Watcher methods
181 188
182 void OnFileCanReadWithoutBlocking(int /* fd */) override; 189 void OnFileCanReadWithoutBlocking(int /* fd */) override;
183 190
(...skipping 27 matching lines...) Expand all
211 void DidCompleteWrite(); 218 void DidCompleteWrite();
212 219
213 // Handles stats and logging. |result| is the number of bytes transferred, on 220 // Handles stats and logging. |result| is the number of bytes transferred, on
214 // success, or the net error code on failure. On success, LogRead takes in a 221 // success, or the net error code on failure. On success, LogRead takes in a
215 // sockaddr and its length, which are mandatory, while LogWrite takes in an 222 // sockaddr and its length, which are mandatory, while LogWrite takes in an
216 // optional IPEndPoint. 223 // optional IPEndPoint.
217 void LogRead(int result, const char* bytes, socklen_t addr_len, 224 void LogRead(int result, const char* bytes, socklen_t addr_len,
218 const sockaddr* addr) const; 225 const sockaddr* addr) const;
219 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const; 226 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const;
220 227
221 // Returns the OS error code (or 0 on success).
222 int CreateSocket(int addr_family);
223
224 // Same as SendTo(), except that address is passed by pointer 228 // Same as SendTo(), except that address is passed by pointer
225 // instead of by reference. It is called from Write() with |address| 229 // instead of by reference. It is called from Write() with |address|
226 // set to NULL. 230 // set to NULL.
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 // Bind().
238 int SetSocketOptions(); 242 int SetMulticastOptions();
239 int DoBind(const IPEndPoint& address); 243 int DoBind(const IPEndPoint& address);
240 // Binds to a random port on |address|. 244 // Binds to a random port on |address|.
241 int RandomBind(const IPAddressNumber& address); 245 int RandomBind(const IPAddressNumber& address);
242 246
243 int socket_; 247 int socket_;
244 int addr_family_; 248 int addr_family_;
249 bool is_connected_;
245 250
246 // Bitwise-or'd combination of SocketOptions. Specifies the set of 251 // Bitwise-or'd combination of SocketOptions. Specifies the set of
247 // options that should be applied to |socket_| before Bind(). 252 // options that should be applied to |socket_| before Bind().
248 int socket_options_; 253 int socket_options_;
249 254
250 // Multicast interface. 255 // Multicast interface.
251 uint32 multicast_interface_; 256 uint32 multicast_interface_;
252 257
253 // Multicast socket options cached for SetSocketOption. 258 // Multicast socket options cached for SetMulticastOption.
254 // Cannot be used after Bind(). 259 // Cannot be used after Bind().
255 int multicast_time_to_live_; 260 int multicast_time_to_live_;
256 261
257 // How to do source port binding, used only when UDPSocket is part of 262 // How to do source port binding, used only when UDPSocket is part of
258 // UDPClientSocket, since UDPServerSocket provides Bind. 263 // UDPClientSocket, since UDPServerSocket provides Bind.
259 DatagramSocket::BindType bind_type_; 264 DatagramSocket::BindType bind_type_;
260 265
261 // PRNG function for generating port numbers. 266 // PRNG function for generating port numbers.
262 RandIntCallback rand_int_cb_; 267 RandIntCallback rand_int_cb_;
263 268
(...skipping 27 matching lines...) Expand all
291 CompletionCallback write_callback_; 296 CompletionCallback write_callback_;
292 297
293 BoundNetLog net_log_; 298 BoundNetLog net_log_;
294 299
295 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); 300 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent);
296 }; 301 };
297 302
298 } // namespace net 303 } // namespace net
299 304
300 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ 305 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_
OLDNEW
« no previous file with comments | « net/udp/udp_socket.h ('k') | net/udp/udp_socket_libevent.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698