| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 // UDPSocketLibevent | |
| 10 // Accessor API for a UDP socket in either client or server form. | |
| 11 // | |
| 12 // Client form: | |
| 13 // In this case, we're connecting to a specific server, so the client will | |
| 14 // usually use: | |
| 15 // Connect(address) // Connect to a UDP server | |
| 16 // Read/Write // Reads/Writes all go to a single destination | |
| 17 // | |
| 18 // Server form: | |
| 19 // In this case, we want to read/write to many clients which are connecting | |
| 20 // to this server. First the server 'binds' to an addres, then we read from | |
| 21 // clients and write responses to them. | |
| 22 // Example: | |
| 23 // Bind(address/port) // Binds to port for reading from clients | |
| 24 // RecvFrom/SendTo // Each read can come from a different client | |
| 25 // // Writes need to be directed to a specific | |
| 26 // // address. | |
| 27 | |
| 28 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 29 #include "base/ref_counted.h" | 10 #include "base/ref_counted.h" |
| 30 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
| 31 #include "base/threading/non_thread_safe.h" | 12 #include "base/threading/non_thread_safe.h" |
| 32 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
| 33 #include "net/base/ip_endpoint.h" | 14 #include "net/base/ip_endpoint.h" |
| 34 #include "net/base/net_log.h" | 15 #include "net/base/net_log.h" |
| 35 #include "net/socket/client_socket.h" | 16 #include "net/socket/client_socket.h" |
| 36 #include "net/udp/datagram_socket.h" | |
| 37 | 17 |
| 38 namespace net { | 18 namespace net { |
| 39 | 19 |
| 40 class BoundNetLog; | 20 class BoundNetLog; |
| 41 | 21 |
| 42 class UDPSocketLibevent : public base::NonThreadSafe { | 22 class UDPSocketLibevent : public base::NonThreadSafe { |
| 43 public: | 23 public: |
| 44 UDPSocketLibevent(net::NetLog* net_log, | 24 UDPSocketLibevent(net::NetLog* net_log, |
| 45 const net::NetLog::Source& source); | 25 const net::NetLog::Source& source); |
| 46 virtual ~UDPSocketLibevent(); | 26 virtual ~UDPSocketLibevent(); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| | 86 // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address| |
| 107 // alive until the callback is called. | 87 // alive until the callback is called. |
| 108 int SendTo(IOBuffer* buf, | 88 int SendTo(IOBuffer* buf, |
| 109 int buf_len, | 89 int buf_len, |
| 110 const IPEndPoint& address, | 90 const IPEndPoint& address, |
| 111 CompletionCallback* callback); | 91 CompletionCallback* callback); |
| 112 | 92 |
| 113 // Returns true if the socket is already connected or bound. | 93 // Returns true if the socket is already connected or bound. |
| 114 bool is_connected() const { return socket_ != kInvalidSocket; } | 94 bool is_connected() const { return socket_ != kInvalidSocket; } |
| 115 | 95 |
| 116 IPEndPoint* local_address() { return local_address_.get(); } | |
| 117 | |
| 118 private: | 96 private: |
| 119 static const int kInvalidSocket = -1; | 97 static const int kInvalidSocket = -1; |
| 120 | 98 |
| 121 class ReadWatcher : public MessageLoopForIO::Watcher { | 99 class ReadWatcher : public MessageLoopForIO::Watcher { |
| 122 public: | 100 public: |
| 123 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} | 101 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} |
| 124 | 102 |
| 125 // MessageLoopForIO::Watcher methods | 103 // MessageLoopForIO::Watcher methods |
| 126 | 104 |
| 127 virtual void OnFileCanReadWithoutBlocking(int /* fd */) { | 105 virtual void OnFileCanReadWithoutBlocking(int /* fd */) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 157 }; | 135 }; |
| 158 | 136 |
| 159 void DoReadCallback(int rv); | 137 void DoReadCallback(int rv); |
| 160 void DoWriteCallback(int rv); | 138 void DoWriteCallback(int rv); |
| 161 void DidCompleteRead(); | 139 void DidCompleteRead(); |
| 162 void DidCompleteWrite(); | 140 void DidCompleteWrite(); |
| 163 | 141 |
| 164 // Returns the OS error code (or 0 on success). | 142 // Returns the OS error code (or 0 on success). |
| 165 int CreateSocket(const IPEndPoint& address); | 143 int CreateSocket(const IPEndPoint& address); |
| 166 | 144 |
| 167 int InternalRead(); | 145 int InternalRead(IOBuffer* buf, int buf_len); |
| 168 int InternalWrite(IOBuffer* buf, int buf_len); | 146 int InternalWrite(IOBuffer* buf, int buf_len); |
| 169 | 147 |
| 170 int socket_; | 148 int socket_; |
| 171 | 149 |
| 172 // These are mutable since they're just cached copies to make | 150 // These are mutable since they're just cached copies to make |
| 173 // GetPeerAddress/GetLocalAddress smarter. | 151 // GetPeerAddress/GetLocalAddress smarter. |
| 174 mutable scoped_ptr<IPEndPoint> local_address_; | 152 mutable scoped_ptr<IPEndPoint> local_address_; |
| 175 mutable scoped_ptr<IPEndPoint> remote_address_; | 153 mutable scoped_ptr<IPEndPoint> remote_address_; |
| 176 | 154 |
| 177 // The socket's libevent wrappers | 155 // The socket's libevent wrappers |
| (...skipping 21 matching lines...) Expand all Loading... |
| 199 CompletionCallback* write_callback_; | 177 CompletionCallback* write_callback_; |
| 200 | 178 |
| 201 BoundNetLog net_log_; | 179 BoundNetLog net_log_; |
| 202 | 180 |
| 203 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); | 181 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); |
| 204 }; | 182 }; |
| 205 | 183 |
| 206 } // namespace net | 184 } // namespace net |
| 207 | 185 |
| 208 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ | 186 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ |
| OLD | NEW |