Ejemplo n.º 1
0
        public async void Dispose()
        {
            var peer = new FeatherUdpPeer <BinaryMessage>();

            Assert.False(peer.IsDisposed);
            peer.Bind(12347);
            Assert.False(peer.IsDisposed);
            peer.Dispose();
            Assert.True(peer.IsDisposed);

            Assert.Throws <ObjectDisposedException>(() => {
                peer.SendTo(new IPEndPoint(IPAddress.Loopback, 12346), TestMessage1);
            });

            await Assert.ThrowsAsync <ObjectDisposedException>(async() => {
                await peer.SendToAsync(new IPEndPoint(IPAddress.Loopback, 12346), TestMessage1);
            });
        }
Ejemplo n.º 2
0
        public void ReceiveMessage()
        {
            var stage = 0;
            var block = new AutoResetEvent(false);

            using (var peer = new FeatherUdpPeer <BinaryMessage>()) {
                peer.OnMessageReceived += (endpoint, message) => {
                    if (stage == 0)
                    {
                        Assert.Equal(TestPayload1, message.Export().ToArray());
                        stage = 1;
                    }
                    else if (stage == 1)
                    {
                        Assert.Equal(TestPayload2, message.Export().ToArray());
                        stage = 2;
                        block.Set();
                    }
                    else
                    {
                        Assert.False(true);
                    }
                };
                peer.Bind(12348);

                using (var socket = new Socket(SocketType.Dgram, ProtocolType.Udp)) {
                    socket.SendTo(TestPayload1, new IPEndPoint(IPAddress.Loopback, 12348));
                    Thread.Sleep(10);
                    socket.SendTo(TestPayload2, new IPEndPoint(IPAddress.Loopback, 12348));
                }
            }

            block.WaitOne(1000);

            Assert.Equal(2, stage);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("What is your name?");
            var myName = Console.ReadLine();

            using (var peer = new FeatherUdpPeer <GenericMessage>()) {
                // Watch for messages to arrive
                peer.OnMessageReceived += (endpoint, message) => {
                    var name = message.ReadString();
                    var body = message.ReadString();
                    Console.WriteLine($"{name}> {body}");
                };

                // Bind socket to receive messages
                peer.Bind(12345);

                // Loop sending messages
                Console.WriteLine("Type message to send.");
                while (true)
                {
                    var body = Console.ReadLine();
                    if (body == string.Empty)
                    {
                        break;
                    }

                    // Create message
                    var message = new GenericMessage();
                    message.WriteString(myName);
                    message.WriteString(body);

                    // Broadcast message on the local network (similarly, we could send to a specific address on the internet instead)
                    peer.SendTo(new IPEndPoint(IPAddress.Broadcast, 12345), message);
                }
            }
        }