PageRenderTime 378ms CodeModel.GetById 370ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 1ms

/src/Manos.IO/Libev/Loop.cs

http://github.com/jacksonh/manos
C# | 131 lines | 99 code | 32 blank | 0 comment | 11 complexity | 6a0077851d7e12db22ae68acf885ec96 MD5 | raw file
  1using System;
  2using System.Runtime.InteropServices;
  3using System.Net.Sockets;
  4
  5namespace Libev
  6{
  7	class Loop
  8	{
  9		private IntPtr _native;
 10		private GCHandle _handle;
 11		private static readonly bool _isV4;
 12
 13		static Loop ()
 14		{
 15			_isV4 = ev_version_major () >= 4;
 16		}
 17
 18		public static bool IsV4 { get { return _isV4; } }
 19
 20		public Loop ()
 21		{
 22			int backends = ev_supported_backends ();
 23			if (backends == 0)
 24				throw new InvalidOperationException ("No supported backend in libev");
 25			
 26			_native = ev_loop_new (0);
 27			
 28			if (_native == IntPtr.Zero)
 29				throw new Exception ("Unable to create native loop");
 30			
 31			_handle = GCHandle.Alloc (this);
 32		}
 33
 34		~Loop ()
 35		{
 36			Dispose ();
 37		}
 38
 39		public IntPtr Handle {
 40			get {
 41				ThrowIfDisposed ();
 42				return _native; 
 43			}
 44		}
 45
 46		public void Dispose ()
 47		{
 48			if (_native == IntPtr.Zero)
 49				return;
 50			
 51			ev_loop_destroy (_native);
 52			_native = IntPtr.Zero;
 53			_handle.Free ();
 54		}
 55
 56		public void RunBlocking ()
 57		{
 58			ThrowIfDisposed ();
 59			
 60			Run (LoopType.Blocking);
 61		}
 62
 63		public void RunNonBlocking ()
 64		{
 65			ThrowIfDisposed ();
 66			
 67			Run (LoopType.NonBlocking);
 68		}
 69
 70		public void RunOneShot ()
 71		{
 72			ThrowIfDisposed ();
 73			
 74			Run (LoopType.Oneshot);
 75		}
 76
 77		public void Run (LoopType type)
 78		{
 79			ThrowIfDisposed ();
 80
 81			if (IsV4)
 82				ev_run (_native, type);
 83			else
 84				ev_loop (_native, type);
 85		}
 86
 87		public void Unloop (UnloopType type)
 88		{
 89			ThrowIfDisposed ();
 90
 91			if (IsV4)
 92				ev_break (_native, type);
 93			else 
 94				ev_unloop (_native, type);
 95		}
 96
 97		private void ThrowIfDisposed ()
 98		{
 99			if (_native == IntPtr.Zero)
100				throw new ObjectDisposedException ("native object has been disposed.");
101		}
102
103		[DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
104		private static extern int ev_version_major ();
105
106		[DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
107		private static extern int ev_version_minor ();
108
109		[DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
110		private static extern IntPtr ev_loop_new (uint flags);
111
112		[DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
113		private static extern void ev_loop (IntPtr loop, LoopType type);
114
115		[DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
116		private static extern void ev_loop_destroy (IntPtr loop);
117
118		[DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
119		private static extern void ev_unloop (IntPtr loop, UnloopType flags);
120
121		[DllImport("libev", CallingConvention = CallingConvention.Cdecl)]
122		private static extern void ev_run (IntPtr loop, LoopType type);
123
124		[DllImport("libev", CallingConvention = CallingConvention.Cdecl)]
125		private static extern void ev_break (IntPtr loop, UnloopType flags);
126
127		[DllImport("libev", CallingConvention = CallingConvention.Cdecl)]
128		private static extern int ev_supported_backends ();
129	}
130}
131