| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Diagnostics; |
| | 4 | | using System.Net; |
| | 5 | | using System.Net.Sockets; |
| | 6 | | using System.Text; |
| | 7 | |
|
| | 8 | | namespace Ice.Internal; |
| | 9 | |
|
| | 10 | | #pragma warning disable CA1001 // _readEventArgs and _writeEventArgs are disposed by destroy. |
| | 11 | | internal sealed class UdpTransceiver : Transceiver |
| | 12 | | #pragma warning restore CA1001 |
| | 13 | | { |
| 0 | 14 | | public Socket fd() => _fd; |
| | 15 | |
|
| | 16 | | public int initialize(Buffer readBuffer, Buffer writeBuffer, ref bool hasMoreData) |
| | 17 | | { |
| 1 | 18 | | if (_state == StateNeedConnect) |
| | 19 | | { |
| 1 | 20 | | _state = StateConnectPending; |
| | 21 | | try |
| | 22 | | { |
| 1 | 23 | | if (_sourceAddr != null) |
| | 24 | | { |
| 0 | 25 | | _fd.Bind(_sourceAddr); |
| | 26 | | } |
| 1 | 27 | | _fd.Connect(_addr); |
| 1 | 28 | | } |
| 0 | 29 | | catch (System.Net.Sockets.SocketException ex) |
| | 30 | | { |
| 0 | 31 | | if (Network.wouldBlock(ex)) |
| | 32 | | { |
| 0 | 33 | | return SocketOperation.Connect; |
| | 34 | | } |
| 0 | 35 | | throw new Ice.ConnectFailedException(ex); |
| | 36 | | } |
| 0 | 37 | | catch (System.Exception ex) |
| | 38 | | { |
| 0 | 39 | | throw new Ice.ConnectFailedException(ex); |
| | 40 | | } |
| 1 | 41 | | _state = StateConnected; |
| | 42 | | } |
| | 43 | |
|
| | 44 | | Debug.Assert(_state >= StateConnected); |
| 1 | 45 | | return SocketOperation.None; |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | public int closing(bool initiator, Ice.LocalException ex) => |
| | 49 | | // |
| | 50 | | // Nothing to do. |
| | 51 | | // |
| 0 | 52 | | SocketOperation.None; |
| | 53 | |
|
| | 54 | | public void close() |
| | 55 | | { |
| 1 | 56 | | if (_fd != null) |
| | 57 | | { |
| | 58 | | try |
| | 59 | | { |
| 1 | 60 | | _fd.Close(); |
| 1 | 61 | | } |
| 0 | 62 | | catch (System.IO.IOException) |
| | 63 | | { |
| 0 | 64 | | } |
| 1 | 65 | | _fd = null; |
| | 66 | | } |
| 1 | 67 | | } |
| | 68 | |
|
| | 69 | | public EndpointI bind() |
| | 70 | | { |
| 1 | 71 | | if (Network.isMulticast((IPEndPoint)_addr)) |
| | 72 | | { |
| 1 | 73 | | Network.setReuseAddress(_fd, true); |
| 1 | 74 | | _mcastAddr = (IPEndPoint)_addr; |
| 1 | 75 | | if (AssemblyUtil.isWindows) |
| | 76 | | { |
| | 77 | | // Windows does not allow binding to the mcast address itself so we bind to INADDR_ANY instead. |
| 0 | 78 | | _addr = new IPEndPoint( |
| 0 | 79 | | _addr.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, |
| 0 | 80 | | _port); |
| | 81 | | } |
| | 82 | |
|
| 1 | 83 | | _addr = Network.doBind(_fd, _addr); |
| 1 | 84 | | if (_port == 0) |
| | 85 | | { |
| 0 | 86 | | _mcastAddr.Port = ((IPEndPoint)_addr).Port; |
| | 87 | | } |
| 1 | 88 | | Network.setMcastGroup(_fd, _mcastAddr.Address, _mcastInterface); |
| | 89 | | } |
| | 90 | | else |
| | 91 | | { |
| 1 | 92 | | _addr = Network.doBind(_fd, _addr); |
| | 93 | | } |
| 1 | 94 | | _bound = true; |
| 1 | 95 | | _endpoint = _endpoint.endpoint(this); |
| 1 | 96 | | return _endpoint; |
| | 97 | | } |
| | 98 | |
|
| | 99 | | public void destroy() |
| | 100 | | { |
| 1 | 101 | | _readEventArgs.Dispose(); |
| 1 | 102 | | _writeEventArgs.Dispose(); |
| 1 | 103 | | } |
| | 104 | |
|
| | 105 | | public int write(Buffer buf) |
| | 106 | | { |
| 1 | 107 | | if (!buf.b.hasRemaining()) |
| | 108 | | { |
| 0 | 109 | | return SocketOperation.None; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | Debug.Assert(buf.b.position() == 0); |
| | 113 | | Debug.Assert(_fd != null && _state >= StateConnected); |
| | 114 | |
|
| | 115 | | // The caller is supposed to check the send size before by calling checkSendSize |
| | 116 | | Debug.Assert(Math.Min(_maxPacketSize, _sndSize - _udpOverhead) >= buf.size()); |
| | 117 | |
|
| | 118 | | int ret; |
| | 119 | | while (true) |
| | 120 | | { |
| | 121 | | try |
| | 122 | | { |
| 1 | 123 | | if (_state == StateConnected) |
| | 124 | | { |
| 1 | 125 | | ret = _fd.Send(buf.b.rawBytes(), 0, buf.size(), SocketFlags.None); |
| | 126 | | } |
| | 127 | | else |
| | 128 | | { |
| 1 | 129 | | if (_peerAddr == null) |
| | 130 | | { |
| 0 | 131 | | throw new Ice.SocketException(); |
| | 132 | | } |
| 1 | 133 | | ret = _fd.SendTo(buf.b.rawBytes(), 0, buf.size(), SocketFlags.None, _peerAddr); |
| | 134 | | } |
| 1 | 135 | | break; |
| | 136 | | } |
| 0 | 137 | | catch (System.Net.Sockets.SocketException ex) |
| | 138 | | { |
| 0 | 139 | | if (Network.interrupted(ex)) |
| | 140 | | { |
| 0 | 141 | | continue; |
| | 142 | | } |
| | 143 | |
|
| 0 | 144 | | if (Network.wouldBlock(ex)) |
| | 145 | | { |
| 0 | 146 | | return SocketOperation.Write; |
| | 147 | | } |
| | 148 | |
|
| 0 | 149 | | if (Network.connectionLost(ex)) |
| | 150 | | { |
| 0 | 151 | | throw new Ice.ConnectionLostException(ex); |
| | 152 | | } |
| | 153 | | else |
| | 154 | | { |
| 0 | 155 | | throw new Ice.SocketException(ex); |
| | 156 | | } |
| | 157 | | } |
| 0 | 158 | | catch (System.Exception e) |
| | 159 | | { |
| 0 | 160 | | throw new Ice.SyscallException(e); |
| | 161 | | } |
| | 162 | | } |
| | 163 | |
|
| | 164 | | Debug.Assert(ret > 0); |
| | 165 | | Debug.Assert(ret == buf.b.limit()); |
| 1 | 166 | | buf.b.position(buf.b.limit()); |
| 1 | 167 | | return SocketOperation.None; |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | | public int read(Buffer buf, ref bool hasMoreData) |
| | 171 | | { |
| 1 | 172 | | if (!buf.b.hasRemaining()) |
| | 173 | | { |
| 1 | 174 | | return SocketOperation.None; |
| | 175 | | } |
| | 176 | |
|
| | 177 | | Debug.Assert(buf.b.position() == 0); |
| | 178 | | Debug.Assert(_fd != null); |
| | 179 | |
|
| 0 | 180 | | int packetSize = Math.Min(_maxPacketSize, _rcvSize - _udpOverhead); |
| 0 | 181 | | buf.resize(packetSize, true); |
| 0 | 182 | | buf.b.position(0); |
| | 183 | |
|
| | 184 | | int ret; |
| | 185 | | while (true) |
| | 186 | | { |
| | 187 | | try |
| | 188 | | { |
| 0 | 189 | | EndPoint peerAddr = _peerAddr; |
| 0 | 190 | | if (peerAddr == null) |
| | 191 | | { |
| 0 | 192 | | if (_addr.AddressFamily == AddressFamily.InterNetwork) |
| | 193 | | { |
| 0 | 194 | | peerAddr = new IPEndPoint(IPAddress.Any, 0); |
| | 195 | | } |
| | 196 | | else |
| | 197 | | { |
| | 198 | | Debug.Assert(_addr.AddressFamily == AddressFamily.InterNetworkV6); |
| 0 | 199 | | peerAddr = new IPEndPoint(IPAddress.IPv6Any, 0); |
| | 200 | | } |
| | 201 | | } |
| | 202 | |
|
| | 203 | | // TODO: Workaround for https://github.com/dotnet/corefx/issues/31182 |
| 0 | 204 | | if (_state == StateConnected || |
| 0 | 205 | | (AssemblyUtil.isMacOS && _fd.AddressFamily == AddressFamily.InterNetworkV6 && _fd.DualMode)) |
| | 206 | | { |
| 0 | 207 | | ret = _fd.Receive(buf.b.rawBytes(), 0, buf.b.limit(), SocketFlags.None); |
| | 208 | | } |
| | 209 | | else |
| | 210 | | { |
| 0 | 211 | | ret = _fd.ReceiveFrom(buf.b.rawBytes(), 0, buf.b.limit(), SocketFlags.None, ref peerAddr); |
| 0 | 212 | | _peerAddr = (IPEndPoint)peerAddr; |
| | 213 | | } |
| 0 | 214 | | break; |
| | 215 | | } |
| 0 | 216 | | catch (System.Net.Sockets.SocketException e) |
| | 217 | | { |
| 0 | 218 | | if (Network.recvTruncated(e)) |
| | 219 | | { |
| | 220 | | // The message was truncated and the whole buffer is filled. We ignore |
| | 221 | | // this error here, it will be detected at the connection level when |
| | 222 | | // the Ice message size is checked against the buffer size. |
| 0 | 223 | | ret = buf.size(); |
| 0 | 224 | | break; |
| | 225 | | } |
| | 226 | |
|
| 0 | 227 | | if (Network.interrupted(e)) |
| | 228 | | { |
| 0 | 229 | | continue; |
| | 230 | | } |
| | 231 | |
|
| 0 | 232 | | if (Network.wouldBlock(e)) |
| | 233 | | { |
| 0 | 234 | | return SocketOperation.Read; |
| | 235 | | } |
| | 236 | |
|
| 0 | 237 | | if (Network.connectionLost(e)) |
| | 238 | | { |
| 0 | 239 | | throw new ConnectionLostException(e); |
| | 240 | | } |
| | 241 | | else |
| | 242 | | { |
| 0 | 243 | | throw new Ice.SocketException(e); |
| | 244 | | } |
| | 245 | | } |
| 0 | 246 | | catch (System.Exception e) |
| | 247 | | { |
| 0 | 248 | | throw new SyscallException(e); |
| | 249 | | } |
| | 250 | | } |
| | 251 | |
|
| 0 | 252 | | if (ret == 0) |
| | 253 | | { |
| 0 | 254 | | throw new Ice.ConnectionLostException(); |
| | 255 | | } |
| | 256 | |
|
| | 257 | | Debug.Assert(_state != StateNeedConnect); |
| | 258 | |
|
| 0 | 259 | | buf.resize(ret, true); |
| 0 | 260 | | buf.b.position(ret); |
| | 261 | |
|
| 0 | 262 | | return SocketOperation.None; |
| 0 | 263 | | } |
| | 264 | |
|
| | 265 | | public bool startRead(Buffer buf, AsyncCallback callback, object state) |
| | 266 | | { |
| | 267 | | Debug.Assert(buf.b.position() == 0); |
| | 268 | |
|
| 1 | 269 | | int packetSize = Math.Min(_maxPacketSize, _rcvSize - _udpOverhead); |
| 1 | 270 | | buf.resize(packetSize, true); |
| 1 | 271 | | buf.b.position(0); |
| | 272 | |
|
| | 273 | | try |
| | 274 | | { |
| | 275 | | // TODO: Workaround for https://github.com/dotnet/corefx/issues/31182 |
| 1 | 276 | | if (_state == StateConnected || |
| 1 | 277 | | (AssemblyUtil.isMacOS && _fd.AddressFamily == AddressFamily.InterNetworkV6 && _fd.DualMode)) |
| | 278 | | { |
| 1 | 279 | | _readCallback = callback; |
| 1 | 280 | | _readEventArgs.UserToken = state; |
| 1 | 281 | | _readEventArgs.SetBuffer(buf.b.rawBytes(), buf.b.position(), packetSize); |
| 1 | 282 | | return !_fd.ReceiveAsync(_readEventArgs); |
| | 283 | | } |
| | 284 | | else |
| | 285 | | { |
| | 286 | | Debug.Assert(_incoming); |
| 1 | 287 | | _readCallback = callback; |
| 1 | 288 | | _readEventArgs.UserToken = state; |
| 1 | 289 | | _readEventArgs.SetBuffer(buf.b.rawBytes(), 0, buf.b.limit()); |
| 1 | 290 | | return !_fd.ReceiveFromAsync(_readEventArgs); |
| | 291 | | } |
| | 292 | | } |
| 0 | 293 | | catch (System.Net.Sockets.SocketException ex) |
| | 294 | | { |
| 0 | 295 | | if (Network.recvTruncated(ex)) |
| | 296 | | { |
| | 297 | | // Nothing todo |
| 0 | 298 | | return true; |
| | 299 | | } |
| | 300 | | else |
| | 301 | | { |
| 0 | 302 | | if (Network.connectionLost(ex)) |
| | 303 | | { |
| 0 | 304 | | throw new Ice.ConnectionLostException(ex); |
| | 305 | | } |
| | 306 | | else |
| | 307 | | { |
| 0 | 308 | | throw new Ice.SocketException(ex); |
| | 309 | | } |
| | 310 | | } |
| | 311 | | } |
| 1 | 312 | | } |
| | 313 | |
|
| | 314 | | public void finishRead(Buffer buf) |
| | 315 | | { |
| 1 | 316 | | if (_fd == null) |
| | 317 | | { |
| 0 | 318 | | return; |
| | 319 | | } |
| | 320 | |
|
| | 321 | | int ret; |
| | 322 | | try |
| | 323 | | { |
| 1 | 324 | | if (_readEventArgs.SocketError != SocketError.Success) |
| | 325 | | { |
| 0 | 326 | | throw new System.Net.Sockets.SocketException((int)_readEventArgs.SocketError); |
| | 327 | | } |
| 1 | 328 | | ret = _readEventArgs.BytesTransferred; |
| | 329 | | // TODO: Workaround for https://github.com/dotnet/corefx/issues/31182 |
| 1 | 330 | | if (_state != StateConnected && |
| 1 | 331 | | !(AssemblyUtil.isMacOS && _fd.AddressFamily == AddressFamily.InterNetworkV6 && _fd.DualMode)) |
| | 332 | | { |
| 1 | 333 | | _peerAddr = _readEventArgs.RemoteEndPoint; |
| | 334 | | } |
| 1 | 335 | | } |
| 0 | 336 | | catch (System.Net.Sockets.SocketException ex) |
| | 337 | | { |
| 0 | 338 | | if (Network.recvTruncated(ex)) |
| | 339 | | { |
| | 340 | | // The message was truncated and the whole buffer is filled. We ignore |
| | 341 | | // this error here, it will be detected at the connection level when |
| | 342 | | // the Ice message size is checked against the buffer size. |
| 0 | 343 | | ret = buf.size(); |
| | 344 | | } |
| | 345 | | else |
| | 346 | | { |
| 0 | 347 | | if (Network.connectionLost(ex)) |
| | 348 | | { |
| 0 | 349 | | throw new Ice.ConnectionLostException(ex); |
| | 350 | | } |
| | 351 | |
|
| 0 | 352 | | if (Network.connectionRefused(ex)) |
| | 353 | | { |
| 0 | 354 | | throw new Ice.ConnectionRefusedException(ex); |
| | 355 | | } |
| | 356 | | else |
| | 357 | | { |
| 0 | 358 | | throw new Ice.SocketException(ex); |
| | 359 | | } |
| | 360 | | } |
| 0 | 361 | | } |
| | 362 | |
|
| 1 | 363 | | if (ret == 0) |
| | 364 | | { |
| 0 | 365 | | throw new Ice.ConnectionLostException(); |
| | 366 | | } |
| | 367 | |
|
| | 368 | | Debug.Assert(_state != StateNeedConnect); |
| 1 | 369 | | buf.resize(ret, true); |
| 1 | 370 | | buf.b.position(ret); |
| 1 | 371 | | } |
| | 372 | |
|
| | 373 | | public bool startWrite(Buffer buf, AsyncCallback callback, object state, out bool messageWritten) |
| | 374 | | { |
| 0 | 375 | | if (!_incoming && _state < StateConnected) |
| | 376 | | { |
| | 377 | | Debug.Assert(_addr != null); |
| 0 | 378 | | messageWritten = false; |
| 0 | 379 | | if (_sourceAddr != null) |
| | 380 | | { |
| 0 | 381 | | _fd.Bind(_sourceAddr); |
| | 382 | | } |
| 0 | 383 | | _writeEventArgs.UserToken = state; |
| 0 | 384 | | return !_fd.ConnectAsync(_writeEventArgs); |
| | 385 | | } |
| | 386 | |
|
| | 387 | | Debug.Assert(_fd != null); |
| | 388 | |
|
| | 389 | | // The caller is supposed to check the send size before by calling checkSendSize |
| | 390 | | Debug.Assert(Math.Min(_maxPacketSize, _sndSize - _udpOverhead) >= buf.size()); |
| | 391 | |
|
| | 392 | | Debug.Assert(buf.b.position() == 0); |
| | 393 | |
|
| | 394 | | bool completedSynchronously; |
| | 395 | | try |
| | 396 | | { |
| 0 | 397 | | _writeCallback = callback; |
| | 398 | |
|
| 0 | 399 | | if (_state == StateConnected) |
| | 400 | | { |
| 0 | 401 | | _writeEventArgs.UserToken = state; |
| 0 | 402 | | _writeEventArgs.SetBuffer(buf.b.rawBytes(), 0, buf.b.limit()); |
| 0 | 403 | | completedSynchronously = !_fd.SendAsync(_writeEventArgs); |
| | 404 | | } |
| | 405 | | else |
| | 406 | | { |
| 0 | 407 | | if (_peerAddr == null) |
| | 408 | | { |
| 0 | 409 | | throw new Ice.SocketException(); |
| | 410 | | } |
| 0 | 411 | | _writeEventArgs.RemoteEndPoint = _peerAddr; |
| 0 | 412 | | _writeEventArgs.UserToken = state; |
| 0 | 413 | | _writeEventArgs.SetBuffer(buf.b.rawBytes(), 0, buf.b.limit()); |
| 0 | 414 | | completedSynchronously = !_fd.SendToAsync(_writeEventArgs); |
| | 415 | | } |
| 0 | 416 | | } |
| 0 | 417 | | catch (System.Net.Sockets.SocketException ex) |
| | 418 | | { |
| 0 | 419 | | if (Network.connectionLost(ex)) |
| | 420 | | { |
| 0 | 421 | | throw new Ice.ConnectionLostException(ex); |
| | 422 | | } |
| | 423 | | else |
| | 424 | | { |
| 0 | 425 | | throw new Ice.SocketException(ex); |
| | 426 | | } |
| | 427 | | } |
| | 428 | |
|
| 0 | 429 | | messageWritten = true; |
| 0 | 430 | | return completedSynchronously; |
| | 431 | | } |
| | 432 | |
|
| | 433 | | public void finishWrite(Buffer buf) |
| | 434 | | { |
| 0 | 435 | | if (_fd == null) |
| | 436 | | { |
| 0 | 437 | | buf.b.position(buf.size()); // Assume all the data was sent for at-most-once semantics. |
| 0 | 438 | | _writeEventArgs = null; |
| 0 | 439 | | return; |
| | 440 | | } |
| | 441 | |
|
| 0 | 442 | | if (!_incoming && _state < StateConnected) |
| | 443 | | { |
| 0 | 444 | | if (_writeEventArgs.SocketError != SocketError.Success) |
| | 445 | | { |
| 0 | 446 | | var ex = |
| 0 | 447 | | new System.Net.Sockets.SocketException((int)_writeEventArgs.SocketError); |
| 0 | 448 | | if (Network.connectionRefused(ex)) |
| | 449 | | { |
| 0 | 450 | | throw new Ice.ConnectionRefusedException(ex); |
| | 451 | | } |
| | 452 | | else |
| | 453 | | { |
| 0 | 454 | | throw new Ice.ConnectFailedException(ex); |
| | 455 | | } |
| | 456 | | } |
| 0 | 457 | | return; |
| | 458 | | } |
| | 459 | |
|
| | 460 | | int ret; |
| | 461 | | try |
| | 462 | | { |
| 0 | 463 | | if (_writeEventArgs.SocketError != SocketError.Success) |
| | 464 | | { |
| 0 | 465 | | throw new System.Net.Sockets.SocketException((int)_writeEventArgs.SocketError); |
| | 466 | | } |
| 0 | 467 | | ret = _writeEventArgs.BytesTransferred; |
| 0 | 468 | | } |
| 0 | 469 | | catch (System.Net.Sockets.SocketException ex) |
| | 470 | | { |
| 0 | 471 | | if (Network.connectionLost(ex)) |
| | 472 | | { |
| 0 | 473 | | throw new Ice.ConnectionLostException(ex); |
| | 474 | | } |
| | 475 | | else |
| | 476 | | { |
| 0 | 477 | | throw new Ice.SocketException(ex); |
| | 478 | | } |
| | 479 | | } |
| | 480 | |
|
| 0 | 481 | | if (ret == 0) |
| | 482 | | { |
| 0 | 483 | | throw new Ice.ConnectionLostException(); |
| | 484 | | } |
| | 485 | |
|
| | 486 | | Debug.Assert(ret > 0); |
| | 487 | | Debug.Assert(ret == buf.b.limit()); |
| 0 | 488 | | buf.b.position(buf.b.position() + ret); |
| 0 | 489 | | } |
| | 490 | |
|
| 1 | 491 | | public string protocol() => _instance.protocol(); |
| | 492 | |
|
| | 493 | | public ConnectionInfo getInfo(bool incoming, string adapterName, string connectionId) |
| | 494 | | { |
| 1 | 495 | | if (_fd is null) |
| | 496 | | { |
| 0 | 497 | | return new UDPConnectionInfo(incoming, adapterName, connectionId); |
| | 498 | | } |
| | 499 | | else |
| | 500 | | { |
| 1 | 501 | | EndPoint localEndpoint = Network.getLocalAddress(_fd); |
| | 502 | |
|
| 1 | 503 | | if (_state == StateNotConnected) // a server connection |
| | 504 | | { |
| | 505 | | Debug.Assert(incoming); |
| | 506 | |
|
| | 507 | | // Since this info is cached in the Connection object shared by all the clients, we don't store the |
| | 508 | | // remote address/port of the latest client in this info. |
| 1 | 509 | | return new UDPConnectionInfo( |
| 1 | 510 | | incoming, |
| 1 | 511 | | adapterName, |
| 1 | 512 | | connectionId, |
| 1 | 513 | | Network.endpointAddressToString(localEndpoint), |
| 1 | 514 | | Network.endpointPort(localEndpoint), |
| 1 | 515 | | remoteAddress: "", |
| 1 | 516 | | remotePort: -1, |
| 1 | 517 | | _mcastAddr is not null ? Network.endpointAddressToString(_mcastAddr) : "", |
| 1 | 518 | | _mcastAddr is not null ? Network.endpointPort(_mcastAddr) : -1, |
| 1 | 519 | | _rcvSize, |
| 1 | 520 | | _sndSize); |
| | 521 | | } |
| | 522 | | else // client connection |
| | 523 | | { |
| | 524 | | Debug.Assert(!incoming); |
| 1 | 525 | | EndPoint remoteEndpoint = Network.getRemoteAddress(_fd); |
| | 526 | |
|
| 1 | 527 | | return new UDPConnectionInfo( |
| 1 | 528 | | incoming, |
| 1 | 529 | | adapterName, |
| 1 | 530 | | connectionId, |
| 1 | 531 | | Network.endpointAddressToString(localEndpoint), |
| 1 | 532 | | Network.endpointPort(localEndpoint), |
| 1 | 533 | | remoteEndpoint is not null ? Network.endpointAddressToString(remoteEndpoint) : "", |
| 1 | 534 | | remoteEndpoint is not null ? Network.endpointPort(remoteEndpoint) : -1, |
| 1 | 535 | | _mcastAddr is not null ? Network.endpointAddressToString(_mcastAddr) : "", |
| 1 | 536 | | _mcastAddr is not null ? Network.endpointPort(_mcastAddr) : -1, |
| 1 | 537 | | _rcvSize, |
| 1 | 538 | | _sndSize); |
| | 539 | | } |
| | 540 | | } |
| | 541 | | } |
| | 542 | |
|
| | 543 | | public void checkSendSize(Buffer buf) |
| | 544 | | { |
| | 545 | | // |
| | 546 | | // The maximum packetSize is either the maximum allowable UDP packet size, or |
| | 547 | | // the UDP send buffer size (which ever is smaller). |
| | 548 | | // |
| 1 | 549 | | int packetSize = Math.Min(_maxPacketSize, _sndSize - _udpOverhead); |
| 1 | 550 | | if (packetSize < buf.size()) |
| | 551 | | { |
| 1 | 552 | | throw new Ice.DatagramLimitException(); |
| | 553 | | } |
| 1 | 554 | | } |
| | 555 | |
|
| 1 | 556 | | public void setBufferSize(int rcvSize, int sndSize) => setBufSize(rcvSize, sndSize); |
| | 557 | |
|
| | 558 | | public override string ToString() |
| | 559 | | { |
| 1 | 560 | | if (_fd == null) |
| | 561 | | { |
| 0 | 562 | | return "<closed>"; |
| | 563 | | } |
| | 564 | |
|
| | 565 | | string s; |
| 1 | 566 | | if (_incoming && !_bound) |
| | 567 | | { |
| 1 | 568 | | s = "local address = " + Network.addrToString(_addr); |
| | 569 | | } |
| 1 | 570 | | else if (_state == StateNotConnected) |
| | 571 | | { |
| 1 | 572 | | s = "local address = " + Network.localAddrToString(Network.getLocalAddress(_fd)); |
| 1 | 573 | | if (_peerAddr != null) |
| | 574 | | { |
| 0 | 575 | | s += "\nremote address = " + Network.addrToString(_peerAddr); |
| | 576 | | } |
| | 577 | | } |
| | 578 | | else |
| | 579 | | { |
| 1 | 580 | | s = Network.fdToString(_fd); |
| | 581 | | } |
| | 582 | |
|
| 1 | 583 | | if (_mcastAddr != null) |
| | 584 | | { |
| 1 | 585 | | s += "\nmulticast address = " + Network.addrToString(_mcastAddr); |
| | 586 | | } |
| 1 | 587 | | return s; |
| | 588 | | } |
| | 589 | |
|
| | 590 | | public string toDetailedString() |
| | 591 | | { |
| 1 | 592 | | var s = new StringBuilder(ToString()); |
| 1 | 593 | | if (_mcastAddr is not null) |
| | 594 | | { |
| 1 | 595 | | List<string> intfs = Network.getInterfacesForMulticast( |
| 1 | 596 | | _mcastInterface, |
| 1 | 597 | | Network.getProtocolSupport(_mcastAddr.Address)); |
| 1 | 598 | | if (intfs.Count != 0) |
| | 599 | | { |
| 1 | 600 | | s.Append("\nlocal interfaces = "); |
| 1 | 601 | | s.Append(string.Join(", ", intfs.ToArray())); |
| | 602 | | } |
| | 603 | | } |
| 1 | 604 | | return s.ToString(); |
| | 605 | | } |
| | 606 | |
|
| 1 | 607 | | public int effectivePort() => Network.endpointPort(_addr); |
| | 608 | |
|
| | 609 | | // |
| | 610 | | // Only for use by UdpConnector. |
| | 611 | | // |
| 1 | 612 | | internal UdpTransceiver( |
| 1 | 613 | | ProtocolInstance instance, |
| 1 | 614 | | EndPoint addr, |
| 1 | 615 | | EndPoint sourceAddr, |
| 1 | 616 | | string mcastInterface, |
| 1 | 617 | | int mcastTtl) |
| | 618 | | { |
| 1 | 619 | | _instance = instance; |
| 1 | 620 | | _addr = addr; |
| 1 | 621 | | _sourceAddr = sourceAddr; |
| | 622 | |
|
| 1 | 623 | | _readEventArgs = new SocketAsyncEventArgs(); |
| 1 | 624 | | _readEventArgs.RemoteEndPoint = _addr; |
| 1 | 625 | | _readEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(ioCompleted); |
| | 626 | |
|
| 1 | 627 | | _writeEventArgs = new SocketAsyncEventArgs(); |
| 1 | 628 | | _writeEventArgs.RemoteEndPoint = _addr; |
| 1 | 629 | | _writeEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(ioCompleted); |
| | 630 | |
|
| 1 | 631 | | _mcastInterface = mcastInterface; |
| 1 | 632 | | _state = StateNeedConnect; |
| 1 | 633 | | _incoming = false; |
| | 634 | |
|
| | 635 | | try |
| | 636 | | { |
| 1 | 637 | | _fd = Network.createSocket(true, _addr.AddressFamily); |
| 1 | 638 | | setBufSize(-1, -1); |
| 1 | 639 | | Network.setBlock(_fd, false); |
| 1 | 640 | | if (Network.isMulticast((IPEndPoint)_addr)) |
| | 641 | | { |
| 1 | 642 | | if (_mcastInterface.Length > 0) |
| | 643 | | { |
| 1 | 644 | | Network.setMcastInterface(_fd, _mcastInterface, _addr.AddressFamily); |
| | 645 | | } |
| 1 | 646 | | if (mcastTtl != -1) |
| | 647 | | { |
| 0 | 648 | | Network.setMcastTtl(_fd, mcastTtl, _addr.AddressFamily); |
| | 649 | | } |
| | 650 | | } |
| 1 | 651 | | } |
| 1 | 652 | | catch (Ice.LocalException) |
| | 653 | | { |
| 1 | 654 | | _fd = null; |
| 1 | 655 | | throw; |
| | 656 | | } |
| 1 | 657 | | } |
| | 658 | |
|
| | 659 | | // |
| | 660 | | // Only for use by UdpEndpoint. |
| | 661 | | // |
| 1 | 662 | | internal UdpTransceiver( |
| 1 | 663 | | UdpEndpointI endpoint, |
| 1 | 664 | | ProtocolInstance instance, |
| 1 | 665 | | string host, |
| 1 | 666 | | int port, |
| 1 | 667 | | string mcastInterface) |
| | 668 | | { |
| 1 | 669 | | _endpoint = endpoint; |
| 1 | 670 | | _instance = instance; |
| 1 | 671 | | _state = StateNotConnected; |
| 1 | 672 | | _mcastInterface = mcastInterface; |
| 1 | 673 | | _incoming = true; |
| 1 | 674 | | _port = port; |
| | 675 | |
|
| | 676 | | try |
| | 677 | | { |
| 1 | 678 | | _addr = Network.getAddressForServer(host, port, instance.protocolSupport(), instance.preferIPv6()); |
| | 679 | |
|
| 1 | 680 | | _readEventArgs = new SocketAsyncEventArgs(); |
| 1 | 681 | | _readEventArgs.RemoteEndPoint = _addr; |
| 1 | 682 | | _readEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(ioCompleted); |
| | 683 | |
|
| 1 | 684 | | _writeEventArgs = new SocketAsyncEventArgs(); |
| 1 | 685 | | _writeEventArgs.RemoteEndPoint = _addr; |
| 1 | 686 | | _writeEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(ioCompleted); |
| | 687 | |
|
| 1 | 688 | | _fd = Network.createServerSocket(true, _addr.AddressFamily, instance.protocolSupport()); |
| 1 | 689 | | setBufSize(-1, -1); |
| 1 | 690 | | Network.setBlock(_fd, false); |
| 1 | 691 | | } |
| 0 | 692 | | catch (Ice.LocalException) |
| | 693 | | { |
| 0 | 694 | | _readEventArgs?.Dispose(); |
| 0 | 695 | | _writeEventArgs?.Dispose(); |
| 0 | 696 | | _fd = null; |
| 0 | 697 | | throw; |
| | 698 | | } |
| 1 | 699 | | } |
| | 700 | |
|
| | 701 | | private void setBufSize(int rcvSize, int sndSize) |
| | 702 | | { |
| | 703 | | Debug.Assert(_fd != null); |
| | 704 | |
|
| 1 | 705 | | for (int i = 0; i < 2; ++i) |
| | 706 | | { |
| | 707 | | bool isSnd; |
| | 708 | | string direction; |
| | 709 | | string prop; |
| | 710 | | int dfltSize; |
| | 711 | | int sizeRequested; |
| 1 | 712 | | if (i == 0) |
| | 713 | | { |
| 1 | 714 | | isSnd = false; |
| 1 | 715 | | direction = "receive"; |
| 1 | 716 | | prop = "Ice.UDP.RcvSize"; |
| 1 | 717 | | dfltSize = Network.getRecvBufferSize(_fd); |
| 1 | 718 | | sizeRequested = rcvSize; |
| 1 | 719 | | _rcvSize = dfltSize; |
| | 720 | | } |
| | 721 | | else |
| | 722 | | { |
| 1 | 723 | | isSnd = true; |
| 1 | 724 | | direction = "send"; |
| 1 | 725 | | prop = "Ice.UDP.SndSize"; |
| 1 | 726 | | dfltSize = Network.getSendBufferSize(_fd); |
| 1 | 727 | | sizeRequested = sndSize; |
| 1 | 728 | | _sndSize = dfltSize; |
| | 729 | | } |
| | 730 | |
|
| | 731 | | // |
| | 732 | | // Get property for buffer size if size not passed in. |
| | 733 | | // |
| 1 | 734 | | if (sizeRequested == -1) |
| | 735 | | { |
| 1 | 736 | | sizeRequested = _instance.properties().getPropertyAsIntWithDefault(prop, dfltSize); |
| | 737 | | } |
| | 738 | | // |
| | 739 | | // Check for sanity. |
| | 740 | | // |
| 1 | 741 | | if (sizeRequested < (_udpOverhead + Protocol.headerSize)) |
| | 742 | | { |
| 0 | 743 | | _instance.logger().warning("Invalid " + prop + " value of " + sizeRequested + " adjusted to " + |
| 0 | 744 | | dfltSize); |
| 0 | 745 | | sizeRequested = dfltSize; |
| | 746 | | } |
| | 747 | |
|
| 1 | 748 | | if (sizeRequested != dfltSize) |
| | 749 | | { |
| | 750 | | // |
| | 751 | | // Try to set the buffer size. The kernel will silently adjust |
| | 752 | | // the size to an acceptable value. Then read the size back to |
| | 753 | | // get the size that was actually set. |
| | 754 | | // |
| | 755 | | int sizeSet; |
| 1 | 756 | | if (i == 0) |
| | 757 | | { |
| 1 | 758 | | Network.setRecvBufferSize(_fd, sizeRequested); |
| 1 | 759 | | _rcvSize = Network.getRecvBufferSize(_fd); |
| 1 | 760 | | sizeSet = _rcvSize; |
| | 761 | | } |
| | 762 | | else |
| | 763 | | { |
| 1 | 764 | | Network.setSendBufferSize(_fd, sizeRequested); |
| 1 | 765 | | _sndSize = Network.getSendBufferSize(_fd); |
| 1 | 766 | | sizeSet = _sndSize; |
| | 767 | | } |
| | 768 | |
|
| | 769 | | // |
| | 770 | | // Warn if the size that was set is less than the requested size |
| | 771 | | // and we have not already warned |
| | 772 | | // |
| 1 | 773 | | if (sizeSet < sizeRequested) |
| | 774 | | { |
| 0 | 775 | | BufSizeWarnInfo winfo = _instance.getBufSizeWarn(Ice.UDPEndpointType.value); |
| 0 | 776 | | if ((isSnd && (!winfo.sndWarn || winfo.sndSize != sizeRequested)) || |
| 0 | 777 | | (!isSnd && (!winfo.rcvWarn || winfo.rcvSize != sizeRequested))) |
| | 778 | | { |
| 0 | 779 | | _instance.logger().warning("UDP " + direction + " buffer size: requested size of " + |
| 0 | 780 | | sizeRequested + " adjusted to " + sizeSet); |
| | 781 | |
|
| 0 | 782 | | if (isSnd) |
| | 783 | | { |
| 0 | 784 | | _instance.setSndBufSizeWarn(Ice.UDPEndpointType.value, sizeRequested); |
| | 785 | | } |
| | 786 | | else |
| | 787 | | { |
| 0 | 788 | | _instance.setRcvBufSizeWarn(Ice.UDPEndpointType.value, sizeRequested); |
| | 789 | | } |
| | 790 | | } |
| | 791 | | } |
| | 792 | | } |
| | 793 | | } |
| 1 | 794 | | } |
| | 795 | |
|
| | 796 | | internal void ioCompleted(object sender, SocketAsyncEventArgs e) |
| | 797 | | { |
| 1 | 798 | | switch (e.LastOperation) |
| | 799 | | { |
| | 800 | | case SocketAsyncOperation.Receive: |
| | 801 | | case SocketAsyncOperation.ReceiveFrom: |
| 1 | 802 | | _readCallback(e.UserToken); |
| 1 | 803 | | break; |
| | 804 | | case SocketAsyncOperation.Send: |
| | 805 | | case SocketAsyncOperation.Connect: |
| 0 | 806 | | _writeCallback(e.UserToken); |
| 0 | 807 | | break; |
| | 808 | | default: |
| 0 | 809 | | throw new ArgumentException("The last operation completed on the socket was not a receive or send"); |
| | 810 | | } |
| | 811 | | } |
| | 812 | |
|
| | 813 | | private UdpEndpointI _endpoint; |
| | 814 | | private readonly ProtocolInstance _instance; |
| | 815 | | private int _state; |
| | 816 | | private readonly bool _incoming; |
| | 817 | | private int _rcvSize; |
| | 818 | | private int _sndSize; |
| | 819 | | private Socket _fd; |
| | 820 | | private EndPoint _addr; |
| | 821 | | private readonly EndPoint _sourceAddr; |
| | 822 | | private IPEndPoint _mcastAddr; |
| | 823 | | private EndPoint _peerAddr; |
| | 824 | | private readonly string _mcastInterface; |
| | 825 | |
|
| | 826 | | private readonly int _port; |
| | 827 | | private bool _bound; |
| | 828 | |
|
| | 829 | | private SocketAsyncEventArgs _writeEventArgs; |
| | 830 | | private readonly SocketAsyncEventArgs _readEventArgs; |
| | 831 | | private AsyncCallback _writeCallback; |
| | 832 | | private AsyncCallback _readCallback; |
| | 833 | |
|
| | 834 | | private const int StateNeedConnect = 0; |
| | 835 | | private const int StateConnectPending = 1; |
| | 836 | | private const int StateConnected = 2; |
| | 837 | | private const int StateNotConnected = 3; |
| | 838 | |
|
| | 839 | | // |
| | 840 | | // The maximum IP datagram size is 65535. Subtract 20 bytes for the IP header and 8 bytes for the UDP header |
| | 841 | | // to get the maximum payload. |
| | 842 | | // |
| | 843 | | private const int _udpOverhead = 20 + 8; |
| | 844 | | private const int _maxPacketSize = 65535 - _udpOverhead; |
| | 845 | | } |