Ejemplo n.º 1
0
        public void ClientShouldReceiveAllServerMessages()
        {
            var msgOrder = new[] {
                ServerMsgId.KeepAlive, ServerMsgId.Error, ServerMsgId.Joined,
                ServerMsgId.Start, ServerMsgId.Inputs, ServerMsgId.Finish
            };

            _serverSidePipe.SendMessageUsingBuffer(new ServerMsgKeepAlive(), _sendBuffer);
            _serverSidePipe.SendMessageUsingBuffer(new ServerMsgError(ServerErrorId.InternalError), _sendBuffer);
            _serverSidePipe.SendMessageUsingBuffer(new ServerMsgJoined(), _sendBuffer);
            _clientSidePipe.ReceiveMessages();

            _serverSidePipe.SendMessageUsingBuffer(new ServerMsgStart(0, new [] { "foo", "bar" }, 1, 25), _sendBuffer);
            _serverSidePipe.SendMessageUsingBuffer(new ServerMsgInput(3, 2, 1), _sendBuffer);
            _serverSidePipe.SendMessageUsingBuffer(new ServerMsgFinish(new [] { 0u, 1u }, new [] { 2, 3 }), _sendBuffer);
            _clientSidePipe.ReceiveMessages();

            Assert.True(_clientSidePipe.ReceiveOrder.Count == msgOrder.Length);
            foreach (var msgId in msgOrder)
            {
                Assert.True(msgId == _clientSidePipe.ReceiveOrder.Dequeue());

                switch (msgId)
                {
                case ServerMsgId.KeepAlive:
                case ServerMsgId.Joined:
                    break;

                case ServerMsgId.Error:
                    _clientSidePipe.ErrorMessages.Dequeue();
                    break;

                case ServerMsgId.Start:
                    _clientSidePipe.StartMessages.Dequeue();
                    break;

                case ServerMsgId.Inputs:
                    _clientSidePipe.InputMessages.Dequeue();
                    break;

                case ServerMsgId.Finish:
                    _clientSidePipe.FinishMessages.Dequeue();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
Ejemplo n.º 2
0
        private ProcessingResult ProcessState()
        {
            _pipe.ReceiveMessages();

            if (!_pipe.IsConnected || _pipe.IsReceiveTimeout())
            {
                _log?.ConnectionLost();
                CloseSession();
                return(ProcessingResult.Error(SessionError.ConnectionError));
            }

            switch (State)
            {
            case ClientSessionState.Joining:
                var joined = ProcessJoiningStateMessages();
                if (joined)
                {
                    State = ClientSessionState.WaitingPlayers;
                    _log?.Joined();
                    return(ProcessingResult.Joined());
                }
                break;

            case ClientSessionState.WaitingPlayers:
                TrySendKeepAlive();
                var optMsgStart = ProcessWaitingStateMessages();
                if (optMsgStart.HasValue)
                {
                    State = ClientSessionState.Active;
                    _log?.GameStarted();
                    return(ProcessingResult.Started(optMsgStart.Value));
                }
                break;

            case ClientSessionState.Active:
                var optMsgFinish = ProcessActiveStateMessages();
                if (optMsgFinish.HasValue)
                {
                    CloseSession();
                    _log?.GameFinished();
                    return(ProcessingResult.Finished(optMsgFinish.Value));
                }

                TrySendKeepAlive();
                return(ProcessingResult.Active());

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(ProcessingResult.None());
        }