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

Unified Diff: net/socket/tcp_server_socket_win.cc

Issue 10907154: Allow server sockets to rebind to same port if there is nothing actively listening on that port. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Windows build Created 8 years, 3 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/socket/tcp_server_socket_win.cc
diff --git a/net/socket/tcp_server_socket_win.cc b/net/socket/tcp_server_socket_win.cc
index eb15ffbdaf9bb691a57642e77e4b9fa46a357f86..e2a1b78a1e939e5cbdff029a10711ad6b94d674b 100644
--- a/net/socket/tcp_server_socket_win.cc
+++ b/net/socket/tcp_server_socket_win.cc
@@ -21,6 +21,7 @@ TCPServerSocketWin::TCPServerSocketWin(net::NetLog* net_log,
: socket_(INVALID_SOCKET),
socket_event_(WSA_INVALID_EVENT),
accept_socket_(NULL),
+ reuse_address_(false),
net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) {
net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE,
source.ToEventParametersCallback());
@@ -56,11 +57,16 @@ int TCPServerSocketWin::Listen(const IPEndPoint& address, int backlog) {
return result;
}
+ int result = SetSocketOptions();
+ if (result < 0) {
wtc 2012/09/13 18:32:56 Nit: this should also be changed to result != OK.
justinlin 2012/09/14 03:54:40 Done.
+ return result;
+ }
+
SockaddrStorage storage;
if (!address.ToSockAddr(storage.addr, &storage.addr_len))
return ERR_INVALID_ARGUMENT;
- int result = bind(socket_, storage.addr, storage.addr_len);
+ result = bind(socket_, storage.addr, storage.addr_len);
if (result < 0) {
PLOG(ERROR) << "bind() returned an error";
result = MapSystemError(WSAGetLastError());
@@ -115,6 +121,26 @@ int TCPServerSocketWin::Accept(
return result;
}
+void TCPServerSocketWin::AllowAddressReuse() {
+ DCHECK(CalledOnValidThread());
+ DCHECK_EQ(socket_, INVALID_SOCKET);
+ DCHECK_EQ(socket_event_, WSA_INVALID_EVENT);
+
+ reuse_address_ = true;
+}
+
+int TCPServerSocketWin::SetSocketOptions() {
+ BOOL true_value = 1;
+ if (reuse_address_) {
+ int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR,
+ reinterpret_cast<const char*>(&true_value),
+ sizeof(true_value));
+ if (rv < 0)
+ return MapSystemError(errno);
+ }
+ return OK;
+}
+
int TCPServerSocketWin::AcceptInternal(scoped_ptr<StreamSocket>* socket) {
SockaddrStorage storage;
int new_socket = accept(socket_, storage.addr, &storage.addr_len);
« net/socket/tcp_server_socket_libevent.cc ('K') | « net/socket/tcp_server_socket_win.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698