示例#1
0
        public void Respond <T>(T obj)
        {
            //Serialize
            string data = RSN_Tools.SerializeObject(obj);

            //Create a response packet and send.
            RSN_Server.SendDataToClient(client.stream, data, packet.id, packet.parseType, type: RSN_PacketType.EncodedMessage);
        }
        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));
        }
示例#3
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;
            }
        }
示例#4
0
 public int SendData <T>(RSN_ClientResponse callback, T data)
 {
     try
     {
         int msgId = currentMessage;
         currentMessage++;
         //Add the callback to the dictonary
         callbacks.Add(msgId, callback);
         //serialize data
         string dataString = RSN_Tools.SerializeObject(data);
         int    type       = 0;
         try
         {
             //Reverse lookup the dictonary so we know what type this is.
             type = registeredDataTypes.FirstOrDefault(x => x.Value == data.GetType()).Key;
         }
         catch
         {
             //Bad type.
             throw new Exception("Bad type when sending data! Make sure it is registered.");
         }
         if (type == 0)
         {
             throw new Exception("Bad type when sending data! Make sure it is registered.");
         }
         //Create a packet
         RSN_Packet packet = new RSN_Packet(token, RSN_PacketType.EncodedMessage, msgId, dataString, type);
         //Submit
         RawWrite(packet.EncodePacket());
         //return id
         return(msgId);
     } catch (Exception ex)
     {
         OnError(RSN_Exception_ErrorType.Exception, "", ex);
         return(-1);
     }
 }
示例#5
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);
        }
示例#6
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);
            }
        }