/// <summary> /// Receive data from the group and log it. /// </summary> private void Receive() { // Only attempt to receive if you have already joined the group if (_joined) { Array.Clear(_receiveBuffer, 0, _receiveBuffer.Length); _client.BeginReceiveFromGroup(_receiveBuffer, 0, _receiveBuffer.Length, result => { IPEndPoint source; // Complete the asynchronous operation. The source field will // contain the IP address of the device that sent the message _client.EndReceiveFromGroup(result, out source); if (OnClientFound != null && _receiveBuffer[0] == 1) { byte[] temp = new byte[4]; Array.Copy(_receiveBuffer, 1, temp, 0, 4); OnClientFound(temp); } // Call receive again to continue to "listen" for the next message from the group Receive(); }, null); } else { //Log("Cannot receive. You are currently not joined to the group", true); } }
public Task <ReceivedUdpData> ReceiveAsync() { ThrowIfDisposed(); var taskCompletionSource = new System.Threading.Tasks.TaskCompletionSource <ReceivedUdpData>(); byte[] buffer = new byte[SsdpConstants.DefaultUdpSocketBufferSize]; _UdpClient.BeginReceiveFromGroup(buffer, 0, buffer.Length, (asyncResult) => { IPEndPoint receivedFromEndPoint; _UdpClient.EndReceiveFromGroup(asyncResult, out receivedFromEndPoint); var tcs = asyncResult.AsyncState as System.Threading.Tasks.TaskCompletionSource <ReceivedUdpData>; var result = new ReceivedUdpData() { ReceivedFrom = new UdpEndPoint() { IPAddress = receivedFromEndPoint.Address.ToString(), Port = receivedFromEndPoint.Port }, Buffer = buffer, ReceivedBytes = buffer.Length }; tcs.SetResult(result); }, taskCompletionSource ); return(taskCompletionSource.Task); }
private void OnRecieveFrom() { var buffer = new byte[MAX_BUFFER_SIZE]; socket.BeginReceiveFromGroup(buffer, 0, buffer.Length, BeginReceiveFromGroupCallBack, buffer); }
public Task <ReceivedUdpData> ReceiveAsync() { ThrowIfDisposed(); var taskCompletionSource = new System.Threading.Tasks.TaskCompletionSource <ReceivedUdpData>(); byte[] buffer = new byte[SsdpConstants.DefaultUdpSocketBufferSize]; try { _UdpClient.BeginReceiveFromGroup(buffer, 0, buffer.Length, (asyncResult) => { IPEndPoint receivedFromEndPoint; try { _UdpClient.EndReceiveFromGroup(asyncResult, out receivedFromEndPoint); var tcs = asyncResult.AsyncState as System.Threading.Tasks.TaskCompletionSource <ReceivedUdpData>; var result = new ReceivedUdpData() { ReceivedFrom = new UdpEndPoint() { IPAddress = receivedFromEndPoint.Address.ToString(), Port = receivedFromEndPoint.Port }, Buffer = buffer, ReceivedBytes = buffer.Length }; tcs.SetResult(result); } catch (SocketException se) { if (se.SocketErrorCode == SocketError.Shutdown || se.SocketErrorCode == SocketError.OperationAborted || se.SocketErrorCode == SocketError.NotConnected) { throw new SocketClosedException(se.Message, se); } throw; } }, taskCompletionSource ); } catch (SocketException se) { if (se.SocketErrorCode == SocketError.Shutdown || se.SocketErrorCode == SocketError.OperationAborted || se.SocketErrorCode == SocketError.NotConnected) { throw new SocketClosedException(se.Message, se); } throw; } return(taskCompletionSource.Task); }
/// <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)); }
private void Receive() { // Only attempt to receive if you have already joined the group if (_joined) { Array.Clear(_receiveBuffer, 0, _receiveBuffer.Length); _client.BeginReceiveFromGroup(_receiveBuffer, 0, _receiveBuffer.Length, result => { IPEndPoint source; // Complete the asynchronous operation. The source field will // contain the IP address of the device that sent the message _client.EndReceiveFromGroup(result, out source); // Get the received data from the buffer. string dataReceived = Encoding.UTF8.GetString(_receiveBuffer, 0, _receiveBuffer.Length); string[] msg = dataReceived.Split(','); double latitude; double longitude; //double heading; try { if (Double.TryParse(msg[0], out latitude)) { current.Latitude = latitude; } if (Double.TryParse(msg[1], out longitude)) { current.Longitude = longitude; } //if (Double.TryParse(msg[2], out heading)) //current.Course = heading; } catch { } // Create a log entry. //string message = String.Format("[{0}]: {1}", source.Address.ToString(), dataReceived); // Log it. Log(dataReceived, "sub"); // Call receive again to continue to "listen" for the next message from the group Receive(); }, null); } else { MessageBox.Show("Cant receive - not connected to Multicast group"); } }
/// <summary> /// Extends BeginReceiveFromGroup so that buffer offset of 0 and call to Array.Length are not needed. /// <example> /// udpanysourcemulticastclient.BeginReceiveFromGroup(buffer, callback); /// </example> /// </summary> public static IAsyncResult BeginReceiveFromGroup(this UdpAnySourceMulticastClient udpanysourcemulticastclient, Byte[] buffer, AsyncCallback callback) { if (udpanysourcemulticastclient == null) { throw new ArgumentNullException("udpanysourcemulticastclient"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } return(udpanysourcemulticastclient.BeginReceiveFromGroup(buffer, 0, buffer.Length, callback)); }
private void StartReceiving() { while (IsStarted) { try { client.BeginReceiveFromGroup(mReceived, 0, mReceived.Length, StartReceiving, null); } catch (Exception) { } active.WaitOne(); } }
void GroupJoined(IAsyncResult result) { MulticastData = Encoding.UTF8.GetBytes(MulticastMessage); keepsearching = true; MulticastSocket.BeginSendToGroup(MulticastData, 0, MulticastData.Length, callback_send, null); while (keepsearching) { try { byte[] buffer = new byte[MulticastData.Length]; MulticastSocket.BeginReceiveFromGroup(buffer, 0, buffer.Length, DoneReceiveFromGroup, buffer); } catch (Exception ex) { Debug.WriteLine("Stopped Group read due to " + ex.Message); keepsearching = false; } } }
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); }
private static void Receive() { // Only attempt to receive if you have already joined the group if (_joined) { Array.Clear(_receiveBuffer, 0, _receiveBuffer.Length); _client.BeginReceiveFromGroup(_receiveBuffer, 0, _receiveBuffer.Length, result => { IPEndPoint source; // Complete the asynchronous operation. The source field will // contain the IP address of the device that sent the message _client.EndReceiveFromGroup(result, out source); OnReceivedData(_receiveBuffer); // Call receive again to continue to "listen" for the next message from the group Receive(); }, null); } }
private void Receive() { myClient.BeginReceiveFromGroup(receiveBuffer, 0, receiveBuffer.Length, result => { try { IPEndPoint source; myClient.EndReceiveFromGroup(result, out source); string sourceIPAddress = source.Address.ToString(); Dispatcher.BeginInvoke(() => { count++; textBlock1.Text = count.ToString(); //var b = new WriteableBitmap( width, height ); //for ( int i = 0; i < b.Pixels.Length; i++ ) { // int index = i * 4; // b.Pixels[i] = receiveBuffer[index + 0] << 24 | // receiveBuffer[index + 1] << 16 | // receiveBuffer[index + 2] << 8 | // receiveBuffer[index + 3]; //} //image1.Source = b; var b = new WriteableBitmap(width, height); for (int i = 0; i < skeleton.Length; i++) { int index = i * 4; skeleton[i] = receiveBuffer[index + 3] << 24 | receiveBuffer[index + 2] << 16 | receiveBuffer[index + 1] << 8 | receiveBuffer[index + 0]; } canvas1.Children.Clear(); for (int i = 0; i < skeleton.Length; i += 2) { int x = i; int y = i + 1; //b.Pixels[y * width + x] = 0xFFFFFFF; var e = new Ellipse() { Fill = new SolidColorBrush() { Color = Colors.Red }, Width = 10, Height = 10, }; Canvas.SetLeft(e, skeleton[x]); Canvas.SetTop(e, skeleton[y]); canvas1.Children.Add(e); } }); Receive(); } catch (Exception ex) { Receive(); Dispatcher.BeginInvoke(() => { MessageBox.Show("recv error " + ex.Message); }); } }, null ); }