/// <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); }
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()); } }
/// <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); } }
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); }
/// <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); }
/// <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; }
/// <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)); }
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; }
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); }
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); }