예제 #1
0
        /// <summary>解码</summary>
        /// <param name="context"></param>
        /// <param name="pk"></param>
        /// <returns></returns>
        protected override IList <IMessage> Decode(IHandlerContext context, Packet pk)
        {
            var ss = context.Owner as IExtend;
            var pc = ss["Codec"] as PacketCodec;

            if (pc == null)
            {
                ss["Codec"] = pc = new PacketCodec {
                    GetLength = DefaultMessage.GetLength
                }
            }
            ;

            var pks  = pc.Parse(pk);
            var list = pks.Select(e =>
            {
                var msg = new DefaultMessage();
                if (!msg.Read(e))
                {
                    return(null);
                }

                return(msg as IMessage);
            }).ToList();

            return(list);
        }
예제 #2
0
        public void TestMultiplePacket()
        {
            var codec = new PacketCodec();

            var a = new ConnectPacket();
            var b = new DisconnectPacket();

            var stream = new MemoryStream();
            var writer = new BinaryWriter(stream);
            var data   = ByteJoin.Combine(codec.Encode(a), codec.Encode(b));

            var reader = new BinaryReader(new MemoryStream(data));
            var iter   = new PacketEnumerator(reader);

            // 1st packet
            {
                Assert.True(iter.MoveNext());
                ConnectPacket p;
                Assert.True(iter.GetCurrent(out p));
                Assert.Equal(a, p);
            }

            // 2nd packet
            {
                Assert.True(iter.MoveNext());
                DisconnectPacket p;
                Assert.True(iter.GetCurrent(out p));
                Assert.Equal(b, p);
            }

            // end of stream
            {
                Assert.False(iter.MoveNext());
            }
        }
예제 #3
0
        /// <summary>解码</summary>
        /// <param name="context"></param>
        /// <param name="pk"></param>
        /// <returns></returns>
        protected override IList <Packet> Decode(IHandlerContext context, Packet pk)
        {
            var ss = context.Owner as IExtend;
            var pc = ss["Codec"] as PacketCodec;

            if (pc == null)
            {
                ss["Codec"] = pc = new PacketCodec {
                    Expire = Expire, GetLength = p => GetLength(p, Offset, Size)
                }
            }
            ;

            var pks = pc.Parse(pk);

            // 跳过头部长度
            var len = Offset + Math.Abs(Size);

            foreach (var item in pks)
            {
                item.Set(item.Data, item.Offset + len, item.Count - len);
            }

            return(pks);
        }
    }
예제 #4
0
        void HandleDisconnect(Session session, DisconnectPacket p)
        {
            if (session.State == SessionState.Disconnected)
            {
                log.Info($"already disconnected: id={session.ID}");
                return;
            }

            // 연결 종료는 소켓이 끊어질떄도 있고
            // 유저가 직접 종료시키는 경우도 있다
            // disconnect를 여러번 호출해도 꺠지지 않도록 하자
            // 연결 끊은것을 연결 종료 패킷처럼 다루면
            // 상위 레이어에서의 처리가 간단해진다
            if (session.WorldID != null)
            {
                var leave = new WorldLeavePacket();
                var codec = new PacketCodec();
                var bytes = codec.Encode(leave);
                var pair  = new ReceivedPacket <WorldLeavePacket>(session, bytes);
                HandleWorldLeave(session, pair);
            }

            log.Info($"disconnect: id={session.ID}");
            var disconnect = new DisconnectPacket();

            session.SendLazy(disconnect);

            // 연결 끊어도 된다는 표시 해두기. 언제 끊어도 상관 없어야한다
            NetworkStack.Session.CloseSessionActive(session);
        }
