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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 // Sets corresponding flags in |socket_options_| to allow the socket | 106 // Sets corresponding flags in |socket_options_| to allow the socket |
107 // to share the local address to which the socket will be bound with | 107 // to share the local address to which the socket will be bound with |
108 // other processes. Should be called before Bind(). | 108 // other processes. Should be called before Bind(). |
109 void AllowAddressReuse(); | 109 void AllowAddressReuse(); |
110 | 110 |
111 // Sets corresponding flags in |socket_options_| to allow sending | 111 // Sets corresponding flags in |socket_options_| to allow sending |
112 // and receiving packets to and from broadcast addresses. Should be | 112 // and receiving packets to and from broadcast addresses. Should be |
113 // called before Bind(). | 113 // called before Bind(). |
114 void AllowBroadcast(); | 114 void AllowBroadcast(); |
115 | 115 |
116 // Join the multicast group. | |
117 // |group_address| is the group address to join, could be either | |
118 // an IPv4 or IPv6 address. | |
119 // Return a network error code. | |
120 int JoinGroup(const IPAddressNumber& group_address) const; | |
121 | |
122 // Leave the multicast group. | |
123 // |group_address| is the group address to leave, could be either | |
124 // an IPv4 or IPv6 address. If the socket hasn't joined the group, | |
125 // it will be ignored. | |
126 // It's optional to leave the multicast group before destroying | |
127 // the socket. It will be done by the OS. | |
128 // Return a network error code. | |
129 int LeaveGroup(const IPAddressNumber& group_address) const; | |
130 | |
131 // Set the time-to-live option for UDP packets sent to the multicast | |
132 // group address. The default value of this option is 1. | |
133 // Cannot be negative or more than 255. | |
134 // Should be called before Bind(). | |
wtc
2013/04/17 22:06:50
Add: Return a network error code.
Bei Zhang
2013/04/19 19:40:14
Done.
| |
135 int SetMulticastTimeToLive(int time_to_live); | |
136 | |
137 // Set the loopback flag for UDP socket. If this flag is true, the host | |
138 // will receive packets sent to the joined group from itself. | |
139 // The default value of this option is true. | |
140 // Should be called before Bind(). | |
141 int SetMulticastLoopbackMode(bool loopback); | |
142 | |
116 private: | 143 private: |
117 static const int kInvalidSocket = -1; | 144 static const int kInvalidSocket = -1; |
118 | 145 |
119 enum SocketOptions { | 146 enum SocketOptions { |
120 SOCKET_OPTION_REUSE_ADDRESS = 1 << 0, | 147 SOCKET_OPTION_REUSE_ADDRESS = 1 << 0, |
121 SOCKET_OPTION_BROADCAST = 1 << 1 | 148 SOCKET_OPTION_BROADCAST = 1 << 1, |
149 SOCKET_OPTION_MULTICAST_LOOP = 1 << 2 | |
122 }; | 150 }; |
123 | 151 |
124 class ReadWatcher : public MessageLoopForIO::Watcher { | 152 class ReadWatcher : public MessageLoopForIO::Watcher { |
125 public: | 153 public: |
126 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} | 154 explicit ReadWatcher(UDPSocketLibevent* socket) : socket_(socket) {} |
127 | 155 |
128 // MessageLoopForIO::Watcher methods | 156 // MessageLoopForIO::Watcher methods |
129 | 157 |
130 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE; | 158 virtual void OnFileCanReadWithoutBlocking(int /* fd */) OVERRIDE; |
131 | 159 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); | 209 int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); |
182 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); | 210 int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); |
183 | 211 |
184 // Applies |socket_options_| to |socket_|. Should be called before | 212 // Applies |socket_options_| to |socket_|. Should be called before |
185 // Bind(). | 213 // Bind(). |
186 int SetSocketOptions(); | 214 int SetSocketOptions(); |
187 int DoBind(const IPEndPoint& address); | 215 int DoBind(const IPEndPoint& address); |
188 int RandomBind(const IPEndPoint& address); | 216 int RandomBind(const IPEndPoint& address); |
189 | 217 |
190 int socket_; | 218 int socket_; |
219 int addr_family_; | |
191 | 220 |
192 // Bitwise-or'd combination of SocketOptions. Specifies the set of | 221 // Bitwise-or'd combination of SocketOptions. Specifies the set of |
193 // options that should be applied to |socket_| before Bind(). | 222 // options that should be applied to |socket_| before Bind(). |
194 int socket_options_; | 223 int socket_options_; |
195 | 224 |
225 // Multicast socket options cached for SetSocketOption. | |
226 // Cannot be used after Bind(). | |
wtc
2013/04/17 22:06:50
"Cannot be used after Bind()": Is this still true?
Bei Zhang
2013/04/19 19:40:14
Yes, it's true this time (it wasn't). If write to
| |
227 int multicast_time_to_live_; | |
228 | |
196 // How to do source port binding, used only when UDPSocket is part of | 229 // How to do source port binding, used only when UDPSocket is part of |
197 // UDPClientSocket, since UDPServerSocket provides Bind. | 230 // UDPClientSocket, since UDPServerSocket provides Bind. |
198 DatagramSocket::BindType bind_type_; | 231 DatagramSocket::BindType bind_type_; |
199 | 232 |
200 // PRNG function for generating port numbers. | 233 // PRNG function for generating port numbers. |
201 RandIntCallback rand_int_cb_; | 234 RandIntCallback rand_int_cb_; |
202 | 235 |
203 // These are mutable since they're just cached copies to make | 236 // These are mutable since they're just cached copies to make |
204 // GetPeerAddress/GetLocalAddress smarter. | 237 // GetPeerAddress/GetLocalAddress smarter. |
205 mutable scoped_ptr<IPEndPoint> local_address_; | 238 mutable scoped_ptr<IPEndPoint> local_address_; |
(...skipping 24 matching lines...) Expand all Loading... | |
230 CompletionCallback write_callback_; | 263 CompletionCallback write_callback_; |
231 | 264 |
232 BoundNetLog net_log_; | 265 BoundNetLog net_log_; |
233 | 266 |
234 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); | 267 DISALLOW_COPY_AND_ASSIGN(UDPSocketLibevent); |
235 }; | 268 }; |
236 | 269 |
237 } // namespace net | 270 } // namespace net |
238 | 271 |
239 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ | 272 #endif // NET_UDP_UDP_SOCKET_LIBEVENT_H_ |
OLD | NEW |