Ejemplo n.º 1
0
        private void MessageReceiver()
        {
            while (true)
            {
                byte[] data = _NetworkComms.GetMessage();

                if (data == null)
                {
                    Thread.Sleep(50);
                    continue;
                }

                Message msg = JsonSerializer.Deserialize <Message>(data);

                if (msg != null)
                {
                    //Ignore our own message
                    if (msg.Sender != _UniqueID)
                    {
                        switch (msg.MsgType)
                        {
                        case MessageType.TurnInfo:
                            HandleTurnInfoMessage(JsonSerializer.Deserialize <TurnInfoMessage>(data));
                            break;

                        case MessageType.Response:
                            ResponseMessage responseMessage = JsonSerializer.Deserialize <ResponseMessage>(data);

                            //Only add message to queue if we are receiver
                            if (responseMessage.Receiver == _UniqueID)
                            {
                                ResponseMessages.Enqueue(responseMessage);
                            }

                            break;

                        case MessageType.AdvertiseLobby:
                            AdvertiseLobbyMessages.Enqueue(JsonSerializer.Deserialize <AdvertiseLobbyMessage>(data));
                            break;

                        case MessageType.JoinLobby:
                            JoinLobbyMessage joinLobbyMessage = JsonSerializer.Deserialize <JoinLobbyMessage>(data);

                            //Only add message to queue if we are receiver
                            if (joinLobbyMessage.Receiver == _UniqueID)
                            {
                                JoinLobbyMessages.Enqueue(joinLobbyMessage);
                            }
                            break;

                        default:
                            Debug.WriteLine("Unknown message type: " + msg.MsgType);
                            Debug.WriteLine(BitConverter.ToString(data));
                            break;
                        }
                    }
                }
            }
        }