| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Net; |
| | 4 | | using System.Net.Sockets; |
| | 5 | | using System.Text; |
| | 6 | |
|
| | 7 | | namespace Ice.Internal; |
| | 8 | |
|
| | 9 | | internal class TcpAcceptor : Acceptor |
| | 10 | | { |
| | 11 | | public virtual void close() |
| | 12 | | { |
| 1 | 13 | | if (_acceptFd != null) |
| | 14 | | { |
| 0 | 15 | | Network.closeSocketNoThrow(_acceptFd); |
| 0 | 16 | | _acceptFd = null; |
| | 17 | | } |
| 1 | 18 | | if (_fd != null) |
| | 19 | | { |
| 1 | 20 | | Network.closeSocketNoThrow(_fd); |
| 1 | 21 | | _fd = null; |
| | 22 | | } |
| 1 | 23 | | } |
| | 24 | |
|
| | 25 | | public virtual EndpointI listen() |
| | 26 | | { |
| | 27 | | try |
| | 28 | | { |
| 1 | 29 | | _addr = Network.doBind(_fd, _addr); |
| 1 | 30 | | Network.doListen(_fd, _backlog); |
| 1 | 31 | | } |
| 0 | 32 | | catch (SystemException) |
| | 33 | | { |
| 0 | 34 | | _fd = null; |
| 0 | 35 | | throw; |
| | 36 | | } |
| 1 | 37 | | _endpoint = _endpoint.endpoint(this); |
| 1 | 38 | | return _endpoint; |
| | 39 | | } |
| | 40 | |
|
| | 41 | | public virtual bool startAccept(AsyncCallback callback, object state) |
| | 42 | | { |
| | 43 | | try |
| | 44 | | { |
| 1 | 45 | | _result = _fd.BeginAccept( |
| 1 | 46 | | (IAsyncResult result) => |
| 1 | 47 | | { |
| 1 | 48 | | if (!result.CompletedSynchronously) |
| 1 | 49 | | { |
| 1 | 50 | | callback(result.AsyncState); |
| 1 | 51 | | } |
| 1 | 52 | | }, |
| 1 | 53 | | state); |
| 1 | 54 | | } |
| 0 | 55 | | catch (System.Exception ex) |
| | 56 | | { |
| 0 | 57 | | throw new Ice.SocketException(ex); |
| | 58 | | } |
| 1 | 59 | | return _result.CompletedSynchronously; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | public virtual void finishAccept() |
| | 63 | | { |
| 1 | 64 | | if (_fd != null) |
| | 65 | | { |
| 1 | 66 | | _acceptFd = null; |
| | 67 | | try |
| | 68 | | { |
| 1 | 69 | | _acceptFd = _fd.EndAccept(_result); |
| 1 | 70 | | } |
| 0 | 71 | | catch (System.Exception ex) |
| | 72 | | { |
| 0 | 73 | | _acceptError = new Ice.SocketException(ex); |
| 0 | 74 | | } |
| | 75 | | } |
| 1 | 76 | | } |
| | 77 | |
|
| | 78 | | public virtual Transceiver accept() |
| | 79 | | { |
| 1 | 80 | | if (_acceptFd == null) |
| | 81 | | { |
| 0 | 82 | | throw _acceptError; |
| | 83 | | } |
| | 84 | |
|
| 1 | 85 | | Socket acceptFd = _acceptFd; |
| 1 | 86 | | _acceptFd = null; |
| 1 | 87 | | _acceptError = null; |
| 1 | 88 | | return new TcpTransceiver(_instance, new StreamSocket(_instance, acceptFd)); |
| | 89 | | } |
| | 90 | |
|
| 0 | 91 | | public string protocol() => _instance.protocol(); |
| | 92 | |
|
| 1 | 93 | | public override string ToString() => Network.addrToString(_addr); |
| | 94 | |
|
| 1 | 95 | | public string toDetailedString() => $"local address = {this}"; |
| | 96 | |
|
| 1 | 97 | | internal int effectivePort() => _addr.Port; |
| | 98 | |
|
| 1 | 99 | | internal TcpAcceptor(TcpEndpointI endpoint, ProtocolInstance instance, string host, int port) |
| | 100 | | { |
| 1 | 101 | | _endpoint = endpoint; |
| 1 | 102 | | _instance = instance; |
| 1 | 103 | | _backlog = instance.properties().getIcePropertyAsInt("Ice.TCP.Backlog"); |
| | 104 | |
|
| | 105 | | try |
| | 106 | | { |
| 1 | 107 | | int protocol = _instance.protocolSupport(); |
| 1 | 108 | | _addr = (IPEndPoint)Network.getAddressForServer(host, port, protocol, _instance.preferIPv6()); |
| 1 | 109 | | _fd = Network.createServerSocket(false, _addr.AddressFamily, protocol); |
| 1 | 110 | | Network.setBlock(_fd, false); |
| 1 | 111 | | Network.setTcpBufSize(_fd, _instance); |
| 1 | 112 | | } |
| 0 | 113 | | catch (System.Exception) |
| | 114 | | { |
| 0 | 115 | | _fd = null; |
| 0 | 116 | | throw; |
| | 117 | | } |
| 1 | 118 | | } |
| | 119 | |
|
| | 120 | | private TcpEndpointI _endpoint; |
| | 121 | | private readonly ProtocolInstance _instance; |
| | 122 | | private Socket _fd; |
| | 123 | | private Socket _acceptFd; |
| | 124 | | private Ice.SocketException _acceptError; |
| | 125 | | private readonly int _backlog; |
| | 126 | | private IPEndPoint _addr; |
| | 127 | | private IAsyncResult _result; |
| | 128 | | } |