Exemple #1
0
        private async Task PublishDataToTrafficReaderAsync(TcpDataReader trafficReader)
        {
            try
            {
#if NETSTANDARD2_1
                var buffer = await trafficReader.AllocateBufferToWriteAsync(_cancellationToken.Token);

                var readSize =
                    await SocketStream.ReadAsync(buffer, _cancellationToken.Token);


                while (readSize > 0)
                {
                    SocketStatistic.WeHaveReceiveEvent(readSize);

                    trafficReader.CommitWrittenData(readSize);

                    buffer = await trafficReader.AllocateBufferToWriteAsync(_cancellationToken.Token);

                    readSize =
                        await SocketStream.ReadAsync(buffer, _cancellationToken.Token);
                }
#else
                var buffer = await trafficReader.AllocateBufferToWriteLegacyAsync(_cancellationToken.Token);

                var readSize =
                    await SocketStream.ReadAsync(buffer.buffer, buffer.start, buffer.len, _cancellationToken.Token);

                while (readSize > 0)
                {
                    SocketStatistic.WeHaveReceiveEvent(readSize);

                    trafficReader.CommitWrittenData(readSize);

                    buffer = await trafficReader.AllocateBufferToWriteLegacyAsync(_cancellationToken.Token);

                    readSize =
                        await SocketStream.ReadAsync(buffer.buffer, buffer.start, buffer.len, _cancellationToken.Token);
                }
#endif
            }
            catch (Exception e)
            {
                Log.InvokeExceptionLog(this, e, true);
            }
            finally
            {
                Log.InvokeInfoLog(this, "Disconnected from Traffic Reader Loop");
                trafficReader.Stop();
            }
        }
        public static async Task NewPackageAsync(this TcpDataReader tcpDataReader, ReadOnlyMemory <byte> bytes)
        {
            var buf = await tcpDataReader.AllocateBufferToWriteAsync(CancellationToken.None);

            bytes.CopyTo(buf);
            tcpDataReader.CommitWrittenData(bytes.Length);
        }
Exemple #3
0
        public static async Task NewPackageAsync(this TcpDataReader tcpDataReader, byte[] data)
        {
            var remainSize = data.Length;
            var pos        = 0;

            while (remainSize > 0)
            {
                var cancellationToken = new CancellationTokenSource();
                var buf = await tcpDataReader.AllocateBufferToWriteAsync(cancellationToken.Token);

                var copySize = buf.Length < remainSize ? buf.Length : remainSize;

                new ReadOnlyMemory <byte>(data, pos, copySize).CopyTo(buf);
                tcpDataReader.CommitWrittenData(copySize);
                pos        += copySize;
                remainSize -= copySize;
            }
        }