Chromium Code Reviews

Side by Side Diff: net/socket/tcp_server_socket_libevent.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: review changes Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
OLDNEW
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 #include "net/socket/tcp_server_socket_libevent.h" 5 #include "net/socket/tcp_server_socket_libevent.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <netdb.h> 9 #include <netdb.h>
10 #include <sys/socket.h> 10 #include <sys/socket.h>
(...skipping 45 matching lines...)
56 PLOG(ERROR) << "socket() returned an error"; 56 PLOG(ERROR) << "socket() returned an error";
57 return MapSystemError(errno); 57 return MapSystemError(errno);
58 } 58 }
59 59
60 if (SetNonBlocking(socket_)) { 60 if (SetNonBlocking(socket_)) {
61 int result = MapSystemError(errno); 61 int result = MapSystemError(errno);
62 Close(); 62 Close();
63 return result; 63 return result;
64 } 64 }
65 65
66 int result = SetSocketOptions();
67 if (result < 0) {
68 return result;
69 }
70
66 SockaddrStorage storage; 71 SockaddrStorage storage;
67 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) 72 if (!address.ToSockAddr(storage.addr, &storage.addr_len))
68 return ERR_INVALID_ARGUMENT; 73 return ERR_INVALID_ARGUMENT;
69 74
70 int result = bind(socket_, storage.addr, storage.addr_len); 75 result = bind(socket_, storage.addr, storage.addr_len);
71 if (result < 0) { 76 if (result < 0) {
72 PLOG(ERROR) << "bind() returned an error"; 77 PLOG(ERROR) << "bind() returned an error";
73 result = MapSystemError(errno); 78 result = MapSystemError(errno);
74 Close(); 79 Close();
75 return result; 80 return result;
76 } 81 }
77 82
78 result = listen(socket_, backlog); 83 result = listen(socket_, backlog);
79 if (result < 0) { 84 if (result < 0) {
80 PLOG(ERROR) << "listen() returned an error"; 85 PLOG(ERROR) << "listen() returned an error";
(...skipping 102 matching lines...)
183 CompletionCallback callback = accept_callback_; 188 CompletionCallback callback = accept_callback_;
184 accept_callback_.Reset(); 189 accept_callback_.Reset();
185 callback.Run(result); 190 callback.Run(result);
186 } 191 }
187 } 192 }
188 193
189 void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { 194 void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) {
190 NOTREACHED(); 195 NOTREACHED();
191 } 196 }
192 197
198 void TCPServerSocketLibevent::AllowAddressReuse() {
199 DCHECK(CalledOnValidThread());
200 DCHECK_EQ(socket_, kInvalidSocket);
201
202 socket_options_ |= SOCKET_OPTION_REUSE_ADDRESS;
203 }
204
205 int TCPServerSocketLibevent::SetSocketOptions() {
206 int true_value = 1;
207 if (socket_options_ & SOCKET_OPTION_REUSE_ADDRESS) {
208 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &true_value,
209 sizeof(true_value));
210 if (rv < 0)
211 return MapSystemError(errno);
212 }
213 return OK;
214 }
215
193 } // namespace net 216 } // namespace net
OLDNEW

Powered by Google App Engine