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_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.h" | 10 #include "base/message_loop.h" |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 bool SetReceiveBufferSize(int32 size); | 96 bool SetReceiveBufferSize(int32 size); |
97 | 97 |
98 // Set the send buffer size (in bytes) for the socket. | 98 // Set the send buffer size (in bytes) for the socket. |
99 bool SetSendBufferSize(int32 size); | 99 bool SetSendBufferSize(int32 size); |
100 | 100 |
101 // Returns true if the socket is already connected or bound. | 101 // Returns true if the socket is already connected or bound. |
102 bool is_connected() const { return socket_ != kInvalidSocket; } | 102 bool is_connected() const { return socket_ != kInvalidSocket; } |
103 | 103 |
104 const BoundNetLog& NetLog() const { return net_log_; } | 104 const BoundNetLog& NetLog() const { return net_log_; } |
105 | 105 |
| 106 // Sets corresponding flags in |socket_options_| to allow the socket |
| 107 // to share the local address to which socket will be bound with |
| 108 // other processes. Should be called before Bind(). |
| 109 void AllowAddressReuse(); |
| 110 |
| 111 // Sets corresponding flags in |socket_options_| to allow sending |
| 112 // and receiving packets sent to and from broadcast |
| 113 // addresses. Should be called before Bind(). |
| 114 void AllowBroadcast(); |
| 115 |
106 private: | 116 private: |
107 static const int kInvalidSocket = -1; | 117 static const int kInvalidSocket = -1; |
108 | 118 |
| 119 enum SocketOptions { |
| 120 SOCKET_OPTION_REUSE_ADDRESS = 1 << 0, |
| 121 SOCKET_OPTION_BROADCAST = 1 << 1 |
| 122 }; |
| 123 |
109 class ReadWatcher : public MessageLoopForIO::Watcher { | 124 class ReadWatcher : public MessageLoopForIO::Watcher { |
110 public: | 125 public: |
111 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} | 126 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} |
112 | 127 |
113 // MessageLoopForIO::Watcher methods | 128 // MessageLoopForIO::Watcher methods |
114 | 129 |
115 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { | 130 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE { |
116 if (!socket_->read_callback_.is_null()) | 131 if (!socket_->read_callback_.is_null()) |
117 socket_->DidCompleteRead(); | 132 socket_->DidCompleteRead(); |
118 } | 133 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 // set to NULL. | 180 // set to NULL. |
166 int SendToOrWrite(IOBuffer* buf, | 181 int SendToOrWrite(IOBuffer* buf, |
167 int buf_len, | 182 int buf_len, |
168 const IPEndPoint* address, | 183 const IPEndPoint* address, |
169 const CompletionCallback& callback); | 184 const CompletionCallback& callback); |
170 | 185 |
171 int InternalConnect(const IPEndPoint& address); | 186 int InternalConnect(const IPEndPoint& address); |
172 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); | 187 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); |
173 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); | 188 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); |
174 | 189 |
| 190 // Applies |socket_options_| to |socket_|. Should be called before |
| 191 // Bind(). |
| 192 int SetSocketOptions(); |
175 int DoBind(const IPEndPoint& address); | 193 int DoBind(const IPEndPoint& address); |
176 int RandomBind(const IPEndPoint& address); | 194 int RandomBind(const IPEndPoint& address); |
177 | 195 |
178 int socket_; | 196 int socket_; |
179 | 197 |
| 198 // Bitwise-or'd combination of SocketOptions. Specifies set of |
| 199 // options that should be applied to |socket_| before bind. |
| 200 int socket_options_; |
| 201 |
180 // How to do source port binding, used only when UDPSocket is part of | 202 // How to do source port binding, used only when UDPSocket is part of |
181 // UDPClientSocket, since UDPServerSocket provides Bind. | 203 // UDPClientSocket, since UDPServerSocket provides Bind. |
182 DatagramSocket::BindType bind_type_; | 204 DatagramSocket::BindType bind_type_; |
183 | 205 |
184 // PRNG function for generating port numbers. | 206 // PRNG function for generating port numbers. |
185 RandIntCallback rand_int_cb_; | 207 RandIntCallback rand_int_cb_; |
186 | 208 |
187 // These are mutable since they're just cached copies to make | 209 // These are mutable since they're just cached copies to make |
188 // GetPeerAddress/GetLocalAddress smarter. | 210 // GetPeerAddress/GetLocalAddress smarter. |
189 mutable scoped_ptr<IPEndPoint> local_address_; | 211 mutable scoped_ptr<IPEndPoint> local_address_; |
(...skipping 24 matching lines...) Expand all Loading... |
214 CompletionCallback write_callback_; | 236 CompletionCallback write_callback_; |
215 | 237 |
216 BoundNetLog net_log_; | 238 BoundNetLog net_log_; |
217 | 239 |
218 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); | 240 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); |
219 }; | 241 }; |
220 | 242 |
221 } // namespace net | 243 } // namespace net |
222 | 244 |
223 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ | 245 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ |
OLD | NEW |