Advertisement
Guest User

Untitled

a guest
Mar 26th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 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. pub const Loop = struct {
  96. uv_loop: c.uv_loop_t,
  97.  
  98. fn init() !Loop {
  99. var res: Loop = Loop{ .uv_loop = undefined };
  100.  
  101. var r: i32 = c.uv_loop_init(&res.uv_loop);
  102. try check_uv(r);
  103.  
  104. return res;
  105. }
  106.  
  107. fn close(self: *Loop) !void {
  108. var r: i32 = c.uv_loop_close(&self.uv_loop);
  109. try check_uv(r);
  110. }
  111.  
  112. fn run(self: *Loop) !i32 {
  113. var r: i32 = c.uv_run(&self.uv_loop, c.uv_run_mode.UV_RUN_DEFAULT);
  114. try check_uv(r);
  115. return r;
  116. }
  117.  
  118. fn stop(self: *Loop) void {
  119. c.uv_stop(&self.uv_loop);
  120. }
  121.  
  122. fn alive(self: *Loop) bool {
  123. return c.uv_loop_alive(&self.uv_loop) != 0;
  124. }
  125.  
  126. fn backendTimeout(self: *Loop) i32 {
  127. return c.uv_backend_timeout(&self.uv_loop);
  128. }
  129.  
  130. fn now(self: *Loop) u64 {
  131. return c.uv_now(&self.uv_loop);
  132. }
  133.  
  134. fn updateTime(self: *Loop) void {
  135. c.uv_update_time(&self.uv_loop);
  136. }
  137.  
  138. fn sleep(self: *Loop, milliseconds: u64) !void {
  139. suspend {
  140. var handle: c.uv_timer_t = undefined;
  141. var r: i32 = c.uv_timer_init(&self.uv_loop, &handle);
  142. try check_uv(r);
  143. handle.data = @frame();
  144. }
  145. }
  146. };
  147.  
  148. var loop: Loop = undefined;
  149.  
  150. fn amain() !void {
  151. warn("starting amain\n", .{});
  152. var sleeper = async loop.sleep(1000);
  153. _ = try await sleeper;
  154. warn("exiting amain\n", .{});
  155. }
  156.  
  157. pub fn main() !void {
  158. loop = try Loop.init();
  159. _ = async amainWrap();
  160. var res: i32 = try loop.run();
  161. if (res > 0) warn("uv loop exited with more than one handle ({} remaining)\n", .{res});
  162. }
  163.  
  164. fn amainWrap() void {
  165. amain() catch |e| {
  166. std.debug.warn("{}\n", .{e});
  167. if (@errorReturnTrace()) |trace| {
  168. std.debug.dumpStackTrace(trace.*);
  169. }
  170. std.process.exit(1);
  171. };
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement