示例#1
0
        private static List <Task> StartInteractive(KissCommunication kisser, string srcAddress, string serialAddress)
        {
            List <Task> tasks = new List <Task>();

            if (!string.IsNullOrEmpty(srcAddress))
            {
                tasks.Add(Task.Run(() => kisser.Connect(srcAddress)));
            }
            if (!string.IsNullOrEmpty(serialAddress))
            {
                tasks.Add(Task.Run(() =>
                {
                    kisser.ConnectSerial(serialAddress);
                }));
            }
            StartPingLoop();
            return(tasks);
        }
示例#2
0
        static void Main(string[] args)
        {
#if DEBUG
            for (int i = 0; i < args.Length; i++)
            {
                OutputWriter.WriteLine($"{i}: {args[i]}");
            }
#endif
            Stream nps = null;

            if (!(args?.Length > 0))
            {
                Help();
                return;
            }

            TaskScheduler.UnobservedTaskException      += TaskScheduler_UnobservedTaskException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            AppDomain.CurrentDomain.ProcessExit        += CurrentDomain_ProcessExit;

            ParseArgs(args);

            FileStream npsFs            = null;
            bool       npsIsStandardOut = true;

            if (!string.IsNullOrEmpty(_dbFn))
            {
                _dataStore = new LocalDatastore(_dbFn);
            }

            List <Task> runTasks;

            _serverPosters = _destUrls.Select(url =>
            {
                var ret          = new DataPosting(url);
                ret.Offset       = _offset;
                ret.OnException += (sender, ex) =>
                                   ErrorWriter.WriteLine($"Post error ({url}): {ex.GetType()}: {ex.Message}");
                return(ret);
            }).ToList();

            if (_connectUDP)
            {
                var udpListener = new SocketListener(_incomingPort.Value, _outgoingPort.Value, _outgoingPort.Value + 2, false);
                _dataReceiver = udpListener;
                runTasks      = udpListener.StartAsync(CancellationToken.None);
            }
            else
            {
                var communicator = new KissCommunication();
                _dataReceiver = communicator;

                npsIsStandardOut = string.IsNullOrEmpty(_npsFn);
                if (npsIsStandardOut)
                {
                    nps = Console.OpenStandardOutput();
                }
                else if (!_npsFn.Equals("DISCARD", StringComparison.OrdinalIgnoreCase))
                {
                    npsFs = new FileStream(_npsFn, FileMode.Append, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, 4096);
                    nps   = Stream.Synchronized(npsFs);
                    FlushForever(nps);
                }
                communicator.NonPacketStream = nps;

                if (!Console.IsInputRedirected)
                {
                    runTasks = StartInteractive(communicator, _srcNetworkAddress, _serialAddress);
                }
                else
                {
                    runTasks = StartNonInteractive(communicator, _srcNetworkAddress, _serialAddress);
                }

                if (_outgoingPort.HasValue && _incomingPort.HasValue)
                {
                    var udpListener = new SocketListener(_incomingPort.Value, _outgoingPort.Value, _incomingPort.Value + 2, true);
                    runTasks.AddRange(udpListener.StartAsync(_exitingSource.Token));
                    communicator.PacketReceived += (sender, e) => udpListener.Write(e.ToArray());
                    udpListener.PacketReceived  += (sender, e) => communicator.Write(e.ToArray());
                    udpListener.PacketReceived6 += (sender, e) => communicator.Write(e.ToArray(), 6);
                }
            }

            _dataReceiver.PacketReceived += OnPacketReceived;

            try
            {
                if (!Console.IsInputRedirected)
                {
                    Console.WriteLine("Running Interactively...");
                    RunInteractive(npsIsStandardOut);
                }
                else
                {
                    Console.WriteLine("Running non-interactively");
                }
                Task.WaitAll(runTasks.ToArray());
            }
            finally
            {
                if (npsFs != null)
                {
                    npsFs.Flush();
                    npsFs.Close();
                }
            }
        }