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
222
223
224
225
226
227
228
|
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************
#include <Ice/UdpTransceiver.h>
#include <Ice/Instance.h>
#include <Ice/TraceLevels.h>
#include <Ice/LoggerUtil.h>
#include <Ice/Buffer.h>
#include <Ice/Network.h>
#include <Ice/Exception.h>
using namespace std;
using namespace Ice;
using namespace IceInternal;
SOCKET
IceInternal::UdpTransceiver::fd()
{
return _fd;
}
void
IceInternal::UdpTransceiver::close()
{
if (_traceLevels->network >= 1)
{
Trace out(_logger, _traceLevels->networkCat);
out << "closing " << _protocolName << " connection\n" << toString();
}
SOCKET fd = _fd;
_fd = INVALID_SOCKET;
closeSocket(fd);
}
void
IceInternal::UdpTransceiver::shutdown()
{
}
void
IceInternal::UdpTransceiver::write(Buffer& buf, int)
{
assert(buf.i == buf.b.begin());
#ifndef NDEBUG
const int packetSize = 64 * 1024; // TODO: configurable
assert(packetSize >= static_cast<int>(buf.b.size())); // TODO: exception
#endif
repeat:
int ret = ::send(_fd, buf.b.begin(), buf.b.size(), 0);
if (ret == SOCKET_ERROR)
{
if (interrupted())
{
goto repeat;
}
SocketException ex(__FILE__, __LINE__);
ex.error = getSocketErrno();
throw ex;
}
if (_traceLevels->network >= 3)
{
Trace out(_logger, _traceLevels->networkCat);
out << "sent " << ret << " bytes via " << _protocolName << "\n" << toString();
}
assert(ret == static_cast<int>(buf.b.size()));
buf.i = buf.b.end();
}
void
IceInternal::UdpTransceiver::read(Buffer& buf, int)
{
assert(buf.i == buf.b.begin());
const int packetSize = 64 * 1024; // TODO: configurable
assert(packetSize >= static_cast<int>(buf.b.size())); // TODO: exception
buf.b.resize(packetSize);
buf.i = buf.b.begin();
repeat:
int ret;
if (_connect)
{
//
// If we must connect, then we connect to the first peer that
// sends us a packet.
//
struct sockaddr_in peerAddr;
memset(&peerAddr, 0, sizeof(struct sockaddr_in));
socklen_t len = sizeof(peerAddr);
ret = recvfrom(_fd, buf.b.begin(), packetSize, 0, reinterpret_cast<struct sockaddr*>(&peerAddr), &len);
if (ret != SOCKET_ERROR)
{
doConnect(_fd, peerAddr, -1);
_connect = false; // We're connected now
if (_traceLevels->network >= 1)
{
Trace out(_logger, _traceLevels->networkCat);
out << "connected " << _protocolName << " socket\n" << toString();
}
}
}
else
{
ret = ::recv(_fd, buf.b.begin(), packetSize, 0);
}
if (ret == SOCKET_ERROR)
{
if (interrupted())
{
goto repeat;
}
SocketException ex(__FILE__, __LINE__);
ex.error = getSocketErrno();
throw ex;
}
if (_traceLevels->network >= 3)
{
Trace out(_logger, _traceLevels->networkCat);
out << "received " << ret << " bytes via " << _protocolName << "\n" << toString();
}
buf.b.resize(ret);
buf.i = buf.b.end();
}
string
IceInternal::UdpTransceiver::toString() const
{
return fdToString(_fd);
}
bool
IceInternal::UdpTransceiver::equivalent(const string& host, int port) const
{
struct sockaddr_in addr;
getAddress(host, port, addr);
return memcmp(&addr, &_addr, sizeof(struct sockaddr_in)) == 0;
}
int
IceInternal::UdpTransceiver::effectivePort()
{
return ntohs(_addr.sin_port);
}
void
IceInternal::UdpTransceiver::setProtocolName(const string& protocolName)
{
_protocolName = protocolName;
}
IceInternal::UdpTransceiver::UdpTransceiver(const InstancePtr& instance, const string& host, int port,
const string& protocolName) :
_instance(instance),
_traceLevels(instance->traceLevels()),
_logger(instance->logger()),
_incoming(false),
_connect(true),
_protocolName(protocolName)
{
try
{
_fd = createSocket(true);
getAddress(host, port, _addr);
doConnect(_fd, _addr, -1);
_connect = false; // We're connected now
if (_traceLevels->network >= 1)
{
Trace out(_logger, _traceLevels->networkCat);
out << "starting to send " << _protocolName << " packets\n" << toString();
}
}
catch(...)
{
_fd = INVALID_SOCKET;
throw;
}
}
IceInternal::UdpTransceiver::UdpTransceiver(const InstancePtr& instance, const string& host, int port,
bool connect, const string& protocolName) :
_instance(instance),
_traceLevels(instance->traceLevels()),
_logger(instance->logger()),
_incoming(true),
_connect(connect),
_protocolName(protocolName)
{
try
{
_fd = createSocket(true);
getAddress(host, port, _addr);
doBind(_fd, _addr);
if (_traceLevels->network >= 1)
{
Trace out(_logger, _traceLevels->networkCat);
out << "starting to receive " << _protocolName << " packets\n" << toString();
}
}
catch(...)
{
_fd = INVALID_SOCKET;
throw;
}
}
IceInternal::UdpTransceiver::~UdpTransceiver()
{
assert(_fd == INVALID_SOCKET);
}
|