// This file was generated by the Gtk# code generator.
// Any changes made will be lost if regenerated.

namespace Gst.RtspServerSharp {

	using System;
	using System.Runtime.InteropServices;

#region Autogenerated code
	[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
	internal delegate bool RTSPSendFuncNative(IntPtr buffer, byte channel, IntPtr user_data);

	internal class RTSPSendFuncInvoker {

		RTSPSendFuncNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

		~RTSPSendFuncInvoker ()
		{
			if (__notify == null)
				return;
			__notify (__data);
		}

		internal RTSPSendFuncInvoker (RTSPSendFuncNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal RTSPSendFuncInvoker (RTSPSendFuncNative native_cb, IntPtr data) : this (native_cb, data, null) {}

		internal RTSPSendFuncInvoker (RTSPSendFuncNative native_cb, IntPtr data, GLib.DestroyNotify notify)
		{
			this.native_cb = native_cb;
			__data = data;
			__notify = notify;
		}

		internal Gst.RtspServer.RTSPSendFunc Handler {
			get {
				return new Gst.RtspServer.RTSPSendFunc(InvokeNative);
			}
		}

		bool InvokeNative (Gst.Buffer buffer, byte channel)
		{
			bool __result = native_cb (buffer == null ? IntPtr.Zero : buffer.Handle, channel, __data);
			return __result;
		}
	}

	internal class RTSPSendFuncWrapper {

		public bool NativeCallback (IntPtr buffer, byte channel, IntPtr user_data)
		{
			try {
				bool __ret = managed (buffer == IntPtr.Zero ? null : (Gst.Buffer) GLib.Opaque.GetOpaque (buffer, typeof (Gst.Buffer), false), channel);
				if (release_on_call)
					gch.Free ();
				return __ret;
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, false);
				return false;
			}
		}

		bool release_on_call = false;
		GCHandle gch;

		public void PersistUntilCalled ()
		{
			release_on_call = true;
			gch = GCHandle.Alloc (this);
		}

		internal RTSPSendFuncNative NativeDelegate;
		Gst.RtspServer.RTSPSendFunc managed;

		public RTSPSendFuncWrapper (Gst.RtspServer.RTSPSendFunc managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new RTSPSendFuncNative (NativeCallback);
		}

		public static Gst.RtspServer.RTSPSendFunc GetManagedDelegate (RTSPSendFuncNative native)
		{
			if (native == null)
				return null;
			RTSPSendFuncWrapper wrapper = (RTSPSendFuncWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
