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

Unified Diff: net/udp/udp_socket_libevent.cc

Issue 10739002: Added broadcasting feature to UDP server sockets. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed Win code. Created 8 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: net/udp/udp_socket_libevent.cc
diff --git a/net/udp/udp_socket_libevent.cc b/net/udp/udp_socket_libevent.cc
index bb1cf91ede1fac18c855dca63eb4b12d701a9244..3782db3c24b5bcacd8ed01af2a39270357055d3d 100644
--- a/net/udp/udp_socket_libevent.cc
+++ b/net/udp/udp_socket_libevent.cc
@@ -41,6 +41,7 @@ UDPSocketLibevent::UDPSocketLibevent(
net::NetLog* net_log,
const net::NetLog::Source& source)
: socket_(kInvalidSocket),
+ socket_options_(0),
bind_type_(bind_type),
rand_int_cb_(rand_int_cb),
read_watcher_(this),
@@ -251,6 +252,9 @@ int UDPSocketLibevent::Bind(const IPEndPoint& address) {
int rv = CreateSocket(address);
if (rv < 0)
return rv;
+ rv = SetSocketOptions();
+ if (rv < 0)
+ return rv;
rv = DoBind(address);
if (rv < 0)
return rv;
@@ -274,6 +278,20 @@ bool UDPSocketLibevent::SetSendBufferSize(int32 size) {
return rv == 0;
}
+void UDPSocketLibevent::AllowAddressReuse() {
+ DCHECK(CalledOnValidThread());
+ DCHECK(!is_connected());
+
+ socket_options_ |= SOCKET_OPTION_REUSE_ADDRESS;
+}
+
+void UDPSocketLibevent::AllowBroadcast() {
+ DCHECK(CalledOnValidThread());
+ DCHECK(!is_connected());
+
+ socket_options_ |= SOCKET_OPTION_BROADCAST;
+}
+
void UDPSocketLibevent::DoReadCallback(int rv) {
DCHECK_NE(rv, ERR_IO_PENDING);
DCHECK(!read_callback_.is_null());
@@ -430,6 +448,29 @@ int UDPSocketLibevent::InternalSendTo(IOBuffer* buf, int buf_len,
return result;
}
+int UDPSocketLibevent::SetSocketOptions() {
+ int true_value = 1;
+ if (socket_options_ & SOCKET_OPTION_REUSE_ADDRESS) {
+ int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &true_value,
+ sizeof(true_value));
+ if (rv < 0)
+ return MapSystemError(errno);
+#if defined(SO_REUSEPORT)
+ rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEPORT, &true_value,
+ sizeof(true_value));
+ if (rv < 0)
+ return MapSystemError(errno);
+#endif
+ }
+ if (socket_options_ & SOCKET_OPTION_BROADCAST) {
+ int rv = setsockopt(socket_, SOL_SOCKET, SO_BROADCAST, &true_value,
+ sizeof(true_value));
+ if (rv < 0)
+ return MapSystemError(errno);
+ }
+ return OK;
+}
+
int UDPSocketLibevent::DoBind(const IPEndPoint& address) {
SockaddrStorage storage;
if (!address.ToSockAddr(storage.addr, &storage.addr_len))

Powered by Google App Engine
This is Rietveld 408576698