1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
// Copyright (c) 2011 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 <mstcpip.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/tcp_client_socket.h"
namespace net {
TCPServerSocketWin::TCPServerSocketWin(net::NetLog* net_log,
const net::NetLog::Source& source)
: socket_(INVALID_SOCKET),
socket_event_(WSA_INVALID_EVENT),
accept_socket_(NULL),
accept_callback_(NULL),
net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) {
scoped_refptr<NetLog::EventParameters> params;
if (source.is_valid())
params = new NetLogSourceParameter("source_dependency", source);
net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params);
EnsureWinsockInit();
}
TCPServerSocketWin::~TCPServerSocketWin() {
Close();
net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE, NULL);
}
int TCPServerSocketWin::Listen(const IPEndPoint& address, int backlog) {
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;
}
socket_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socket_ < 0) {
PLOG(ERROR) << "socket() returned an error";
return MapSystemError(WSAGetLastError());
}
if (SetNonBlocking(socket_)) {
int result = MapSystemError(WSAGetLastError());
Close();
return result;
}
struct sockaddr_storage addr_storage;
size_t addr_len = sizeof(addr_storage);
struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage);
if (!address.ToSockAddr(addr, &addr_len))
return ERR_INVALID_ARGUMENT;
int result = bind(socket_, addr, 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 OK;
}
int TCPServerSocketWin::GetLocalAddress(IPEndPoint* address) const {
DCHECK(CalledOnValidThread());
DCHECK(address);
struct sockaddr_storage addr_storage;
socklen_t addr_len = sizeof(addr_storage);
struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage);
if (getsockname(socket_, addr, &addr_len))
return MapSystemError(WSAGetLastError());
if (!address->FromSockAddr(addr, addr_len))
return ERR_FAILED;
return OK;
}
int TCPServerSocketWin::Accept(
scoped_ptr<StreamSocket>* socket, CompletionCallback* callback) {
DCHECK(CalledOnValidThread());
DCHECK(socket);
DCHECK(callback);
DCHECK(!accept_callback_);
net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT, NULL);
int result = AcceptInternal(socket);
if (result == ERR_IO_PENDING) {
// Start watching
WSAEventSelect(socket_, socket_event_, FD_ACCEPT);
accept_watcher_.StartWatching(socket_event_, this);
accept_socket_ = socket;
accept_callback_ = callback;
}
return result;
}
int TCPServerSocketWin::AcceptInternal(scoped_ptr<StreamSocket>* socket) {
struct sockaddr_storage addr_storage;
socklen_t addr_len = sizeof(addr_storage);
struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage);
int result = accept(socket_, addr, &addr_len);
if (result < 0) {
int net_error = MapSystemError(WSAGetLastError());
if (net_error != ERR_IO_PENDING)
net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error);
return net_error;
}
IPEndPoint address;
if (!address.FromSockAddr(addr, addr_len)) {
NOTREACHED();
if (closesocket(result) < 0)
PLOG(ERROR) << "closesocket";
net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED);
return ERR_FAILED;
}
TCPClientSocket* tcp_socket = new TCPClientSocket(
AddressList::CreateFromIPAddress(address.address(), address.port()),
net_log_.net_log(), net_log_.source());
tcp_socket->AdoptSocket(result);
socket->reset(tcp_socket);
net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT,
make_scoped_refptr(new NetLogStringParameter(
"address", address.ToString())));
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) {
WSANETWORKEVENTS ev;
if (WSAEnumNetworkEvents(socket_, socket_event_, &ev) == SOCKET_ERROR) {
PLOG(ERROR) << "WSAEnumNetworkEvents()";
return;
}
if (ev.lNetworkEvents & FD_ACCEPT) {
int result = AcceptInternal(accept_socket_);
if (result != ERR_IO_PENDING) {
CompletionCallback* c = accept_callback_;
accept_callback_ = NULL;
accept_socket_ = NULL;
c->Run(result);
}
}
}
} // namespace net
|