Ejemplo n.º 1
0
        public static void Process(ClientSocket userSocket, byte[] packet)
        {
            var msgLogin = (MsgLogin)packet;
            var username = msgLogin.GetUsername();
            var password = msgLogin.GetPassword();

            userSocket.UseCompression = msgLogin.ClientSupportCompression;
            FConsole.WriteLine($"MsgLogin: {username} with password {password} (compress: {userSocket.UseCompression}) requesting login.");

            var user = new User
            {
                Socket   = userSocket,
                Username = username,
                Password = password
            };

            user.Socket.OnDisconnect += user.OnDisconnect;
            user.Socket.StateObject   = user;

            if (Core.Db.Authenticate(username, password))
            {
                Core.Db.LoadUser(ref user);
            }
            else
            {
                Core.Db.AddUser(user);
            }

            msgLogin.UniqueId = user.Id;
            Collections.OnlineUsers.TryAdd(user.Id, user);
            user.Send(msgLogin, true);

            var userInfoPacket = MsgUser.Create(user.Id, user.Nickname, user.AvatarUrl, user.Email, true);

            foreach (var friendId in user.Friends)
            {
                var friend = Oracle.GetUserFromId(friendId);
                if (friend.Socket != null && friend.Socket.IsConnected)
                {
                    friend.Send(userInfoPacket);
                }
            }
            user.Send(userInfoPacket);
            FConsole.WriteLine($"MsgLogin Deserializing & Processing took {((((float)PacketRouter.Stopwatch.ElapsedTicks) / Stopwatch.Frequency) * 1000000):0} microsec");
        }
Ejemplo n.º 2
0
        public static void Process(User user, byte[] packet)
        {
            var msg = (MsgDataRequest)packet;

            FConsole.WriteLine($"MsgDataRequest: {Oracle.GetUserFromId(msg.UserId).Username} requests {msg.Type} on {msg.TargetId} with param {msg.Param}");
            if (user == null)
            {
                return;
            }
            switch (msg.Type)
            {
            case MsgDataRequestType.Friends:
                foreach (var friendId in user.Friends)
                {
                    var friend = Oracle.GetUserFromId(friendId);
                    if (friend != null)
                    {
                        var online = false;
                        if (friend.Socket != null)
                        {
                            online = friend.Socket.IsConnected;
                        }

                        user.Send(MsgUser.Create(friend.Id, friend.Nickname, friend.AvatarUrl, friend.Email, online));
                    }
                }
                user.Send(msg);
                break;

            case MsgDataRequestType.VServers:
                foreach (var serverId in user.VirtualServers)
                {
                    var server = Oracle.GetServerFromId(serverId);
                    if (server != null)
                    {
                        user.Send(MsgVServer.Create(server.Id, server.Name, server.IconUrl, server.CreatedTime, server.LastActivity));
                    }
                }
                user.Send(msg);
                break;

            case MsgDataRequestType.Channels:
                var dbServer = Oracle.GetServerFromId(msg.TargetId);
                if (dbServer != null)
                {
                    foreach (var channelId in dbServer.Channels)
                    {
                        user.Send(MsgChannel.Create(channelId.Key, dbServer.Id, channelId.Value.Name));
                    }
                }

                user.Send(msg);
                break;

            case MsgDataRequestType.Messages:
                var dbChannel = Oracle.GetServerChannelFromId(msg.TargetId, msg.Param);
                if (dbChannel != null)
                {
                    foreach (var(_, message) in dbChannel.Messages)
                    {
                        user.Send(MsgText.Create(message.Id, message.AuthorId, message.Text, msg.TargetId, dbChannel.Id, message.Timestamp));
                    }
                }

                user.Send(msg);
                break;

            default:
                FConsole.WriteLine("Invalid packet received from " + user.Username);
                user.Socket.Disconnect("Server.Networking.PacketRouter.ProcessDataRequest() Invalid packet received");
                break;
            }

            FConsole.WriteLine($"MsgDataRequest Deserializing & Processing took {((((float)PacketRouter.Stopwatch.ElapsedTicks) / Stopwatch.Frequency) * 1000000):0} microsec");
        }