| 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/socket/tcp_listen_socket.h" | 5 #include "net/socket/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 13 matching lines...) Expand all Loading... |
| 24 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
| 25 #include "net/base/winsock_init.h" | 25 #include "net/base/winsock_init.h" |
| 26 #include "net/socket/socket_descriptor.h" | 26 #include "net/socket/socket_descriptor.h" |
| 27 | 27 |
| 28 using std::string; | 28 using std::string; |
| 29 | 29 |
| 30 namespace net { | 30 namespace net { |
| 31 | 31 |
| 32 // static | 32 // static |
| 33 scoped_ptr<TCPListenSocket> TCPListenSocket::CreateAndListen( | 33 scoped_ptr<TCPListenSocket> TCPListenSocket::CreateAndListen( |
| 34 const string& ip, int port, StreamListenSocket::Delegate* del) { | 34 const string& ip, |
| 35 int port, |
| 36 StreamListenSocket::Delegate* del) { |
| 35 SocketDescriptor s = CreateAndBind(ip, port); | 37 SocketDescriptor s = CreateAndBind(ip, port); |
| 36 if (s == kInvalidSocket) | 38 if (s == kInvalidSocket) |
| 37 return scoped_ptr<TCPListenSocket>(); | 39 return scoped_ptr<TCPListenSocket>(); |
| 38 scoped_ptr<TCPListenSocket> sock(new TCPListenSocket(s, del)); | 40 scoped_ptr<TCPListenSocket> sock(new TCPListenSocket(s, del)); |
| 39 sock->Listen(); | 41 sock->Listen(); |
| 40 return sock.Pass(); | 42 return sock.Pass(); |
| 41 } | 43 } |
| 42 | 44 |
| 43 TCPListenSocket::TCPListenSocket(SocketDescriptor s, | 45 TCPListenSocket::TCPListenSocket(SocketDescriptor s, |
| 44 StreamListenSocket::Delegate* del) | 46 StreamListenSocket::Delegate* del) |
| 45 : StreamListenSocket(s, del) { | 47 : StreamListenSocket(s, del) { |
| 46 } | 48 } |
| 47 | 49 |
| 48 TCPListenSocket::~TCPListenSocket() {} | 50 TCPListenSocket::~TCPListenSocket() { |
| 51 } |
| 49 | 52 |
| 50 SocketDescriptor TCPListenSocket::CreateAndBind(const string& ip, int port) { | 53 SocketDescriptor TCPListenSocket::CreateAndBind(const string& ip, int port) { |
| 51 SocketDescriptor s = CreatePlatformSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | 54 SocketDescriptor s = CreatePlatformSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 52 if (s != kInvalidSocket) { | 55 if (s != kInvalidSocket) { |
| 53 #if defined(OS_POSIX) | 56 #if defined(OS_POSIX) |
| 54 // Allow rapid reuse. | 57 // Allow rapid reuse. |
| 55 static const int kOn = 1; | 58 static const int kOn = 1; |
| 56 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &kOn, sizeof(kOn)); | 59 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &kOn, sizeof(kOn)); |
| 57 #endif | 60 #endif |
| 58 sockaddr_in addr; | 61 sockaddr_in addr; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 73 return s; | 76 return s; |
| 74 } | 77 } |
| 75 | 78 |
| 76 SocketDescriptor TCPListenSocket::CreateAndBindAnyPort(const string& ip, | 79 SocketDescriptor TCPListenSocket::CreateAndBindAnyPort(const string& ip, |
| 77 int* port) { | 80 int* port) { |
| 78 SocketDescriptor s = CreateAndBind(ip, 0); | 81 SocketDescriptor s = CreateAndBind(ip, 0); |
| 79 if (s == kInvalidSocket) | 82 if (s == kInvalidSocket) |
| 80 return kInvalidSocket; | 83 return kInvalidSocket; |
| 81 sockaddr_in addr; | 84 sockaddr_in addr; |
| 82 socklen_t addr_size = sizeof(addr); | 85 socklen_t addr_size = sizeof(addr); |
| 83 bool failed = getsockname(s, reinterpret_cast<struct sockaddr*>(&addr), | 86 bool failed = |
| 84 &addr_size) != 0; | 87 getsockname(s, reinterpret_cast<struct sockaddr*>(&addr), &addr_size) != |
| 88 0; |
| 85 if (addr_size != sizeof(addr)) | 89 if (addr_size != sizeof(addr)) |
| 86 failed = true; | 90 failed = true; |
| 87 if (failed) { | 91 if (failed) { |
| 88 LOG(ERROR) << "Could not determine bound port, getsockname() failed"; | 92 LOG(ERROR) << "Could not determine bound port, getsockname() failed"; |
| 89 #if defined(OS_WIN) | 93 #if defined(OS_WIN) |
| 90 closesocket(s); | 94 closesocket(s); |
| 91 #elif defined(OS_POSIX) | 95 #elif defined(OS_POSIX) |
| 92 close(s); | 96 close(s); |
| 93 #endif | 97 #endif |
| 94 return kInvalidSocket; | 98 return kInvalidSocket; |
| 95 } | 99 } |
| 96 *port = base::NetToHost16(addr.sin_port); | 100 *port = base::NetToHost16(addr.sin_port); |
| 97 return s; | 101 return s; |
| 98 } | 102 } |
| 99 | 103 |
| 100 void TCPListenSocket::Accept() { | 104 void TCPListenSocket::Accept() { |
| 101 SocketDescriptor conn = AcceptSocket(); | 105 SocketDescriptor conn = AcceptSocket(); |
| 102 if (conn == kInvalidSocket) | 106 if (conn == kInvalidSocket) |
| 103 return; | 107 return; |
| 104 scoped_ptr<TCPListenSocket> sock( | 108 scoped_ptr<TCPListenSocket> sock(new TCPListenSocket(conn, socket_delegate_)); |
| 105 new TCPListenSocket(conn, socket_delegate_)); | 109 // It's up to the delegate to AddRef if it wants to keep it around. |
| 106 // It's up to the delegate to AddRef if it wants to keep it around. | |
| 107 #if defined(OS_POSIX) | 110 #if defined(OS_POSIX) |
| 108 sock->WatchSocket(WAITING_READ); | 111 sock->WatchSocket(WAITING_READ); |
| 109 #endif | 112 #endif |
| 110 socket_delegate_->DidAccept(this, sock.PassAs<StreamListenSocket>()); | 113 socket_delegate_->DidAccept(this, sock.PassAs<StreamListenSocket>()); |
| 111 } | 114 } |
| 112 | 115 |
| 113 TCPListenSocketFactory::TCPListenSocketFactory(const string& ip, int port) | 116 TCPListenSocketFactory::TCPListenSocketFactory(const string& ip, int port) |
| 114 : ip_(ip), | 117 : ip_(ip), port_(port) { |
| 115 port_(port) { | |
| 116 } | 118 } |
| 117 | 119 |
| 118 TCPListenSocketFactory::~TCPListenSocketFactory() {} | 120 TCPListenSocketFactory::~TCPListenSocketFactory() { |
| 121 } |
| 119 | 122 |
| 120 scoped_ptr<StreamListenSocket> TCPListenSocketFactory::CreateAndListen( | 123 scoped_ptr<StreamListenSocket> TCPListenSocketFactory::CreateAndListen( |
| 121 StreamListenSocket::Delegate* delegate) const { | 124 StreamListenSocket::Delegate* delegate) const { |
| 122 return TCPListenSocket::CreateAndListen(ip_, port_, delegate) | 125 return TCPListenSocket::CreateAndListen(ip_, port_, delegate) |
| 123 .PassAs<StreamListenSocket>(); | 126 .PassAs<StreamListenSocket>(); |
| 124 } | 127 } |
| 125 | 128 |
| 126 } // namespace net | 129 } // namespace net |
| OLD | NEW |