private static UdpAnySourceMulticastClient CreateMulticastClientAndJoinGroup(string ipAddress, int localPort) { var retVal = new UdpAnySourceMulticastClient(IPAddress.Parse(ipAddress), localPort); var signal = new System.Threading.ManualResetEvent(false); try { retVal.BeginJoinGroup( (joinResult) => { retVal.EndJoinGroup(joinResult); retVal.MulticastLoopback = true; retVal.SendBufferSize = retVal.ReceiveBufferSize = SsdpConstants.DefaultUdpSocketBufferSize; signal.Set(); }, null); signal.WaitOne(); } finally { signal.Dispose(); } return(retVal); }
internal static void Join() { // Initialize the receive buffer _receiveBuffer = new byte[MAX_MESSAGE_SIZE]; // Create the UdpAnySourceMulticastClient instance using the defined // GROUP_ADDRESS and GROUP_PORT constants. UdpAnySourceMulticastClient is a // client receiver for multicast traffic from any source, also known as Any Source Multicast (ASM) _client = new UdpAnySourceMulticastClient(IPAddress.Parse(GROUP_ADDRESS), GROUP_PORT); // Make a request to join the group. _client.BeginJoinGroup( result => { // Complete the join _client.EndJoinGroup(result); // The MulticastLoopback property controls whether you receive multicast // packets that you send to the multicast group. Default value is true, // meaning that you also receive the packets you send to the multicast group. // To stop receiving these packets, you can set the property following to false _client.MulticastLoopback = true; // Set a flag indicating that we have now joined the multicast group _joined = true; Receive(); }, null); }
public void StartFinder() { try { _receiveBuffer = new byte[MAX_MESSAGE_SIZE]; // Create the UdpAnySourceMulticastClient instance using the defined // GROUP_ADDRESS and GROUP_PORT constants. UdpAnySourceMulticastClient is a // client receiver for multicast traffic from any source, also known as Any Source Multicast (ASM) _client = new UdpAnySourceMulticastClient(IPAddress.Parse(GROUP_ADDRESS), GROUP_PORT); // Make a request to join the group. _client.BeginJoinGroup( result => { // Complete the join _client.EndJoinGroup(result); // The MulticastLoopback property controls whether you receive multicast // packets that you send to the multicast group. Default value is true, // meaning that you also receive the packets you send to the multicast group. // To stop receiving these packets, you can set the property following to false _client.MulticastLoopback = false; // Set a flag indicating that we have now joined the multicast group _joined = true; // Wait for data from the group. This is an asynchronous operation // and will not block the UI thread. Receive(); }, null); } catch { } }
/// <summary> /// Extends BeginReceiveFromGroup so that when a state object is not needed, null does not need to be passed. /// <example> /// udpanysourcemulticastclient.BeginReceiveFromGroup(buffer, offset, count, callback); /// </example> /// </summary> public static IAsyncResult BeginReceiveFromGroup(this UdpAnySourceMulticastClient udpanysourcemulticastclient, Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback) { if (udpanysourcemulticastclient == null) { throw new ArgumentNullException("udpanysourcemulticastclient"); } return(udpanysourcemulticastclient.BeginReceiveFromGroup(buffer, offset, count, callback, null)); }
/// <summary> /// Extends BeginJoinGroup so that when a state object is not needed, null does not need to be passed. /// <example> /// udpanysourcemulticastclient.BeginJoinGroup(callback); /// </example> /// </summary> public static IAsyncResult BeginJoinGroup(this UdpAnySourceMulticastClient udpanysourcemulticastclient, AsyncCallback callback) { if (udpanysourcemulticastclient == null) { throw new ArgumentNullException("udpanysourcemulticastclient"); } return(udpanysourcemulticastclient.BeginJoinGroup(callback, null)); }
/// <summary> /// Extends BeginSendTo so that when a state object is not needed, null does not need to be passed. /// <example> /// udpanysourcemulticastclient.BeginSendTo(buffer, offset, count, remoteEndPoint, callback); /// </example> /// </summary> public static IAsyncResult BeginSendTo(this UdpAnySourceMulticastClient udpanysourcemulticastclient, Byte[] buffer, Int32 offset, Int32 count, System.Net.IPEndPoint remoteEndPoint, AsyncCallback callback) { if (udpanysourcemulticastclient == null) { throw new ArgumentNullException("udpanysourcemulticastclient"); } return(udpanysourcemulticastclient.BeginSendTo(buffer, offset, count, remoteEndPoint, callback, null)); }
/// <summary> /// Extends BeginSendToGroup so that buffer offset of 0 and call to Array.Length are not needed. /// <example> /// udpanysourcemulticastclient.BeginSendToGroup(buffer, callback, state); /// </example> /// </summary> public static IAsyncResult BeginSendToGroup(this UdpAnySourceMulticastClient udpanysourcemulticastclient, Byte[] buffer, AsyncCallback callback, Object state) { if (udpanysourcemulticastclient == null) { throw new ArgumentNullException("udpanysourcemulticastclient"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } return(udpanysourcemulticastclient.BeginSendToGroup(buffer, 0, buffer.Length, callback, state)); }
/// <summary> /// Extends BeginSendTo so that buffer offset of 0 and call to Array.Length are not needed. /// <example> /// udpanysourcemulticastclient.BeginSendTo(buffer, remoteEndPoint, callback); /// </example> /// </summary> public static IAsyncResult BeginSendTo(this UdpAnySourceMulticastClient udpanysourcemulticastclient, Byte[] buffer, System.Net.IPEndPoint remoteEndPoint, AsyncCallback callback) { if (udpanysourcemulticastclient == null) { throw new ArgumentNullException("udpanysourcemulticastclient"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } return(udpanysourcemulticastclient.BeginSendTo(buffer, 0, buffer.Length, remoteEndPoint, callback)); }
private static void CloseSharedChannel() { IsJoined = false; var client = _client; if (client != null) { try { client.Dispose(); } catch { } _client = null; _log.Info("Closed shared MDNS channel."); } }
private static async Task OpenSharedChannelAsync() { await _mutex.WaitAsync().ConfigureAwait(false); if (IsJoined || !_shouldJoinChannel) { return; } CloseSharedChannel(); _log.Info("Opening shared MDNS channel..."); while (!IsJoined && _shouldJoinChannel) { try { _client = new UdpAnySourceMulticastClient(IPAddress.Parse(BonjourUtility.MulticastDNSAddress), BonjourUtility.MulticastDNSPort); _log.Debug("Joining multicast group..."); await _client.JoinGroupAsync().ConfigureAwait(false); IsJoined = true; BeginReceiveMessageLoop(); } catch (Exception e) { _log.Warning("Caught exception while opening UDP socket"); _log.Debug("Exception details: " + e.ToString()); CloseSharedChannel(); } if (!IsJoined) { await TaskEx.Delay(1000).ConfigureAwait(false); } else { _log.Info("Successfully opened shared MDNS channel."); } } _mutex.Release(); }
public void Find(Action <IPAddress> callback) { FoundCallback = callback; MulticastSocket = new UdpAnySourceMulticastClient(IPAddress.Parse("239.255.255.250"), PortNumber); MulticastSocket.BeginJoinGroup((result) => { try { MulticastSocket.EndJoinGroup(result); GroupJoined(result); } catch (Exception ex) { Debug.WriteLine("EndjoinGroup exception {0}", ex.Message); // This can happen eg when wifi is off FoundCallback(null); } }, null); }
public IPAddress Find() { WaitEvent.Reset(); MulticastSocket = new UdpAnySourceMulticastClient(IPAddress.Parse("239.255.255.250"), PortNumber); MulticastSocket.BeginJoinGroup((result) => { try { MulticastSocket.EndJoinGroup(result); GroupJoined(result); } catch (Exception ex) { WaitEvent.Set(); Debug.WriteLine("EndjoinGroup exception {0}", ex.Message); } }, null); WaitEvent.WaitOne(); return(FoundIPAddress); }
public static Task SendToGroupAsync(this UdpAnySourceMulticastClient client, byte[] buffer, int offset, int count) { return(Task.Factory.FromAsync(client.BeginSendToGroup, client.EndSendToGroup, buffer, offset, count, null)); }
public static Task JoinGroupAsync(this UdpAnySourceMulticastClient client) { return(Task.Factory.FromAsync(client.BeginJoinGroup, client.EndJoinGroup, null)); }
public MyUdpAnySourceMulticastClient() { socket = new UdpAnySourceMulticastClient(IPAddress.Parse(GROUP_ADDRESS), GROUP_PORT); }
/// <summary> /// Initializes a new instance of the <see cref="UdpAnySourceMulticastchannel"/> class. /// </summary> /// <param name="address">The address.</param> /// <param name="port">The port.</param> /// <param name="maxMessageSize">Size of the max message.</param> public UdpAnySourceMulticastChannel(IPAddress address, int port, int maxMessageSize) { this.ReceiveBuffer = new byte[maxMessageSize]; this.Client = new UdpAnySourceMulticastClient(address, port); }
public MDnsClient(IPEndPoint endpoint) { local = endpoint; client = new UdpAnySourceMulticastClient(endpoint.Address, endpoint.Port); receiver = new Thread(StartReceiving); }
public MulticastUdpSocket(int localPort) { _UdpClient = CreateMulticastClientAndJoinGroup(SsdpConstants.MulticastLocalAdminAddress, localPort); }
public MDnsClient(IPEndPoint endpoint) { local = endpoint; client = new UdpAnySourceMulticastClient(endpoint.Address, endpoint.Port); }
public static Task <UdpAnySourceMulticastClientReceieveFromGroupResult> ReceiveFromGroupAsync(this UdpAnySourceMulticastClient client, byte[] buffer, int offset, int count) { var taskCompletionSource = new TaskCompletionSource <UdpAnySourceMulticastClientReceieveFromGroupResult>(); client.BeginReceiveFromGroup(buffer, offset, count, asyncResult => { try { IPEndPoint source; int length = client.EndReceiveFromGroup(asyncResult, out source); taskCompletionSource.TrySetResult(new UdpAnySourceMulticastClientReceieveFromGroupResult(length, source)); } catch (Exception ex) { taskCompletionSource.TrySetException(ex); } }, null); return(taskCompletionSource.Task); }