PageRenderTime 140ms CodeModel.GetById 133ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Manos.IO/Libev/AsyncWatcher.cs

http://github.com/jacksonh/manos
C# | 96 lines | 60 code | 13 blank | 23 comment | 0 complexity | 44324f5debfd339cb4f733898c1e1544 MD5 | raw file
 1//
 2// Copyright (C) 2010 Jackson Harper (jackson@manosdemono.com)
 3//
 4// Permission is hereby granted, free of charge, to any person obtaining
 5// a copy of this software and associated documentation files (the
 6// "Software"), to deal in the Software without restriction, including
 7// without limitation the rights to use, copy, modify, merge, publish,
 8// distribute, sublicense, and/or sell copies of the Software, and to
 9// permit persons to whom the Software is furnished to do so, subject to
10// the following conditions:
11// 
12// The above copyright notice and this permission notice shall be
13// included in all copies or substantial portions of the Software.
14// 
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22//
23//
24using System;
25using System.Runtime.InteropServices;
26using System.Net.Sockets;
27using Manos;
28using Manos.IO;
29
30namespace Libev
31{
32	class AsyncWatcher : Watcher, IAsyncWatcher
33	{
34		private Action<AsyncWatcher, EventTypes> callback;
35		private static UnmanagedWatcherCallback unmanaged_callback;
36
37		static AsyncWatcher ()
38		{
39			unmanaged_callback = StaticCallback;
40		}
41
42		public AsyncWatcher (Loop loop, Action<AsyncWatcher, EventTypes> callback)
43			: base(loop)
44		{
45			this.callback = callback;
46			watcher_ptr = manos_async_watcher_create (unmanaged_callback, GCHandle.ToIntPtr (gc_handle));
47		}
48
49		protected override void DestroyWatcher ()
50		{
51			manos_async_watcher_destroy (watcher_ptr);
52		}
53
54		private static void StaticCallback (IntPtr data, EventTypes revents)
55		{
56			try {
57				var handle = GCHandle.FromIntPtr (data);
58				var watcher = (AsyncWatcher) handle.Target;
59				watcher.callback (watcher, revents);
60			} catch (Exception e) {
61				Console.Error.WriteLine ("Error handling async event: {0}", e.Message);
62				Console.Error.WriteLine (e.StackTrace);
63			}
64		}
65
66		public void Send ()
67		{
68			ev_async_send (Loop.Handle, watcher_ptr);
69		}
70
71		protected override void StartImpl ()
72		{
73			ev_async_start (Loop.Handle, watcher_ptr);
74		}
75
76		protected override void StopImpl ()
77		{
78			ev_async_stop (Loop.Handle, watcher_ptr);
79		}
80
81		[DllImport("libev", CallingConvention = CallingConvention.Cdecl)]
82		private static extern void ev_async_start (IntPtr loop, IntPtr watcher);
83
84		[DllImport("libev", CallingConvention = CallingConvention.Cdecl)]
85		private static extern void ev_async_stop (IntPtr loop, IntPtr watcher);
86
87		[DllImport("libev", CallingConvention = CallingConvention.Cdecl)]
88		private static extern void ev_async_send (IntPtr loop, IntPtr watcher);
89
90		[DllImport("libmanos", CallingConvention = CallingConvention.Cdecl)]
91		private static extern IntPtr manos_async_watcher_create (UnmanagedWatcherCallback callback, IntPtr data);
92
93		[DllImport("libmanos", CallingConvention = CallingConvention.Cdecl)]
94		private static extern void manos_async_watcher_destroy (IntPtr watcher);
95	}
96}