public async Task CanSendAndReceivePsnPackets()
        {
            var server = new PsnServer("Test System", IPAddress.Loopback);

            var trackers = new[]
            {
                new PsnTracker(0, "Tracker Foo", Tuple.Create(1f, 2f, 3f), timestamp: 2452352452),
                new PsnTracker(1, "Tracker Bar", Tuple.Create(4f, 5f, 6f), Tuple.Create(7f, 8f, 9f))
            }.ToList();

            server.SetTrackers(trackers);

            var client = new PsnClient(IPAddress.Loopback);

            client.StartListening();

            using (var monitoredClient = client.Monitor())
            {
                server.SendData();
                server.SendInfo();

                await Task.Delay(100);

                monitoredClient.Should().Raise(nameof(PsnClient.DataPacketReceived));
                monitoredClient.Should().Raise(nameof(PsnClient.InfoPacketReceived));
            }

            var receivedTackers = client.Trackers.Values.ToList();

            receivedTackers.Should().BeEquivalentTo(trackers, options =>
                                                    options.Excluding(o => o.DataLastReceived).Excluding(o => o.InfoLastReceived));

            client.Dispose();
            server.Dispose();
        }
예제 #2
0
        public static void Main(string[] args)
        {
            Console.WriteLine(new string('*', Console.WindowWidth - 1));
            Console.WriteLine("DBDesign.PosiStageDotNet.Client Example");
            Console.WriteLine(new string('*', Console.WindowWidth - 1));
            Console.WriteLine();

            PsnClient client;

            switch (args.Length)
            {
            case 0:
                client = new PsnClient(IPAddress.Loopback);
                Console.WriteLine(
                    $"Listening on default multicast IP '{PsnClient.DefaultMulticastIp}', default port {PsnClient.DefaultPort}");
                break;

            case 1:
            case 2:
            {
                IPAddress ip;

                if (!IPAddress.TryParse(args[0], out ip))
                {
                    Console.WriteLine($"Invalid IP address value '{args[0]}'");
                    return;
                }

                int port;

                if (args.Length == 2)
                {
                    if (!int.TryParse(args[1], out port))
                    {
                        Console.WriteLine($"Invalid UDP port value '{args[1]}'");
                        return;
                    }

                    if (port < ushort.MinValue + 1 || port > ushort.MaxValue)
                    {
                        Console.WriteLine($"UDP port value out of valid range '{args[1]}'");
                        return;
                    }
                }
                else
                {
                    port = PsnClient.DefaultPort;
                }

                client = new PsnClient(ip, PsnClient.DefaultMulticastIp, port);
                Console.WriteLine(
                    $"Listening on custom multicast IP '{PsnClient.DefaultMulticastIp}', custom port {PsnClient.DefaultPort}");
            }
            break;

            default:
                Console.WriteLine(
                    "Invalid args. Format is 'Imp.PosiStageDotNet.Client [CustomMulticastIP] [CustomPort]");
                Console.WriteLine("E.g. 'Imp.PosiStageDotNet.Client 236.10.10.10 56565");
                return;
            }

            Console.WriteLine("Press any key to exit");
            Console.WriteLine("");
            Console.WriteLine(new string('*', Console.WindowWidth - 1));
            Console.WriteLine("");

            client.StartListening();

            while (!Console.KeyAvailable)
            {
                if (client.Trackers.Any())
                {
                    Console.WriteLine(new string('*', Console.WindowWidth - 1));
                    Console.WriteLine("");

                    foreach (var pair in client.Trackers)
                    {
                        Console.WriteLine(pair.Value);
                        Console.WriteLine("");
                    }

                    Console.WriteLine(new string('*', Console.WindowWidth - 1));
                    Console.WriteLine("");
                }

                Thread.Sleep(1000);
            }

            Console.ReadKey();

            Console.WriteLine("");
            Console.WriteLine(new string('*', Console.WindowWidth - 1));
            Console.WriteLine("");

            Console.WriteLine("Disposing client...");

            client.Dispose();

            Console.WriteLine("Client disposed. Exiting...");
            Console.WriteLine("");
            Console.WriteLine(new string('*', Console.WindowWidth - 1));
            Console.WriteLine("");
        }