Exemple #1
0
 internal TcpTransceiver(Communicator communicator, Socket fd)
 {
     _communicator = communicator;
     Socket        = fd;
     try
     {
         Network.SetBufSize(Socket, _communicator, Transport.TCP);
         _desc = Network.SocketToString(Socket);
     }
     catch (Exception)
     {
         Socket.CloseNoThrow();
         throw;
     }
 }
Exemple #2
0
        public override string ToString()
        {
            try
            {
                var sb = new StringBuilder();
                if (_incoming)
                {
                    sb.Append("local address = " + Network.LocalAddrToString(Network.GetLocalAddress(Socket)));
                    if (_peerAddr != null)
                    {
                        sb.Append($"\nremote address = {_peerAddr}");
                    }
                }
                else
                {
                    sb.Append(Network.SocketToString(Socket));
                }
                if (MulticastAddress != null)
                {
                    sb.Append($"\nmulticast address = {MulticastAddress}");
                }

                List <string> interfaces;
                if (MulticastAddress == null)
                {
                    interfaces = Network.GetHostsForEndpointExpand(_addr.ToString(), Network.EnableBoth, true);
                }
                else
                {
                    Debug.Assert(_multicastInterface != null);
                    interfaces = Network.GetInterfacesForMulticast(_multicastInterface,
                                                                   Network.GetIPVersion(MulticastAddress.Address));
                }
                if (interfaces.Count != 0)
                {
                    sb.Append("\nlocal interfaces = ");
                    sb.Append(string.Join(", ", interfaces));
                }
                return(sb.ToString());
            }
            catch (ObjectDisposedException)
            {
                return("<closed>");
            }
        }
Exemple #3
0
        public async ValueTask InitializeAsync(CancellationToken cancel)
        {
            if (_addr != null)
            {
                try
                {
                    // Bind the socket to the source address if one is set.
                    if (_sourceAddr != null)
                    {
                        Socket.Bind(new IPEndPoint(_sourceAddr, 0));
                    }

                    // Connect to the server or proxy server.
                    // TODO: use the cancelable ConnectAsync with 5.0
                    await Socket.ConnectAsync(_proxy?.Address ?? _addr).WaitAsync(cancel).ConfigureAwait(false);

                    _desc = Network.SocketToString(Socket, _proxy, _addr);

                    if (_proxy != null)
                    {
                        await _proxy.ConnectAsync(Socket, _addr, cancel).ConfigureAwait(false);
                    }
                }
                catch (SocketException) when(cancel.IsCancellationRequested)
                {
                    throw new OperationCanceledException(cancel);
                }
                catch (SocketException ex) when(ex.SocketErrorCode == SocketError.ConnectionRefused)
                {
                    throw new ConnectionRefusedException(ex);
                }
                catch (SocketException ex)
                {
                    throw new ConnectFailedException(ex);
                }
            }
        }