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