Beispiel #1
0
        /// <summary>
        /// Registers a packet type.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        public void Subscribe <P>(Action <P> callback)
            where P : Packet, new()
        {
            var info = PacketManager.GetInformation(typeof(P));

            List <Action <byte[]> > handlers = null;

            if (this.handlers.TryGetValue(info.Id, out handlers))
            {
                handlers.Add(new Action <byte[]>((data) =>
                {
                    P packet = info.Constructor() as P;
                    if (packet != null)
                    {
                        packet.Read(data);
                    }
                    callback(packet);
                }));
            }
            else
            {
                var list = new List <Action <byte[]> >();
                list.Add(new Action <byte[]>((data) =>
                {
                    P packet = info.Constructor() as P;
                    if (packet != null)
                    {
                        packet.Read(data);
                    }
                    callback(packet);
                }));
                this.handlers.Add(info.Id, list);
            }
        }
Beispiel #2
0
 /// <summary>
 /// Reads the packet from a buffer.
 /// </summary>
 /// <param name="buffer"></param>
 public void Read(byte[] buffer)
 {
     using (MemoryStream stream = new MemoryStream(buffer))
     {
         using (BinaryReader reader = new BinaryReader(stream, Encoding.UTF8))
         {
             PacketInfo info = PacketManager.GetInformation(this.GetType());
             info.Reader(reader, this);
         }
     }
 }
Beispiel #3
0
 /// <summary>
 /// Writes the packet to a buffer.
 /// </summary>
 /// <returns></returns>
 public byte[] Write()
 {
     using (MemoryStream stream = new MemoryStream())
     {
         using (BinaryWriter writer = new BinaryWriter(stream))
         {
             PacketInfo info = PacketManager.GetInformation(this.GetType());
             info.Writer(writer, this);
             writer.Flush();
             return(stream.ToArray());
         }
     }
 }
Beispiel #4
0
        /// <summary>
        /// Registers a packet type.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        public void Register <P>(Action <T, P> callback)
            where P : Packet, new()
        {
            var info = PacketManager.GetInformation(typeof(P));

            if (this.handlers.ContainsKey(info.Id))
            {
                throw new Exception("Packet handler already registered.");
            }
            this.handlers.Add(info.Id, new Action <T, byte[]>((p, data) =>
            {
                P packet = info.Constructor() as P;
                if (packet != null)
                {
                    packet.Read(data);
                }
                callback(p, packet);
            }));
        }