/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. //
  24. using System;
  25. using System.Runtime.InteropServices;
  26. using System.Net.Sockets;
  27. using Manos;
  28. using Manos.IO;
  29. namespace Libev
  30. {
  31. class AsyncWatcher : Watcher, IAsyncWatcher
  32. {
  33. private Action<AsyncWatcher, EventTypes> callback;
  34. private static UnmanagedWatcherCallback unmanaged_callback;
  35. static AsyncWatcher ()
  36. {
  37. unmanaged_callback = StaticCallback;
  38. }
  39. public AsyncWatcher (Loop loop, Action<AsyncWatcher, EventTypes> callback)
  40. : base(loop)
  41. {
  42. this.callback = callback;
  43. watcher_ptr = manos_async_watcher_create (unmanaged_callback, GCHandle.ToIntPtr (gc_handle));
  44. }
  45. protected override void DestroyWatcher ()
  46. {
  47. manos_async_watcher_destroy (watcher_ptr);
  48. }
  49. private static void StaticCallback (IntPtr data, EventTypes revents)
  50. {
  51. try {
  52. var handle = GCHandle.FromIntPtr (data);
  53. var watcher = (AsyncWatcher) handle.Target;
  54. watcher.callback (watcher, revents);
  55. } catch (Exception e) {
  56. Console.Error.WriteLine ("Error handling async event: {0}", e.Message);
  57. Console.Error.WriteLine (e.StackTrace);
  58. }
  59. }
  60. public void Send ()
  61. {
  62. ev_async_send (Loop.Handle, watcher_ptr);
  63. }
  64. protected override void StartImpl ()
  65. {
  66. ev_async_start (Loop.Handle, watcher_ptr);
  67. }
  68. protected override void StopImpl ()
  69. {
  70. ev_async_stop (Loop.Handle, watcher_ptr);
  71. }
  72. [DllImport("libev", CallingConvention = CallingConvention.Cdecl)]
  73. private static extern void ev_async_start (IntPtr loop, IntPtr watcher);
  74. [DllImport("libev", CallingConvention = CallingConvention.Cdecl)]
  75. private static extern void ev_async_stop (IntPtr loop, IntPtr watcher);
  76. [DllImport("libev", CallingConvention = CallingConvention.Cdecl)]
  77. private static extern void ev_async_send (IntPtr loop, IntPtr watcher);
  78. [DllImport("libmanos", CallingConvention = CallingConvention.Cdecl)]
  79. private static extern IntPtr manos_async_watcher_create (UnmanagedWatcherCallback callback, IntPtr data);
  80. [DllImport("libmanos", CallingConvention = CallingConvention.Cdecl)]
  81. private static extern void manos_async_watcher_destroy (IntPtr watcher);
  82. }
  83. }