예제 #1
0
        public void HandleMessage(SRSClientSession state, NetworkMessage message)
        {
            try
            {
                Logger.Trace($"Received:  Msg - {message.MsgType} from {state.SRSGuid}");

                if (!HandleConnectedClient(state, message))
                {
                    Logger.Info($"Invalid Client - disconnecting {state.SRSGuid}");
                }

                switch (message.MsgType)
                {
                case NetworkMessage.MessageType.PING:
                    // Do nothing for now
                    break;

                case NetworkMessage.MessageType.UPDATE:
                    HandleClientMetaDataUpdate(state, message, true);
                    break;

                case NetworkMessage.MessageType.RADIO_UPDATE:
                    bool showTuned = _serverSettings.GetGeneralSetting(ServerSettingsKeys.SHOW_TUNED_COUNT)
                                     .BoolValue;
                    HandleClientMetaDataUpdate(state, message, !showTuned);
                    HandleClientRadioUpdate(state, message, showTuned);
                    break;

                case NetworkMessage.MessageType.SYNC:
                    HandleRadioClientsSync(state, message);
                    break;

                case NetworkMessage.MessageType.SERVER_SETTINGS:
                    HandleServerSettingsMessage();
                    break;

                default:
                    Logger.Warn("Recevied unknown message type");
                    break;
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Exception Handling Message " + ex.Message);
            }
        }
        private void HandleClientMetaDataUpdate(SRSClientSession session, NetworkMessage message, bool send)
        {
            if (_clients.ContainsKey(message.Client.ClientGuid))
            {
                var client = _clients[message.Client.ClientGuid];

                if (client != null)
                {
                    bool redrawClientAdminList = client.Name != message.Client.Name || client.Coalition != message.Client.Coalition;

                    //copy the data we need
                    client.LastUpdate = DateTime.Now.Ticks;
                    client.Name       = message.Client.Name;
                    client.Coalition  = message.Client.Coalition;
                    client.Seat       = message.Client.Seat;

                    //send update to everyone
                    //Remove Client Radio Info
                    var replyMessage = new NetworkMessage
                    {
                        MsgType = NetworkMessage.MessageType.UPDATE,
                        Client  = new SRClient
                        {
                            ClientGuid = client.ClientGuid,
                            Coalition  = client.Coalition,
                            Name       = client.Name,
                            Seat       = client.Seat
                        }
                    };

                    if (send)
                    {
                        MulticastAllExeceptOne(replyMessage.Encode(), session.Id);
                    }

                    // Only redraw client admin UI of server if really needed
                    if (redrawClientAdminList)
                    {
                        _eventAggregator.PublishOnUIThread(new ServerStateMessage(true,
                                                                                  new List <SRClient>(_clients.Values)));
                    }
                }
            }
        }
        private void HandleClientRadioUpdate(SRSClientSession session, NetworkMessage message, bool send)
        {
            if (_clients.ContainsKey(message.Client.ClientGuid))
            {
                var client = _clients[message.Client.ClientGuid];

                if (client != null)
                {
                    //shouldnt be the case but just incase...
                    if (message.Client.GameState == null)
                    {
                        message.Client.GameState = new PlayerGameState();
                    }
                    //update to local ticks
                    message.Client.GameState.LastUpdate = DateTime.Now.Ticks;

                    var changed = false;

                    if (client.GameState == null)
                    {
                        client.GameState = message.Client.GameState;
                        changed          = true;
                    }
                    else
                    {
                        changed = !client.GameState.Equals(message.Client.GameState) || client.Coalition != message.Client.Coalition;
                    }

                    client.LastUpdate = DateTime.Now.Ticks;
                    client.Name       = message.Client.Name;
                    client.Coalition  = message.Client.Coalition;
                    client.Seat       = message.Client.Seat;
                    client.GameState  = message.Client.GameState;
                    client.Seat       = message.Client.Seat;

                    TimeSpan lastSent = new TimeSpan(DateTime.Now.Ticks - client.LastRadioUpdateSent);

                    //send update to everyone
                    //Remove Client Radio Info
                    if (send)
                    {
                        NetworkMessage replyMessage;
                        if ((changed || lastSent.TotalSeconds > 180))
                        {
                            client.LastRadioUpdateSent = DateTime.Now.Ticks;
                            replyMessage = new NetworkMessage
                            {
                                MsgType = NetworkMessage.MessageType.RADIO_UPDATE,
                                Client  = new SRClient
                                {
                                    ClientGuid = client.ClientGuid,
                                    Coalition  = client.Coalition,
                                    Name       = client.Name,
                                    GameState  = client.GameState, //send radio info
                                    Seat       = client.Seat
                                }
                            };
                            Multicast(replyMessage.Encode());
                        }
                    }
                }
            }
        }