public static async Task Invoke(Packet reader, Session session)
        {
            var message = reader.Header.Message;

            Tuple<MethodInfo, Type> data;

            if (MessageHandlers.TryGetValue(message, out data))
            {
                var handlerObj = Activator.CreateInstance(data.Item2) as ClientPacket;

                handlerObj.Packet = reader;

                await Task.Run(() => handlerObj.Read());

                // Fix the position after the last read.
                reader.Read(1);

                if (handlerObj.IsReadComplete)
                    data.Item1.Invoke(null, new object[] { handlerObj, session });
                else
                    Log.Error($"Packet read for '{data.Item2.Name}' failed.");
            }
            else
            {
                var msgName = Enum.GetName(typeof(ClientMessage), message) ?? Enum.GetName(typeof(GlobalClientMessage), message);

                if (msgName == null)
                    Log.Network($"Received unknown opcode '0x{message:X}, Length: {reader.Data.Length}'.");
                else
                    Log.Network($"Packet handler for '{msgName} (0x{message:X}), Length: {reader.Data.Length}' not implemented.");
            }

        }
        public static async void HandleCharacterList(CharacterList characterList, Session session)
        {
            var accountEntitlementUpdate = new AccountEntitlementUpdate();

            session.Account.Entitlements.ForEach(e => accountEntitlementUpdate.Entitlements.Add((Entitlement)e.Id, e.Value));

            await session.Send(accountEntitlementUpdate);

            // Empty character list for now.
            await session.Send(new Server.Character.CharacterList());
        }
        public static void HandleGatewayRequest(GatewayRequest gatewayRequest, Session session)
        {
            Log.Debug($"Got new connection for Account '{gatewayRequest.LoginName}'.");

            session.Account = DB.Auth.Single<Account>(a => a.Email == gatewayRequest.LoginName && a.GatewayTicket != "");

            if (session.Account != null)
                session.Crypt = new Framework.Cryptography.PacketCrypt(gatewayRequest.GatewayTicket);
            else
            {
                Log.Debug($"Connection for Account '{gatewayRequest.LoginName}' can't be validated.");

                session.Dispose();
            }
        }
        public static async void HandleRealmListChanged(RealmListChanged realmListChanged, Session session)
        {
            var realms = DB.Auth.Select<Realm>();
            var realmList = new RealmList();

            realms.ForEach(r =>
            {
                realmList.Realms.Add(new RealmListEntry
                {
                    Id         = r.Id,
                    Name       = r.Name,
                    Type       = r.Type,
                    Status     = r.Status,
                    Population = r.Population
                });
            });

            await session.Send(realmList);
        }
 public static async void HandleRequestRealmTransfer(RequestRealmTransfer requestRealmTransfer, Session session)
 {
     await session.Send(new RealmTransfer());
     await session.Send(new Server.Character.CharacterList());
 }