예제 #1
0
        public async Task Run()
        {
            while (!disposed)
            {
                try {
                    logger.info("starting connection...");
                    AsyncSocket tempSocket = null;
                    foreach (var telegramEndpoint in dc.Endpoints)
                    {
                        logger.debug("connecting to {}:{}...", telegramEndpoint.Host, telegramEndpoint.Port);
                        try {
                            tempSocket = new AsyncSocket();
                            await tempSocket.Connect(telegramEndpoint.Host, telegramEndpoint.Port);

                            logger.debug("connect success");
                            break;
                        } catch (TelegramSocketException e) {
                            logger.info("connect to {0}:{1} error: {2}", telegramEndpoint.Host, telegramEndpoint.Port, e);
                            tempSocket = null;
                        }
                    }

                    if (tempSocket == null)
                    {
                        logger.warning("connection failed... wait 2 seconds and retry");
                        await Task.Delay(TimeSpan.FromSeconds(3));

                        continue;
                    }

                    lock (this) {
                        socket      = tempSocket;
                        sendCounter = 0;
                    }



                    logger.debug("send connected event...");
                    OnConnectedEvent();

                    input.Clear();
                    while (!disposed)
                    {
                        logger.debug("reading new chunk...");
                        input.AddChunk(await socket.Read());
                    }
                } catch (Exception e) {
                    logger.info("connection error: {0}", e);
                }

                lock (this) {
                    socket = null;
                }

                logger.debug("disconnected");
                OnDisconnectedEvent();
            }
        }
예제 #2
0
 private void OnRead(object sender, SocketAsyncEventArgs args)
 {
     if (state == NetworkGatewayState.ESTABLISHED)
     {
         if (args.SocketError == SocketError.Success && socket.Connected && args.BytesTransferred > 0)
         {
             //logger.debug("input transport data: {0}", BitConverter.ToString(args.Buffer, 0, args.BytesTransferred));
             //inputStream.Write(args.Buffer, 0, args.BytesTransferred);
             //CheckInput();
             input.AddChunk(args.Buffer, 0, args.BytesTransferred);
             ReadAsync(args.Buffer);
         }
         else if (state != NetworkGatewayState.DISPOSED)
         {
             logger.info("read error {0}, reconnecting", args.SocketError);
             TryReconnect();
         }
     }
     //else {
     //throw new InvalidOperationException("state is non-ESTABLISHED");
     //}
 }