| Index: net/socket/tcp_socket_win.cc
|
| diff --git a/net/socket/tcp_server_socket_win.cc b/net/socket/tcp_socket_win.cc
|
| similarity index 65%
|
| rename from net/socket/tcp_server_socket_win.cc
|
| rename to net/socket/tcp_socket_win.cc
|
| index 0ac77be5e81439e768e55e49db4966dfbb1c94b2..48935bbf04def2591c4c89b938d74252a707779c 100644
|
| --- a/net/socket/tcp_server_socket_win.cc
|
| +++ b/net/socket/tcp_socket_win.cc
|
| @@ -1,50 +1,54 @@
|
| -// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "net/socket/tcp_server_socket_win.h"
|
| +#include "net/socket/tcp_socket_win.h"
|
|
|
| #include <mstcpip.h>
|
|
|
| +#include "base/logging.h"
|
| #include "net/base/ip_endpoint.h"
|
| #include "net/base/net_errors.h"
|
| #include "net/base/net_util.h"
|
| #include "net/base/winsock_init.h"
|
| #include "net/base/winsock_util.h"
|
| #include "net/socket/socket_net_log_params.h"
|
| -#include "net/socket/tcp_client_socket.h"
|
|
|
| namespace net {
|
|
|
| -TCPServerSocketWin::TCPServerSocketWin(net::NetLog* net_log,
|
| - const net::NetLog::Source& source)
|
| +TCPSocketWin::TCPSocketWin(net::NetLog* net_log,
|
| + const net::NetLog::Source& source)
|
| : socket_(INVALID_SOCKET),
|
| socket_event_(WSA_INVALID_EVENT),
|
| accept_socket_(NULL),
|
| + accept_address_(NULL),
|
| net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) {
|
| net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE,
|
| source.ToEventParametersCallback());
|
| EnsureWinsockInit();
|
| }
|
|
|
| -TCPServerSocketWin::~TCPServerSocketWin() {
|
| +TCPSocketWin::~TCPSocketWin() {
|
| Close();
|
| net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE);
|
| }
|
|
|
| -int TCPServerSocketWin::Listen(const IPEndPoint& address, int backlog) {
|
| +int TCPSocketWin::Create(AddressFamily family) {
|
| DCHECK(CalledOnValidThread());
|
| - DCHECK_GT(backlog, 0);
|
| DCHECK_EQ(socket_, INVALID_SOCKET);
|
| - DCHECK_EQ(socket_event_, WSA_INVALID_EVENT);
|
|
|
| - socket_event_ = WSACreateEvent();
|
| - if (socket_event_ == WSA_INVALID_EVENT) {
|
| - PLOG(ERROR) << "WSACreateEvent()";
|
| - return ERR_FAILED;
|
| + int address_family = AF_UNSPEC;
|
| + switch (family) {
|
| + case ADDRESS_FAMILY_IPV4:
|
| + address_family = AF_INET;
|
| + break;
|
| + case ADDRESS_FAMILY_IPV6:
|
| + address_family = AF_INET6;
|
| + break;
|
| + default:
|
| + NOTREACHED();
|
| }
|
| -
|
| - socket_ = socket(address.GetSockAddrFamily(), SOCK_STREAM, IPPROTO_TCP);
|
| + socket_ = socket(address_family, SOCK_STREAM, IPPROTO_TCP);
|
| if (socket_ == INVALID_SOCKET) {
|
| PLOG(ERROR) << "socket() returned an error";
|
| return MapSystemError(WSAGetLastError());
|
| @@ -56,38 +60,52 @@ int TCPServerSocketWin::Listen(const IPEndPoint& address, int backlog) {
|
| return result;
|
| }
|
|
|
| - int result = SetSocketOptions();
|
| - if (result != OK) {
|
| + return OK;
|
| +}
|
| +
|
| +int TCPSocketWin::Adopt(SOCKET socket) {
|
| + DCHECK(CalledOnValidThread());
|
| + DCHECK_EQ(socket_, INVALID_SOCKET);
|
| +
|
| + socket_ = socket;
|
| +
|
| + if (SetNonBlocking(socket_)) {
|
| + int result = MapSystemError(WSAGetLastError());
|
| Close();
|
| return result;
|
| }
|
|
|
| + return OK;
|
| +}
|
| +
|
| +SOCKET TCPSocketWin::Release() {
|
| + DCHECK(CalledOnValidThread());
|
| + DCHECK_EQ(socket_event_, WSA_INVALID_EVENT);
|
| + DCHECK(accept_callback_.is_null());
|
| +
|
| + SOCKET result = socket_;
|
| + socket_ = INVALID_SOCKET;
|
| + return result;
|
| +}
|
| +
|
| +int TCPSocketWin::Bind(const IPEndPoint& address) {
|
| + DCHECK(CalledOnValidThread());
|
| + DCHECK_NE(socket_, INVALID_SOCKET);
|
| +
|
| SockaddrStorage storage;
|
| - if (!address.ToSockAddr(storage.addr, &storage.addr_len)) {
|
| - Close();
|
| + if (!address.ToSockAddr(storage.addr, &storage.addr_len))
|
| return ERR_ADDRESS_INVALID;
|
| - }
|
|
|
| - result = bind(socket_, storage.addr, storage.addr_len);
|
| + int result = bind(socket_, storage.addr, storage.addr_len);
|
| if (result < 0) {
|
| PLOG(ERROR) << "bind() returned an error";
|
| - result = MapSystemError(WSAGetLastError());
|
| - Close();
|
| - return result;
|
| - }
|
| -
|
| - result = listen(socket_, backlog);
|
| - if (result < 0) {
|
| - PLOG(ERROR) << "listen() returned an error";
|
| - result = MapSystemError(WSAGetLastError());
|
| - Close();
|
| - return result;
|
| + return MapSystemError(WSAGetLastError());
|
| }
|
|
|
| return OK;
|
| }
|
|
|
| -int TCPServerSocketWin::GetLocalAddress(IPEndPoint* address) const {
|
| +int TCPSocketWin::GetLocalAddress(IPEndPoint* address) const {
|
| DCHECK(CalledOnValidThread());
|
| DCHECK(address);
|
|
|
| @@ -100,8 +118,31 @@ int TCPServerSocketWin::GetLocalAddress(IPEndPoint* address) const {
|
| return OK;
|
| }
|
|
|
| -int TCPServerSocketWin::Accept(
|
| - scoped_ptr<StreamSocket>* socket, const CompletionCallback& callback) {
|
| +int TCPSocketWin::Listen(int backlog) {
|
| + DCHECK(CalledOnValidThread());
|
| + DCHECK_GT(backlog, 0);
|
| + DCHECK_NE(socket_, INVALID_SOCKET);
|
| + DCHECK_EQ(socket_event_, WSA_INVALID_EVENT);
|
| +
|
| + socket_event_ = WSACreateEvent();
|
| + if (socket_event_ == WSA_INVALID_EVENT) {
|
| + PLOG(ERROR) << "WSACreateEvent()";
|
| + return ERR_FAILED;
|
| + }
|
| +
|
| + int result = listen(socket_, backlog);
|
| + if (result < 0) {
|
| + PLOG(ERROR) << "listen() returned an error";
|
| + result = MapSystemError(WSAGetLastError());
|
| + return result;
|
| + }
|
| +
|
| + return OK;
|
| +}
|
| +
|
| +int TCPSocketWin::Accept(scoped_ptr<TCPSocketWin>* socket,
|
| + IPEndPoint* address,
|
| + const CompletionCallback& callback) {
|
| DCHECK(CalledOnValidThread());
|
| DCHECK(socket);
|
| DCHECK(!callback.is_null());
|
| @@ -109,21 +150,22 @@ int TCPServerSocketWin::Accept(
|
|
|
| net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT);
|
|
|
| - int result = AcceptInternal(socket);
|
| + int result = AcceptInternal(socket, address);
|
|
|
| if (result == ERR_IO_PENDING) {
|
| - // Start watching
|
| + // Start watching.
|
| WSAEventSelect(socket_, socket_event_, FD_ACCEPT);
|
| accept_watcher_.StartWatching(socket_event_, this);
|
|
|
| accept_socket_ = socket;
|
| + accept_address_ = address;
|
| accept_callback_ = callback;
|
| }
|
|
|
| return result;
|
| }
|
|
|
| -int TCPServerSocketWin::SetSocketOptions() {
|
| +int TCPSocketWin::SetExclusiveAddrUse() {
|
| // On Windows, a bound end point can be hijacked by another process by
|
| // setting SO_REUSEADDR. Therefore a Windows-only option SO_EXCLUSIVEADDRUSE
|
| // was introduced in Windows NT 4.0 SP4. If the socket that is bound to the
|
| @@ -149,7 +191,21 @@ int TCPServerSocketWin::SetSocketOptions() {
|
| return OK;
|
| }
|
|
|
| -int TCPServerSocketWin::AcceptInternal(scoped_ptr<StreamSocket>* socket) {
|
| +void TCPSocketWin::Close() {
|
| + if (socket_ != INVALID_SOCKET) {
|
| + if (closesocket(socket_) < 0)
|
| + PLOG(ERROR) << "closesocket";
|
| + socket_ = INVALID_SOCKET;
|
| + }
|
| +
|
| + if (socket_event_) {
|
| + WSACloseEvent(socket_event_);
|
| + socket_event_ = WSA_INVALID_EVENT;
|
| + }
|
| +}
|
| +
|
| +int TCPSocketWin::AcceptInternal(scoped_ptr<TCPSocketWin>* socket,
|
| + IPEndPoint* address) {
|
| SockaddrStorage storage;
|
| int new_socket = accept(socket_, storage.addr, &storage.addr_len);
|
| if (new_socket < 0) {
|
| @@ -159,44 +215,30 @@ int TCPServerSocketWin::AcceptInternal(scoped_ptr<StreamSocket>* socket) {
|
| return net_error;
|
| }
|
|
|
| - IPEndPoint address;
|
| - if (!address.FromSockAddr(storage.addr, storage.addr_len)) {
|
| + IPEndPoint ip_end_point;
|
| + if (!ip_end_point.FromSockAddr(storage.addr, storage.addr_len)) {
|
| NOTREACHED();
|
| if (closesocket(new_socket) < 0)
|
| PLOG(ERROR) << "closesocket";
|
| net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED);
|
| return ERR_FAILED;
|
| }
|
| - scoped_ptr<TCPClientSocket> tcp_socket(new TCPClientSocket(
|
| - AddressList(address),
|
| + scoped_ptr<TCPSocketWin> tcp_socket(new TCPSocketWin(
|
| net_log_.net_log(), net_log_.source()));
|
| - int adopt_result = tcp_socket->AdoptSocket(new_socket);
|
| + int adopt_result = tcp_socket->Adopt(new_socket);
|
| if (adopt_result != OK) {
|
| - if (closesocket(new_socket) < 0)
|
| - PLOG(ERROR) << "closesocket";
|
| net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result);
|
| return adopt_result;
|
| }
|
| socket->reset(tcp_socket.release());
|
| + if (address)
|
| + *address = ip_end_point;
|
| net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT,
|
| - CreateNetLogIPEndPointCallback(&address));
|
| + CreateNetLogIPEndPointCallback(&ip_end_point));
|
| return OK;
|
| }
|
|
|
| -void TCPServerSocketWin::Close() {
|
| - if (socket_ != INVALID_SOCKET) {
|
| - if (closesocket(socket_) < 0)
|
| - PLOG(ERROR) << "closesocket";
|
| - socket_ = INVALID_SOCKET;
|
| - }
|
| -
|
| - if (socket_event_) {
|
| - WSACloseEvent(socket_event_);
|
| - socket_event_ = WSA_INVALID_EVENT;
|
| - }
|
| -}
|
| -
|
| -void TCPServerSocketWin::OnObjectSignaled(HANDLE object) {
|
| +void TCPSocketWin::OnObjectSignaled(HANDLE object) {
|
| WSANETWORKEVENTS ev;
|
| if (WSAEnumNetworkEvents(socket_, socket_event_, &ev) == SOCKET_ERROR) {
|
| PLOG(ERROR) << "WSAEnumNetworkEvents()";
|
| @@ -204,9 +246,10 @@ void TCPServerSocketWin::OnObjectSignaled(HANDLE object) {
|
| }
|
|
|
| if (ev.lNetworkEvents & FD_ACCEPT) {
|
| - int result = AcceptInternal(accept_socket_);
|
| + int result = AcceptInternal(accept_socket_, accept_address_);
|
| if (result != ERR_IO_PENDING) {
|
| accept_socket_ = NULL;
|
| + accept_address_ = NULL;
|
| CompletionCallback callback = accept_callback_;
|
| accept_callback_.Reset();
|
| callback.Run(result);
|
|
|