| 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 #include "net/base/tcp_listen_socket.h" | 5 #include "net/base/tcp_listen_socket.h" | 
| 6 | 6 | 
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) | 
| 8 // winsock2.h must be included first in order to ensure it is included before | 8 // winsock2.h must be included first in order to ensure it is included before | 
| 9 // windows.h. | 9 // windows.h. | 
| 10 #include <winsock2.h> | 10 #include <winsock2.h> | 
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 69 #elif defined(OS_POSIX) | 69 #elif defined(OS_POSIX) | 
| 70       close(s); | 70       close(s); | 
| 71 #endif | 71 #endif | 
| 72       LOG(ERROR) << "Could not bind socket to " << ip << ":" << port; | 72       LOG(ERROR) << "Could not bind socket to " << ip << ":" << port; | 
| 73       s = kInvalidSocket; | 73       s = kInvalidSocket; | 
| 74     } | 74     } | 
| 75   } | 75   } | 
| 76   return s; | 76   return s; | 
| 77 } | 77 } | 
| 78 | 78 | 
|  | 79 SocketDescriptor TCPListenSocket::CreateAndBindAnyPort(const string& ip, | 
|  | 80                                                        int* port) { | 
|  | 81   SocketDescriptor s = CreateAndBind(ip, 0); | 
|  | 82   if (s == kInvalidSocket) | 
|  | 83     return kInvalidSocket; | 
|  | 84   sockaddr_in addr; | 
|  | 85   socklen_t addr_size = sizeof(addr); | 
|  | 86   bool failed = getsockname(s, reinterpret_cast<struct sockaddr*>(&addr), | 
|  | 87                             &addr_size) != 0; | 
|  | 88   if (addr_size != sizeof(addr)) | 
|  | 89     failed = true; | 
|  | 90   if (failed) { | 
|  | 91     LOG(ERROR) << "Could not determine bound port, getsockname() failed"; | 
|  | 92 #if defined(OS_WIN) | 
|  | 93     closesocket(s); | 
|  | 94 #elif defined(OS_POSIX) | 
|  | 95     close(s); | 
|  | 96 #endif | 
|  | 97     return kInvalidSocket; | 
|  | 98   } | 
|  | 99   *port = base::NetToHost16(addr.sin_port); | 
|  | 100   return s; | 
|  | 101 } | 
|  | 102 | 
| 79 void TCPListenSocket::Accept() { | 103 void TCPListenSocket::Accept() { | 
| 80   SocketDescriptor conn = AcceptSocket(); | 104   SocketDescriptor conn = AcceptSocket(); | 
| 81   if (conn == kInvalidSocket) | 105   if (conn == kInvalidSocket) | 
| 82     return; | 106     return; | 
| 83   scoped_refptr<TCPListenSocket> sock( | 107   scoped_refptr<TCPListenSocket> sock( | 
| 84       new TCPListenSocket(conn, socket_delegate_)); | 108       new TCPListenSocket(conn, socket_delegate_)); | 
| 85   // It's up to the delegate to AddRef if it wants to keep it around. | 109   // It's up to the delegate to AddRef if it wants to keep it around. | 
| 86 #if defined(OS_POSIX) | 110 #if defined(OS_POSIX) | 
| 87   sock->WatchSocket(WAITING_READ); | 111   sock->WatchSocket(WAITING_READ); | 
| 88 #endif | 112 #endif | 
| 89   socket_delegate_->DidAccept(this, sock); | 113   socket_delegate_->DidAccept(this, sock); | 
| 90 } | 114 } | 
| 91 | 115 | 
| 92 TCPListenSocketFactory::TCPListenSocketFactory(const string& ip, int port) | 116 TCPListenSocketFactory::TCPListenSocketFactory(const string& ip, int port) | 
| 93     : ip_(ip), | 117     : ip_(ip), | 
| 94       port_(port) { | 118       port_(port) { | 
| 95 } | 119 } | 
| 96 | 120 | 
| 97 TCPListenSocketFactory::~TCPListenSocketFactory() {} | 121 TCPListenSocketFactory::~TCPListenSocketFactory() {} | 
| 98 | 122 | 
| 99 scoped_refptr<StreamListenSocket> TCPListenSocketFactory::CreateAndListen( | 123 scoped_refptr<StreamListenSocket> TCPListenSocketFactory::CreateAndListen( | 
| 100     StreamListenSocket::Delegate* delegate) const { | 124     StreamListenSocket::Delegate* delegate) const { | 
| 101   return TCPListenSocket::CreateAndListen(ip_, port_, delegate); | 125   return TCPListenSocket::CreateAndListen(ip_, port_, delegate); | 
| 102 } | 126 } | 
| 103 | 127 | 
| 104 }  // namespace net | 128 }  // namespace net | 
| OLD | NEW | 
|---|