PageRenderTime 137ms CodeModel.GetById 131ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Manos.IO/Libev/IdleWatcher.cs

http://github.com/jacksonh/manos
C# | 65 lines | 52 code | 13 blank | 0 comment | 0 complexity | e6f01fa3348c0c3860f809e89337b7f5 MD5 | raw file
 1using System;
 2using System.Runtime.InteropServices;
 3using System.Net.Sockets;
 4using Manos.IO;
 5
 6namespace Libev
 7{
 8	class IdleWatcher : Watcher, IIdleWatcher
 9	{
10		private Action<IdleWatcher, EventTypes> callback;
11		private static UnmanagedWatcherCallback unmanaged_callback;
12
13		static IdleWatcher ()
14		{
15			unmanaged_callback = StaticCallback;
16		}
17
18		public IdleWatcher (Loop loop, Action<IdleWatcher, EventTypes> callback) : base (loop)
19		{
20			this.callback = callback;
21			
22			watcher_ptr = manos_idle_watcher_create (unmanaged_callback, GCHandle.ToIntPtr (gc_handle));
23		}
24
25		protected override void DestroyWatcher ()
26		{
27			manos_idle_watcher_destroy (watcher_ptr);
28		}
29
30		private static void StaticCallback (IntPtr data, EventTypes revents)
31		{
32			try {
33				var handle = GCHandle.FromIntPtr (data);
34				var watcher = (IdleWatcher) handle.Target;
35				watcher.callback (watcher, revents);
36			} catch (Exception e) {
37				Console.Error.WriteLine ("Error handling idle event: {0}", e.Message);
38				Console.Error.WriteLine (e.StackTrace);
39			}
40		}
41
42		protected override void StartImpl ()
43		{
44			ev_idle_start (Loop.Handle, watcher_ptr);
45		}
46
47		protected override void StopImpl ()
48		{
49			ev_idle_stop (Loop.Handle, watcher_ptr);
50		}
51
52		[DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
53		private static extern void ev_idle_start (IntPtr loop, IntPtr watcher);
54
55		[DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
56		private static extern void ev_idle_stop (IntPtr loop, IntPtr watcher);
57
58		[DllImport ("libmanos", CallingConvention = CallingConvention.Cdecl)]
59		private static extern IntPtr manos_idle_watcher_create (UnmanagedWatcherCallback callback, IntPtr data);
60
61		[DllImport ("libmanos", CallingConvention = CallingConvention.Cdecl)]
62		private static extern void manos_idle_watcher_destroy (IntPtr watcher);
63	}
64}
65