예제 #1
0
        private void StartUDPCommandListener()
        {
            Task.Factory.StartNew(() =>
            {
                while (!_stop)
                {
                    var localEp = new IPEndPoint(IPAddress.Any, _globalSettings.GetNetworkSetting(GlobalSettingsKeys.CommandListenerUDP));
                    try
                    {
                        _udpCommandListener = new UdpClient(localEp);
                        break;
                    }
                    catch (Exception ex)
                    {
                        Logger.Warn(ex, $"Unable to bind to the UDP Command Listener Socket Port: {localEp.Port}");
                        Thread.Sleep(500);
                    }
                }

                while (!_stop)
                {
                    try
                    {
                        var groupEp = new IPEndPoint(IPAddress.Any, 0);
                        var bytes   = _udpCommandListener.Receive(ref groupEp);

                        //Logger.Info("Recevied Message from UDP COMMAND INTERFACE: "+ Encoding.UTF8.GetString(
                        //          bytes, 0, bytes.Length));
                        var message =
                            JsonConvert.DeserializeObject <UDPInterfaceCommand>(Encoding.UTF8.GetString(
                                                                                    bytes, 0, bytes.Length));

                        if (message?.Command == UDPInterfaceCommand.UDPCommandType.FREQUENCY_DELTA)
                        {
                            RadioHelper.UpdateRadioFrequency(message.Frequency, message.RadioId);
                        }
                        else if (message?.Command == UDPInterfaceCommand.UDPCommandType.FREQUENCY_SET)
                        {
                            RadioHelper.UpdateRadioFrequency(message.Frequency, message.RadioId, false, true);
                        }
                        else if (message?.Command == UDPInterfaceCommand.UDPCommandType.ACTIVE_RADIO)
                        {
                            RadioHelper.SelectRadio(message.RadioId);
                        }
                        else if (message?.Command == UDPInterfaceCommand.UDPCommandType.TOGGLE_GUARD)
                        {
                            RadioHelper.ToggleGuard(message.RadioId);
                        }
                        else if (message?.Command == UDPInterfaceCommand.UDPCommandType.GUARD)
                        {
                            RadioHelper.SetGuard(message.RadioId, message.Enabled);
                        }
                        else if (message?.Command == UDPInterfaceCommand.UDPCommandType.CHANNEL_UP)
                        {
                            RadioHelper.RadioChannelUp(message.RadioId);
                        }
                        else if (message?.Command == UDPInterfaceCommand.UDPCommandType.CHANNEL_DOWN)
                        {
                            RadioHelper.RadioChannelDown(message.RadioId);
                        }
                        else if (message?.Command == UDPInterfaceCommand.UDPCommandType.SET_VOLUME)
                        {
                            RadioHelper.SetRadioVolume(message.Volume, message.RadioId);
                        }
                        else if (message?.Command == UDPInterfaceCommand.UDPCommandType.TRANSPONDER_POWER)
                        {
                            TransponderHelper.SetPower(message.Enabled);
                        }
                        else if (message?.Command == UDPInterfaceCommand.UDPCommandType.TRANSPONDER_M1_CODE)
                        {
                            TransponderHelper.SetMode1(message.Code);
                        }
                        else if (message?.Command == UDPInterfaceCommand.UDPCommandType.TRANSPONDER_M3_CODE)
                        {
                            TransponderHelper.SetMode3(message.Code);
                        }
                        else if (message?.Command == UDPInterfaceCommand.UDPCommandType.TRANSPONDER_M4)
                        {
                            TransponderHelper.SetMode4(message.Enabled);
                        }
                        else if (message?.Command == UDPInterfaceCommand.UDPCommandType.TRANSPONDER_IDENT)
                        {
                            TransponderHelper.SetIdent(message.Enabled);
                        }
                        else
                        {
                            Logger.Error("Unknown UDP Command!");
                        }
                    }
                    catch (SocketException e)
                    {
                        // SocketException is raised when closing app/disconnecting, ignore so we don't log "irrelevant" exceptions
                        if (!_stop)
                        {
                            Logger.Error(e, "SocketException Handling DCS  Message");
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception Handling DCS  Message");
                    }
                }

                try
                {
                    _udpCommandListener.Close();
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Exception stoping DCS listener ");
                }
            });
        }
        public void Start()
        {
            Task.Factory.StartNew(() =>
            {
                while (!_stop)
                {
                    var localEp = new IPEndPoint(IPAddress.Any,
                                                 _globalSettings.GetNetworkSetting(GlobalSettingsKeys.LotATCIncomingUDP));
                    try
                    {
                        _lotATCPositionListener = new UdpClient(localEp);
                        break;
                    }
                    catch (Exception ex)
                    {
                        Logger.Warn(ex, $"Unable to bind to the LotATC Export Listener Socket Port: {localEp.Port}");
                        Thread.Sleep(500);
                    }
                }

                while (!_stop)
                {
                    try
                    {
                        var groupEp = new IPEndPoint(IPAddress.Any, 0);
                        var bytes   = _lotATCPositionListener.Receive(ref groupEp);

                        var lotAtcPositionWrapper =
                            JsonConvert.DeserializeObject <LotATCMessageWrapper>(Encoding.UTF8.GetString(
                                                                                     bytes, 0, bytes.Length));

                        if (lotAtcPositionWrapper != null)
                        {
                            if (lotAtcPositionWrapper.los != null)
                            {
                                HandleLOSResponse(lotAtcPositionWrapper.los);
                            }
                            else if (lotAtcPositionWrapper.controller != null)
                            {
                                HandleLotATCUpdate(lotAtcPositionWrapper.controller);
                            }
                        }
                    }
                    catch (SocketException e)
                    {
                        if (!_stop)
                        {
                            Logger.Error(e, "SocketException Handling LotATC UDP Message");
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception Handling LotATC UDP Message");
                    }
                }

                try
                {
                    _lotATCPositionListener.Close();
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Exception stoping LotATC UDP listener");
                }
            });

            StartLotATCLOSSender();
        }
        public void Start()
        {
            _vaicomUDPListener = new UdpClient();
            _vaicomUDPListener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress,
                                                      true);
            _vaicomUDPListener.ExclusiveAddressUse = false; // only if you want to send/receive on same machine.

            var localEp = new IPEndPoint(IPAddress.Any,
                                         _globalSettings.GetNetworkSetting(GlobalSettingsKeys.VAICOMIncomingUDP));

            _vaicomUDPListener.Client.Bind(localEp);

            Task.Factory.StartNew(() =>
            {
                using (_vaicomUDPListener)
                {
                    while (!_stop)
                    {
                        try
                        {
                            var groupEp = new IPEndPoint(IPAddress.Any,
                                                         _globalSettings.GetNetworkSetting(GlobalSettingsKeys.VAICOMIncomingUDP));
                            var bytes = _vaicomUDPListener.Receive(ref groupEp);

                            var vaicomMessageWrapper =
                                JsonConvert.DeserializeObject <VAICOMMessageWrapper>(Encoding.UTF8.GetString(
                                                                                         bytes, 0, bytes.Length));

                            if (vaicomMessageWrapper != null)
                            {
                                if (vaicomMessageWrapper.MessageType == 1)
                                {
                                    vaicomMessageWrapper.LastReceivedAt = DateTime.Now.Ticks;
                                    _clientStateSingleton.InhibitTX     = vaicomMessageWrapper;
                                }
                            }
                        }
                        catch (SocketException e)
                        {
                            if (!_stop)
                            {
                                Logger.Error(e, "SocketException Handling VAICOM UDP Message");
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Error(e, "Exception Handling VAICOM UDP Message");
                        }
                    }

                    try
                    {
                        _vaicomUDPListener.Close();
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception stoping VAICOM UDP listener");
                    }
                }
            });
        }
        public void Start()
        {
            //reset last sent
            _clientStateSingleton.LastSent = 0;

            Task.Factory.StartNew(() =>
            {
                while (!_stop)
                {
                    try
                    {
                        var localEp     = new IPEndPoint(IPAddress.Any, _globalSettings.GetNetworkSetting(GlobalSettingsKeys.DCSIncomingUDP));
                        _dcsUdpListener = new UdpClient(localEp);
                        break;
                    }
                    catch (Exception ex)
                    {
                        Logger.Warn(ex, $"Unable to bind to the DCS Export Listener Socket Port: {_globalSettings.GetNetworkSetting(GlobalSettingsKeys.DCSIncomingUDP)}");
                        Thread.Sleep(500);
                    }
                }

                while (!_stop)
                {
                    try
                    {
                        var groupEp = new IPEndPoint(IPAddress.Any, 0);
                        var bytes   = _dcsUdpListener.Receive(ref groupEp);

                        var str = Encoding.UTF8.GetString(
                            bytes, 0, bytes.Length).Trim();

                        var message =
                            JsonConvert.DeserializeObject <DCSPlayerRadioInfo>(str);

                        Logger.Debug($"Recevied Message from DCS {str}");

                        if (!string.IsNullOrWhiteSpace(message.name) && message.name != "Unknown" && message.name != _clientStateSingleton.LastSeenName)
                        {
                            _clientStateSingleton.LastSeenName = message.name;
                        }

                        _clientStateSingleton.DcsExportLastReceived = DateTime.Now.Ticks;

                        //sync with others
                        //Radio info is marked as Stale for FC3 aircraft after every frequency change

                        ProcessRadioInfo(message);
                    }
                    catch (SocketException e)
                    {
                        // SocketException is raised when closing app/disconnecting, ignore so we don't log "irrelevant" exceptions
                        if (!_stop)
                        {
                            Logger.Error(e, "SocketException Handling DCS Message");
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception Handling DCS Message");
                    }
                }

                try
                {
                    _dcsUdpListener.Close();
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Exception stoping DCS listener ");
                }
            });
        }
예제 #5
0
        //used for the result
        private void StartDCSLOSBroadcastListener()
        {
            _dcsLOSListener = new UdpClient();
            _dcsLOSListener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress,
                                                   true);
            _dcsLOSListener.ExclusiveAddressUse = false; // only if you want to send/receive on same machine.

            var localEp = new IPEndPoint(IPAddress.Any, _globalSettings.GetNetworkSetting(GlobalSettingsKeys.DCSLOSIncomingUDP));

            _dcsLOSListener.Client.Bind(localEp);

            Task.Factory.StartNew(() =>
            {
                using (_dcsLOSListener)
                {
                    //    var count = 0;
                    while (!_stop)
                    {
                        try
                        {
                            var groupEp = new IPEndPoint(IPAddress.Any,
                                                         _globalSettings.GetNetworkSetting(GlobalSettingsKeys.DCSLOSIncomingUDP));
                            var bytes = _dcsLOSListener.Receive(ref groupEp);

                            /*   Logger.Debug(Encoding.UTF8.GetString(
                             *      bytes, 0, bytes.Length));*/
                            var playerInfo =
                                JsonConvert.DeserializeObject <DCSLosCheckResult[]>(Encoding.UTF8.GetString(
                                                                                        bytes, 0, bytes.Length));

                            foreach (var player in playerInfo)
                            {
                                SRClient client;

                                if (_clients.TryGetValue(player.id, out client))
                                {
                                    client.LineOfSightLoss = player.los;

                                    //  Logger.Debug(client.ToString());
                                }
                            }
                        }
                        catch (SocketException e)
                        {
                            // SocketException is raised when closing app/disconnecting, ignore so we don't log "irrelevant" exceptions
                            if (!_stop)
                            {
                                Logger.Error(e, "SocketException Handling DCS Los Result Message");
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Error(e, "Exception Handling DCS Los Result Message");
                        }
                    }

                    try
                    {
                        _dcsLOSListener.Close();
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception stoping DCS LOS Result listener ");
                    }
                }
            });
        }
        public void Start()
        {
            _lotATCPositionListener = new UdpClient();
            _lotATCPositionListener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress,
                                                           true);
            _lotATCPositionListener.ExclusiveAddressUse = false; // only if you want to send/receive on same machine.

            var localEp = new IPEndPoint(IPAddress.Any,
                                         _globalSettings.GetNetworkSetting(GlobalSettingsKeys.LotATCIncomingUDP));

            _lotATCPositionListener.Client.Bind(localEp);

            Task.Factory.StartNew(() =>
            {
                using (_lotATCPositionListener)
                {
                    while (!_stop)
                    {
                        try
                        {
                            var groupEp = new IPEndPoint(IPAddress.Any,
                                                         _globalSettings.GetNetworkSetting(GlobalSettingsKeys.LotATCIncomingUDP));
                            var bytes = _lotATCPositionListener.Receive(ref groupEp);

                            var lotAtcPositionWrapper =
                                JsonConvert.DeserializeObject <LotATCMessageWrapper>(Encoding.UTF8.GetString(
                                                                                         bytes, 0, bytes.Length));

                            if (lotAtcPositionWrapper != null)
                            {
                                if (lotAtcPositionWrapper.los != null)
                                {
                                    HandleLOSResponse(lotAtcPositionWrapper.los);
                                }
                                else if (lotAtcPositionWrapper.controller != null)
                                {
                                    HandleLotATCUpdate(lotAtcPositionWrapper.controller);
                                }
                            }
                        }
                        catch (SocketException e)
                        {
                            if (!_stop)
                            {
                                Logger.Error(e, "SocketException Handling LotATC UDP Message");
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Error(e, "Exception Handling LotATC UDP Message");
                        }
                    }

                    try
                    {
                        _lotATCPositionListener.Close();
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception stoping LotATC UDP listener");
                    }
                }
            });

            StartLotATCLOSSender();
        }
        //used for the result
        private void StartDCSLOSBroadcastListener()
        {
            Task.Factory.StartNew(() =>
            {
                while (!_stop)
                {
                    try
                    {
                        var localEp     = new IPEndPoint(IPAddress.Any, _globalSettings.GetNetworkSetting(GlobalSettingsKeys.DCSLOSIncomingUDP));
                        _dcsLOSListener = new UdpClient(localEp);
                        break;
                    }
                    catch (Exception ex)
                    {
                        Logger.Warn(ex, $"Unable to bind to the DCS LOS Listner Socket Port: {_globalSettings.GetNetworkSetting(GlobalSettingsKeys.DCSLOSIncomingUDP)}");
                        Thread.Sleep(500);
                    }
                }

                //    var count = 0;
                while (!_stop)
                {
                    try
                    {
                        var groupEp = new IPEndPoint(IPAddress.Any, 0);
                        var bytes   = _dcsLOSListener.Receive(ref groupEp);

                        /*   Logger.Debug(Encoding.UTF8.GetString(
                         *      bytes, 0, bytes.Length));*/
                        var playerInfo =
                            JsonConvert.DeserializeObject <DCSLosCheckResult[]>(Encoding.UTF8.GetString(
                                                                                    bytes, 0, bytes.Length));

                        foreach (var player in playerInfo)
                        {
                            SRClient client;

                            if (_clients.TryGetValue(player.id, out client))
                            {
                                client.LineOfSightLoss = player.los;

                                //  Logger.Debug(client.ToString());
                            }
                        }
                    }
                    catch (SocketException e)
                    {
                        // SocketException is raised when closing app/disconnecting, ignore so we don't log "irrelevant" exceptions
                        if (!_stop)
                        {
                            Logger.Error(e, "SocketException Handling DCS Los Result Message");
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception Handling DCS Los Result Message");
                    }
                }

                try
                {
                    _dcsLOSListener.Close();
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Exception stoping DCS LOS Result listener ");
                }
            });
        }
        public void Start()
        {
            _clientStateSingleton.LastPostionCoalitionSent = 0;

            Task.Factory.StartNew(() =>
            {
                while (!_stop)
                {
                    var localEp = new IPEndPoint(IPAddress.Any,
                                                 _globalSettings.GetNetworkSetting(GlobalSettingsKeys.DCSIncomingGameGUIUDP));
                    try
                    {
                        _dcsGameGuiUdpListener = new UdpClient(localEp);
                        break;
                    }
                    catch (Exception ex)
                    {
                        Logger.Warn(ex, $"Unable to bind to the DCS GameGUI Socket Port: {localEp.Port}");
                        Thread.Sleep(500);
                    }
                }

                //    var count = 0;
                while (!_stop)
                {
                    try
                    {
                        var groupEp = new IPEndPoint(IPAddress.Any, 0);
                        var bytes   = _dcsGameGuiUdpListener.Receive(ref groupEp);

                        var updatedPlayerInfo =
                            JsonConvert.DeserializeObject <DCSPlayerSideInfo>(Encoding.UTF8.GetString(
                                                                                  bytes, 0, bytes.Length));

                        if (updatedPlayerInfo != null)
                        {
                            var shouldUpdate = _serverSettings.GetSettingAsBool(ServerSettingsKeys.DISTANCE_ENABLED) || _serverSettings.GetSettingAsBool(ServerSettingsKeys.LOS_ENABLED);

                            var currentInfo = _clientStateSingleton.PlayerCoaltionLocationMetadata;

                            bool changed = !updatedPlayerInfo.Equals(currentInfo);
                            //copy the bits we need  - leave position

                            currentInfo.name = updatedPlayerInfo.name;
                            currentInfo.side = updatedPlayerInfo.side;
                            currentInfo.seat = updatedPlayerInfo.seat;

                            //this will clear any stale positions if nothing is currently connected
                            _clientStateSingleton.ClearPositionsIfExpired();

                            //only update if position is changed
                            if (_clientStateSingleton.DcsPlayerRadioInfo.IsCurrent() && (changed || shouldUpdate))
                            {
                                _clientSideUpdate();
                            }

                            //     count = 0;

                            _clientStateSingleton.DcsGameGuiLastReceived = DateTime.Now.Ticks;
                        }
                    }
                    catch (SocketException e)
                    {
                        // SocketException is raised when closing app/disconnecting, ignore so we don't log "irrelevant" exceptions
                        if (!_stop)
                        {
                            Logger.Error(e, "SocketException Handling DCS GameGUI Message");
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception Handling DCS GameGUI Message");
                    }
                }

                try
                {
                    _dcsGameGuiUdpListener.Close();
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Exception stoping DCS listener ");
                }
            });
        }
예제 #9
0
        public void Start()
        {
            _dcsUdpListener = new UdpClient();
            _dcsUdpListener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            _dcsUdpListener.ExclusiveAddressUse = false; // only if you want to send/receive on same machine.

            //   var multicastaddress = IPAddress.Parse("239.255.50.10");
            //      _dcsUdpListener.JoinMulticastGroup(multicastaddress);

            var localEp = new IPEndPoint(IPAddress.Any, _globalSettings.GetNetworkSetting(GlobalSettingsKeys.DCSIncomingUDP));

            _dcsUdpListener.Client.Bind(localEp);
            //   activeRadioUdpClient.Client.ReceiveTimeout = 10000;

            //reset last sent
            _clientStateSingleton.LastSent = 0;

            Task.Factory.StartNew(() =>
            {
                using (_dcsUdpListener)
                {
                    while (!_stop)
                    {
                        try
                        {
                            var groupEp = new IPEndPoint(IPAddress.Any,
                                                         _globalSettings.GetNetworkSetting(GlobalSettingsKeys.DCSIncomingUDP));
                            var bytes = _dcsUdpListener.Receive(ref groupEp);

                            var str = Encoding.UTF8.GetString(
                                bytes, 0, bytes.Length).Trim();

                            var message =
                                JsonConvert.DeserializeObject <DCSPlayerRadioInfo>(str);

                            Logger.Debug($"Recevied Message from DCS {str}");

                            if (!string.IsNullOrWhiteSpace(message.name) && message.name != "Unknown" && message.name != _clientStateSingleton.LastSeenName)
                            {
                                _clientStateSingleton.LastSeenName = message.name;
                            }

                            _clientStateSingleton.DcsExportLastReceived = DateTime.Now.Ticks;

                            //sync with others
                            //Radio info is marked as Stale for FC3 aircraft after every frequency change

                            ProcessRadioInfo(message);
                        }
                        catch (SocketException e)
                        {
                            // SocketException is raised when closing app/disconnecting, ignore so we don't log "irrelevant" exceptions
                            if (!_stop)
                            {
                                Logger.Error(e, "SocketException Handling DCS Message");
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Error(e, "Exception Handling DCS Message");
                        }
                    }

                    try
                    {
                        _dcsUdpListener.Close();
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception stoping DCS listener ");
                    }
                }
            });
        }
예제 #10
0
        public void Start()
        {
            _dcsGameGuiUdpListener = new UdpClient();
            _dcsGameGuiUdpListener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress,
                                                          true);
            _dcsGameGuiUdpListener.ExclusiveAddressUse = false; // only if you want to send/receive on same machine.

            //    var multicastaddress = IPAddress.Parse("239.255.50.10");
            //   _dcsGameGuiUdpListener.JoinMulticastGroup(multicastaddress);

            var localEp = new IPEndPoint(IPAddress.Any,
                                         _globalSettings.GetNetworkSetting(GlobalSettingsKeys.DCSIncomingGameGUIUDP));

            _dcsGameGuiUdpListener.Client.Bind(localEp);
            //   activeRadioUdpClient.Client.ReceiveTimeout = 10000;

            Task.Factory.StartNew(() =>
            {
                using (_dcsGameGuiUdpListener)
                {
                    //    var count = 0;
                    while (!_stop)
                    {
                        try
                        {
                            var groupEp = new IPEndPoint(IPAddress.Any,
                                                         _globalSettings.GetNetworkSetting(GlobalSettingsKeys.DCSIncomingGameGUIUDP));
                            var bytes = _dcsGameGuiUdpListener.Receive(ref groupEp);

                            var updatedPlayerInfo =
                                JsonConvert.DeserializeObject <DCSPlayerSideInfo>(Encoding.UTF8.GetString(
                                                                                      bytes, 0, bytes.Length));

                            if (updatedPlayerInfo != null)
                            {
                                var currentInfo = _clientStateSingleton.PlayerCoaltionLocationMetadata;

                                //copy the bits we need  - leave position
                                currentInfo.name = updatedPlayerInfo.name;
                                currentInfo.side = updatedPlayerInfo.side;

                                //this will clear any stale positions if nothing is currently connected
                                _clientStateSingleton.ClearPositionsIfExpired();

                                _clientSideUpdate();
                                //     count = 0;

                                _clientStateSingleton.DcsGameGuiLastReceived = DateTime.Now.Ticks;
                            }
                        }
                        catch (SocketException e)
                        {
                            // SocketException is raised when closing app/disconnecting, ignore so we don't log "irrelevant" exceptions
                            if (!_stop)
                            {
                                Logger.Error(e, "SocketException Handling DCS GameGUI Message");
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Error(e, "Exception Handling DCS GameGUI Message");
                        }
                    }

                    try
                    {
                        _dcsGameGuiUdpListener.Close();
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception stoping DCS listener ");
                    }
                }
            });
        }
        public void Start()
        {
            //reset last sent
            _clientStateSingleton.LastSent = 0;

            Task.Factory.StartNew(() =>
            {
                while (!_stop)
                {
                    var localEp = new IPEndPoint(IPAddress.Any, _globalSettings.GetNetworkSetting(GlobalSettingsKeys.IL2IncomingUDP));
                    try
                    {
                        _il2UdpListener = new UdpClient(localEp);
                        break;
                    }
                    catch (Exception ex)
                    {
                        Logger.Warn(ex, $"Unable to bind to the IL2 Export Listener Socket Port: {localEp.Port}");
                        Thread.Sleep(500);
                    }
                }

                while (!_stop)
                {
                    try
                    {
                        var groupEp = new IPEndPoint(IPAddress.Any, 0);
                        var bytes   = _il2UdpListener.Receive(ref groupEp);

                        if (bytes.Length > 2)
                        {
                            var messages = IL2UDPMessage.Process(bytes);

                            //IL2 running
                            _clientStateSingleton.IL2ExportLastReceived      = DateTime.Now.Ticks;
                            _clientStateSingleton.PlayerGameState.LastUpdate = DateTime.Now.Ticks;

                            foreach (var msg in messages)
                            {
                                Logger.Debug($"Recevied Message from IL2 {msg.ToString()}");
                                ProcessUDPMessage(msg);
                            }
                        }
                    }
                    catch (SocketException e)
                    {
                        // SocketException is raised when closing app/disconnecting, ignore so we don't log "irrelevant" exceptions
                        if (!_stop)
                        {
                            Logger.Error(e, "SocketException Handling IL2 Message");
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception Handling IL2 Message");
                    }
                }

                try
                {
                    _il2UdpListener.Close();
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Exception stoping IL2 listener ");
                }
            });
        }
예제 #12
0
        public void Start()
        {
            Task.Factory.StartNew(() =>
            {
                while (!_stop)
                {
                    try
                    {
                        var localEp = new IPEndPoint(IPAddress.Any,
                                                     _globalSettings.GetNetworkSetting(GlobalSettingsKeys.VAICOMIncomingUDP));
                        _vaicomUDPListener = new UdpClient(localEp);
                        break;
                    }
                    catch (Exception ex)
                    {
                        Logger.Warn(ex, $"Unable to bind to the VAICOM Listener Socket Port: {_globalSettings.GetNetworkSetting(GlobalSettingsKeys.VAICOMIncomingUDP)}");
                        Thread.Sleep(500);
                    }
                }
                while (!_stop)
                {
                    try
                    {
                        var groupEp = new IPEndPoint(IPAddress.Any, 0);
                        var bytes   = _vaicomUDPListener.Receive(ref groupEp);

                        var vaicomMessageWrapper =
                            JsonConvert.DeserializeObject <VAICOMMessageWrapper>(Encoding.UTF8.GetString(
                                                                                     bytes, 0, bytes.Length));

                        if (vaicomMessageWrapper != null)
                        {
                            if (vaicomMessageWrapper.MessageType == 1)
                            {
                                if (_globalSettings.GetClientSettingBool(GlobalSettingsKeys.VAICOMTXInhibitEnabled))
                                {
                                    vaicomMessageWrapper.LastReceivedAt = DateTime.Now.Ticks;
                                    _clientStateSingleton.InhibitTX     = vaicomMessageWrapper;
                                }
                            }
                        }
                    }
                    catch (SocketException e)
                    {
                        if (!_stop)
                        {
                            Logger.Error(e, "SocketException Handling VAICOM UDP Message");
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception Handling VAICOM UDP Message");
                    }
                }

                try
                {
                    _vaicomUDPListener.Close();
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Exception stoping VAICOM UDP listener");
                }
            });
        }
예제 #13
0
        private void StartUDPCommandListener()
        {
            _udpCommandListener = new UdpClient();
            _udpCommandListener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            _udpCommandListener.ExclusiveAddressUse = false; // only if you want to send/receive on same machine.

            var localEp = new IPEndPoint(IPAddress.Any, _globalSettings.GetNetworkSetting(GlobalSettingsKeys.CommandListenerUDP));

            _udpCommandListener.Client.Bind(localEp);

            Task.Factory.StartNew(() =>
            {
                using (_udpCommandListener)
                {
                    while (!_stop)
                    {
                        try
                        {
                            var groupEp = new IPEndPoint(IPAddress.Any,
                                                         _globalSettings.GetNetworkSetting(GlobalSettingsKeys.CommandListenerUDP));
                            var bytes = _udpCommandListener.Receive(ref groupEp);

                            //Logger.Info("Recevied Message from UDP COMMAND INTERFACE: "+ Encoding.UTF8.GetString(
                            //          bytes, 0, bytes.Length));
                            var message =
                                JsonConvert.DeserializeObject <UDPInterfaceCommand>(Encoding.UTF8.GetString(
                                                                                        bytes, 0, bytes.Length));

                            if (message?.Command == UDPInterfaceCommand.UDPCommandType.FREQUENCY)
                            {
                                RadioHelper.UpdateRadioFrequency(message.Frequency, message.RadioId);
                            }
                            else if (message?.Command == UDPInterfaceCommand.UDPCommandType.ACTIVE_RADIO)
                            {
                                RadioHelper.SelectRadio(message.RadioId);
                            }
                            else if (message?.Command == UDPInterfaceCommand.UDPCommandType.TOGGLE_GUARD)
                            {
                                RadioHelper.ToggleGuard(message.RadioId);
                            }
                            else if (message?.Command == UDPInterfaceCommand.UDPCommandType.CHANNEL_UP)
                            {
                                RadioHelper.RadioChannelUp(message.RadioId);
                            }
                            else if (message?.Command == UDPInterfaceCommand.UDPCommandType.CHANNEL_DOWN)
                            {
                                RadioHelper.RadioChannelDown(message.RadioId);
                            }
                            else if (message?.Command == UDPInterfaceCommand.UDPCommandType.SET_VOLUME)
                            {
                                RadioHelper.SetRadioVolume(message.Volume, message.RadioId);
                            }
                            else
                            {
                                Logger.Error("Unknown UDP Command!");
                            }
                        }
                        catch (SocketException e)
                        {
                            // SocketException is raised when closing app/disconnecting, ignore so we don't log "irrelevant" exceptions
                            if (!_stop)
                            {
                                Logger.Error(e, "SocketException Handling DCS  Message");
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Error(e, "Exception Handling DCS  Message");
                        }
                    }

                    try
                    {
                        _udpCommandListener.Close();
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception stoping DCS listener ");
                    }
                }
            });
        }