public Type deserializeAs;          //The data type to deserialize this as.

        public void OnEvent(string raw, RSN_Server_Client client, RSN_Packet packet)
        {
            //Deserialize this
            object des = RSN_Tools.DeserializeObject(raw, deserializeAs);

            //Call callback
            callback(des, new RSN_ServerResponse_Data(packet, client, deserializeAs, packet.parseType));
        }
Exemple #2
0
        private void OnGotData(RSN_Server_Client client, RSN_Packet packet)
        {
            //Called on new data.
            switch (packet.type)
            {
            case RSN_PacketType.EncodedMessage:
                //Handle.
                //Verify token. If the token is incorrect, ignore the packet.
                if (!VerifyToken(packet.token))
                {
                    //Incorrect. Send a packet telling the user this.
                    SendDataToClient(client.stream, "{}", packet.id, 0, "                ", RSN_PacketType.AuthFail);
                    return;
                }
                //Get the callback and type.
                RSN_Server_CallbackConfig conf = null;
                try
                {
                    conf = registeredDataTypes[packet.parseType];
                }
                catch
                {
                    //Bad. Ignore!
                    return;
                }
                conf.OnEvent(packet.body, client, packet);
                break;

            case RSN_PacketType.Auth:
                //Trying to auth. Check the password.
                RSN_AuthPacketType auth  = (RSN_AuthPacketType)RSN_Tools.DeserializeObject(packet.body, typeof(RSN_AuthPacketType));
                string             token = RSN_Tools.GenerateRandomString(16);
                if (auth.password == password)
                {
                    //Ok!
                    auth.wasAuthOkay = true;
                    registeredTokens.Add(token);
                    auth.token = token;
                }
                else
                {
                    //Bad!
                    auth.wasAuthOkay = false;
                    auth.token       = "                ";
                }
                //Respond
                string raw = RSN_Tools.SerializeObject(auth);
                SendDataToClient(client.stream, raw, packet.id, 0, token, RSN_PacketType.Auth);
                break;
            }
        }
Exemple #3
0
        private void OnGotMessage(RSN_Packet packet)
        {
            if (packet.type == RSN_PacketType.AuthFail)
            {
                //Error.
                OnError(RSN_Exception_ErrorType.AuthError, "Token provided wasn't accepted by the server. Maybe you're sending requests before the server has served the token?");
                return;
            }

            try
            {
                switch (packet.type)
                {
                case RSN_PacketType.EncodedMessage:
                    //Find callback
                    RSN_ClientResponse callback = null;
                    try
                    {
                        //Get the callback
                        callback = callbacks[packet.id];
                    }
                    catch
                    {
                        //Wasn't found. Ignore.
                        break;
                    }
                    //Find the data type we should parse this as.
                    Type type = null;
                    try
                    {
                        type = registeredDataTypes[packet.parseType];
                    }
                    catch
                    {
                        //Bad! Ignore
                        return;
                    }
                    Type t = type;
                    //Deserialize
                    object obj = RSN_Tools.DeserializeObject(packet.body, t);
                    callback(obj);
                    break;

                case RSN_PacketType.Auth:
                    //Auth message. Check if login was okay, then set the token or fail
                    RSN_AuthPacketType auth = (RSN_AuthPacketType)RSN_Tools.DeserializeObject(packet.body, typeof(RSN_AuthPacketType));
                    if (auth.wasAuthOkay)
                    {
                        //OK!
                        token  = auth.token;
                        isAuth = true;
                    }
                    else
                    {
                        //Bad...
                        OnError(RSN_Exception_ErrorType.AuthError, "Couldn't be authorized with the server. Check the password.");
                    }
                    break;
                }
            } catch (Exception ex)
            {
                OnError(RSN_Exception_ErrorType.Exception, "Failed to process request. The request goes as follows: '" + packet.body + "'.", ex);
            }
        }