Exemple #1
0
        public override async Task OnConnected(AsyncTcpClient tcp)
        {
            Console.WriteLine("Connected");

            await tcp.WriteAsync(query);
            await tcp.FlushAsync();
            await Task.Delay(1000, tcp.Token);

            while (true)
            {
                int len = await tcp.ReadSomeAsync(reply, 500);
                if (len == 0) break;

                Console.Write("{0} byte(s) received:", len);

                var tail = string.Empty;
                if (len > 16) { len = 16; tail = "..."; }
                for (int i = 0; i < len; i++) Console.Write(" {0:X2}", reply[i]);
                Console.WriteLine(tail);
            }
        }
Exemple #2
0
        private async Task InternalRunAsync(AsyncTcpConnector connector, int connectTimeout, CancellationToken ct)
        {
            while (!ct.IsCancellationRequested)
            {
                OnConnecting();
                Task timerTask = null;

                using (var tcp = new AsyncTcpClient(ct))
                {
                    var cr = await tcp.TryConnectAsync(connector, connectTimeout);
                    timerTask = cr.TimerTask;

                    if (cr.Connected)
                    {
                        try
                        {
                            await OnConnected(tcp);
                        }
                        catch (IOException e)
                        {
                            if (!(e.InnerException is SocketException)) throw;
                        }
                        catch (OperationCanceledException)
                        {
                        }
                    }
                }

                try
                {
                    await timerTask;
                }
                catch (OperationCanceledException)
                {
                }
            }
        }
Exemple #3
0
 public abstract Task OnConnected(AsyncTcpClient tcp);