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

Side by Side Diff: net/socket/tcp_socket_posix.cc

Issue 2815993002: Adds a method to TCPServerSocket to adopt a socket. (Closed)
Patch Set: Refined GetSocketDescriptor per review. Created 3 years, 7 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 unified diff | Download patch
« no previous file with comments | « net/socket/tcp_socket_posix.h ('k') | net/socket/tcp_socket_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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_socket.h" 5 #include "net/socket/tcp_socket.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <netinet/tcp.h> 8 #include <netinet/tcp.h>
9 #include <sys/socket.h> 9 #include <sys/socket.h>
10 10
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 168
169 int TCPSocketPosix::Open(AddressFamily family) { 169 int TCPSocketPosix::Open(AddressFamily family) {
170 DCHECK(!socket_); 170 DCHECK(!socket_);
171 socket_.reset(new SocketPosix); 171 socket_.reset(new SocketPosix);
172 int rv = socket_->Open(ConvertAddressFamily(family)); 172 int rv = socket_->Open(ConvertAddressFamily(family));
173 if (rv != OK) 173 if (rv != OK)
174 socket_.reset(); 174 socket_.reset();
175 return rv; 175 return rv;
176 } 176 }
177 177
178 int TCPSocketPosix::AdoptConnectedSocket(int socket_fd, 178 int TCPSocketPosix::AdoptConnectedSocket(SocketDescriptor socket,
179 const IPEndPoint& peer_address) { 179 const IPEndPoint& peer_address) {
180 DCHECK(!socket_); 180 DCHECK(!socket_);
181 181
182 SockaddrStorage storage; 182 SockaddrStorage storage;
183 if (!peer_address.ToSockAddr(storage.addr, &storage.addr_len) && 183 if (!peer_address.ToSockAddr(storage.addr, &storage.addr_len) &&
184 // For backward compatibility, allows the empty address. 184 // For backward compatibility, allows the empty address.
185 !(peer_address == IPEndPoint())) { 185 !(peer_address == IPEndPoint())) {
186 return ERR_ADDRESS_INVALID; 186 return ERR_ADDRESS_INVALID;
187 } 187 }
188 188
189 socket_.reset(new SocketPosix); 189 socket_.reset(new SocketPosix);
190 int rv = socket_->AdoptConnectedSocket(socket_fd, storage); 190 int rv = socket_->AdoptConnectedSocket(socket, storage);
191 if (rv != OK) 191 if (rv != OK)
192 socket_.reset(); 192 socket_.reset();
193 return rv; 193 return rv;
194 }
195
196 int TCPSocketPosix::AdoptUnconnectedSocket(SocketDescriptor socket) {
197 DCHECK(!socket_);
198
199 socket_.reset(new SocketPosix);
200 int rv = socket_->AdoptUnconnectedSocket(socket);
201 if (rv != OK)
202 socket_.reset();
203 return rv;
194 } 204 }
195 205
196 int TCPSocketPosix::Bind(const IPEndPoint& address) { 206 int TCPSocketPosix::Bind(const IPEndPoint& address) {
197 DCHECK(socket_); 207 DCHECK(socket_);
198 208
199 SockaddrStorage storage; 209 SockaddrStorage storage;
200 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) 210 if (!address.ToSockAddr(storage.addr, &storage.addr_len))
201 return ERR_ADDRESS_INVALID; 211 return ERR_ADDRESS_INVALID;
202 212
203 return socket_->Bind(storage); 213 return socket_->Bind(storage);
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 488
479 void TCPSocketPosix::EndLoggingMultipleConnectAttempts(int net_error) { 489 void TCPSocketPosix::EndLoggingMultipleConnectAttempts(int net_error) {
480 if (logging_multiple_connect_attempts_) { 490 if (logging_multiple_connect_attempts_) {
481 LogConnectEnd(net_error); 491 LogConnectEnd(net_error);
482 logging_multiple_connect_attempts_ = false; 492 logging_multiple_connect_attempts_ = false;
483 } else { 493 } else {
484 NOTREACHED(); 494 NOTREACHED();
485 } 495 }
486 } 496 }
487 497
498 SocketDescriptor TCPSocketPosix::ReleaseSocketDescriptorForTesting() {
499 SocketDescriptor socket_descriptor = socket_->ReleaseConnectedSocket();
500 socket_.reset();
501 return socket_descriptor;
502 }
503
488 void TCPSocketPosix::AcceptCompleted( 504 void TCPSocketPosix::AcceptCompleted(
489 std::unique_ptr<TCPSocketPosix>* tcp_socket, 505 std::unique_ptr<TCPSocketPosix>* tcp_socket,
490 IPEndPoint* address, 506 IPEndPoint* address,
491 const CompletionCallback& callback, 507 const CompletionCallback& callback,
492 int rv) { 508 int rv) {
493 DCHECK_NE(ERR_IO_PENDING, rv); 509 DCHECK_NE(ERR_IO_PENDING, rv);
494 callback.Run(HandleAcceptCompleted(tcp_socket, address, rv)); 510 callback.Run(HandleAcceptCompleted(tcp_socket, address, rv));
495 } 511 }
496 512
497 int TCPSocketPosix::HandleAcceptCompleted( 513 int TCPSocketPosix::HandleAcceptCompleted(
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
814 if (info.tcpi_rtt > 0) { 830 if (info.tcpi_rtt > 0) {
815 *out_rtt = base::TimeDelta::FromMicroseconds(info.tcpi_rtt); 831 *out_rtt = base::TimeDelta::FromMicroseconds(info.tcpi_rtt);
816 return true; 832 return true;
817 } 833 }
818 } 834 }
819 #endif // defined(TCP_INFO) 835 #endif // defined(TCP_INFO)
820 return false; 836 return false;
821 } 837 }
822 838
823 } // namespace net 839 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/tcp_socket_posix.h ('k') | net/socket/tcp_socket_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698