Beispiel #1
0
        public void SendSerialized(ISerializablePacket Packet)
        {
            PacketOutStream Out = PacketProcessor.WritePacket(Packet);

            byte[] ToSend = Out.ToArray();
            SendTCP(ToSend);
            Out.Dispose();
        }
Beispiel #2
0
        public void SendTCPWithSize(byte[] ToSend)
        {
            PacketOutStream St = new PacketOutStream();

            St.WriteEncoded7Bit(ToSend.Length);
            St.Write(ToSend);

            SendTCP(St.ToArray());
        }
        public static bool WriteField(ref PacketOutStream Stream, ISerializableField Field, int FieldIndex, int FieldType)
        {
            if (FieldType == (int)EPacketFieldType.Invalid)
            {
                return(false);
            }

            PacketOutStream NewStream = new PacketOutStream();

            long FieldResult;

            PacketOutStream.Encode2Parameters(out FieldResult, FieldType, FieldIndex);
            NewStream.WriteEncoded7Bit(FieldResult);

            if (Field == null || Field.Serialize(ref NewStream, false))
            {
                Stream.Write(NewStream.ToArray());
            }

            return(true);
        }
        public static bool WritePacket(ref PacketOutStream Stream, ISerializablePacket Packet, bool WithSize = true, bool WithTerminator = true, bool WithOpcode = true)
        {
            if (Packet == null)
            {
                return(false);
            }

            lock (Packet)
            {
                Packet.ConvertToField();

                PacketOutStream Data = new PacketOutStream();

                if (WithOpcode)
                {
                    Data.WriteEncoded7Bit(Packet.Opcode);
                }

                foreach (ISerializableField Field in Packet.GetFields().Values)
                {
                    WriteField(ref Data, Field);
                }

                if (WithTerminator && Packet.Opcode != (int)Opcodes.ProtocolHandshakeCompression)
                {
                    WriteField(ref Data, null, 0, (int)EPacketFieldType.Terminator);
                }

                if (WithSize)
                {
                    Stream.WriteEncoded7Bit(Data.Length);
                }

                Stream.Write(Data.ToArray());

                Packet.Fields.Clear();
            }

            return(true);
        }
        public static byte[] FieldToBytes(ISerializablePacket Packet, string FieldName)
        {
            FieldInfo Info = Packet.GetType().GetField(FieldName);

            ISerializableFieldAttribute[] FieldsAttr = Info.GetCustomAttributes(typeof(ISerializableFieldAttribute), true) as ISerializableFieldAttribute[];
            if (FieldsAttr != null && FieldsAttr.Length > 0)
            {
                ISerializableField Field = Activator.CreateInstance(FieldsAttr[0].GetSerializableType()) as ISerializableField;
                Field.Index      = FieldsAttr[0].Index;
                Field.val        = Info.GetValue(Packet);
                Field.PacketType = PacketProcessor.GetFieldType(Field);

                PacketOutStream Str = new PacketOutStream();
                Field.Serialize(ref Str, true);
                byte[] Result = Str.ToArray();
                return(Result);
            }
            else
            {
                return(null);
            }
        }