| ... | @@ -6,6 +6,7 @@ | ... | @@ -6,6 +6,7 @@ |
| 6 | | 6 | |
| 7 | const std = @import("../../std.zig"); | 7 | const std = @import("../../std.zig"); |
| 8 | | 8 | |
| | 9 | const io = std.io; |
| 9 | const os = std.os; | 10 | const os = std.os; |
| 10 | const ip = std.x.net.ip; | 11 | const ip = std.x.net.ip; |
| 11 | | 12 | |
| ... | @@ -58,6 +59,28 @@ pub const Domain = extern enum(u16) { | ... | @@ -58,6 +59,28 @@ pub const Domain = extern enum(u16) { |
| 58 | pub const Client = struct { | 59 | pub const Client = struct { |
| 59 | socket: Socket, | 60 | socket: Socket, |
| 60 | | 61 | |
| | 62 | /// Implements `std.io.Reader`. |
| | 63 | pub const Reader = struct { |
| | 64 | client: Client, |
| | 65 | flags: u32, |
| | 66 | |
| | 67 | /// Implements `readFn` for `std.io.Reader`. |
| | 68 | pub fn read(self: Client.Reader, buffer: []u8) !usize { |
| | 69 | return self.client.read(buffer, self.flags); |
| | 70 | } |
| | 71 | }; |
| | 72 | |
| | 73 | /// Implements `std.io.Writer`. |
| | 74 | pub const Writer = struct { |
| | 75 | client: Client, |
| | 76 | flags: u32, |
| | 77 | |
| | 78 | /// Implements `writeFn` for `std.io.Writer`. |
| | 79 | pub fn write(self: Client.Writer, buffer: []const u8) !usize { |
| | 80 | return self.client.write(buffer, self.flags); |
| | 81 | } |
| | 82 | }; |
| | 83 | |
| 61 | /// Opens a new client. | 84 | /// Opens a new client. |
| 62 | pub fn init(domain: tcp.Domain, flags: u32) !Client { | 85 | pub fn init(domain: tcp.Domain, flags: u32) !Client { |
| 63 | return Client{ | 86 | return Client{ |
| ... | @@ -89,6 +112,22 @@ pub const Client = struct { | ... | @@ -89,6 +112,22 @@ pub const Client = struct { |
| 89 | return self.socket.connect(address.into()); | 112 | return self.socket.connect(address.into()); |
| 90 | } | 113 | } |
| 91 | | 114 | |
| | 115 | /// Extracts the error set of a function. |
| | 116 | /// TODO: remove after Socket.{read, write} error unions are well-defined across different platforms |
| | 117 | fn ErrorSetOf(comptime Function: anytype) type { |
| | 118 | return @typeInfo(@typeInfo(@TypeOf(Function)).Fn.return_type.?).ErrorUnion.error_set; |
| | 119 | } |
| | 120 | |
| | 121 | /// Wrap `tcp.Client` into `std.io.Reader`. |
| | 122 | pub fn reader(self: Client, flags: u32) io.Reader(Client.Reader, ErrorSetOf(Client.Reader.read), Client.Reader.read) { |
| | 123 | return .{ .context = .{ .client = self, .flags = flags } }; |
| | 124 | } |
| | 125 | |
| | 126 | /// Wrap `tcp.Client` into `std.io.Writer`. |
| | 127 | pub fn writer(self: Client, flags: u32) io.Writer(Client.Writer, ErrorSetOf(Client.Writer.write), Client.Writer.write) { |
| | 128 | return .{ .context = .{ .client = self, .flags = flags } }; |
| | 129 | } |
| | 130 | |
| 92 | /// Read data from the socket into the buffer provided with a set of flags | 131 | /// Read data from the socket into the buffer provided with a set of flags |
| 93 | /// specified. It returns the number of bytes read into the buffer provided. | 132 | /// specified. It returns the number of bytes read into the buffer provided. |
| 94 | pub fn read(self: Client, buf: []u8, flags: u32) !usize { | 133 | pub fn read(self: Client, buf: []u8, flags: u32) !usize { |