public void ReceiveDataTest()
        {
            _serverAction = "Send";
            string data = _networkCommunicator.ReceiveData();

            Assert.AreEqual(_receiveTestData, data);
        }
示例#2
0
        public static bool ServerIsHostingGame(IPAddress serverAddress)
        {
            string data = string.Empty;

            using (NetworkCommunicator client = ConnectToServer(serverAddress))
            {
                if (client != null)
                {
                    data = client.ReceiveData();
                    client.SendData(MessageConstants.CloseConnection + MessageConstants.MessageEndDelimiter);
                }
            }

            return(data == (MessageConstants.ServerAvailable + MessageConstants.MessageEndDelimiter));
        }
示例#3
0
        public void Listen()
        {
            TcpListener listener = null;
            TcpClient   client   = null;

            try
            {
                int port = IPUtil.DefaultPort;
                listener = new TcpListener(IPUtil.GetLocalIpAddress(), port);
                listener.Start();

                _log.InfoFormat("Waiting for a connection on port: {0} ", port);

                while (_listen)
                {
                    if (listener.Pending())
                    {
                        client = listener.AcceptTcpClient();
                        NetworkCommunicator networkCommunicator = new NetworkCommunicator(client);
                        _log.InfoFormat("Client {0} connected.", networkCommunicator.RemoteEndPoint);

                        //acknowledge client is able to join this game
                        networkCommunicator.SendData(MessageConstants.ServerAvailable);

                        string data = networkCommunicator.ReceiveData();
                        if (data == MessageConstants.CloseConnection)
                        {
                            networkCommunicator.Dispose();
                            _log.InfoFormat("Closed client connection - client was just finding servers.");
                        }
                        else if (data == MessageConstants.JoinGame)
                        {
                            OnPlayerJoined(networkCommunicator);
                        }
                    }
                    Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                ErrorUtil.WriteError(ex);
                throw;
            }
            finally
            {
                listener.Stop();
            }
        }
        private void Server()
        {
            _listener = new TcpListener(IPUtil.GetLocalIpAddress(), IPUtil.DefaultPort);
            _listener.Start();
            _client = _listener.AcceptTcpClient();
            NetworkCommunicator networkCommunicator = new NetworkCommunicator(_client);

            Console.WriteLine("Client connected to server successfully.");
            if (_serverAction == "Receive")
            {
                _dataServerReceived = networkCommunicator.ReceiveData();
            }
            else if (_serverAction == "Send")
            {
                networkCommunicator.SendData(_receiveTestData);
            }
        }
示例#5
0
        private void ClientGameLoop(object client)
        {
            NetworkCommunicator networkCommunicator = (NetworkCommunicator)client;
            ManualResetEvent    sentDataThisTurn    = new ManualResetEvent(false);
            ManualResetEvent    endOfTurnWait       = new ManualResetEvent(false);

            try
            {
                string data = string.Empty;

                _playersThatSentDataThisTurnList.Add(sentDataThisTurn);
                _endOfTurnWaitList.Add(endOfTurnWait);
                _currentTurnClientActions.Add(new PlayerAction(networkCommunicator.RemoteEndPoint.ToString(), MessageConstants.PlayerActionNone));

                _log.InfoFormat("Client {0} connected running on thread: {1}, waiting for start game / 1st turn data", networkCommunicator.RemoteEndPoint, Thread.CurrentThread.ManagedThreadId);
                data = networkCommunicator.ReceiveData();
                bool readFromClient = false;

                if (data == MessageConstants.StartGame)
                {
                    readFromClient = true;
                    RunGame();
                    foreach (NetworkCommunicator clientPlayer in _players)
                    {
                        clientPlayer.SendData(MessageConstants.GameStarting);
                    }
                    _completeGameState.StartNewGame(_currentTurnClientActions);
                }

                // Loop to receive all the data sent by the client.
                // this is now the main game loop for the game - 1 thread per client
                bool gameIsOn = true;
                while (gameIsOn) //issue [B.2.3] of the design document
                {
                    if (readFromClient)
                    {
                        data = networkCommunicator.ReceiveData();
                    }
                    else
                    {
                        readFromClient = true;
                    }

                    foreach (PlayerAction action in _currentTurnClientActions)
                    {
                        if (action.PlayerID == networkCommunicator.RemoteEndPoint.ToString())
                        {
                            action.Action = data;
                        }
                    }
                    sentDataThisTurn.Set();

                    _gameStateProcessed.WaitOne();

                    networkCommunicator.SendData(_completeGameState.ToString());
                    endOfTurnWait.Set();
                    WaitHandle.WaitAll(_endOfTurnWaitArray); //issue [B.2.3] of the design document
                    _gameStateProcessed.Reset();
                }
            }
            catch (Exception ex)
            {
                ErrorUtil.WriteError(ex);
                throw;
            }
            finally
            {
                sentDataThisTurn.Close();
                endOfTurnWait.Close();
                networkCommunicator.Dispose();
            }
        }