Advertisement
Guest User

Untitled

a guest
Mar 26th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. const std = @import("std");
  2. const warn = @import("std").debug.warn; // XXX DEBUG
  3. const assert = std.debug.assert;
  4.  
  5. const c = @cImport({
  6. @cInclude("uv.h");
  7. });
  8.  
  9. pub const UVError = error{ E2BIG, EACCES, EADDRINUSE, EADDRNOTAVAIL, EAFNOSUPPORT, EAGAIN, EAI_ADDRFAMILY, EAI_AGAIN, EAI_BADFLAGS, EAI_BADHINTS, EAI_CANCELED, EAI_FAIL, EAI_FAMILY, EAI_MEMORY, EAI_NODATA, EAI_NONAME, EAI_OVERFLOW, EAI_PROTOCOL, EAI_SERVICE, EAI_SOCKTYPE, EALREADY, EBADF, EBUSY, ECANCELED, ECHARSET, ECONNABORTED, ECONNREFUSED, ECONNRESET, EDESTADDRREQ, EEXIST, EFAULT, EFBIG, EHOSTUNREACH, EINTR, EINVAL, EIO, EISCONN, EISDIR, ELOOP, EMFILE, EMSGSIZE, ENAMETOOLONG, ENETDOWN, ENETUNREACH, ENFILE, ENOBUFS, ENODEV, ENOENT, ENOMEM, ENONET, ENOPROTOOPT, ENOSPC, ENOSYS, ENOTCONN, ENOTDIR, ENOTEMPTY, ENOTSOCK, ENOTSUP, EPERM, EPIPE, EPROTO, EPROTONOSUPPORT, EPROTOTYPE, ERANGE, EROFS, ESHUTDOWN, ESPIPE, ESRCH, ETIMEDOUT, ETXTBSY, EXDEV, UNKNOWN, EOF, ENXIO, EMLINK };
  10.  
  11. fn check_uv(r: i32) !void {
  12. if (r >= 0) return;
  13.  
  14. return switch (r) {
  15. // All positive (including 0) numbers
  16. c.UV_E2BIG => UVError.E2BIG,
  17. c.UV_EACCES => UVError.EACCES,
  18. c.UV_EADDRINUSE => UVError.EADDRINUSE,
  19. c.UV_EADDRNOTAVAIL => UVError.EADDRNOTAVAIL,
  20. c.UV_EAFNOSUPPORT => UVError.EAFNOSUPPORT,
  21. c.UV_EAGAIN => UVError.EAGAIN,
  22. c.UV_EAI_ADDRFAMILY => UVError.EAI_ADDRFAMILY,
  23. c.UV_EAI_AGAIN => UVError.EAI_AGAIN,
  24. c.UV_EAI_BADFLAGS => UVError.EAI_BADFLAGS,
  25. c.UV_EAI_BADHINTS => UVError.EAI_BADHINTS,
  26. c.UV_EAI_CANCELED => UVError.EAI_CANCELED,
  27. c.UV_EAI_FAIL => UVError.EAI_FAIL,
  28. c.UV_EAI_FAMILY => UVError.EAI_FAMILY,
  29. c.UV_EAI_MEMORY => UVError.EAI_MEMORY,
  30. c.UV_EAI_NODATA => UVError.EAI_NODATA,
  31. c.UV_EAI_NONAME => UVError.EAI_NONAME,
  32. c.UV_EAI_OVERFLOW => UVError.EAI_OVERFLOW,
  33. c.UV_EAI_PROTOCOL => UVError.EAI_PROTOCOL,
  34. c.UV_EAI_SERVICE => UVError.EAI_SERVICE,
  35. c.UV_EAI_SOCKTYPE => UVError.EAI_SOCKTYPE,
  36. c.UV_EALREADY => UVError.EALREADY,
  37. c.UV_EBADF => UVError.EBADF,
  38. c.UV_EBUSY => UVError.EBUSY,
  39. c.UV_ECANCELED => UVError.ECANCELED,
  40. c.UV_ECHARSET => UVError.ECHARSET,
  41. c.UV_ECONNABORTED => UVError.ECONNABORTED,
  42. c.UV_ECONNREFUSED => UVError.ECONNREFUSED,
  43. c.UV_ECONNRESET => UVError.ECONNRESET,
  44. c.UV_EDESTADDRREQ => UVError.EDESTADDRREQ,
  45. c.UV_EEXIST => UVError.EEXIST,
  46. c.UV_EFAULT => UVError.EFAULT,
  47. c.UV_EFBIG => UVError.EFBIG,
  48. c.UV_EHOSTUNREACH => UVError.EHOSTUNREACH,
  49. c.UV_EINTR => UVError.EINTR,
  50. c.UV_EINVAL => UVError.EINVAL,
  51. c.UV_EIO => UVError.EIO,
  52. c.UV_EISCONN => UVError.EISCONN,
  53. c.UV_EISDIR => UVError.EISDIR,
  54. c.UV_ELOOP => UVError.ELOOP,
  55. c.UV_EMFILE => UVError.EMFILE,
  56. c.UV_EMSGSIZE => UVError.EMSGSIZE,
  57. c.UV_ENAMETOOLONG => UVError.ENAMETOOLONG,
  58. c.UV_ENETDOWN => UVError.ENETDOWN,
  59. c.UV_ENETUNREACH => UVError.ENETUNREACH,
  60. c.UV_ENFILE => UVError.ENFILE,
  61. c.UV_ENOBUFS => UVError.ENOBUFS,
  62. c.UV_ENODEV => UVError.ENODEV,
  63. c.UV_ENOENT => UVError.ENOENT,
  64. c.UV_ENOMEM => UVError.ENOMEM,
  65. c.UV_ENONET => UVError.ENONET,
  66. c.UV_ENOPROTOOPT => UVError.ENOPROTOOPT,
  67. c.UV_ENOSPC => UVError.ENOSPC,
  68. c.UV_ENOSYS => UVError.ENOSYS,
  69. c.UV_ENOTCONN => UVError.ENOTCONN,
  70. c.UV_ENOTDIR => UVError.ENOTDIR,
  71. c.UV_ENOTEMPTY => UVError.ENOTEMPTY,
  72. c.UV_ENOTSOCK => UVError.ENOTSOCK,
  73. c.UV_ENOTSUP => UVError.ENOTSUP,
  74. c.UV_EPERM => UVError.EPERM,
  75. c.UV_EPIPE => UVError.EPIPE,
  76. c.UV_EPROTO => UVError.EPROTO,
  77. c.UV_EPROTONOSUPPORT => UVError.EPROTONOSUPPORT,
  78. c.UV_EPROTOTYPE => UVError.EPROTOTYPE,
  79. c.UV_ERANGE => UVError.ERANGE,
  80. c.UV_EROFS => UVError.EROFS,
  81. c.UV_ESHUTDOWN => UVError.ESHUTDOWN,
  82. c.UV_ESPIPE => UVError.ESPIPE,
  83. c.UV_ESRCH => UVError.ESRCH,
  84. c.UV_ETIMEDOUT => UVError.ETIMEDOUT,
  85. c.UV_ETXTBSY => UVError.ETXTBSY,
  86. c.UV_EXDEV => UVError.EXDEV,
  87. c.UV_UNKNOWN => UVError.UNKNOWN,
  88. c.UV_EOF => UVError.EOF,
  89. c.UV_ENXIO => UVError.ENXIO,
  90. c.UV_EMLINK => UVError.EMLINK,
  91. else => unreachable,
  92. };
  93. }
  94.  
  95. fn cb_loop_sleep(handle: [*c]c.uv_timer_t) callconv(.C) void {
  96. var frame = @ptrCast(*@Frame(Loop.sleep), (handle.*).data orelse unreachable);
  97. resume frame;
  98. }
  99.  
  100. pub const Loop = struct {
  101. uv_loop: c.uv_loop_t,
  102.  
  103. fn init() !Loop {
  104. var res: Loop = Loop{ .uv_loop = undefined };
  105.  
  106. var r: i32 = c.uv_loop_init(&res.uv_loop);
  107. try check_uv(r);
  108.  
  109. return res;
  110. }
  111.  
  112. fn close(self: *Loop) !void {
  113. var r: i32 = c.uv_loop_close(&self.uv_loop);
  114. try check_uv(r);
  115. }
  116.  
  117. fn run(self: *Loop) !i32 {
  118. var r: i32 = c.uv_run(&self.uv_loop, c.uv_run_mode.UV_RUN_DEFAULT);
  119. try check_uv(r);
  120. return r;
  121. }
  122.  
  123. fn stop(self: *Loop) void {
  124. c.uv_stop(&self.uv_loop);
  125. }
  126.  
  127. fn alive(self: *Loop) bool {
  128. return c.uv_loop_alive(&self.uv_loop) != 0;
  129. }
  130.  
  131. fn backendTimeout(self: *Loop) i32 {
  132. return c.uv_backend_timeout(&self.uv_loop);
  133. }
  134.  
  135. fn now(self: *Loop) u64 {
  136. return c.uv_now(&self.uv_loop);
  137. }
  138.  
  139. fn updateTime(self: *Loop) void {
  140. c.uv_update_time(&self.uv_loop);
  141. }
  142.  
  143. fn sleep(self: *Loop, milliseconds: u64) !void {
  144. suspend {
  145. var handle: c.uv_timer_t = undefined;
  146. var r: i32 = c.uv_timer_init(&self.uv_loop, &handle);
  147. try check_uv(r);
  148. //handle.data = @frame();
  149. r = c.uv_timer_start(&handle, cb_loop_sleep, milliseconds, 0);
  150. try check_uv(r);
  151. }
  152. }
  153. };
  154.  
  155. var loop: Loop = undefined;
  156.  
  157. fn amain() !void {
  158. warn("starting amain\n", .{});
  159. try loop.sleep(1000);
  160. warn("exiting amain\n", .{});
  161. }
  162.  
  163. pub fn main() !void {
  164. loop = try Loop.init();
  165. _ = async amainWrap();
  166. var res: i32 = try loop.run();
  167. if (res > 0) warn("uv loop exited with more than one handle ({} remaining)\n", .{res});
  168. }
  169.  
  170. fn amainWrap() void {
  171. amain() catch |e| {
  172. std.debug.warn("{}\n", .{e});
  173. if (@errorReturnTrace()) |trace| {
  174. std.debug.dumpStackTrace(trace.*);
  175. }
  176. std.process.exit(1);
  177. };
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement