/// <summary> /// Callback function for sender ready event /// </summary> /// <param name="ar"></param> private static void SendCallback(IAsyncResult ar) { // Retrieve the communication object from the state object. UDPCommunicator communicator = (UDPCommunicator)ar.AsyncState; try { // Retrieve socket Socket client = communicator.m_client; // Complete sending the data to the remote device. int bytes_sent = client.EndSend(ar); Interlocked.Add(ref communicator.m_downstream_bytes, bytes_sent); // Signal that all bytes have been sent. communicator.m_sender_lock.Release(); // signal communication manager about the freed buffer communicator.m_communication_manager.GenerateEvent(); } catch { // TODO: Error handling } }
/// <summary> /// Callback function for data received event /// </summary> /// <param name="ar"></param> private static void ReceiveCallback(IAsyncResult ar) { // Retrieve the communication object from the state object. UDPCommunicator communicator = (UDPCommunicator)ar.AsyncState; try { // Retrieve socket Socket client = communicator.m_client; // Read data from the remote device. int bytes_read = client.EndReceive(ar); if (bytes_read > 0) { communicator.m_received_data_length = bytes_read; communicator.m_thread_event.Set(); } else { } } catch { // TODO: Error handling } }