public GuessNumberResponseData ProcessRequest(GuessNumberRequestData requestData)
        {
            GuessNumberResponseData responseData = new GuessNumberResponseData();

            if (requestData.NewGame)
            {
                NewGame();
            }
            responseData.RightNumber = Guess(requestData.GuessedNumber);
            return(responseData);
        }
Beispiel #2
0
        public void StartListening()
        {
            listeningCondition = true;
            guessNumberService = new GuessNumberService();

            tcpListener = new TcpListener(address, portNumber);
            tcpListener.Start();

            listeningProcess = Task.Run(() =>
            {
                byte[] bytes = new byte[256];
                string data  = null;
                try
                {
                    while (listeningCondition)
                    {
                        TcpClient client = tcpListener.AcceptTcpClient();
                        data             = null;

                        NetworkStream stream = client.GetStream();

                        int i;

                        while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            data = Encoding.ASCII.GetString(bytes, 0, i);

                            GuessNumberRequestData requestData   = DataConvertor.FromString <GuessNumberRequestData>(data);
                            GuessNumberResponseData responseData = guessNumberService.ProcessRequest(requestData);
                            data = DataConvertor.ToString <GuessNumberResponseData>(responseData);

                            byte[] msg = Encoding.ASCII.GetBytes(data);

                            stream.Write(msg, 0, msg.Length);
                        }

                        client.Close();
                    }
                }
                finally
                {
                    tcpListener.Stop();
                }
            });
        }