Exemple #1
0
        void OnRead(Tcp tcpClient, IReadCompletion completion)
        {
            if (completion.Error != null)
            {
                Console.WriteLine($"Ping pong : failed, read error {completion.Error}.");
                tcpClient.Dispose();
                return;
            }

            IRange data = completion.Data;

            if (data == null)
            {
                return;
            }

            string message = data.GetString(Encoding.UTF8);

            foreach (char token in message)
            {
                if (token == SplitToken)
                {
                    this.state = 0;
                }
                else
                {
                    if (token != PingMessage[this.state])
                    {
                        Console.WriteLine($"Ping pong : failed, wrong message token received {token}.");
                        tcpClient.Dispose();
                        return;
                    }

                    this.state++;
                }

                if (this.state == 0)
                {
                    this.pongs++;
                    long duration = this.loop.Now - this.startTime;
                    if (duration > DurationInMilliseconds)
                    {
                        tcpClient.Dispose();
                        this.server.Dispose();
                        return;
                    }
                    else
                    {
                        this.WritePing(tcpClient, null);
                    }
                }
            }
        }
Exemple #2
0
        void OnServerRead(Tcp tcpClient, IReadCompletion completion)
        {
            if (this.totalBytesRead == 0)
            {
                this.loop.UpdateTime();
                this.startTime = this.loop.Now;
            }
            int count = completion.Data.Count;

            this.bytesRead      += count;
            this.totalBytesRead += count;
        }
Exemple #3
0
        static void OnRead(Tcp tcpClient, IReadCompletion completion)
        {
            if (completion.Error != null)
            {
                Console.WriteLine($"Echo server read failed {completion.Error}");
                tcpClient.Dispose();
                return;
            }

            IRange data = completion.Data;

            if (data == null ||
                data.Count == 0)
            {
                return;
            }

            string message = data.GetString(Encoding.UTF8);

            Console.WriteLine($"Echo server received : {message}");

            /*
             * Scan for the letter Q which signals that we should quit the server.
             * If we get QS it means close the stream.
             */
            if (message.StartsWith("Q"))
            {
                Console.WriteLine("Echo server closing stream.");
                tcpClient.Dispose();

                if (message.EndsWith("QS"))
                {
                    return;
                }

                Console.WriteLine("Echo server shutting down.");
                tcp.Dispose();
            }
            else
            {
                Console.WriteLine("Echo server sending echo back.");
                byte[] array = Encoding.UTF8.GetBytes($"ECHO [{message}]");
                tcpClient.QueueWrite(array, OnWriteCompleted);
            }
        }
Exemple #4
0
        static void OnRead(Tcp tcpClient, IReadCompletion completion)
        {
            if (completion.Error != null)
            {
                Console.WriteLine($"Echo client error {completion.Error}");
                tcpClient.Dispose();
                return;
            }

            IRange data = completion.Data;

            if (data.Count == 0)
            {
                return;
            }

            string message = data.GetString(Encoding.UTF8);

            Console.WriteLine($"Echo client received : {message}");

            Console.WriteLine("Message received, sending Q to server");
            tcpClient.QueueWrite(Encoding.UTF8.GetBytes("Q"), OnWriteCompleted);
        }
Exemple #5
0
        void OnRead(Tcp tcpClient, IReadCompletion completion)
        {
            if (completion.Error != null)
            {
                Console.WriteLine($"{nameof(EchoServer)} read failed, {completion.Error}");
                tcpClient.Dispose();
                return;
            }

            IRange data = completion.Data;

            if (data.Count == 0)
            {
                return;
            }

            string message = data.GetString(Encoding.UTF8);

            /*
             * Scan for the letter Q which signals that we should quit the server.
             * If we get QS it means close the stream.
             */
            if (message.StartsWith("Q"))
            {
                tcpClient.Dispose();

                if (message.EndsWith("QS"))
                {
                    this.Dispose();
                }
            }
            else
            {
                byte[] array = data.Copy();
                tcpClient.QueueWrite(array, this.OnWriteCompleted);
            }
        }
Exemple #6
0
 static void OnRead(Tcp tcpClient, IReadCompletion completion)
 {
     // NOP
 }