private void StartExport()
        {
            _stop = false;

            string exportFilePath = ServerSettingsStore.Instance.GetServerSetting(ServerSettingsKeys.CLIENT_EXPORT_FILE_PATH).StringValue;

            if (string.IsNullOrWhiteSpace(exportFilePath) || exportFilePath == DEFAULT_CLIENT_EXPORT_FILE)
            {
                // Make sure we're using a full file path in case we're falling back to default values
                exportFilePath = Path.Combine(GetCurrentDirectory(), DEFAULT_CLIENT_EXPORT_FILE);
            }
            else
            {
                // Normalize file path read from config to ensure properly escaped local path
                exportFilePath = NormalizePath(exportFilePath);
            }

            string exportFileDirectory = Path.GetDirectoryName(exportFilePath);

            if (!Directory.Exists(exportFileDirectory))
            {
                Logger.Warn($"Client export directory \"{exportFileDirectory}\" does not exist, trying to create it");

                try
                {
                    Directory.CreateDirectory(exportFileDirectory);
                } catch (Exception ex)
                {
                    Logger.Error(ex, $"Failed to create client export directory \"{exportFileDirectory}\", falling back to default path");

                    // Failed to create desired client export directory, fall back to default path in current application directory
                    exportFilePath = NormalizePath(Path.Combine(GetCurrentDirectory(), DEFAULT_CLIENT_EXPORT_FILE));
                }
            }

            Task.Factory.StartNew(() =>
            {
                while (!_stop)
                {
                    if (ServerSettingsStore.Instance.GetGeneralSetting(ServerSettingsKeys.CLIENT_EXPORT_ENABLED).BoolValue)
                    {
                        ClientListExport data = new ClientListExport {
                            Clients = _connectedClients.Values, ServerVersion = UpdaterChecker.VERSION
                        };
                        var json = JsonConvert.SerializeObject(data) + "\n";
                        try
                        {
                            File.WriteAllText(exportFilePath, json);
                        }
                        catch (IOException e)
                        {
                            Logger.Error(e);
                        }
                    }
                    Thread.Sleep(5000);
                }
            });
        }
        private void StartExport()
        {
            _stop = false;

            string exportFilePath = ServerSettingsStore.Instance.GetServerSetting(ServerSettingsKeys.CLIENT_EXPORT_FILE_PATH).StringValue;

            if (string.IsNullOrWhiteSpace(exportFilePath) || exportFilePath == DEFAULT_CLIENT_EXPORT_FILE)
            {
                // Make sure we're using a full file path in case we're falling back to default values
                exportFilePath = Path.Combine(GetCurrentDirectory(), DEFAULT_CLIENT_EXPORT_FILE);
            }
            else
            {
                // Normalize file path read from config to ensure properly escaped local path
                exportFilePath = NormalizePath(exportFilePath);
            }

            string exportFileDirectory = Path.GetDirectoryName(exportFilePath);

            if (!Directory.Exists(exportFileDirectory))
            {
                Logger.Warn($"Client export directory \"{exportFileDirectory}\" does not exist, trying to create it");

                try
                {
                    Directory.CreateDirectory(exportFileDirectory);
                } catch (Exception ex)
                {
                    Logger.Error(ex, $"Failed to create client export directory \"{exportFileDirectory}\", falling back to default path");

                    // Failed to create desired client export directory, fall back to default path in current application directory
                    exportFilePath = NormalizePath(Path.Combine(GetCurrentDirectory(), DEFAULT_CLIENT_EXPORT_FILE));
                }
            }

            Task.Factory.StartNew(() =>
            {
                while (!_stop)
                {
                    if (ServerSettingsStore.Instance.GetGeneralSetting(ServerSettingsKeys.CLIENT_EXPORT_ENABLED).BoolValue)
                    {
                        ClientListExport data = new ClientListExport {
                            Clients = _connectedClients.Values, ServerVersion = UpdaterChecker.VERSION
                        };
                        var json = JsonConvert.SerializeObject(data) + "\n";
                        try
                        {
                            File.WriteAllText(exportFilePath, json);
                        }
                        catch (IOException e)
                        {
                            Logger.Error(e);
                        }
                    }
                    Thread.Sleep(5000);
                }
            });

            var udpSocket = new UdpClient();


            Task.Factory.StartNew(() =>
            {
                using (udpSocket)
                {
                    while (!_stop)
                    {
                        try
                        {
                            if (ServerSettingsStore.Instance.GetGeneralSetting(ServerSettingsKeys.LOTATC_EXPORT_ENABLED)
                                .BoolValue)
                            {
                                var host = new IPEndPoint(IPAddress.Parse(ServerSettingsStore.Instance.GetGeneralSetting(ServerSettingsKeys.LOTATC_EXPORT_IP).StringValue),
                                                          ServerSettingsStore.Instance.GetGeneralSetting(ServerSettingsKeys.LOTATC_EXPORT_PORT).IntValue);

                                ClientListExport data = new ClientListExport {
                                    ServerVersion = UpdaterChecker.VERSION, Clients = new List <SRClient>()
                                };

                                foreach (var srClient in _connectedClients.Values)
                                {
                                    if (srClient.isCurrent() && srClient.RadioInfo?.iff != null)
                                    {
                                        data.Clients.Add(new SRClient()
                                        {
                                            ClientGuid = srClient.ClientGuid,
                                            RadioInfo  = new DCSPlayerRadioInfo()
                                            {
                                                radios     = null,
                                                unitId     = srClient.RadioInfo.unitId,
                                                iff        = srClient.RadioInfo.iff,
                                                unit       = srClient.RadioInfo?.unit,
                                                latLng     = srClient?.LatLngPosition,
                                                inAircraft = srClient.RadioInfo.inAircraft,
                                            },
                                            Coalition      = srClient.Coalition,
                                            Name           = srClient?.Name,
                                            LatLngPosition = srClient?.LatLngPosition,
                                            Position       = new DcsPosition()
                                        });
                                    }
                                }

                                var byteData =
                                    Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data) + "\n");

                                udpSocket.Send(byteData, byteData.Length, host);
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Error(e, "Exception Sending LotATC Client Info");
                        }

                        //every 2s
                        Thread.Sleep(2000);
                    }

                    try
                    {
                        udpSocket.Close();
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception stoping LotATC Client Info");
                    }
                }
            });
        }
