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 OnNewClient(System.IAsyncResult result)
        {
            TcpListener listener = (TcpListener)result.AsyncState;
            TcpClient   client   = listener.EndAcceptTcpClient(result);
            //Found client. Add it to the list.
            var c = new RSN_Server_Client(client);

            clients.Add(c);
            //Wait again
            //Look for new clients
            server.BeginAcceptTcpClient(new AsyncCallback(OnNewClient), server);
        }
Exemple #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;
            }
        }
Exemple #4
0
 public RSN_ServerResponse_Data(RSN_Packet _packet, RSN_Server_Client _client, Type _type, int _id)
 {
     t      = _type;
     client = _client;
     packet = _packet;
 }