示例#1
0
        private async Task ConnectAsync(IPAddress ip, int port, CancellationToken cancellationToken = default)
        {
            await CloseAsync();

            _client = new TcpClient();
            cancellationToken.ThrowIfCancellationRequested();
            Console.WriteLine($"Connecting to {ip}:{port}");
            try
            {
                await _client.ConnectAsync(ip, port);
            }
            catch (Exception e)
            {
                Log.LogException(e);
                OnConnectError?.Invoke(NetError.CantStablishConnection);
                throw;
            }
            await CloseIfCanceled(cancellationToken);

            _stream      = _client.GetStream();
            _isConnected = true;
            OnConnected?.Invoke();

            _ = Task.Factory.StartNew(() =>
                                      Receive(cancellationToken),
                                      cancellationToken: cancellationToken)
                .ConfigureAwait(false);
        }
示例#2
0
 public ConnectErrorCallBack(Connector connector
                             , OnConnectError <T> onConnectError
                             , T context
                             , Exception exception)
 {
     _connector      = connector;
     _onConnectError = onConnectError;
     _exception      = exception;
     _context        = context;
 }
示例#3
0
        private bool ConnectToServer()
        {
            //建立IPEndPoint
            IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(_ip), _port);

            TcpClient = new TcpClient();

            //開始連線
            try
            {
                TcpClient.Connect(ipe);
                if (TcpClient.Connected)
                {
                    if (_keepAlive)   // 保持連線
                    {
                        TcpClient.Client.IOControl(IOControlCode.KeepAliveValues, KeepAlive(1, 1000, 1000), null);
                    }
                    Manager = factory.CreateManager(TcpClient);
                    OnConnected?.Invoke(this, Manager);
                    _manualClose = false;
                    Task.Run(() =>
                    {
                        Manager.Communicate();
                        Manager = null;
                        while (!_manualClose && AutoReconnect)
                        {
                            Thread.Sleep(AutoReconnectDelay);
                            OnReconnecting?.Invoke(this, EventArgs.Empty);
                            if (ConnectToServer())
                            {
                                OnReconnected(this, Manager);
                                break;
                            }
                            OnReconnectError?.Invoke(this, EventArgs.Empty);
                        }
                    }).ConfigureAwait(false);
                    return(true);
                }
            }
            catch (Exception)
            {
                TcpClient.Close();
            }
            OnConnectError?.Invoke(this, EventArgs.Empty);
            return(false);
        }
示例#4
0
 public ConnectRequest(ILog logger
                       , Connector connector
                       , BuildProtocol <T> buildProtocol
                       , OnConnectError <T> throwError
                       , T context
                       , ByteBuffer byteBuffer)
     : base(null)
 {
     _logger        = logger;
     _socket        = null;
     _connector     = connector;
     _buildProtocol = buildProtocol;
     _throwError    = throwError;
     _context       = context;
     _byteBuffer    = byteBuffer;
     if (null != _byteBuffer)
     {
         PinObject(_byteBuffer.Array);
     }
 }
示例#5
0
 private void ThreadTask()
 {
     try
     {
         while (!m_Terminate)
         {
             if (m_SocketConn == null || !m_SocketConn.Connected)
             {
                 var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                 try
                 {
                     socket.Connect(m_RemoteAddress, m_RemotePort);
                     m_SocketConn = new SocketConn(socket, false);
                     m_SocketConn.OnEventTrace += ScktConn_OnEventTrace;
                     m_SocketConn.OnDisconnect += new SocketConn.DlgDisconnect(SocketConn_Disconnect);
                     m_SocketConn.OnSendError  += new SocketConn.DlgSendError(SocketConn_SendError);
                     if (m_StartScktConnReadThread)
                     {
                         m_SocketConn.OnReceiveData  += new SocketConn.DlgReceiveData(SocketConn_ReceiveData);
                         m_SocketConn.OnReceiveError += new SocketConn.DlgReceiveError(SocketConn_ReceiveError);
                         m_SocketConn.StartReadThread();
                     }
                     OnConnect?.Invoke(m_SocketConn);
                 }
                 catch (Exception e)
                 {
                     Trace(EventType.Error,
                           "Exception connecting the socket. Remote endpoint: {0}",
                           RemoteEndPoint);
                     OnConnectError?.Invoke(e);
                     TryToReconnect();
                 }
             }
             m_ReconnectSignal.WaitOne(RETRY_CONNECT);
         }
     }
     catch (Exception exc)
     {
         Trace(exc);
     }
 }
示例#6
0
 public async Task InvokeConnectError()
 {
     _tokenSource.Cancel();
     OnConnectError?.Invoke("");
 }