Esempio n. 1
0
 /// <summary>Соеднинение с сервером</summary>
 public void Connect()
 {
     if (_client == null)
     {
         _client = new TcpClient()
         {
             SendTimeout = Timeout, ReceiveTimeout = Timeout
         }
     }
     ;
     if (!_client.Connected)
     {
         try
         {
             _client.Connect(Host, Port);
             _clientStream = new StreamReader(UseSsl ? _client.GetSslStream(Host, CertificateValidator) : _client.GetStream(), Encoding, false); //Encoding.GetEncoding(1251), GetEncoding(20866)
             AfterConnect?.Invoke();
         }
         catch (SocketException e)
         {
             Disconnect(true);
             throw new Exception("Unable to connect to ({0}:{1})  : {2}".FormatStr(Host, Port, e.Message), e);
         }
         catch
         {
             Disconnect(true);
             throw;
         }
     }
 }
Esempio n. 2
0
 /// <summary>Соеднинение с сервером</summary>
 public void Connect()
 {
     if (socket == null)
     {
         socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         socket.ReceiveTimeout = Timeout;
         socket.SendTimeout    = Timeout;
     }
     if (!socket.Connected)
     {
         try
         {
             socket.Connect(Dns.GetHostEntry(Host).AddressList[0] + "", Port);
             AfterConnect?.Invoke();
         }
         catch (Exception ex)
         {
             if (socket != null)
             {
                 socket.Close();                 //проверка нужна для случая, если в AfterConnect вызвали Disconnect
             }
             socket = null;
             throw new Exception("Ошибка при попытке соединения с сервером ({0}:{1}) : {2}".FormatStr(Host, Port, ex.Message), ex);
         }
     }
 }
Esempio n. 3
0
        private void _listener_Accepted(IChannelListener <TChannel> sender, TChannel channel)
        {
            channel.OnDisconnect += Channel_OnDisconnect;

            if (!channel.IsConnected)
            {
                return;
            }
            var connection = _connectionBuilder.UseChannel(channel).Build();

            var beforeConnectEventArgs = new BeforeConnectEventArgs <TContract, TChannel>(connection);

            BeforeConnect?.Invoke(this, beforeConnectEventArgs);
            if (!beforeConnectEventArgs.AllowConnection)
            {
                return;
            }

            channel.AllowReceive = true;
            _connections.TryAdd(channel, connection);
            AfterConnect?.Invoke(this, connection);
        }