Ejemplo n.º 1
0
        public async Task ReceiveLoop()
        {
            await Task.Delay(0);

            Print.AsInfo("SocketLoad server started.");
            var data = new List <byte>();

            server.Listen(100);

            var end = Task.Run(() => Console.ReadLine());

            Print.AsWarn("The server will listen indefinitely, press <Enter> to stop accepting clients and quit.");

            while (true)
            {
                var accept = server.AcceptAsync();
                accept.ConfigureAwait(false);
                if (Task.WaitAny(end, accept) == 1)
                {
                    var client = accept.Result;
                    new Task(() =>
                             Process(AllocType.Heap, client, (len) => Print.AsSuccess(
                                         "{0} bytes received from {1}.",
                                         len,
                                         ((IPEndPoint)client.RemoteEndPoint).Port.ToString())).Wait()
                             ).Start();
                }
                else
                {
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        public async Task Send(int msgCount, int size, bool randomSize, int sleepms = 0)
        {
            Print.AsInfo("SocketLoad client Send()");
            Print.AsWarn("Do not close the client even if all data seems to be sent, this will tear down the connection");

            var ns       = client.GetStream();
            var rdm      = new Random();
            var sendBuff = new Memory <byte>(new byte[size + 4]);
            var header   = sendBuff.Slice(0, 4);

            byte v = 0;

            for (int i = 4; i < sendBuff.Length; i++)
            {
                sendBuff.Span[i] = ++v;
            }

            for (; msgCount > 0; msgCount--)
            {
                var len = randomSize ? rdm.Next(1024, size) : size;
                if (BitConverter.TryWriteBytes(header.Span, len))
                {
                    var hs = header.Span.ToArray();
                    Print.AsInfo("Frame length: {0}", len);
                    Print.AsInfo("Frame header bytes: {0}.{1}.{2}.{3}", hs[0], hs[1], hs[2], hs[3]);
                    Print.AsInfo("Header value double check: {0} ", BitConverter.ToInt32(hs, 0));
                    if (sleepms > 0)
                    {
                        await Task.Delay(sleepms);
                    }
                    await ns.WriteAsync(sendBuff.Slice(0, len + 4));
                }
                else
                {
                    Print.AsError("Can't set a header");
                    break;
                }
            }
        }