Ejemplo n.º 1
0
    public bool ValidateToken(string token)
    {
        byte[]    tokenData = TCPHelper.MessageBytes(token);
        TcpClient client    = new TcpClient(Globals.MIDDLEWARE_IP, Globals.TOKENSYSTEM_PORT);

        //TcpClient client = new TcpClient("192.168.87.107", GlobalVariables.TOKENSYSTEM_PORT);

        client.GetStream().Write(tokenData, 0, tokenData.Length);
        //Await response from TokenSystem
        bool done = false;

        while (!done)
        {
            if (client.GetStream().DataAvailable)
            {
                Debug.Log("incoming response!");
                string response = TCPHelper.ReadStreamOnce(client.GetStream());

                HttpStatusCode responseCode = (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), response);

                if (responseCode == HttpStatusCode.OK)
                {
                    Debug.Log("VALID");
                    done = true;
                    return(true);
                }
                else
                {
                    Debug.Log("INVALID");
                    done = true;
                    return(false);
                }
            }
        }

        return(false);
    }
Ejemplo n.º 2
0
    private bool RequestCharacterCreation(TcpClient client)
    {
        List <PlayerDataModel> requests = new List <PlayerDataModel>();
        PlayerDataModel        model    = new PlayerDataModel()
        {
            PlayerDataRequest = PlayerDataRequest.Create,
            UserID            = clientName
        };

        requests.Add(model);
        //Sent to database
        byte[] data = TCPHelper.MessageBytesNewton(requests);
        client.GetStream().Write(data, 0, data.Length);

        PlayerDataModel responseModel = null;
        //Wait for response
        bool done = false;

        while (!done)
        {
            if (TCPHelper.Connected(client))
            {
                if (client.GetStream().DataAvailable)
                {
                    Debug.Log("INCOMMING DATA");
                    string responseString = TCPHelper.ReadStreamOnce(client.GetStream());
                    responseModel = JsonConvert.DeserializeObject <PlayerDataModel>(responseString);
                    done          = true;
                }
            }
            else
            {
                done = true;
            }

            Thread.Sleep(16);
        }

        switch (responseModel.PlayerDataStatus)
        {
        case PlayerDataStatus.None:
            break;

        case PlayerDataStatus.Success:
            return(true);

        case PlayerDataStatus.ConnectionFailed:
            break;

        case PlayerDataStatus.AlreadyExists:
            break;

        case PlayerDataStatus.DoesNotExist:
            break;

        default:
            break;
        }

        return(false);
    }
Ejemplo n.º 3
0
    private PlayerDataModel GetCharacterFromDB()
    {
        TcpClient client = new TcpClient(GlobalVariables.LOADBALANCER_IP, GlobalVariables.GAME_DATABASE_LOADBALANCER_PORT);
        //TcpClient client = new TcpClient("127.0.0.1", GlobalVariables.GAME_DATABASE_LOADBALANCER_PORT);

        List <PlayerDataModel> requests = new List <PlayerDataModel>();
        PlayerDataModel        model    = new PlayerDataModel()
        {
            PlayerDataRequest = PlayerDataRequest.Read,
            UserID            = clientName
        };

        requests.Add(model);
        //Sent to database
        byte[] data = TCPHelper.MessageBytesNewton(requests);

        client.GetStream().Write(data, 0, data.Length);

        PlayerDataModel responseModel = null;
        //Wait for response
        bool done = false;

        while (!done)
        {
            if (TCPHelper.Connected(client))
            {
                if (client.GetStream().DataAvailable)
                {
                    Debug.Log("INCOMMING DATA");
                    string responseString = TCPHelper.ReadStreamOnce(client.GetStream());
                    responseModel = JsonConvert.DeserializeObject <PlayerDataModel>(responseString);
                    done          = true;
                }
            }
            else
            {
                done = true;
            }

            Thread.Sleep(16);
        }
        //When response have been received, check the response
        switch (responseModel.PlayerDataStatus)
        {
        case PlayerDataStatus.None:
            break;

        case PlayerDataStatus.Success:
            return(responseModel);

        case PlayerDataStatus.ConnectionFailed:
            break;

        case PlayerDataStatus.AlreadyExists:
            break;

        case PlayerDataStatus.DoesNotExist:
        {
            if (RequestCharacterCreation(client))
            {
                return(new PlayerDataModel()
                    {
                        PlayerDataStatus = PlayerDataStatus.DoesNotExist
                    });
            }
        }
        break;

        default:
            break;
        }
        return(null);
    }