private async void UdpClientAction() { try { udpClient = new UdpClient(4536); while (IsStarted) { var udpReceiveResult = await udpClient.ReceiveAsync(); if (!udpReceiveResult.RemoteEndPoint.Address.ToString().Equals(Networker.GetLocalIp())) { var defconStatus = Encoding.ASCII.GetString(udpReceiveResult.Buffer); if (int.TryParse(defconStatus, out int parsedDefconStatus)) { if (parsedDefconStatus > 0 && parsedDefconStatus < 6) { new SettingsService().SaveSetting("DefconStatus", defconStatus.ToString()); Intent widgetIntent = new Intent(this, typeof(MyDefconWidget)); widgetIntent.SetAction("com.marcusrunge.MyDEFCON.DEFCON_UPDATE"); widgetIntent.PutExtra("DefconStatus", defconStatus.ToString()); Intent statusReceiverIntent = new Intent(this, typeof(ForegroundDefconStatusReceiver)); statusReceiverIntent.SetAction("com.marcusrunge.MyDEFCON.STATUS_RECEIVER_ACTION"); statusReceiverIntent.PutExtra("DefconStatus", defconStatus.ToString()); SendBroadcast(widgetIntent); SendBroadcast(statusReceiverIntent); if (_settingsService.GetSetting <bool>("isStatusUpdateAlertEnabled")) { Notifier.AlertWithVibration(); Notifier.AlertWithAudioNotification(this, _settingsService.GetSetting <int>("StatusUpdateAlertSelection")); } } else if (parsedDefconStatus == 0 && _settingsService.GetSetting <bool>("IsMulticastEnabled") && DateTimeOffset.Now > _lastConnect.AddSeconds(5)) { Intent tcpActionIntent = new Intent(this, typeof(TcpActionReceiver)); tcpActionIntent.SetAction("com.marcusrunge.MyDEFCON.TCP_ACTION"); tcpActionIntent.PutExtra("RemoteEndPointAddress", udpReceiveResult.RemoteEndPoint.Address.ToString()); SendBroadcast(tcpActionIntent); _lastConnect = DateTimeOffset.Now; } } } } if (udpClient != null) { udpClient.Close(); udpClient.Dispose(); } } catch { } }
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId) { _isServiceRunning = true; _isConnectionBlocked = false; _lastConnect = DateTimeOffset.MinValue; try { _settingsService = MainActivity.Container.Resolve <ISettingsService>(); _eventService = MainActivity.Container.Resolve <IEventService>(); } catch (Exception) { return(StartCommandResult.RedeliverIntent); } _eventService.BlockConnectionEvent += (s, e) => _isConnectionBlocked = (e as BlockConnectionEventArgs).Blocked; _udpClient = new UdpClient(4536); Task.Run(async() => { try { while (_isServiceRunning) { var udpReceiveResult = await _udpClient.ReceiveAsync(); if (!udpReceiveResult.RemoteEndPoint.Address.ToString().Equals(Networker.GetLocalIp())) { var defconStatus = Encoding.ASCII.GetString(udpReceiveResult.Buffer); if (int.TryParse(defconStatus, out int parsedDefconStatus) && !_isConnectionBlocked) { if (parsedDefconStatus > 0 && parsedDefconStatus < 6) { new SettingsService().SaveSetting("DefconStatus", defconStatus.ToString()); Intent widgetIntent = new Intent(this, typeof(MyDefconWidget)); widgetIntent.SetAction("com.marcusrunge.MyDEFCON.DEFCON_UPDATE"); widgetIntent.PutExtra("DefconStatus", defconStatus.ToString()); Intent statusReceiverIntent = new Intent(this, typeof(DefconStatusReceiver)); statusReceiverIntent.SetAction("com.marcusrunge.MyDEFCON.STATUS_RECEIVER_ACTION"); statusReceiverIntent.PutExtra("DefconStatus", defconStatus.ToString()); SendBroadcast(widgetIntent); SendBroadcast(statusReceiverIntent); if (!_settingsService.GetSetting <bool>("IsForegroundServiceEnabled") && _settingsService.GetSetting <bool>("isStatusUpdateAlertEnabled")) { Notifier.AlertWithVibration(); Notifier.AlertWithAudioNotification(this, _settingsService.GetSetting <int>("StatusUpdateAlertSelection")); } } else if (parsedDefconStatus == 0 && _settingsService.GetSetting <bool>("IsMulticastEnabled") && DateTimeOffset.Now > _lastConnect.AddSeconds(5)) { Intent tcpActionIntent = new Intent(this, typeof(TcpActionReceiver)); tcpActionIntent.SetAction("com.marcusrunge.MyDEFCON.TCP_ACTION"); tcpActionIntent.PutExtra("RemoteEndPointAddress", udpReceiveResult.RemoteEndPoint.Address.ToString()); SendBroadcast(tcpActionIntent); _lastConnect = DateTimeOffset.Now; } } _isConnectionBlocked = false; } } } catch { } }, _cancellationToken); //return base.OnStartCommand(intent, flags, startId); return(StartCommandResult.Sticky); }