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

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

Issue 992733002: Remove //net (except for Android test stuff) and sdch (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 9 months 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef NET_UDP_UDP_SOCKET_LIBEVENT_H_
6 #define NET_UDP_UDP_SOCKET_LIBEVENT_H_
7
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/threading/non_thread_safe.h"
12 #include "net/base/address_family.h"
13 #include "net/base/completion_callback.h"
14 #include "net/base/io_buffer.h"
15 #include "net/base/ip_endpoint.h"
16 #include "net/base/net_export.h"
17 #include "net/base/net_log.h"
18 #include "net/base/rand_callback.h"
19 #include "net/socket/socket_descriptor.h"
20 #include "net/udp/datagram_socket.h"
21
22 namespace net {
23
24 class NET_EXPORT UDPSocketLibevent : public base::NonThreadSafe {
25 public:
26 UDPSocketLibevent(DatagramSocket::BindType bind_type,
27 const RandIntCallback& rand_int_cb,
28 net::NetLog* net_log,
29 const net::NetLog::Source& source);
30 virtual ~UDPSocketLibevent();
31
32 // Opens the socket.
33 // Returns a net error code.
34 int Open(AddressFamily address_family);
35
36 // Connects the socket to connect with a certain |address|.
37 // Should be called after Open().
38 // Returns a net error code.
39 int Connect(const IPEndPoint& address);
40
41 // Binds the address/port for this socket to |address|. This is generally
42 // only used on a server. Should be called after Open().
43 // Returns a net error code.
44 int Bind(const IPEndPoint& address);
45
46 // Closes the socket.
47 // TODO(rvargas, hidehiko): Disallow re-Open() after Close().
48 void Close();
49
50 // Copies the remote udp address into |address| and returns a net error code.
51 int GetPeerAddress(IPEndPoint* address) const;
52
53 // Copies the local udp address into |address| and returns a net error code.
54 // (similar to getsockname)
55 int GetLocalAddress(IPEndPoint* address) const;
56
57 // IO:
58 // Multiple outstanding read requests are not supported.
59 // Full duplex mode (reading and writing at the same time) is supported
60
61 // Reads from the socket.
62 // Only usable from the client-side of a UDP socket, after the socket
63 // has been connected.
64 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
65
66 // Writes to the socket.
67 // Only usable from the client-side of a UDP socket, after the socket
68 // has been connected.
69 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
70
71 // Reads from a socket and receive sender address information.
72 // |buf| is the buffer to read data into.
73 // |buf_len| is the maximum amount of data to read.
74 // |address| is a buffer provided by the caller for receiving the sender
75 // address information about the received data. This buffer must be kept
76 // alive by the caller until the callback is placed.
77 // |address_length| is a ptr to the length of the |address| buffer. This
78 // is an input parameter containing the maximum size |address| can hold
79 // and also an output parameter for the size of |address| upon completion.
80 // |callback| is the callback on completion of the RecvFrom.
81 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
82 // If ERR_IO_PENDING is returned, the caller must keep |buf|, |address|,
83 // and |address_length| alive until the callback is called.
84 int RecvFrom(IOBuffer* buf,
85 int buf_len,
86 IPEndPoint* address,
87 const CompletionCallback& callback);
88
89 // Sends to a socket with a particular destination.
90 // |buf| is the buffer to send
91 // |buf_len| is the number of bytes to send
92 // |address| is the recipient address.
93 // |address_length| is the size of the recipient address
94 // |callback| is the user callback function to call on complete.
95 // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
96 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address|
97 // alive until the callback is called.
98 int SendTo(IOBuffer* buf,
99 int buf_len,
100 const IPEndPoint& address,
101 const CompletionCallback& callback);
102
103 // Sets the receive buffer size (in bytes) for the socket.
104 // Returns a net error code.
105 int SetReceiveBufferSize(int32 size);
106
107 // Sets the send buffer size (in bytes) for the socket.
108 // Returns a net error code.
109 int SetSendBufferSize(int32 size);
110
111 // Returns true if the socket is already connected or bound.
112 bool is_connected() const { return is_connected_; }
113
114 const BoundNetLog& NetLog() const { return net_log_; }
115
116 // Sets corresponding flags in |socket_options_| to allow the socket
117 // to share the local address to which the socket will be bound with
118 // other processes. Should be called between Open() and Bind().
119 // Returns a net error code.
120 int AllowAddressReuse();
121
122 // Sets corresponding flags in |socket_options_| to allow or disallow sending
123 // and receiving packets to and from broadcast addresses.
124 // Returns a net error code.
125 int SetBroadcast(bool broadcast);
126
127 // Joins the multicast group.
128 // |group_address| is the group address to join, could be either
129 // an IPv4 or IPv6 address.
130 // Returns a net error code.
131 int JoinGroup(const IPAddressNumber& group_address) const;
132
133 // Leaves the multicast group.
134 // |group_address| is the group address to leave, could be either
135 // an IPv4 or IPv6 address. If the socket hasn't joined the group,
136 // it will be ignored.
137 // It's optional to leave the multicast group before destroying
138 // the socket. It will be done by the OS.
139 // Returns a net error code.
140 int LeaveGroup(const IPAddressNumber& group_address) const;
141
142 // Sets interface to use for multicast. If |interface_index| set to 0,
143 // default interface is used.
144 // Should be called before Bind().
145 // Returns a net error code.
146 int SetMulticastInterface(uint32 interface_index);
147
148 // Sets the time-to-live option for UDP packets sent to the multicast
149 // group address. The default value of this option is 1.
150 // Cannot be negative or more than 255.
151 // Should be called before Bind().
152 // Returns a net error code.
153 int SetMulticastTimeToLive(int time_to_live);
154
155 // Sets the loopback flag for UDP socket. If this flag is true, the host
156 // will receive packets sent to the joined group from itself.
157 // The default value of this option is true.
158 // Should be called before Bind().
159 // Returns a net error code.
160 //
161 // Note: the behavior of |SetMulticastLoopbackMode| is slightly
162 // different between Windows and Unix-like systems. The inconsistency only
163 // happens when there are more than one applications on the same host
164 // joined to the same multicast group while having different settings on
165 // multicast loopback mode. On Windows, the applications with loopback off
166 // will not RECEIVE the loopback packets; while on Unix-like systems, the
167 // applications with loopback off will not SEND the loopback packets to
168 // other applications on the same host. See MSDN: http://goo.gl/6vqbj
169 int SetMulticastLoopbackMode(bool loopback);
170
171 // Sets the differentiated services flags on outgoing packets. May not
172 // do anything on some platforms.
173 // Returns a net error code.
174 int SetDiffServCodePoint(DiffServCodePoint dscp);
175
176 // Resets the thread to be used for thread-safety checks.
177 void DetachFromThread();
178
179 private:
180 enum SocketOptions {
181 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0
182 };
183
184 class ReadWatcher : public base::MessageLoopForIO::Watcher {
185 public:
186 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {}
187
188 // MessageLoopForIO::Watcher methods
189
190 void OnFileCanReadWithoutBlocking(int /* fd */) override;
191
192 void OnFileCanWriteWithoutBlocking(int /* fd */) override {}
193
194 private:
195 UDPSocketLibevent* const socket_;
196
197 DISALLOW_COPY_AND_ASSIGN(ReadWatcher);
198 };
199
200 class WriteWatcher : public base::MessageLoopForIO::Watcher {
201 public:
202 explicit WriteWatcher(UDPSocketLibevent* socket) : socket_(socket) {}
203
204 // MessageLoopForIO::Watcher methods
205
206 void OnFileCanReadWithoutBlocking(int /* fd */) override {}
207
208 void OnFileCanWriteWithoutBlocking(int /* fd */) override;
209
210 private:
211 UDPSocketLibevent* const socket_;
212
213 DISALLOW_COPY_AND_ASSIGN(WriteWatcher);
214 };
215
216 void DoReadCallback(int rv);
217 void DoWriteCallback(int rv);
218 void DidCompleteRead();
219 void DidCompleteWrite();
220
221 // Handles stats and logging. |result| is the number of bytes transferred, on
222 // success, or the net error code on failure. On success, LogRead takes in a
223 // sockaddr and its length, which are mandatory, while LogWrite takes in an
224 // optional IPEndPoint.
225 void LogRead(int result, const char* bytes, socklen_t addr_len,
226 const sockaddr* addr) const;
227 void LogWrite(int result, const char* bytes, const IPEndPoint* address) const;
228
229 // Same as SendTo(), except that address is passed by pointer
230 // instead of by reference. It is called from Write() with |address|
231 // set to NULL.
232 int SendToOrWrite(IOBuffer* buf,
233 int buf_len,
234 const IPEndPoint* address,
235 const CompletionCallback& callback);
236
237 int InternalConnect(const IPEndPoint& address);
238 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address);
239 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address);
240
241 // Applies |socket_options_| to |socket_|. Should be called before
242 // Bind().
243 int SetMulticastOptions();
244 int DoBind(const IPEndPoint& address);
245 // Binds to a random port on |address|.
246 int RandomBind(const IPAddressNumber& address);
247
248 int socket_;
249 int addr_family_;
250 bool is_connected_;
251
252 // Bitwise-or'd combination of SocketOptions. Specifies the set of
253 // options that should be applied to |socket_| before Bind().
254 int socket_options_;
255
256 // Multicast interface.
257 uint32 multicast_interface_;
258
259 // Multicast socket options cached for SetMulticastOption.
260 // Cannot be used after Bind().
261 int multicast_time_to_live_;
262
263 // How to do source port binding, used only when UDPSocket is part of
264 // UDPClientSocket, since UDPServerSocket provides Bind.
265 DatagramSocket::BindType bind_type_;
266
267 // PRNG function for generating port numbers.
268 RandIntCallback rand_int_cb_;
269
270 // These are mutable since they're just cached copies to make
271 // GetPeerAddress/GetLocalAddress smarter.
272 mutable scoped_ptr<IPEndPoint> local_address_;
273 mutable scoped_ptr<IPEndPoint> remote_address_;
274
275 // The socket's libevent wrappers
276 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_;
277 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_;
278
279 // The corresponding watchers for reads and writes.
280 ReadWatcher read_watcher_;
281 WriteWatcher write_watcher_;
282
283 // The buffer used by InternalRead() to retry Read requests
284 scoped_refptr<IOBuffer> read_buf_;
285 int read_buf_len_;
286 IPEndPoint* recv_from_address_;
287
288 // The buffer used by InternalWrite() to retry Write requests
289 scoped_refptr<IOBuffer> write_buf_;
290 int write_buf_len_;
291 scoped_ptr<IPEndPoint> send_to_address_;
292
293 // External callback; called when read is complete.
294 CompletionCallback read_callback_;
295
296 // External callback; called when write is complete.
297 CompletionCallback write_callback_;
298
299 BoundNetLog net_log_;
300
301 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent);
302 };
303
304 } // namespace net
305
306 #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