예제 #1
0
        async void RouteMessage(string message, IClientHolder clientHolder)
        {

            if (string.IsNullOrEmpty(message))
            {
                return;
            }

            MJsonMessageAll mjsonObject = null;
            try
            {
                mjsonObject = JsonConvert.DeserializeObject<MJsonMessageAll>(message);
            }
            catch (JsonException e)
            {
                if (mjsonObject != null)
                {
                    Debug.WriteLine("Json Deserialize Error! "+e.Message);
                    Debug.Fail("Json Deserialize Error! "+e.Message);
                    var errorMessasge = JsonConvert.SerializeObject(new MJsonMessageError(message));
                    clientHolder.SendMessageToClient(errorMessasge);
                    clientHolder.Disconnect();
                }
                return;
            }

            await semaphore.WaitAsync();
            if (mjsonObject.IsJOIN())
            {
                var roomName = mjsonObject.room;

                // check clientNum
                if ( roomNameWaitingNumMap.ContainsKey(roomName) && roomNameWaitingNumMap[roomName] >= Constants.PLAYER_NUM)
                {
                    Debug.WriteLine("target room is full!");
                    var errorMessasge = JsonConvert.SerializeObject(new MJsonMessageError(message));
                    clientHolder.SendMessageToClient(errorMessasge);
                    clientHolder.Disconnect();
                    return;
                }

                // add client
                clientHolderRoomNameMap.Add(clientHolder, roomName);
                clientHolderClientNameMap.Add(clientHolder, mjsonObject.name);
                var alreadyWaitingInTargetRoom = roomNameWaitingNumMap.ContainsKey(roomName);
                var newWaitingNum = alreadyWaitingInTargetRoom ?
                    roomNameWaitingNumMap[roomName] + 1: 1;
                roomNameWaitingNumMap[roomName] = newWaitingNum;
            }

            foreach( var room in GetStartableRoomList())
            {
                StartGameRoom(room);
            }

            semaphore.Release();

        }
예제 #2
0
        public async void OnGetMessageFromClient(string message, IClientHolder client)
        {
            try
            {
                await semaphore.WaitAsync();
                MJsonMessageAll messageObj = JsonConvert.DeserializeObject<MJsonMessageAll>(message);


                // validate message
                if ( gameContext.ValidateMessage(messageObj) == false)
                {

                    Debug.WriteLine("invalidate MessageType!");
                    gameContext.ValidateMessage(messageObj);
                    OnErrorDetected("invalidate MessageType!");

                    return;
                }

                gameContext.RegisterMessage(messageObj);
                // check all clients have sent message
                if ( gameContext.HasRecievedMessageFromAllClients() == false)
                {
                    return;
                }

                // if server gets messages from all clients, fire event and send message.
                // gameContext.PrintState();
                var isSuccesseded = gameContext.ExecuteAction();

                if ( isSuccesseded == false)
                {
                    Debug.WriteLine("failed execute message !");
                    Debug.WriteLine(gameModel.ErrorMessage);

                    OnErrorDetected(gameModel.ErrorMessage);

                    return;
                }

            }
            finally
            {
                semaphore.Release();
            }
        }