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, _settings.GetNetworkSetting(SettingsKeys.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, _settings.GetNetworkSetting(SettingsKeys.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() { _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 "); } }); }