Beispiel #3
0
        private void StartExport()
        {
            _stop = false;

            string exportFilePath = ServerSettingsStore.Instance.GetServerSetting(ServerSettingsKeys.CLIENT_EXPORT_FILE_PATH).StringValue;

            if (string.IsNullOrWhiteSpace(exportFilePath) || exportFilePath == DEFAULT_CLIENT_EXPORT_FILE)
            {
                // Make sure we're using a full file path in case we're falling back to default values
                exportFilePath = Path.Combine(GetCurrentDirectory(), DEFAULT_CLIENT_EXPORT_FILE);
            }
            else
            {
                // Normalize file path read from config to ensure properly escaped local path
                exportFilePath = NormalizePath(exportFilePath);
            }

            string exportFileDirectory = Path.GetDirectoryName(exportFilePath);

            if (!Directory.Exists(exportFileDirectory))
            {
                Logger.Warn($"Client export directory \"{exportFileDirectory}\" does not exist, trying to create it");

                try
                {
                    Directory.CreateDirectory(exportFileDirectory);
                } catch (Exception ex)
                {
                    Logger.Error(ex, $"Failed to create client export directory \"{exportFileDirectory}\", falling back to default path");

                    // Failed to create desired client export directory, fall back to default path in current application directory
                    exportFilePath = NormalizePath(Path.Combine(GetCurrentDirectory(), DEFAULT_CLIENT_EXPORT_FILE));
                }
            }

            Task.Factory.StartNew(() =>
            {
                while (!_stop)
                {
                    if (ServerSettingsStore.Instance.GetGeneralSetting(ServerSettingsKeys.CLIENT_EXPORT_ENABLED).BoolValue)
                    {
                        ClientListExport data = new ClientListExport {
                            Clients = _connectedClients.Values, ServerVersion = UpdaterChecker.VERSION
                        };
                        var json = JsonConvert.SerializeObject(data, new JsonSerializerSettings {
                            ContractResolver = new JsonNetworkPropertiesResolver()
                        }) + "\n";
                        try
                        {
                            File.WriteAllText(exportFilePath, json);
                        }
                        catch (IOException e)
                        {
                            Logger.Error(e);
                        }
                    }
                    Thread.Sleep(5000);
                }
            });

            var udpSocket = new UdpClient();


            Task.Factory.StartNew(() =>
            {
                using (udpSocket)
                {
                    while (!_stop)
                    {
                        try
                        {
                            if (ServerSettingsStore.Instance.GetGeneralSetting(ServerSettingsKeys.LOTATC_EXPORT_ENABLED)
                                .BoolValue)
                            {
                                var host = new IPEndPoint(IPAddress.Parse(ServerSettingsStore.Instance.GetGeneralSetting(ServerSettingsKeys.LOTATC_EXPORT_IP).StringValue),
                                                          ServerSettingsStore.Instance.GetGeneralSetting(ServerSettingsKeys.LOTATC_EXPORT_PORT).IntValue);

                                ClientListExport data = new ClientListExport {
                                    ServerVersion = UpdaterChecker.VERSION, Clients = new List <SRClient>()
                                };

                                Dictionary <uint, SRClient> firstSeatDict  = new Dictionary <uint, SRClient>();
                                Dictionary <uint, SRClient> secondSeatDict = new Dictionary <uint, SRClient>();

                                foreach (var srClient in _connectedClients.Values)
                                {
                                    if (srClient.RadioInfo?.iff != null)
                                    {
                                        var newClient = new SRClient()
                                        {
                                            ClientGuid = srClient.ClientGuid,
                                            RadioInfo  = new DCSPlayerRadioInfo()
                                            {
                                                radios = null,
                                                unitId = srClient.RadioInfo.unitId,
                                                iff    = srClient.RadioInfo.iff,
                                                unit   = srClient.RadioInfo.unit,
                                            },
                                            Coalition      = srClient.Coalition,
                                            Name           = srClient.Name,
                                            LatLngPosition = srClient?.LatLngPosition,
                                            Seat           = srClient.Seat
                                        };

                                        data.Clients.Add(newClient);

                                        //will need to be expanded as more aircraft have more seats and transponder controls
                                        if (newClient.Seat == 0)
                                        {
                                            firstSeatDict[newClient.RadioInfo.unitId] = newClient;
                                        }
                                        else
                                        {
                                            secondSeatDict[newClient.RadioInfo.unitId] = newClient;
                                        }
                                    }
                                }

                                //now look for other seats and handle the logic
                                foreach (KeyValuePair <uint, SRClient> secondSeatPair in secondSeatDict)
                                {
                                    SRClient firstSeat = null;

                                    firstSeatDict.TryGetValue(secondSeatPair.Key, out firstSeat);
                                    //copy second seat
                                    if (firstSeat != null)
                                    {
                                        //F-14 has RIO IFF Control so use the second seat IFF
                                        if (firstSeat.RadioInfo.unit.StartsWith("F-14"))
                                        {
                                            //copy second to first
                                            firstSeat.RadioInfo.iff = secondSeatPair.Value.RadioInfo.iff;
                                        }
                                        else
                                        {
                                            //copy first to second
                                            secondSeatPair.Value.RadioInfo.iff = firstSeat.RadioInfo.iff;
                                        }
                                    }
                                }

                                var byteData =
                                    Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data, new JsonSerializerSettings {
                                    ContractResolver = new JsonNetworkPropertiesResolver()
                                }) + "\n");

                                udpSocket.Send(byteData, byteData.Length, host);
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Error(e, "Exception Sending LotATC Client Info");
                        }

                        //every 2s
                        Thread.Sleep(2000);
                    }

                    try
                    {
                        udpSocket.Close();
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception stoping LotATC Client Info");
                    }
                }
            });
        }