Exemplo n.º 1
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);
            }));
        }