Example #1
0
        public void Combine()
        {
            _bytes = GetBytes(_msg);
            var byte1 = new byte[2 * _bytes.Length];

            _bytes.CopyTo(byte1, 0);
            _bytes.CopyTo(byte1, _bytes.Length);
            var y = new ArraySegment <byte>(byte1);
            var x = new BufferList {
                y
            };
            var count = x.Total;
            var r     = _receiveFilter.Filter(x, out _rest);

            Assert.Equal(r.Body, _msg, _compare);
            Assert.NotEqual(_rest, count);
            var a = new ArraySegment <byte>(y.Array, y.Offset + y.Count - _rest, _rest);
            var z = new BufferList {
                a
            };

            r = _receiveFilter.Filter(z, out _rest);
            Assert.Equal(r.Body, _msg, _compare);
            Assert.Equal(_rest, 0);
        }
        public void Combine()
        {
            _bytes = GetBytes(_msg);
            var bytes = new byte[409600];
            var count = _bytes.Length;

            Array.Copy(_bytes, 0, bytes, 405504, count);
            Array.Copy(_bytes, 0, bytes, 405504 + count, count);
            var r = _receiveFilter.Filter(bytes, 405504, 2 * count, true, out _rest);

            Assert.Equal(r.Body, _msg, _compare);
            Assert.NotEqual(_rest, 0);
            r = _receiveFilter.Filter(bytes, 405504 + _rest, _rest, true, out _rest);
            Assert.Equal(r.Body, _msg, _compare);
            Assert.Equal(_rest, 0);
        }
        protected override void ClientOnDataReceived(object sender, DataReceivedEventArgs e)
        {
            var filter = new ReceiveFilter();
            var info   = filter.Filter(e.Data, 0, e.Data.Length, true, out int rest);

            using (var scope = Applibs.AutofacConfig.Container.BeginLifetimeScope())
            {
                var cmd    = scope.ResolveKeyed <IServerCommand>(info.Type);
                var result = cmd.Excute(info);
                if (result.Item1 != null)
                {
                    Console.WriteLine($"Command:{info.Type} Content:{info.Body} Execute Fail:{result.Item1.Message}");
                }

                if (!this.timer.Enabled && info.Type == KeyType.LoginSuccess)
                {
                    //// 啟動心跳包
                    this.timer.Start();
                    IsLogin = true;
                }
            }
        }
Example #4
0
        private bool ReaderBuffer(ref ReadOnlySequence <byte> buffer, out SequencePosition consumed, out SequencePosition examined)
        {
            consumed = buffer.Start;
            examined = buffer.End;

            var bytesConsumedTotal = 0L;

            var maxPackageLength = ClientOptions.MaxRequestLength;
            var seqReader        = new SequenceReader <byte>(buffer);

            while (true)
            {
                //过滤解析
                if (ReceiveFilter != null)
                {
                    var packageInfo   = ReceiveFilter.Filter(ref seqReader);
                    var bytesConsumed = seqReader.Consumed;
                    bytesConsumedTotal += bytesConsumed;

                    var len = bytesConsumed;

                    // nothing has been consumed, need more data
                    if (len == 0)
                    {
                        len = seqReader.Length;
                    }

                    if (maxPackageLength > 0 && len > maxPackageLength)
                    {
                        HandleLog.WriteLine($"Package cannot be larger than {maxPackageLength}.");
                        // close the the connection directly
                        Close();
                        return(false);
                    }

                    // continue receive...
                    if (packageInfo == null)
                    {
                        consumed = buffer.GetPosition(bytesConsumedTotal);
                        return(true);
                    }
                    if (!packageInfo.Success)
                    {
                        HandleLog.WriteLine(packageInfo.Message);
                    }
                    else
                    {
                        OnReceived?.Invoke(Socket, packageInfo);
                    }

                    if (seqReader.End) // no more data
                    {
                        examined = consumed = buffer.End;
                        return(true);
                    }

                    seqReader = new SequenceReader <byte>(seqReader.Sequence.Slice(bytesConsumed));
                }
                else
                {
                    examined = consumed = buffer.End;
                    var packageInfo = new TRequestInfo()
                    {
                        Success = true,
                        Raw     = buffer.ToArray()
                    };
                    OnReceived?.Invoke(Socket, packageInfo);
                    return(true);
                }
            }
        }