Beispiel #1
0
        protected void OnMessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
        {
            string remoteAddress = args.RemoteAddress.CanonicalName;

            // Reject messages from this computer
            if (remoteAddress == information.LocalAddress)
            {
                return;
            }

            DataReader reader = args.GetDataReader();

            byte[] data = new byte[reader.UnconsumedBufferLength];
            reader.ReadBytes(data);

            EndpointInformation message = EndpointInformation.Deserialize(data);

            if (message != null)
            {
                // Did message originate from a server?
                if (message.Type == EndpointType.Server)
                {
                    ServerDiscovered?.Invoke(this, message);
                }
            }
        }
Beispiel #2
0
        private async void OnMessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
        {
            string remoteAddress = args.RemoteAddress.CanonicalName;

            // Reject messages from this computer
            if (remoteAddress == information.LocalAddress)
            {
                return;
            }

            using (DataReader reader = args.GetDataReader())
            {
                byte[] data = new byte[reader.UnconsumedBufferLength];
                reader.ReadBytes(data);

                EndpointInformation message = EndpointInformation.Deserialize(data);

                if (message != null)
                {
                    // Did message originate from a client?
                    if (message.Type == EndpointType.Client)
                    {
                        message = new EndpointInformation()
                        {
                            Address = information.LocalAddress,
                            Type    = EndpointType.Server
                        };

                        await SendMessage(message);
                    }
                }
            }
        }
Beispiel #3
0
        public async Task Discover()
        {
            if (socket != null)
            {
                throw new InvalidOperationException();
            }

            EndpointInformation message = new EndpointInformation()
            {
                Address = information.LocalAddress,
                Type    = EndpointType.Client
            };

            await SendMessage(message);
        }
Beispiel #4
0
        protected async Task SendMessage(EndpointInformation message)
        {
            EndpointPair endpoint = new EndpointPair(
                new HostName(information.LocalAddress),
                information.MulticastPort,
                new HostName(information.MulticastAddress),
                information.MulticastPort
                );

            using (IOutputStream outputStream = await socket.GetOutputStreamAsync(endpoint))
                using (DataWriter writer = new DataWriter(outputStream))
                {
                    byte[] data = message.Serialize();
                    writer.WriteBytes(data);
                    await writer.StoreAsync();
                }
        }