public void StartListening() { OptionKeepAlive = true; try { Start(); } catch (Exception ex) { try { _natHandler?.CloseNAT(); } catch { } Logger.Error(ex, $"Unable to start the SRS Server {_serverSettings.GetServerIP()}:{_serverSettings.GetServerPort()}"); MessageBox.Show($"Unable to start the SRS Server\n\nLikely Port {_serverSettings.GetServerPort()} in use\n\nChange the port by editing the .cfg", "Port in use", MessageBoxButton.OK, MessageBoxImage.Error); Environment.Exit(0); } }
public void Listen() { //start threads //packets that need processing new Thread(ProcessPackets).Start(); //outgoing packets new Thread(SendPendingPackets).Start(); transmissionLoggingQueue.Start(); var port = _serverSettings.GetServerPort(); _listener = new UdpClient(); try { _listener.AllowNatTraversal(true); } catch { } _listener.ExclusiveAddressUse = true; _listener.DontFragment = true; _listener.Client.DontFragment = true; _listener.Client.Bind(new IPEndPoint(_serverSettings.GetServerIP(), port)); while (!_stop) { try { var groupEP = new IPEndPoint(_serverSettings.GetServerIP(), port); var rawBytes = _listener.Receive(ref groupEP); if (rawBytes?.Length == 22) { try { //lookup guid here //22 bytes are guid! var guid = Encoding.ASCII.GetString( rawBytes, 0, 22); if (_clientsList.ContainsKey(guid)) { var client = _clientsList[guid]; client.VoipPort = groupEP; //send back ping UDP _listener.Send(rawBytes, rawBytes.Length, groupEP); } } catch (Exception ex) { //dont log because it slows down thread too much... } } else if (rawBytes?.Length > 22) { _pendingProcessingPackets.Add(new PendingPacket { RawBytes = rawBytes, ReceivedFrom = groupEP }); } } catch (Exception e) { Logger.Error(e, "Error receving audio UDP for client " + e.Message); } } try { _listener.Close(); } catch (Exception e) { } }