Beispiel #1
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;
            }
        }
Beispiel #2
0
            new Dictionary <int, Type>(); //Stores data parse types.

        public static RSN_Client Connect(RSN_Client_CallbackConfig[] callbacks, string password, string ip, Int32 port, RSN_Error onError = null)
        {
            //Create client
            TcpClient client = new TcpClient(ip, port);

            //Create class
            RSN_Client output = new RSN_Client();

            output.client        = client;
            output.stream        = client.GetStream();
            output.networkReader = new BinaryReader(output.stream);
            output.networkWriter = new BinaryWriter(output.stream);
            output.errorCallback = onError;

            //Add data parse types
            foreach (RSN_Client_CallbackConfig c in callbacks)
            {
                if (c.id < 1)
                {
                    throw new Exception("Bad id: Must be greater than zero.");
                }
                output.registeredDataTypes.Add(c.id, c.type);
            }

            //Begin thread
            var getThread = new System.Threading.Thread(new System.Threading.ThreadStart(output.GetThread));

            getThread.Start();

            //Log into the server
            //Create class
            RSN_AuthPacketType authPacket = new RSN_AuthPacketType();

            authPacket.password = password;
            RSN_Packet packet = new RSN_Packet(output.token, RSN_PacketType.Auth, output.currentMessage, RSN_Tools.SerializeObject(authPacket), 0);

            output.currentMessage++;
            //Write
            output.RawWrite(packet.EncodePacket());
            //When we get a response for that, we'll set our token.

            return(output);
        }
Beispiel #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);
            }
        }