/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

  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Net.Sockets;
  4. namespace Libev
  5. {
  6. class Loop
  7. {
  8. private IntPtr _native;
  9. private GCHandle _handle;
  10. private static readonly bool _isV4;
  11. static Loop ()
  12. {
  13. _isV4 = ev_version_major () >= 4;
  14. }
  15. public static bool IsV4 { get { return _isV4; } }
  16. public Loop ()
  17. {
  18. int backends = ev_supported_backends ();
  19. if (backends == 0)
  20. throw new InvalidOperationException ("No supported backend in libev");
  21. _native = ev_loop_new (0);
  22. if (_native == IntPtr.Zero)
  23. throw new Exception ("Unable to create native loop");
  24. _handle = GCHandle.Alloc (this);
  25. }
  26. ~Loop ()
  27. {
  28. Dispose ();
  29. }
  30. public IntPtr Handle {
  31. get {
  32. ThrowIfDisposed ();
  33. return _native;
  34. }
  35. }
  36. public void Dispose ()
  37. {
  38. if (_native == IntPtr.Zero)
  39. return;
  40. ev_loop_destroy (_native);
  41. _native = IntPtr.Zero;
  42. _handle.Free ();
  43. }
  44. public void RunBlocking ()
  45. {
  46. ThrowIfDisposed ();
  47. Run (LoopType.Blocking);
  48. }
  49. public void RunNonBlocking ()
  50. {
  51. ThrowIfDisposed ();
  52. Run (LoopType.NonBlocking);
  53. }
  54. public void RunOneShot ()
  55. {
  56. ThrowIfDisposed ();
  57. Run (LoopType.Oneshot);
  58. }
  59. public void Run (LoopType type)
  60. {
  61. ThrowIfDisposed ();
  62. if (IsV4)
  63. ev_run (_native, type);
  64. else
  65. ev_loop (_native, type);
  66. }
  67. public void Unloop (UnloopType type)
  68. {
  69. ThrowIfDisposed ();
  70. if (IsV4)
  71. ev_break (_native, type);
  72. else
  73. ev_unloop (_native, type);
  74. }
  75. private void ThrowIfDisposed ()
  76. {
  77. if (_native == IntPtr.Zero)
  78. throw new ObjectDisposedException ("native object has been disposed.");
  79. }
  80. [DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
  81. private static extern int ev_version_major ();
  82. [DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
  83. private static extern int ev_version_minor ();
  84. [DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
  85. private static extern IntPtr ev_loop_new (uint flags);
  86. [DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
  87. private static extern void ev_loop (IntPtr loop, LoopType type);
  88. [DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
  89. private static extern void ev_loop_destroy (IntPtr loop);
  90. [DllImport ("libev", CallingConvention = CallingConvention.Cdecl)]
  91. private static extern void ev_unloop (IntPtr loop, UnloopType flags);
  92. [DllImport("libev", CallingConvention = CallingConvention.Cdecl)]
  93. private static extern void ev_run (IntPtr loop, LoopType type);
  94. [DllImport("libev", CallingConvention = CallingConvention.Cdecl)]
  95. private static extern void ev_break (IntPtr loop, UnloopType flags);
  96. [DllImport("libev", CallingConvention = CallingConvention.Cdecl)]
  97. private static extern int ev_supported_backends ();
  98. }
  99. }