private void ReceiveCallback(IAsyncResult ar) { try { //Debug.Log("[UDP multicast] Start ReceiveCallback"); // Retrieve the state object and the client socket // from the asynchronous state object. DirectStateObject state = (DirectStateObject)ar.AsyncState; Socket client = state.workSocket; // Read data from the remote device. int bytesRead = client.EndReceive(ar); if (bytesRead > 0 && _isIsActiveThread) { if (!_optiTrackManager.IsFramerateLocked() || (_optiTrackManager.IsFramerateLocked() && !receiveLocked)) { ReadPacket(state.buffer); Lock(); //Debug.Log("read frame"); } else { // Debug.Log("discard frame"); } //client.Shutdown(SocketShutdown.Both); //client.Close(); client.BeginReceive(state.buffer, 0, DirectStateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } else { Debug.LogWarning("[UDP] - End ReceiveCallback OptiTrack"); if (_isIsActiveThread == false) { Debug.LogWarning("[UDP] - Closing port OptiTrack"); _isInitRecieveStatus = false; //client.Shutdown(SocketShutdown.Both); client.Close(); } // Signal that all bytes have been received. } } catch (Exception) { //Debug.LogError(e.ToString()); } }
private bool Receive(Socket client) { try { // Create the state object. DirectStateObject state = new DirectStateObject(); state.workSocket = client; Debug.Log("[UDP multicast] Receive OptiTrack"); // Begin receiving the data from the remote device. client.BeginReceive(state.buffer, 0, DirectStateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } catch (Exception e) { Debug.Log(e.ToString()); return(false); } return(true); }