/src/Manos.IO/Libev/PrepareWatcher.cs

http://github.com/jacksonh/manos · C# · 65 lines · 52 code · 13 blank · 0 comment · 0 complexity · be34c77eee23d9e1396015dbb10789c9 MD5 · raw file

  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Manos;
  4. using Manos.IO;
  5. namespace Libev
  6. {
  7. class PrepareWatcher : Watcher, IPrepareWatcher
  8. {
  9. private Action<PrepareWatcher, EventTypes> callback;
  10. private static UnmanagedWatcherCallback unmanaged_callback;
  11. static PrepareWatcher ()
  12. {
  13. unmanaged_callback = StaticCallback;
  14. }
  15. public PrepareWatcher (Loop loop, Action<PrepareWatcher, EventTypes> callback) : base (loop)
  16. {
  17. this.callback = callback;
  18. watcher_ptr = manos_prepare_watcher_create (unmanaged_callback, GCHandle.ToIntPtr (gc_handle));
  19. }
  20. protected override void DestroyWatcher ()
  21. {
  22. manos_prepare_watcher_destroy (watcher_ptr);
  23. }
  24. private static void StaticCallback (IntPtr data, EventTypes revents)
  25. {
  26. try {
  27. var handle = GCHandle.FromIntPtr (data);
  28. var watcher = (PrepareWatcher) handle.Target;
  29. watcher.callback (watcher, revents);
  30. } catch (Exception e) {
  31. Console.Error.WriteLine ("Error handling prepare event: {0}", e.Message);
  32. Console.Error.WriteLine (e.StackTrace);
  33. }
  34. }
  35. protected override void StartImpl ()
  36. {
  37. ev_prepare_start (Loop.Handle, watcher_ptr);
  38. }
  39. protected override void StopImpl ()
  40. {
  41. ev_prepare_stop (Loop.Handle, watcher_ptr);
  42. }
  43. [DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
  44. private static extern void ev_prepare_start (IntPtr loop, IntPtr watcher);
  45. [DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
  46. private static extern void ev_prepare_stop (IntPtr loop, IntPtr watcher);
  47. [DllImport ("libmanos", CallingConvention = CallingConvention.Cdecl)]
  48. private static extern IntPtr manos_prepare_watcher_create (UnmanagedWatcherCallback callback, IntPtr data);
  49. [DllImport ("libmanos", CallingConvention = CallingConvention.Cdecl)]
  50. private static extern void manos_prepare_watcher_destroy (IntPtr watcher);
  51. }
  52. }