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); } } }
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); } } } }
public async Task Discover() { if (socket != null) { throw new InvalidOperationException(); } EndpointInformation message = new EndpointInformation() { Address = information.LocalAddress, Type = EndpointType.Client }; await SendMessage(message); }
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(); } }