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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <Ice/uwp/TcpAcceptor.h>
#include <Ice/uwp/TcpTransceiver.h>
#include <Ice/uwp/TcpEndpointI.h>
#include <Ice/ProtocolInstance.h>
#include <Ice/LocalException.h>
#include <Ice/LoggerUtil.h>
#include <Ice/Exception.h>
#include <Ice/Properties.h>
#include <IceUtil/StringUtil.h>
using namespace std;
using namespace Ice;
using namespace IceInternal;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Storage::Streams;
using namespace Windows::Networking;
using namespace Windows::Networking::Sockets;
IceUtil::Shared* IceInternal::upCast(TcpAcceptor* p) { return p; }
NativeInfoPtr
IceInternal::TcpAcceptor::getNativeInfo()
{
return this;
}
void
IceInternal::TcpAcceptor::setCompletedHandler(SocketOperationCompletedHandler^ handler)
{
_completedHandler = handler;
}
void
IceInternal::TcpAcceptor::close()
{
IceUtil::Mutex::Lock lock(_mutex);
if(_acceptPending)
{
assert(_accepted.empty());
_completedHandler(SocketOperationRead);
_acceptPending = false;
}
else if(!_accepted.empty())
{
for(deque<StreamSocket^>::const_iterator p = _accepted.begin(); p != _accepted.end(); ++p)
{
closeSocket(*p);
}
_accepted.clear();
}
if(_fd != INVALID_SOCKET)
{
closeSocketNoThrow(_fd);
_fd = INVALID_SOCKET;
}
}
EndpointIPtr
IceInternal::TcpAcceptor::listen()
{
try
{
const_cast<Address&>(_addr) = doBind(_fd, _addr);
}
catch(...)
{
_fd = INVALID_SOCKET;
throw;
}
_endpoint = _endpoint->endpoint(this);
return _endpoint;
}
void
IceInternal::TcpAcceptor::startAccept()
{
assert(_fd != INVALID_SOCKET);
//
// If there are already sockets waiting to be accepted, we just
// notify the selector that the acceptor is ready for acceting the
// new socket. Otherwise, we set the _acceptPending flag, when a
// new socket connection event is received, the message handler
// will notify the selector.
//
IceUtil::Mutex::Lock lock(_mutex);
assert(!_acceptPending);
if(!_accepted.empty())
{
_completedHandler(SocketOperationRead);
}
else
{
_acceptPending = true;
}
}
void
IceInternal::TcpAcceptor::finishAccept()
{
//
// Nothing to do, we just check there's at least one accepted
// socket or the acceptor was closed.
//
IceUtil::Mutex::Lock lock(_mutex);
assert(!_acceptPending && (!_accepted.empty() || _fd == INVALID_SOCKET));
}
TransceiverPtr
IceInternal::TcpAcceptor::accept()
{
if(_fd == INVALID_SOCKET) // Acceptor closed.
{
assert(_accepted.empty());
throw SocketException(__FILE__, __LINE__);
}
StreamSocket^ fd;
{
IceUtil::Mutex::Lock lock(_mutex);
assert(!_accepted.empty());
fd = _accepted.front();
_accepted.pop_front();
}
return new TcpTransceiver(_instance, fd, true);
}
string
IceInternal::TcpAcceptor::protocol() const
{
return _instance->protocol();
}
string
IceInternal::TcpAcceptor::toString() const
{
return addrToString(_addr);
}
string
IceInternal::TcpAcceptor::toDetailedString() const
{
ostringstream os;
os << "local address = " << toString();
vector<string> intfs = getHostsForEndpointExpand(inetAddrToString(_addr), _instance->protocolSupport(), true);
if(!intfs.empty())
{
os << "\nlocal interfaces = ";
os << IceUtilInternal::joinString(intfs, ", ");
}
return os.str();
}
int
IceInternal::TcpAcceptor::effectivePort() const
{
return getPort(_addr);
}
IceInternal::TcpAcceptor::TcpAcceptor(const TcpEndpointIPtr& endpoint,
const ProtocolInstancePtr& instance,
const string& host,
int port) :
_endpoint(endpoint),
_instance(instance),
_addr(getAddressForServer(host, port, _instance->protocolSupport(), instance->preferIPv6())),
_acceptPending(false)
{
_fd = ref new StreamSocketListener();
safe_cast<StreamSocketListener^>(_fd)->ConnectionReceived +=
ref new TypedEventHandler<StreamSocketListener^, StreamSocketListenerConnectionReceivedEventArgs^>(
[=](StreamSocketListener^, StreamSocketListenerConnectionReceivedEventArgs^ args)
{
queueAcceptedSocket(args->Socket);
});
}
IceInternal::TcpAcceptor::~TcpAcceptor()
{
assert(_fd == INVALID_SOCKET);
}
void
IceInternal::TcpAcceptor::queueAcceptedSocket(StreamSocket^ socket)
{
IceUtil::Mutex::Lock lock(_mutex);
if(_fd == INVALID_SOCKET) // Acceptor was closed.
{
closeSocket(socket);
return;
}
_accepted.push_back(socket);
//
// If the acceptor is waiting for a socket to be accepted, notify
// the selector that the acceptor is ready for "read". This will
// in turn caused finishAccept() and accept() to be called by the
// thread pool. If the acceptor isn't ready to accept the socket,
// it is just queued, when startAccept is called it will be dequed.
//
if(_acceptPending)
{
_completedHandler(SocketOperationRead);
_acceptPending = false;
}
}
|