コード例 #1
0
        private void HandlePacketFromServer(byte[] data, TcpClient client)
        {
            //HandlersEvents.Add(0x8c, this._0x8CConnectToGameServer);
            UOStream Data = new UOStream(data);
            Packet packet = new Packet();
            if (data == null) {
                return; }
            if (data.Length < 1) {
                return;
            }
            Data.Position = 0;
            while (Data.Position < Data.Length - 1)
            {
                if (HandlersServer.ContainsKey(Data.PeekBit()))
                {
                    var olddatapos = Data.Position;
                    packet = (Packet)Activator.CreateInstance(HandlersServer[Data.PeekBit()], new object[] { Data });
                    //Logger.Log(packet.OpCode.ToString("x") + "  Handled from Server");
                    Logger.Log(BitConverter.ToString(packet.PacketData, (int)olddatapos, (int)(Data.Position - olddatapos)) + "  Handled from Server");
                    var eventinfo = this.GetType().GetField(packet.GetType().Name, BindingFlags.Instance
                        | BindingFlags.NonPublic);

                    if (eventinfo != null)
                    {
                        var member = eventinfo.GetValue(this);
                        if (member != null)
                        {
                            //Logger.Log(member.ToString());
                            member.GetType().GetMethod("Invoke").Invoke(member, new object[] { packet });
                        }
                        else
                        {
                            //Logger.Log("MEMBER WAS NULL FOR EVENT: " + eventinfo.Name);
                        }

                    }
                    else
                    {
                        //Logger.Log("EVENTFIELD WAS NULL FOR PACKET : " + packet.ToString());
                    }
                }
                else
                {
                    Logger.Log(Data.PeekBit().ToString("x") + BitConverter.ToString(Data.ToArray(), (int)Data.Position, (int)(Data.Length-Data.Position)) + "No Server Handler");
                    break;
                }
                if (Data.Position < Data.Length -1)
                {

                    byte[] tempdata = new byte[Data.Length - Data.Position];
                    Array.Copy(Data.ToArray(), Data.Position, tempdata, 0, Data.Length - Data.Position);
                    Data = new UOStream(tempdata);
                    Logger.Log("IT HAPPENED! Data left after parsing packet" + BitConverter.ToString(tempdata, 0, (int)tempdata.Length));
                    // this should never happen
                }

            }

            return;
        }
コード例 #2
0
        private void HandleClientPacket(byte[] data, int bytesRead)
        {
            UOStream Data = new UOStream(data);
            Packet packet;
            while (Data.Position < Data.Length - 1)
            {
                if (HandlersClient.ContainsKey(Data.PeekBit()))
                {
                    var olddatapos = Data.Position;
                    packet = (Packet)Activator.CreateInstance(HandlersClient[Data.PeekBit()], new object[] { Data });
                    //Logger.Log(packet.OpCode.ToString("x") + "  Handled from Client ");
                    Logger.Log(BitConverter.ToString(packet.PacketData, (int)olddatapos, (int)(Data.Position - olddatapos)) + " len:" + (Data.Position - olddatapos) + "  Handled from Client ");
                    var eventinfo = this.GetType().GetField("Client" + packet.GetType().Name, BindingFlags.Instance
                        | BindingFlags.NonPublic);

                    if (eventinfo != null)
                    {
                        var member = eventinfo.GetValue(this);
                        if (member != null)
                        {
                            //Logger.Log(member.ToString());
                            member.GetType().GetMethod("Invoke").Invoke(member, new object[] { packet });
                        }
                        else
                        {
                            //Logger.Log("MEMBER WAS NULL FOR EVENT: " + eventinfo.Name);
                        }

                    }
                    else
                    {
                        // Logger.Log("EVENTFIELD WAS NULL FOR PACKET : " + packet.ToString());
                    }

                }
                else
                {
                    Logger.Log(Data.PeekBit().ToString("x") + BitConverter.ToString(data, (int)Data.Position, (int)(Data.Length - Data.Position)) + "No Client Handler Discarding");
                    break;
                }
            }
        }