Exemplo n.º 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);
        }
Exemplo n.º 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;
            }
        }
Exemplo n.º 3
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);
     }
 }
Exemplo n.º 4
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);
        }