예제 #5
0
        /// <summary>解码</summary>
        /// <param name="context"></param>
        /// <param name="pk"></param>
        /// <returns></returns>
        protected override IList <IMessage> Decode(IHandlerContext context, Packet pk)
        {
            var ss = context.Owner as IExtend;

            if (ss["Codec"] is not PacketCodec pc)
            {
                ss["Codec"] = pc = new PacketCodec
                {
                    GetLength = DefaultMessage.GetLength,
                    Tracer    = (context.Owner as ISocket)?.Tracer
                };
            }

            var pks  = pc.Parse(pk);
            var list = pks.Select(e =>
            {
                var msg = new DefaultMessage();
                if (!msg.Read(e))
                {
                    return(null);
                }

                return(msg as IMessage);
            }).ToList();

            return(list);
        }
예제 #6
0
파일: LengthFieldCodec.cs 프로젝트: zanpy/X
        /// <summary>解码</summary>
        /// <param name="context"></param>
        /// <param name="pk"></param>
        /// <returns></returns>
        protected override IList <Packet> Decode(IHandlerContext context, Packet pk)
        {
            var ss = context.Owner as IExtend;

            if (ss["Codec"] is not PacketCodec pc)
            {
                ss["Codec"] = pc = new PacketCodec
                {
                    Expire    = Expire,
                    GetLength = p => GetLength(p, Offset, Size),
                    Offset    = Offset,
                    Tracer    = (context.Owner as ISocket)?.Tracer
                };
            }

            var pks = pc.Parse(pk);

            // 跳过头部长度
            var len = Offset + Math.Abs(Size);

            foreach (var item in pks)
            {
                item.Set(item.Data, item.Offset + len, item.Count - len);
            }

            return(pks);
        }
        public ServerNetworkManager()
        {
            this.Started     = false;
            this.StartedLock = new object();

            this.PacketCodec = new PacketCodec();
            this.Listener    = null;
            this.Clients     = new List <ClienNetworkManager>();

            this.AcceptThread  = null;
            this.NetworkThread = null;
        }
예제 #8
0
        /// <summary>解码</summary>
        /// <param name="context"></param>
        /// <param name="pk">包</param>
        /// <returns></returns>
        protected IList <Packet> Decode(IHandlerContext context, Packet pk)
        {
            var ss = context.Owner as IExtend;

            if (ss["Codec"] is not PacketCodec pc)
            {
                ss["Codec"] = pc = new PacketCodec {
                    MaxCache = MaxCacheDataLength, GetLength = GetLineLength
                }
            }
            ;

            return(pc.Parse(pk));
        }
예제 #9
0
        protected override void OnClose(CloseEventArgs e)
        {
            log.Info($"OnClose: code={e.Code}, reason={e.Reason}, wasClean={e.WasClean}");

            // 소켓이 닫히는건 아래쪽 레이어에서 감지한다
            // 상위레이어로 소켓이 닫혔다는걸 알려주기
            var p     = new DisconnectPacket();
            var codec = new PacketCodec();

            transport.Recv(codec.Encode(p));

            NetworkStack.Session.CloseSessionPassive(session);
            session = null;
        }
예제 #10
0
        public void TestEncodeDecode()
        {
            var codec = new PacketCodec();

            var a      = new WelcomePacket(12, 34);
            var stream = new MemoryStream();
            var writer = new BinaryWriter(stream);
            var data   = codec.Encode(a);

            var           reader = new BinaryReader(new MemoryStream(data));
            var           type   = codec.ReadPacketType(reader);
            WelcomePacket b;
            var           ok = codec.TryDecode(type, reader, out b);

            Assert.True(ok);
            Assert.Equal(a, b);
        }
예제 #11
0
        public void TestSinglePacket()
        {
            var codec  = new PacketCodec();
            var a      = new ConnectPacket();
            var stream = new MemoryStream();
            var writer = new BinaryWriter(stream);
            var data   = codec.Encode(a);

            var reader = new BinaryReader(new MemoryStream(data));
            var iter   = new PacketEnumerator(reader);

            Assert.True(iter.MoveNext());
            Assert.Equal((short)a.Type, iter.CurrentType);
            ConnectPacket p;

            Assert.True(iter.GetCurrent(out p));
            Assert.Equal(a, p);
        }