Beispiel #1
0
        static void OnReceive(Udp udp, IDatagramReadCompletion completion)
        {
            if (completion.Error != null)
            {
                completion.Dispose();
                Console.WriteLine($"{nameof(EchoServer)} receive error {completion.Error}");
                udp.ReceiveStop();
                udp.CloseHandle(OnClose);
                return;
            }

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

            data.Dispose();

            IPEndPoint remoteEndPoint = completion.RemoteEndPoint;

            if (string.IsNullOrEmpty(message) ||
                remoteEndPoint == null)
            {
                return;
            }

            WritableBuffer buffer = WritableBuffer.From(Encoding.UTF8.GetBytes(message));

            udp.QueueSend(buffer, remoteEndPoint, OnSendCompleted);
        }
Beispiel #2
0
        static WritableBuffer CreateMessage()
        {
            byte[]         array  = Encoding.UTF8.GetBytes($"Greetings {DateTime.UtcNow}");
            WritableBuffer buffer = WritableBuffer.From(array);

            return(buffer);
        }
Beispiel #3
0
        void OnAccept(StreamHandle stream, ReadableBuffer data)
        {
            string message = data.Count > 0 ? data.ReadString(data.Count, Encoding.UTF8) : null;

            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
            {
                WritableBuffer buffer = WritableBuffer.From(Encoding.UTF8.GetBytes(message));
                stream.QueueWriteStream(buffer, OnWriteCompleted);
            }
        }
Beispiel #4
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.Count > 0 ? data.ReadString(data.Count, Encoding.UTF8) : null;

            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);
        }
 private void OnConnected(Tcp client, Exception exception)
 {
     if (exception != null)
     {
         log($"client error: {exception.Message}");
         client.CloseHandle(this.OnClosed);
     }
     else
     {
         var writableBuffer = WritableBuffer.From(new byte[1024]);
         client.QueueWriteStream(
             writableBuffer,
             (streamHandle, error) =>
         {
             writableBuffer.Dispose();
             if (error != null)
             {
                 log($"write error {error}");
                 streamHandle.CloseHandle(this.OnClosed);
             }
             else
             {
                 client.OnRead(
                     this.OnAccept,
                     (_h, _e) => { log($"read error {_e.Message}"); });
             }
         });
         ////log("client wrote 1024");
     }
 }
Beispiel #6
0
        public Pump(HandleType handleType, int clientCount, bool showIntervalStatistics = false)
        {
            this.handleType             = handleType;
            this.targetConnections      = clientCount;
            this.showIntervalStatistics = showIntervalStatistics;

            var data = new byte[WriteBufferSize];

            this.dataBuffer = WritableBuffer.From(data);

            this.writeHandles = new StreamHandle[MaximumWriteHandles];
        }
Beispiel #7
0
        public UdpPummel(int numberOfSenders, int numberOfReceivers, int timeout)
        {
            this.numberOfSenders   = numberOfSenders;
            this.numberOfReceivers = numberOfReceivers;
            this.timeout           = timeout;

            this.senders       = new Dictionary <Udp, IPEndPoint>();
            this.receivers     = new Udp[this.numberOfReceivers];
            this.packetCounter = PacketCount;
            this.buffer        = WritableBuffer.From(Encoding.UTF8.GetBytes(ExpectedMessage));

            this.loop = new Loop();
        }
Beispiel #8
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);
        }
    private void OnAccept(Tcp client, ReadableBuffer data)
    {
        if (data.Count == 0)
        {
            log($"server OnAccept: data count is 0");
            return;
        }

        ////log($"server read {data.Count}");
        Interlocked.Increment(ref this.receiveCount);

        // Echo back
        var buffer = new byte[data.Count];

        data.ReadBytes(buffer, buffer.Length);
        data.Dispose();

        var writableBuffer = WritableBuffer.From(buffer);

        client.QueueWriteStream(
            writableBuffer,
            (streamHandle, exception) =>
        {
            writableBuffer.Dispose();
            if (exception != null)
            {
                log($"server write error: {exception.Message}");
                streamHandle.CloseHandle(h => h.Dispose());
            }
            else
            {
                client.OnRead(
                    this.OnAccept,
                    (_h, _e) => { log($"read error {_e.Message}"); });
            }
        });

        ////log($"server wrote {buffer.Length}");
    }
Beispiel #10
0
    private void OnAccept(StreamHandle stream, ReadableBuffer data)
    {
        if (data.Count == 0)
        {
            log($"client OnAccept: data count is 0");
            return;
        }

        ////log($"client accept {data.Count}");

        // Echo back
        var buffer = new byte[data.Count];

        data.ReadBytes(buffer, buffer.Length);
        data.Dispose();

        var writableBuffer = WritableBuffer.From(buffer);

        stream.QueueWriteStream(
            writableBuffer,
            (handle, exception) =>
        {
            writableBuffer.Dispose();
            if (exception != null)
            {
                log($"client write error: {exception.Message}");
                handle.CloseHandle(h => h.Dispose());
            }
            else
            {
                stream.OnRead(
                    this.OnAccept,
                    (_h, _e) => { log($"read error {_e.Message}"); });
            }
        });

        ////log($"client wrote {buffer.Length}");
    }
Beispiel #11
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);
            }
        }
Beispiel #12
0
        public void Run()
        {
            this.closedStreams     = 0;
            this.connectionsFailed = 0;

            this.server = new EchoServer(this.handleType);
            this.loop   = this.server.Loop;

            this.buffer = WritableBuffer.From(Encoding.UTF8.GetBytes(Message));

            this.loop.UpdateTime();
            this.start = this.loop.Now;

            this.startTime = this.loop.NowInHighResolution;
            this.StartClients();
            this.activeStreams = this.clientCount;

            this.loop.RunDefault();

            double duration = (this.stopTime - this.startTime) / (double)NanoSeconds;
            double value    = this.closedStreams / duration;

            Console.WriteLine($"{this.handleType} conn pound : {this.clientCount} {TestHelper.Format(value)} accepts/s ({TestHelper.Format(this.connectionsFailed)} failed)");
        }
Beispiel #13
0
 public TcpWriteBatch()
 {
     byte[] bytes = Encoding.UTF8.GetBytes(Content);
     this.dataBuffer = WritableBuffer.From(bytes);
     this.writeCount = 0;
 }
Beispiel #14
0
 public TcpPingPong()
 {
     byte[] content = Encoding.UTF8.GetBytes(PingMessage + SplitToken);
     this.dataBuffer = WritableBuffer.From(content);
 }