Beispiel #1
0
        void OnAccept(StreamHandle stream, ReadableBuffer data)
        {
            string message = data.ReadString(Encoding.UTF8);

            if (string.IsNullOrEmpty(message))
            {
                return;
            }

            //
            // 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"))
            {
                if (message.EndsWith("QS"))
                {
                    stream.CloseHandle(OnClose);
                }
                else
                {
                    this.CloseServer();
                }
            }
            else
            {
                stream.QueueWriteStream(Encoding.UTF8.GetBytes(message), OnWriteCompleted);
            }
        }
Beispiel #2
0
        static void OnReceive(Udp udp, IDatagramReadCompletion completion)
        {
            if (completion.Error != null)
            {
                Console.WriteLine($"Echo server receive error {completion.Error}");
                udp.CloseHandle(OnClosed);
                return;
            }

            IPEndPoint     remoteEndPoint = completion.RemoteEndPoint;
            ReadableBuffer data           = completion.Data;
            string         message        = data.ReadString(Encoding.UTF8);

            if (string.IsNullOrEmpty(message))
            {
                return;
            }
            Console.WriteLine($"Echo server received : {message} from {remoteEndPoint}");

            Console.WriteLine($"Echo server sending echo back to {remoteEndPoint}.");
            byte[]         array  = Encoding.UTF8.GetBytes($"ECHO [{message}]");
            WritableBuffer buffer = WritableBuffer.From(array);

            udp.QueueSend(buffer, remoteEndPoint, OnSendCompleted);
        }
Beispiel #3
0
        void OnClientReceive(Udp udp, IDatagramReadCompletion completion)
        {
            ReadableBuffer buffer  = completion.Data;
            string         message = buffer.ReadString(Encoding.UTF8);

            if (message == "PONG")
            {
                this.clientReceiveCount++;
            }

            udp.CloseHandle(this.OnClose);
        }
Beispiel #4
0
        static void OnReceive(Udp udp, IDatagramReadCompletion completion)
        {
            if (completion.Error != null)
            {
                Console.WriteLine($"Echo client receive error {completion.Error}");
            }

            IPEndPoint     remoteEndPoint = completion.RemoteEndPoint;
            ReadableBuffer data           = completion.Data;
            string         message        = data.ReadString(Encoding.UTF8);

            Console.WriteLine($"Echo client received : {message} from {remoteEndPoint}");
            udp.CloseHandle(OnClosed);
        }
Beispiel #5
0
        void OnClientReceive(Udp udp, IDatagramReadCompletion completion)
        {
            this.receiveError = completion.Error;
            ReadableBuffer buffer  = completion.Data;
            string         message = buffer.ReadString(Encoding.UTF8);

            if (message == "PING")
            {
                this.clientReceiveCount++;
            }

            /* we are done with the client handle, we can close it */
            udp.CloseHandle(this.OnClose);
        }
Beispiel #6
0
        void OnServerReceive(Udp udp, IDatagramReadCompletion completion)
        {
            ReadableBuffer buffer  = completion.Data;
            string         message = buffer.ReadString(Encoding.UTF8);

            if (message == "PING")
            {
                this.serverReceiveCount++;
            }

            udp.ReceiveStop();
            byte[] data = Encoding.UTF8.GetBytes("PONG");
            udp.QueueSend(data, completion.RemoteEndPoint, this.OnServerSendCompleted);
        }
Beispiel #7
0
        static void OnAccept(StreamHandle stream, ReadableBuffer data)
        {
            if (data.Count == 0)
            {
                return;
            }

            string message = data.ReadString(data.Count, Encoding.UTF8);

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

            Console.WriteLine("Message received, sending QS to server");
            byte[]         array  = Encoding.UTF8.GetBytes("QS");
            WritableBuffer buffer = WritableBuffer.From(array);

            stream.QueueWriteStream(buffer, OnWriteCompleted);
        }
Beispiel #8
0
        void OnServerReceive(Udp udp, IDatagramReadCompletion completion)
        {
            this.receiveError = completion.Error;

            ReadableBuffer data    = completion.Data;
            string         message = data.ReadString(Encoding.UTF8);

            if (message == "EXIT")
            {
                this.serverReceiveCount++;
            }

            udp.CloseHandle(this.OnClose);
            this.client?.CloseHandle(this.OnClose);

            this.server.ReceiveStop();
            this.server.CloseHandle(this.OnClose);
        }
Beispiel #9
0
        void OnAccept(StreamHandle stream, ReadableBuffer data)
        {
            string message = data.ReadString(Encoding.UTF8);

            if (string.IsNullOrEmpty(message))
            {
                return;
            }

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

                    this.state++;
                }

                if (this.state == 0)
                {
                    this.pongs++;
                    long duration = this.loop.Now - this.startTime;

                    if (duration > DurationInMilliseconds)
                    {
                        stream.CloseHandle(OnClose);
                        this.server.CloseServer();
                    }
                    else
                    {
                        stream.QueueWriteStream(this.content, OnWriteCompleted);
                    }
                }
            }
        }
Beispiel #10
0
        void OnReceive(Udp udp, IDatagramReadCompletion completion)
        {
            if (completion.Error is OperationException error &&
                error.ErrorCode == ErrorCode.ECANCELED)    // UV_ECANCELED
            {
                return;
            }

            ReadableBuffer data    = completion.Data;
            string         message = data.ReadString(Encoding.UTF8);

            if (!string.IsNullOrEmpty(message) &&
                message != ExpectedMessage)
            {
                Console.WriteLine($"Udp pummel {this.numberOfSenders}v{this.numberOfReceivers} failed, wrong message '{message}' received.");
            }

            this.receiveCount++;
        }
        public void on_read_uv(Tcp Client, ReadableBuffer data)
        {
            try
            {
                string data_raw = data.ReadString(Encoding.UTF8);

                byte[] DataByte = Encoding.UTF8.GetBytes(data_raw);

                stream.Write(DataByte);

                stream.Position = stream.Position - DataByte.Length;

                BeginReadRequest();
            }
            catch
            {
                timer.Change(Timeout.Infinite, Timeout.Infinite);
                CloseSocket();
                Unbind();
            }
        }
        void OnReceive(Udp udp, IDatagramReadCompletion completion)
        {
            if (completion.Error != null ||
                completion.RemoteEndPoint == null)
            {
                return;
            }

            ReadableBuffer buffer  = completion.Data;
            string         message = buffer.ReadString(Encoding.UTF8);

            if (message == "PING" ||
                message == "PANG")
            {
                this.serverReceiveCount++;
            }

            if (this.serverReceiveCount == 2)
            {
                udp.CloseHandle(this.OnClose);
            }
        }
Beispiel #13
0
        static void OnAccept(StreamHandle stream, ReadableBuffer data)
        {
            string message = data.Count > 0 ? data.ReadString(data.Count, Encoding.UTF8) : null;

            data.Dispose();
            if (string.IsNullOrEmpty(message))
            {
                return;
            }

            Console.WriteLine($"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("Server closing stream.");
                stream.Dispose();

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

                Console.WriteLine("Server shutting down.");
                eventLoop.ScheduleStop();
            }
            else
            {
                Console.WriteLine("Server sending echo back.");
                byte[]         array  = Encoding.UTF8.GetBytes($"ECHO [{message}]");
                WritableBuffer buffer = WritableBuffer.From(array);
                stream.QueueWriteStream(buffer, OnWriteCompleted);
            }
        }