Exemple #1
0
        private bool proccessMessage(string uuid, WebSocket socket, string data)
        {
            try
            {
                if (uuidExists(uuid))
                {
                    WebSocketUser user;
                    if (!tryGetUser(uuid, out user))
                    {
                        return(false);
                    }
                    var packet = new WebSocketPacket(true);
                    packet.deserializeFrom(data);


                    CloudServer.GetPluginManager().proccessWSMessage(user, packet);


                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception e)
            {
                return(false);
            }
        }
 public bool trySendMessage(WebSocketPacket packet)
 {
     try
     {
         this.csocket.Send(packet.serialize());
         return(true);
     }catch
     {
         return(false);
     }
 }
Exemple #3
0
        private void _authorizeUser(string uuid, WebSocket csocket, string hash, int id)
        {
            var wuser     = new WebSocketUser(uuid, hash, csocket, id);
            var authOkMsg = new WebSocketPacket();

            foreach (WebSocketUser user in _users.Values.ToList())
            {
                if (user.hash == hash)
                {
                    user.trySendMessage((new WebSocketPacket()).set("cmd", "connection").set("status", "drop").set("msg", "Another user connected"));
                    user.tryDisconnect();
                }
            }
            var client = CloudServer.GetGame().GetClientManager().GetClientByUserID(id);

            if (client != null)
            {
                wuser.attachGameClient(client);
            }

            this._users.Add(hash, wuser);
            wuser.trySendMessage(authOkMsg.set("cmd", "auth").set("res", "ok"));
        }
 public void error(WebSocketPacket packet)
 {
     this.trySendMessage(packet);
     this.csocket.Close();
 }
Exemple #5
0
        public void _proccessMsg(string uuid, WebSocket csocket, string data)
        {
            try
            {
                var packet = new WebSocketPacket(true);
                if (!packet.deserializeFrom(data))
                {
                    csocket.Close();
                }
                else
                {
                    if (packet.get("cmd") != null && packet.get("cmd") == "auth")
                    {
                        if (packet.get("hash") == null || packet.get("hash") == "")
                        {
                            csocket.Send((new WebSocketPacket()).set("cmd", "auth").set("res", "err").set("msg", "Missig params").serialize());
                            csocket.Close();
                            return;
                        }
                        else
                        {
                            string hash = Convert.ToString(packet.get("hash"));
                            using (IQueryAdapter dbClient = CloudServer.GetDatabaseManager().GetQueryReactor())
                            {
                                dbClient.SetQuery("SELECT * FROM users WHERE ws_ticket = @hash");
                                dbClient.AddParameter("hash", hash);
                                dbClient.RunQuery();

                                var rows = dbClient.getTable().Rows;
                                if (rows.Count < 1)
                                {
                                    csocket.Send((new WebSocketPacket()).set("cmd", "auth").set("res", "err").set("msg", "Wrong token").serialize());
                                    csocket.Close();
                                    return;
                                }
                                else
                                {
                                    /*dbClient.SetQuery("UPDATE users SET ws_ticket = '' WHERE ws_ticket = @hash");
                                     * dbClient.AddParameter("hash", hash);
                                     * dbClient.RunQuery();*/

                                    var row    = rows[0];
                                    var userId = Convert.ToInt32(row["id"]);
                                    this._authorizeUser(uuid, csocket, hash, userId);
                                    if (uuidExists(uuid))
                                    {
                                        CloudServer.GetPluginManager().proccessWSOpen(_users[uuid]);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (!this.uuidExists(uuid))
                        {
                            csocket.Send((new WebSocketPacket()).set("cmd", "auth").set("res", "err").set("msg", "Authenticate firstly.").serialize());
                            csocket.Close();
                            return;
                        }
                        else
                        {
                            proccessMessage(uuid, csocket, data);
                        }
                    }
                }
            }
            catch (Exception e)
            {
            }
        }