private static int InvokeInit(IntPtr managed_handle, IntPtr status)
        {
            return(SenderAdapter.InvokeAndUnwrapExceptions(
                       (ApiStatus apiStatus) =>
            {
                SenderAdapter adapter = GetAdapterOrThrow(managed_handle);

                adapter.senderImplementation.Init(apiStatus);
                return apiStatus.ErrorCode;
            },
                       status
                       ));
        }
        private static SenderAdapter GetAdapterOrThrow(IntPtr managed_handle)
        {
            GCHandle      gcHandle = GCHandle.FromIntPtr(managed_handle);
            SenderAdapter adapter  = gcHandle.Target as SenderAdapter;

            // This should never happen, unless there is a bug in the binding implementation.
            if (adapter == null)
            {
                StringBuilder errorBuilder = new StringBuilder(NativeMethods.OpaqueBindingErrorMessage)
                                             .Append("Invalid managed_handle in ISender binding adapter. Expecting non-null RL.Net.Native.SenderAdapter. Got ")
                                             .AppendFormat("{0}.", gcHandle.Target == null ? "null" : gcHandle.Target.GetType().FullName);

                throw new RLException(errorBuilder.ToString());
            }

            return(adapter);
        }
        private static int InvokeSend(IntPtr managed_handle, IntPtr buffer, IntPtr status)
        {
            return(SenderAdapter.InvokeAndUnwrapExceptions(
                       (ApiStatus apiStatus) =>
            {
                SenderAdapter adapter = GetAdapterOrThrow(managed_handle);
                SharedBuffer sharedBuffer = new SharedBuffer(buffer);

                // Buffer must live at least as long as the underlying call.
                unsafe {
                    adapter.senderImplementation.Send(sharedBuffer, apiStatus);
                }

                return apiStatus.ErrorCode;
            },
                       status
                       ));
        }