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

Side by Side Diff: net/udp/udp_socket_win.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_unittest.cc ('k') | net/udp/udp_socket_win.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_WIN_H_ 5 #ifndef NET_UDP_UDP_SOCKET_WIN_H_
6 #define NET_UDP_UDP_SOCKET_WIN_H_ 6 #define NET_UDP_UDP_SOCKET_WIN_H_
7 7
8 #include <qos2.h> 8 #include <qos2.h>
9 #include <winsock2.h> 9 #include <winsock2.h>
10 10
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/threading/non_thread_safe.h" 13 #include "base/threading/non_thread_safe.h"
14 #include "base/win/object_watcher.h" 14 #include "base/win/object_watcher.h"
15 #include "net/base/address_family.h"
15 #include "net/base/completion_callback.h" 16 #include "net/base/completion_callback.h"
16 #include "net/base/net_export.h" 17 #include "net/base/net_export.h"
17 #include "net/base/rand_callback.h" 18 #include "net/base/rand_callback.h"
18 #include "net/base/ip_endpoint.h" 19 #include "net/base/ip_endpoint.h"
19 #include "net/base/io_buffer.h" 20 #include "net/base/io_buffer.h"
20 #include "net/base/net_log.h" 21 #include "net/base/net_log.h"
21 #include "net/udp/datagram_socket.h" 22 #include "net/udp/datagram_socket.h"
22 23
23 namespace net { 24 namespace net {
24 25
25 class NET_EXPORT UDPSocketWin : NON_EXPORTED_BASE(public base::NonThreadSafe) { 26 class NET_EXPORT UDPSocketWin : NON_EXPORTED_BASE(public base::NonThreadSafe) {
26 public: 27 public:
27 UDPSocketWin(DatagramSocket::BindType bind_type, 28 UDPSocketWin(DatagramSocket::BindType bind_type,
28 const RandIntCallback& rand_int_cb, 29 const RandIntCallback& rand_int_cb,
29 net::NetLog* net_log, 30 net::NetLog* net_log,
30 const net::NetLog::Source& source); 31 const net::NetLog::Source& source);
31 virtual ~UDPSocketWin(); 32 virtual ~UDPSocketWin();
32 33
33 // Connect the socket to connect with a certain |address|. 34 // Opens the socket.
35 // Returns a net error code.
36 int Open(AddressFamily address_family);
37
38 // Connects the socket to connect with a certain |address|.
39 // Should be called after Open().
34 // Returns a net error code. 40 // Returns a net error code.
35 int Connect(const IPEndPoint& address); 41 int Connect(const IPEndPoint& address);
36 42
37 // Bind the address/port for this socket to |address|. This is generally 43 // Binds the address/port for this socket to |address|. This is generally
38 // only used on a server. 44 // only used on a server. Should be called after Open().
39 // Returns a net error code. 45 // Returns a net error code.
40 int Bind(const IPEndPoint& address); 46 int Bind(const IPEndPoint& address);
41 47
42 // Close the socket. 48 // Closes the socket.
49 // TODO(rvargas, hidehiko): Disallow re-Open() after Close().
43 void Close(); 50 void Close();
44 51
45 // Copy the remote udp address into |address| and return a network error code. 52 // Copies the remote udp address into |address| and returns a net error code.
46 int GetPeerAddress(IPEndPoint* address) const; 53 int GetPeerAddress(IPEndPoint* address) const;
47 54
48 // Copy the local udp address into |address| and return a network error code. 55 // Copies the local udp address into |address| and returns a net error code.
49 // (similar to getsockname) 56 // (similar to getsockname)
50 int GetLocalAddress(IPEndPoint* address) const; 57 int GetLocalAddress(IPEndPoint* address) const;
51 58
52 // IO: 59 // IO:
53 // Multiple outstanding read requests are not supported. 60 // Multiple outstanding read requests are not supported.
54 // Full duplex mode (reading and writing at the same time) is supported 61 // Full duplex mode (reading and writing at the same time) is supported
55 62
56 // Read from the socket. 63 // Reads from the socket.
57 // Only usable from the client-side of a UDP socket, after the socket 64 // Only usable from the client-side of a UDP socket, after the socket
58 // has been connected. 65 // has been connected.
59 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); 66 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
60 67
61 // Write to the socket. 68 // Writes to the socket.
62 // Only usable from the client-side of a UDP socket, after the socket 69 // Only usable from the client-side of a UDP socket, after the socket
63 // has been connected. 70 // has been connected.
64 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); 71 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
65 72
66 // Read from a socket and receive sender address information. 73 // Reads from a socket and receive sender address information.
67 // |buf| is the buffer to read data into. 74 // |buf| is the buffer to read data into.
68 // |buf_len| is the maximum amount of data to read. 75 // |buf_len| is the maximum amount of data to read.
69 // |address| is a buffer provided by the caller for receiving the sender 76 // |address| is a buffer provided by the caller for receiving the sender
70 // address information about the received data. This buffer must be kept 77 // address information about the received data. This buffer must be kept
71 // alive by the caller until the callback is placed. 78 // alive by the caller until the callback is placed.
72 // |address_length| is a ptr to the length of the |address| buffer. This 79 // |address_length| is a ptr to the length of the |address| buffer. This
73 // is an input parameter containing the maximum size |address| can hold 80 // is an input parameter containing the maximum size |address| can hold
74 // and also an output parameter for the size of |address| upon completion. 81 // and also an output parameter for the size of |address| upon completion.
75 // |callback| is the callback on completion of the RecvFrom. 82 // |callback| is the callback on completion of the RecvFrom.
76 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. 83 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
77 // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|, 84 // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|,
78 // and |address_length| alive until the callback is called. 85 // and |address_length| alive until the callback is called.
79 int RecvFrom(IOBuffer* buf, 86 int RecvFrom(IOBuffer* buf,
80 int buf_len, 87 int buf_len,
81 IPEndPoint* address, 88 IPEndPoint* address,
82 const CompletionCallback& callback); 89 const CompletionCallback& callback);
83 90
84 // Send to a socket with a particular destination. 91 // Sends to a socket with a particular destination.
85 // |buf| is the buffer to send 92 // |buf| is the buffer to send
86 // |buf_len| is the number of bytes to send 93 // |buf_len| is the number of bytes to send
87 // |address| is the recipient address. 94 // |address| is the recipient address.
88 // |address_length| is the size of the recipient address 95 // |address_length| is the size of the recipient address
89 // |callback| is the user callback function to call on complete. 96 // |callback| is the user callback function to call on complete.
90 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. 97 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
91 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| 98 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address|
92 // alive until the callback is called. 99 // alive until the callback is called.
93 int SendTo(IOBuffer* buf, 100 int SendTo(IOBuffer* buf,
94 int buf_len, 101 int buf_len,
95 const IPEndPoint& address, 102 const IPEndPoint& address,
96 const CompletionCallback& callback); 103 const CompletionCallback& callback);
97 104
98 // Set the receive buffer size (in bytes) for the socket. 105 // Sets the receive buffer size (in bytes) for the socket.
99 // Returns a net error code. 106 // Returns a net error code.
100 int SetReceiveBufferSize(int32 size); 107 int SetReceiveBufferSize(int32 size);
101 108
102 // Set the send buffer size (in bytes) for the socket. 109 // Sets the send buffer size (in bytes) for the socket.
103 // Returns a net error code. 110 // Returns a net error code.
104 int SetSendBufferSize(int32 size); 111 int SetSendBufferSize(int32 size);
105 112
106 // Returns true if the socket is already connected or bound. 113 // Returns true if the socket is already connected or bound.
107 bool is_connected() const { return socket_ != INVALID_SOCKET; } 114 bool is_connected() const { return is_connected_; }
108 115
109 const BoundNetLog& NetLog() const { return net_log_; } 116 const BoundNetLog& NetLog() const { return net_log_; }
110 117
111 // Sets corresponding flags in |socket_options_| to allow the socket 118 // Sets corresponding flags in |socket_options_| to allow the socket
112 // to share the local address to which the socket will be bound with 119 // to share the local address to which the socket will be bound with
113 // other processes. Should be called before Bind(). 120 // other processes. Should be called between Open() and Bind().
114 void AllowAddressReuse(); 121 // Returns a net error code.
122 int AllowAddressReuse();
115 123
116 // Sets corresponding flags in |socket_options_| to allow sending 124 // Sets corresponding flags in |socket_options_| to allow sending
117 // and receiving packets to and from broadcast addresses. Should be 125 // and receiving packets to and from broadcast addresses.
118 // called before Bind(). 126 int SetBroadcast(bool broadcast);
119 void AllowBroadcast();
120 127
121 // Join the multicast group. 128 // Joins the multicast group.
122 // |group_address| is the group address to join, could be either 129 // |group_address| is the group address to join, could be either
123 // an IPv4 or IPv6 address. 130 // an IPv4 or IPv6 address.
124 // Return a network error code. 131 // Returns a net error code.
125 int JoinGroup(const IPAddressNumber& group_address) const; 132 int JoinGroup(const IPAddressNumber& group_address) const;
126 133
127 // Leave the multicast group. 134 // Leaves the multicast group.
128 // |group_address| is the group address to leave, could be either 135 // |group_address| is the group address to leave, could be either
129 // an IPv4 or IPv6 address. If the socket hasn't joined the group, 136 // an IPv4 or IPv6 address. If the socket hasn't joined the group,
130 // it will be ignored. 137 // it will be ignored.
131 // It's optional to leave the multicast group before destroying 138 // It's optional to leave the multicast group before destroying
132 // the socket. It will be done by the OS. 139 // the socket. It will be done by the OS.
133 // Return a network error code. 140 // Return a net error code.
134 int LeaveGroup(const IPAddressNumber& group_address) const; 141 int LeaveGroup(const IPAddressNumber& group_address) const;
135 142
136 // Set interface to use for multicast. If |interface_index| set to 0, default 143 // Sets interface to use for multicast. If |interface_index| set to 0,
137 // interface is used. 144 // default interface is used.
138 // Should be called before Bind(). 145 // Should be called before Bind().
139 // Returns a network error code. 146 // Returns a net error code.
140 int SetMulticastInterface(uint32 interface_index); 147 int SetMulticastInterface(uint32 interface_index);
141 148
142 // Set the time-to-live option for UDP packets sent to the multicast 149 // Sets the time-to-live option for UDP packets sent to the multicast
143 // group address. The default value of this option is 1. 150 // group address. The default value of this option is 1.
144 // Cannot be negative or more than 255. 151 // Cannot be negative or more than 255.
145 // Should be called before Bind(). 152 // Should be called before Bind().
146 int SetMulticastTimeToLive(int time_to_live); 153 int SetMulticastTimeToLive(int time_to_live);
147 154
148 // Set the loopback flag for UDP socket. If this flag is true, the host 155 // Sets the loopback flag for UDP socket. If this flag is true, the host
149 // will receive packets sent to the joined group from itself. 156 // will receive packets sent to the joined group from itself.
150 // The default value of this option is true. 157 // The default value of this option is true.
151 // Should be called before Bind(). 158 // Should be called before Bind().
152 // 159 //
153 // Note: the behavior of |SetMulticastLoopbackMode| is slightly 160 // Note: the behavior of |SetMulticastLoopbackMode| is slightly
154 // different between Windows and Unix-like systems. The inconsistency only 161 // different between Windows and Unix-like systems. The inconsistency only
155 // happens when there are more than one applications on the same host 162 // happens when there are more than one applications on the same host
156 // joined to the same multicast group while having different settings on 163 // joined to the same multicast group while having different settings on
157 // multicast loopback mode. On Windows, the applications with loopback off 164 // multicast loopback mode. On Windows, the applications with loopback off
158 // will not RECEIVE the loopback packets; while on Unix-like systems, the 165 // will not RECEIVE the loopback packets; while on Unix-like systems, the
159 // applications with loopback off will not SEND the loopback packets to 166 // applications with loopback off will not SEND the loopback packets to
160 // other applications on the same host. See MSDN: http://goo.gl/6vqbj 167 // other applications on the same host. See MSDN: http://goo.gl/6vqbj
161 int SetMulticastLoopbackMode(bool loopback); 168 int SetMulticastLoopbackMode(bool loopback);
162 169
163 // Set the differentiated services flags on outgoing packets. May not 170 // Sets the differentiated services flags on outgoing packets. May not
164 // do anything on some platforms. 171 // do anything on some platforms.
165 int SetDiffServCodePoint(DiffServCodePoint dscp); 172 int SetDiffServCodePoint(DiffServCodePoint dscp);
166 173
167 // Resets the thread to be used for thread-safety checks. 174 // Resets the thread to be used for thread-safety checks.
168 void DetachFromThread(); 175 void DetachFromThread();
169 176
170 private: 177 private:
171 enum SocketOptions { 178 enum SocketOptions {
172 SOCKET_OPTION_REUSE_ADDRESS = 1 << 0, 179 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0
173 SOCKET_OPTION_BROADCAST = 1 << 1,
174 SOCKET_OPTION_MULTICAST_LOOP = 1 << 2
175 }; 180 };
176 181
177 class Core; 182 class Core;
178 183
179 void DoReadCallback(int rv); 184 void DoReadCallback(int rv);
180 void DoWriteCallback(int rv); 185 void DoWriteCallback(int rv);
181 void DidCompleteRead(); 186 void DidCompleteRead();
182 void DidCompleteWrite(); 187 void DidCompleteWrite();
183 188
184 // Handles stats and logging. |result| is the number of bytes transferred, on 189 // Handles stats and logging. |result| is the number of bytes transferred, on
185 // success, or the net error code on failure. LogRead retrieves the address 190 // success, or the net error code on failure. LogRead retrieves the address
186 // from |recv_addr_storage_|, while LogWrite takes it as an optional argument. 191 // from |recv_addr_storage_|, while LogWrite takes it as an optional argument.
187 void LogRead(int result, const char* bytes) const; 192 void LogRead(int result, const char* bytes) const;
188 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const; 193 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const;
189 194
190 // Returns the OS error code (or 0 on success).
191 int CreateSocket(int addr_family);
192
193 // Same as SendTo(), except that address is passed by pointer 195 // Same as SendTo(), except that address is passed by pointer
194 // instead of by reference. It is called from Write() with |address| 196 // instead of by reference. It is called from Write() with |address|
195 // set to NULL. 197 // set to NULL.
196 int SendToOrWrite(IOBuffer* buf, 198 int SendToOrWrite(IOBuffer* buf,
197 int buf_len, 199 int buf_len,
198 const IPEndPoint* address, 200 const IPEndPoint* address,
199 const CompletionCallback& callback); 201 const CompletionCallback& callback);
200 202
201 int InternalConnect(const IPEndPoint& address); 203 int InternalConnect(const IPEndPoint& address);
202 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); 204 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address);
203 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); 205 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address);
204 206
205 // Applies |socket_options_| to |socket_|. Should be called before 207 // Applies |socket_options_| to |socket_|. Should be called before
206 // Bind(). 208 // Bind().
207 int SetSocketOptions(); 209 int SetMulticastOptions();
208 int DoBind(const IPEndPoint& address); 210 int DoBind(const IPEndPoint& address);
209 // Binds to a random port on |address|. 211 // Binds to a random port on |address|.
210 int RandomBind(const IPAddressNumber& address); 212 int RandomBind(const IPAddressNumber& address);
211 213
212 // Attempts to convert the data in |recv_addr_storage_| and |recv_addr_len_| 214 // Attempts to convert the data in |recv_addr_storage_| and |recv_addr_len_|
213 // to an IPEndPoint and writes it to |address|. Returns true on success. 215 // to an IPEndPoint and writes it to |address|. Returns true on success.
214 bool ReceiveAddressToIPEndpoint(IPEndPoint* address) const; 216 bool ReceiveAddressToIPEndpoint(IPEndPoint* address) const;
215 217
216 SOCKET socket_; 218 SOCKET socket_;
217 int addr_family_; 219 int addr_family_;
220 bool is_connected_;
218 221
219 // Bitwise-or'd combination of SocketOptions. Specifies the set of 222 // Bitwise-or'd combination of SocketOptions. Specifies the set of
220 // options that should be applied to |socket_| before Bind(). 223 // options that should be applied to |socket_| before Bind().
221 int socket_options_; 224 int socket_options_;
222 225
223 // Multicast interface. 226 // Multicast interface.
224 uint32 multicast_interface_; 227 uint32 multicast_interface_;
225 228
226 // Multicast socket options cached for SetSocketOption. 229 // Multicast socket options cached for SetMulticastOption.
227 // Cannot be used after Bind(). 230 // Cannot be used after Bind().
228 int multicast_time_to_live_; 231 int multicast_time_to_live_;
229 232
230 // How to do source port binding, used only when UDPSocket is part of 233 // How to do source port binding, used only when UDPSocket is part of
231 // UDPClientSocket, since UDPServerSocket provides Bind. 234 // UDPClientSocket, since UDPServerSocket provides Bind.
232 DatagramSocket::BindType bind_type_; 235 DatagramSocket::BindType bind_type_;
233 236
234 // PRNG function for generating port numbers. 237 // PRNG function for generating port numbers.
235 RandIntCallback rand_int_cb_; 238 RandIntCallback rand_int_cb_;
236 239
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 SetFlowFn set_flow_func_; 321 SetFlowFn set_flow_func_;
319 322
320 FRIEND_TEST_ALL_PREFIXES(UDPSocketTest, SetDSCPFake); 323 FRIEND_TEST_ALL_PREFIXES(UDPSocketTest, SetDSCPFake);
321 DISALLOW_COPY_AND_ASSIGN(QwaveAPI); 324 DISALLOW_COPY_AND_ASSIGN(QwaveAPI);
322 }; 325 };
323 326
324 327
325 } // namespace net 328 } // namespace net
326 329
327 #endif // NET_UDP_UDP_SOCKET_WIN_H_ 330 #endif // NET_UDP_UDP_SOCKET_WIN_H_
OLDNEW
« no previous file with comments | « net/udp/udp_socket_unittest.cc ('k') | net/udp/udp_socket_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698