Exemplo n.º 1
0
        private void OnConnected(object sender, SocketAsyncEventArgs args)
        {
            if (state == NetworkGatewayState.CONNECTING)
            {
                if (args.SocketError == SocketError.Success)
                {
                    logger.info("connection successfully");
                    state       = NetworkGatewayState.ESTABLISHED;
                    sendCounter = 0;
                    connectTaskCompletionSource.TrySetResult(null);
                    var handler = ConnectedEvent;
                    if (handler != null)
                    {
                        handler();
                    }

                    ReadAsync();
                }
                else
                {
                    logger.info("connection error: {0}", args.SocketError);
                    TryReconnect();
                }
            }
            else
            {
                throw new InvalidOperationException("onConnected in not-INIT state");
            }
        }
Exemplo n.º 2
0
 public TransportGateway() {
     socket = null;
     state = NetworkGatewayState.INIT;
     //inputStream = new MemoryStream(16000);
     input = new CborInputMoveBuffer(512 * 1024);
     input.InputEvent += CheckInput;
 }
Exemplo n.º 3
0
 public TransportGateway()
 {
     socket = null;
     state  = NetworkGatewayState.INIT;
     //inputStream = new MemoryStream(16000);
     input             = new CborInputMoveBuffer(512 * 1024);
     input.InputEvent += CheckInput;
 }
Exemplo n.º 4
0
 public void Dispose()
 {
     logger.info("transport connection closed");
     state = NetworkGatewayState.DISPOSED;
     try {
         socket.Close();
     } catch (Exception e) {
     }
 }
Exemplo n.º 5
0
        private void TryReconnect() {
            if(state == NetworkGatewayState.DISPOSED) {
                connectTaskCompletionSource.TrySetResult(null);
                return;
            }

            if(connectRetries == 0) {
                logger.info("connect failed");
                connectTaskCompletionSource.TrySetException(new TransportConnectException());
                return;
            }

            logger.info("reconnect, remaining retries: {0}", connectRetries);
            state = NetworkGatewayState.INIT;
            connectRetries--;
            endpointIndex = (endpointIndex + 1)%dc.Endpoints.Count;
            Task.Delay(1000).ContinueWith(delegate { Connect(dc); });
        }
Exemplo n.º 6
0
        private void TryReconnect()
        {
            if (state == NetworkGatewayState.DISPOSED)
            {
                connectTaskCompletionSource.TrySetResult(null);
                return;
            }

            if (connectRetries == 0)
            {
                logger.info("connect failed");
                connectTaskCompletionSource.TrySetException(new TransportConnectException());
                return;
            }

            logger.info("reconnect, remaining retries: {0}", connectRetries);
            state = NetworkGatewayState.INIT;
            connectRetries--;
            endpointIndex = (endpointIndex + 1) % dc.Endpoints.Count;
            Task.Delay(1000).ContinueWith(delegate { Connect(dc); });
        }
Exemplo n.º 7
0
        private bool Connect(TelegramDC dc)
        {
            if (state == NetworkGatewayState.INIT)
            {
                logger.info("connecing to {0}:{1}", dc.Endpoints[endpointIndex].Host, dc.Endpoints[endpointIndex].Port);
                this.dc = dc;
                var args = new SocketAsyncEventArgs();
                args.RemoteEndPoint = new DnsEndPoint(dc.Endpoints[endpointIndex].Host, dc.Endpoints[endpointIndex].Port);
                args.Completed     += OnConnected;
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                state  = NetworkGatewayState.CONNECTING;
                bool async = socket.ConnectAsync(args);
                if (!async)
                {
                    OnConnected(this, args);
                }

                return(true);
            }

            throw new InvalidOperationException("connect in not INIT state");
        }
Exemplo n.º 8
0
        private bool Connect(TelegramDC dc) {
            if (state == NetworkGatewayState.INIT) {
                logger.info("connecing to {0}:{1}", dc.Endpoints[endpointIndex].Host, dc.Endpoints[endpointIndex].Port);
                this.dc = dc;
                var args = new SocketAsyncEventArgs();
                args.RemoteEndPoint = new DnsEndPoint(dc.Endpoints[endpointIndex].Host, dc.Endpoints[endpointIndex].Port);
                args.Completed += OnConnected;
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                state = NetworkGatewayState.CONNECTING;
                bool async = socket.ConnectAsync(args);
                if (!async) {
                    OnConnected(this, args);
                }

                return true;
            }

            throw new InvalidOperationException("connect in not INIT state");
        }
Exemplo n.º 9
0
        public void Dispose() {
            logger.info("transport connection closed");
            state = NetworkGatewayState.DISPOSED;
            try {
                socket.Close();
            } catch(Exception e) {

            }
        }
Exemplo n.º 10
0
 private void OnConnected(object sender, SocketAsyncEventArgs args) {
     if (state == NetworkGatewayState.CONNECTING) {
         if (args.SocketError == SocketError.Success) {
             logger.info("connection successfully");
             state = NetworkGatewayState.ESTABLISHED;
             sendCounter = 0;
             connectTaskCompletionSource.TrySetResult(null);
             var handler = ConnectedEvent;
             if(handler != null) handler();
             
             ReadAsync();
         } else {
             logger.info("connection error: {0}", args.SocketError);
             TryReconnect();
         }
     } else {
         throw new InvalidOperationException("onConnected in not-INIT state");
     }
 }