Esempio n. 1
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;
                }
            }
        }
Esempio n. 2
0
        async Task Process(AllocType at, Socket client, Action <int> onmessage, bool stop = false)
        {
            Print.AsInfo(at.ToString() + Environment.NewLine);

            try
            {
                Print.AsInfo("New client" + Environment.NewLine);

                IMemoryHighway hw    = null;
                var            lanes = new int[] { 1025, 2048 };

                switch (at)
                {
                case AllocType.Heap:
                    hw = new HeapHighway(lanes);
                    break;

                case AllocType.MMF:
                    hw = new MappedHighway(lanes);
                    break;

                case AllocType.Marshal:
                    hw = new MarshalHighway(lanes);
                    break;

                default:
                    throw new ArgumentNullException();
                }

                using (hw)
                    using (var ns = new NetworkStream(client))
                    {
                        var header   = new byte[4];
                        var spoon    = new byte[16000];
                        var total    = 0;
                        var read     = 0;
                        var frameLen = 0;

                        while (!stop)
                        {
                            total = 0;
                            read  = await ns.ReadAsync(header, 0, 4).ConfigureAwait(false);

                            Print.AsInfo("Received header bytes: {0}.{1}.{2}.{3}", header[0], header[1], header[2], header[3]);

                            // The other side is gone.
                            // As long as the sender is not disposed/closed the ReadAsync will wait
                            if (read < 1)
                            {
                                Print.AsError("The client is gone.");
                                break;
                            }

                            frameLen = BitConverter.ToInt32(header, 0);

                            if (frameLen < 1)
                            {
                                Print.AsError("Bad header, thread {0}", Thread.CurrentThread.ManagedThreadId);
                                break;
                            }

                            using (var frag = hw.AllocFragment(frameLen))
                            {
                                Print.AsInfo("Frame length:{0}", frameLen);

                                // The sip length guards against jumping into the next frame
                                var sip = 0;
                                while (total < frameLen && !stop)
                                {
                                    sip = frameLen - total;
                                    if (sip > spoon.Length)
                                    {
                                        sip = spoon.Length;
                                    }

                                    // the read amount could be smaller than the sip
                                    read = await ns.ReadAsync(spoon, 0, sip).ConfigureAwait(false);

                                    frag.Write(spoon, total, read);
                                    total += read;

                                    Print.AsInnerInfo("read {0} on thread {1}", read, Thread.CurrentThread.ManagedThreadId);

                                    if (total >= frameLen)
                                    {
                                        onmessage?.Invoke(total);
                                        break;
                                    }
                                }
                            }
                        }
                    }
            }
            catch (MemoryLaneException mex)
            {
                Print.AsError(mex.Message);
                Print.AsError(mex.ErrorCode.ToString());
                Print.AsError(mex.StackTrace);
            }
            catch (Exception ex)
            {
                Print.AsError(ex.Message);
            }
        }