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_POSIX_H_ |
6 #define NET_UDP_UDP_SOCKET_LIBEVENT_H_ | 6 #define NET_UDP_UDP_SOCKET_POSIX_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/address_family.h" |
13 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
15 #include "net/base/ip_endpoint.h" | 15 #include "net/base/ip_endpoint.h" |
16 #include "net/base/net_export.h" | 16 #include "net/base/net_export.h" |
17 #include "net/base/net_util.h" | 17 #include "net/base/net_util.h" |
18 #include "net/base/rand_callback.h" | 18 #include "net/base/rand_callback.h" |
19 #include "net/log/net_log.h" | 19 #include "net/log/net_log.h" |
20 #include "net/socket/socket_descriptor.h" | 20 #include "net/socket/socket_descriptor.h" |
21 #include "net/udp/datagram_socket.h" | 21 #include "net/udp/datagram_socket.h" |
22 | 22 |
23 namespace net { | 23 namespace net { |
24 | 24 |
25 class NET_EXPORT UDPSocketLibevent : public base::NonThreadSafe { | 25 class NET_EXPORT UDPSocketPosix : public base::NonThreadSafe { |
26 public: | 26 public: |
27 UDPSocketLibevent(DatagramSocket::BindType bind_type, | 27 UDPSocketPosix(DatagramSocket::BindType bind_type, |
28 const RandIntCallback& rand_int_cb, | 28 const RandIntCallback& rand_int_cb, |
29 net::NetLog* net_log, | 29 net::NetLog* net_log, |
30 const net::NetLog::Source& source); | 30 const net::NetLog::Source& source); |
31 virtual ~UDPSocketLibevent(); | 31 virtual ~UDPSocketPosix(); |
32 | 32 |
33 // Opens the socket. | 33 // Opens the socket. |
34 // Returns a net error code. | 34 // Returns a net error code. |
35 int Open(AddressFamily address_family); | 35 int Open(AddressFamily address_family); |
36 | 36 |
37 // Connects the socket to connect with a certain |address|. | 37 // Connects the socket to connect with a certain |address|. |
38 // Should be called after Open(). | 38 // Should be called after Open(). |
39 // Returns a net error code. | 39 // Returns a net error code. |
40 int Connect(const IPEndPoint& address); | 40 int Connect(const IPEndPoint& address); |
41 | 41 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 // Resets the thread to be used for thread-safety checks. | 177 // Resets the thread to be used for thread-safety checks. |
178 void DetachFromThread(); | 178 void DetachFromThread(); |
179 | 179 |
180 private: | 180 private: |
181 enum SocketOptions { | 181 enum SocketOptions { |
182 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0 | 182 SOCKET_OPTION_MULTICAST_LOOP = 1 << 0 |
183 }; | 183 }; |
184 | 184 |
185 class ReadWatcher : public base::MessageLoopForIO::Watcher { | 185 class ReadWatcher : public base::MessageLoopForIO::Watcher { |
186 public: | 186 public: |
187 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} | 187 explicit ReadWatcher(UDPSocketPosix* socket) : socket_(socket) {} |
188 | 188 |
189 // MessageLoopForIO::Watcher methods | 189 // MessageLoopForIO::Watcher methods |
190 | 190 |
191 void OnFileCanReadWithoutBlocking(int /* fd */) override; | 191 void OnFileCanReadWithoutBlocking(int /* fd */) override; |
192 | 192 |
193 void OnFileCanWriteWithoutBlocking(int /* fd */) override {} | 193 void OnFileCanWriteWithoutBlocking(int /* fd */) override {} |
194 | 194 |
195 private: | 195 private: |
196 UDPSocketLibevent* const socket_; | 196 UDPSocketPosix* const socket_; |
197 | 197 |
198 DISALLOW_COPY_AND_ASSIGN(ReadWatcher); | 198 DISALLOW_COPY_AND_ASSIGN(ReadWatcher); |
199 }; | 199 }; |
200 | 200 |
201 class WriteWatcher : public base::MessageLoopForIO::Watcher { | 201 class WriteWatcher : public base::MessageLoopForIO::Watcher { |
202 public: | 202 public: |
203 explicit WriteWatcher(UDPSocketLibevent* socket) : socket_(socket) {} | 203 explicit WriteWatcher(UDPSocketPosix* socket) : socket_(socket) {} |
204 | 204 |
205 // MessageLoopForIO::Watcher methods | 205 // MessageLoopForIO::Watcher methods |
206 | 206 |
207 void OnFileCanReadWithoutBlocking(int /* fd */) override {} | 207 void OnFileCanReadWithoutBlocking(int /* fd */) override {} |
208 | 208 |
209 void OnFileCanWriteWithoutBlocking(int /* fd */) override; | 209 void OnFileCanWriteWithoutBlocking(int /* fd */) override; |
210 | 210 |
211 private: | 211 private: |
212 UDPSocketLibevent* const socket_; | 212 UDPSocketPosix* const socket_; |
213 | 213 |
214 DISALLOW_COPY_AND_ASSIGN(WriteWatcher); | 214 DISALLOW_COPY_AND_ASSIGN(WriteWatcher); |
215 }; | 215 }; |
216 | 216 |
217 void DoReadCallback(int rv); | 217 void DoReadCallback(int rv); |
218 void DoWriteCallback(int rv); | 218 void DoWriteCallback(int rv); |
219 void DidCompleteRead(); | 219 void DidCompleteRead(); |
220 void DidCompleteWrite(); | 220 void DidCompleteWrite(); |
221 | 221 |
222 // Handles stats and logging. |result| is the number of bytes transferred, on | 222 // Handles stats and logging. |result| is the number of bytes transferred, on |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 DatagramSocket::BindType bind_type_; | 267 DatagramSocket::BindType bind_type_; |
268 | 268 |
269 // PRNG function for generating port numbers. | 269 // PRNG function for generating port numbers. |
270 RandIntCallback rand_int_cb_; | 270 RandIntCallback rand_int_cb_; |
271 | 271 |
272 // These are mutable since they're just cached copies to make | 272 // These are mutable since they're just cached copies to make |
273 // GetPeerAddress/GetLocalAddress smarter. | 273 // GetPeerAddress/GetLocalAddress smarter. |
274 mutable scoped_ptr<IPEndPoint> local_address_; | 274 mutable scoped_ptr<IPEndPoint> local_address_; |
275 mutable scoped_ptr<IPEndPoint> remote_address_; | 275 mutable scoped_ptr<IPEndPoint> remote_address_; |
276 | 276 |
277 // The socket's libevent wrappers | 277 // The socket's posix wrappers |
278 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; | 278 base::MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; |
279 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; | 279 base::MessageLoopForIO::FileDescriptorWatcher write_socket_watcher_; |
280 | 280 |
281 // The corresponding watchers for reads and writes. | 281 // The corresponding watchers for reads and writes. |
282 ReadWatcher read_watcher_; | 282 ReadWatcher read_watcher_; |
283 WriteWatcher write_watcher_; | 283 WriteWatcher write_watcher_; |
284 | 284 |
285 // The buffer used by InternalRead() to retry Read requests | 285 // The buffer used by InternalRead() to retry Read requests |
286 scoped_refptr<IOBuffer> read_buf_; | 286 scoped_refptr<IOBuffer> read_buf_; |
287 int read_buf_len_; | 287 int read_buf_len_; |
288 IPEndPoint* recv_from_address_; | 288 IPEndPoint* recv_from_address_; |
289 | 289 |
290 // The buffer used by InternalWrite() to retry Write requests | 290 // The buffer used by InternalWrite() to retry Write requests |
291 scoped_refptr<IOBuffer> write_buf_; | 291 scoped_refptr<IOBuffer> write_buf_; |
292 int write_buf_len_; | 292 int write_buf_len_; |
293 scoped_ptr<IPEndPoint> send_to_address_; | 293 scoped_ptr<IPEndPoint> send_to_address_; |
294 | 294 |
295 // External callback; called when read is complete. | 295 // External callback; called when read is complete. |
296 CompletionCallback read_callback_; | 296 CompletionCallback read_callback_; |
297 | 297 |
298 // External callback; called when write is complete. | 298 // External callback; called when write is complete. |
299 CompletionCallback write_callback_; | 299 CompletionCallback write_callback_; |
300 | 300 |
301 BoundNetLog net_log_; | 301 BoundNetLog net_log_; |
302 | 302 |
303 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); | 303 DISALLOW_COPY_AND_ASSIGN(UDPSocketPosix); |
304 }; | 304 }; |
305 | 305 |
306 } // namespace net | 306 } // namespace net |
307 | 307 |
308 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ | 308 #endif // NET_UDP_UDP_SOCKET_POSIX_H_ |
OLD | NEW |