Skip to content

Commit fabc3e4

Browse files
authored
Merge pull request #122 from Raiden1411/websocket
feat: implement custom websocket client
2 parents 01de444 + 807e8c8 commit fabc3e4

File tree

10 files changed

+919
-109
lines changed

10 files changed

+919
-109
lines changed

build.zig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,8 @@ fn addDependencies(
312312
.target = target,
313313
.optimize = optimize,
314314
});
315-
const ws = b.dependency("ws", .{ .target = target, .optimize = optimize });
316315

317316
mod.addImport("c-kzg-4844", c_kzg_4844_dep.module("c-kzg-4844"));
318-
mod.addImport("ws", ws.module("websocket"));
319317
mod.linkLibrary(c_kzg_4844_dep.artifact("c-kzg-4844"));
320318
}
321319
/// Builds and runs the benchmarks

build.zig.zon

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.name = "zabi",
33
// This is a [Semantic Version](https://semver.org/).
44
// In a future version of Zig it will be used for package deduplication.
5-
.version = "0.15.1",
5+
.version = "0.15.4",
66

77
// This field is optional.
88
// This is currently advisory only; Zig does not yet do anything
@@ -15,10 +15,6 @@
1515
// Once all dependencies are fetched, `zig build` no longer requires
1616
// Internet connectivity.
1717
.dependencies = .{
18-
.ws = .{
19-
.url = "git+https://github.com/karlseguin/websocket.zig.git#ba14f387b22210667a2941c1e5e4170eb1854957",
20-
.hash = "1220276bfd080f184d931f7fbd63769151100abfaf047472cb9c09da5a03af3a2b21",
21-
},
2218
.@"c-kzg-4844" = .{ .path = "./pkg/c-kzg-4844" },
2319
},
2420
.paths = .{

examples/autobahn/autobahn.zig

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
const std = @import("std");
2+
const clients = @import("zabi-clients");
3+
4+
const Allocator = std.mem.Allocator;
5+
const WebSocketClient = clients.WebSocketClient;
6+
7+
pub fn main() !void {
8+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
9+
defer _ = gpa.detectLeaks();
10+
11+
const allocator = gpa.allocator();
12+
13+
const cases = [_][]const u8{
14+
"1.1.1", "1.1.2", "1.1.3", "1.1.4", "1.1.5", "1.1.6", "1.1.7", "1.1.8", "1.2.1", "1.2.2", "1.2.3", "1.2.4", "1.2.5", "1.2.6", "1.2.7", "1.2.8",
15+
"2.1", "2.10", "2.11", "2.2", "2.3", "2.4", "2.5", "2.6", "2.7", "2.8", "2.9", "3.1", "3.2", "3.3", "3.4", "3.5",
16+
"3.6", "3.7", "4.1.1", "4.1.2", "4.1.3", "4.1.4", "4.1.5", "4.2.1", "4.2.2", "4.2.3", "4.2.4", "4.2.5", "5.1", "5.2", "5.3", "5.4",
17+
"5.5", "5.6", "5.7", "5.8", "5.9", "5.10", "5.11", "5.12", "5.13", "5.14", "5.15", "5.16", "5.17", "5.18", "5.19", "5.20",
18+
"6.1.1", "6.1.2", "6.1.3", "6.2.1", "6.2.2", "6.2.3", "6.2.4", "6.3.1", "6.3.2", "6.4.1", "6.4.2", "6.4.3", "6.4.4", "6.5.1", "6.5.2", "6.5.3",
19+
"6.5.4", "6.5.5", "6.6.1", "6.6.2", "6.6.3", "6.6.4", "6.6.5", "6.6.6", "6.6.7", "6.6.8", "6.6.9", "6.6.10", "6.6.11", "6.7.1", "6.7.2", "6.7.3",
20+
"6.7.4", "6.8.1", "6.8.2", "6.9.1", "6.9.2", "6.9.3", "6.9.4", "6.10.1", "6.10.2", "6.10.3", "6.11.1", "6.11.2", "6.11.3", "6.11.4", "6.11.5", "6.12.1",
21+
"6.12.2", "6.12.3", "6.12.4", "6.12.5", "6.12.6", "6.12.7", "6.12.8", "6.13.1", "6.13.2", "6.13.3", "6.13.4", "6.13.5", "6.14.1", "6.14.2", "6.14.3", "6.14.4",
22+
"6.14.5", "6.14.6", "6.14.7", "6.14.8", "6.14.9", "6.14.10", "6.15.1", "6.16.1", "6.16.2", "6.16.3", "6.17.1", "6.17.2", "6.17.3", "6.17.4", "6.17.5", "6.18.1",
23+
"6.18.2", "6.18.3", "6.18.4", "6.18.5", "6.19.1", "6.19.2", "6.19.3", "6.19.4", "6.19.5", "6.20.1", "6.20.2", "6.20.3", "6.20.4", "6.20.5", "6.20.6", "6.20.7",
24+
"6.21.1", "6.21.2", "6.21.3", "6.21.4", "6.21.5", "6.21.6", "6.21.7", "6.21.8", "6.22.1", "6.22.10", "6.22.11", "6.22.12", "6.22.13", "6.22.14", "6.22.15", "6.22.16",
25+
"6.22.17", "6.22.18", "6.22.19", "6.22.2", "6.22.20", "6.22.21", "6.22.22", "6.22.23", "6.22.24", "6.22.25", "6.22.26", "6.22.27", "6.22.28", "6.22.29", "6.22.3", "6.22.30",
26+
"6.22.31", "6.22.32", "6.22.33", "6.22.34", "6.22.4", "6.22.5", "6.22.6", "6.22.7", "6.22.8", "6.22.9", "6.23.1", "6.23.2", "6.23.3", "6.23.4", "6.23.5", "6.23.6",
27+
"6.23.7", "7.1.1", "7.1.2", "7.1.3", "7.1.4", "7.1.5", "7.1.6", "7.3.1", "7.3.2", "7.3.3", "7.3.4", "7.3.5", "7.3.6", "7.5.1", "7.7.1", "7.7.2",
28+
"7.7.3", "7.7.4", "7.7.5", "7.7.6", "7.7.7", "7.7.8", "7.7.9", "7.7.10", "7.7.11", "7.7.12", "7.7.13", "7.9.1", "7.9.2", "7.9.3", "7.9.4", "7.9.5",
29+
"7.9.6", "7.9.7", "7.9.8", "7.9.9", "7.13.1", "7.13.2", "9.1.1", "9.1.2", "9.1.3", "9.1.4", "9.1.5", "9.1.6", "9.2.1", "9.2.2", "9.2.3", "9.2.4",
30+
"9.2.5", "9.2.6", "9.3.1", "9.3.2", "9.3.3", "9.3.4", "9.3.5", "9.3.6", "9.3.7", "9.3.8", "9.3.9", "9.4.1", "9.4.2", "9.4.3", "9.4.4", "9.4.5",
31+
"9.4.6", "9.4.7", "9.4.8", "9.4.9", "9.5.1", "9.5.2", "9.5.3", "9.5.4", "9.5.5", "9.5.6", "9.6.1", "9.6.2", "9.6.3", "9.6.4", "9.6.5", "9.6.6",
32+
"9.7.1", "9.7.2", "9.7.3", "9.7.4", "9.7.5", "9.7.6", "9.8.1", "9.8.2", "9.8.3", "9.8.4", "9.8.5", "9.8.6", "10.1.1",
33+
};
34+
35+
// wait 5 seconds for autobanh server to be up
36+
std.time.sleep(std.time.ns_per_s * 1);
37+
38+
for (cases, 0..) |case, i| {
39+
std.debug.print("running case: {s}\n", .{case});
40+
41+
{
42+
var auto = try AutoBanh.init(allocator, case);
43+
defer auto.deinit();
44+
45+
auto.readLoop() catch |err| switch (err) {
46+
error.UnnegociatedReservedBits,
47+
error.ControlFrameTooBig,
48+
error.UnfragmentedContinue,
49+
error.MessageSizeOverflow,
50+
error.UnsupportedOpcode,
51+
error.UnexpectedFragment,
52+
error.MaskedServerMessage,
53+
error.InvalidUtf8Payload,
54+
error.FragmentedControl,
55+
=> {},
56+
else => return err,
57+
};
58+
}
59+
60+
if (@rem(i, 10) == 0) {
61+
try updateReport(allocator);
62+
}
63+
}
64+
try updateReport(allocator);
65+
}
66+
67+
fn updateReport(allocator: Allocator) !void {
68+
const uri = try std.Uri.parse("http://localhost:9001/updateReports?agent=zabi.zig");
69+
var client = try WebSocketClient.connect(allocator, uri);
70+
defer client.deinit();
71+
72+
try client.handshake("localhost:9001");
73+
try client.writeCloseFrame(0);
74+
}
75+
76+
const AutoBanh = struct {
77+
client: WebSocketClient,
78+
79+
pub fn init(allocator: Allocator, case: []const u8) !AutoBanh {
80+
const path = try std.fmt.allocPrint(allocator, "http://localhost:9001/runCase?casetuple={s}&agent=zabi.zig", .{case});
81+
defer allocator.free(path);
82+
83+
const uri = try std.Uri.parse(path);
84+
var client = try WebSocketClient.connect(allocator, uri);
85+
86+
try client.handshake("localhost:9001");
87+
88+
return .{
89+
.client = client,
90+
};
91+
}
92+
93+
pub fn deinit(self: *AutoBanh) void {
94+
self.client.deinit();
95+
}
96+
97+
pub fn readLoop(self: *AutoBanh) !void {
98+
while (true) {
99+
const message = self.client.readMessage() catch |err| switch (err) {
100+
error.BrokenPipe,
101+
error.ConnectionResetByPeer,
102+
error.NotOpenForReading,
103+
=> {
104+
@atomicStore(bool, &self.client.closed_connection, true, .release);
105+
return error.ConnectionClosed;
106+
},
107+
error.InvalidUtf8Payload => {
108+
self.client.close(1007);
109+
return err;
110+
},
111+
else => {
112+
self.client.close(1002);
113+
return err;
114+
},
115+
};
116+
117+
switch (message.opcode) {
118+
.binary,
119+
=> try self.client.writeFrame(message.data, .binary),
120+
.text,
121+
=> try self.client.writeFrame(message.data, .text),
122+
.ping,
123+
=> try self.client.writeFrame(message.data, .pong),
124+
.connection_close,
125+
=> return self.client.close(0),
126+
// Ignore unsolicited pong messages.
127+
.pong,
128+
=> continue,
129+
else => return error.UnexpectedOpcode,
130+
}
131+
}
132+
}
133+
};

examples/autobahn/build.zig

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
7+
_ = b.addModule("autobahn_client", .{
8+
.root_source_file = b.path("autobahn.zig"),
9+
});
10+
11+
const exe = b.addExecutable(.{
12+
.name = "autobahn_client",
13+
.root_source_file = b.path("autobahn.zig"),
14+
.target = target,
15+
.optimize = optimize,
16+
});
17+
18+
addDependencies(b, exe);
19+
b.installArtifact(exe);
20+
}
21+
22+
fn addDependencies(b: *std.Build, step: *std.Build.Step.Compile) void {
23+
const target = step.root_module.resolved_target.?;
24+
const optimize = step.root_module.optimize.?;
25+
26+
const zabi = b.dependency("zabi", .{
27+
.target = target,
28+
.optimize = optimize,
29+
});
30+
31+
step.root_module.addImport("zabi-clients", zabi.module("zabi-clients"));
32+
}

examples/autobahn/build.zig.zon

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.{
2+
.name = "examples",
3+
// This is a [Semantic Version](https://semver.org/).
4+
// In a future version of Zig it will be used for package deduplication.
5+
.version = "0.1.0",
6+
7+
// This field is optional.
8+
// This is currently advisory only; Zig does not yet do anything
9+
// with this value.
10+
//.minimum_zig_version = "0.11.0",
11+
12+
// This field is optional.
13+
// Each dependency must either provide a `url` and `hash`, or a `path`.
14+
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
15+
// Once all dependencies are fetched, `zig build` no longer requires
16+
// Internet connectivity.
17+
.dependencies = .{ .zabi = .{ .path = "../../" } },
18+
.paths = .{
19+
// This makes *all* files, recursively, included in this package. It is generally
20+
// better to explicitly list the files and directories instead, to insure that
21+
// fetching from tarballs, file system paths, and version control all result
22+
// in the same contents hash.
23+
"build.zig",
24+
"build.zig.zon",
25+
"autobahn.zig",
26+
},
27+
}

examples/watch/watch.zig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ pub fn main() !void {
2929
defer id.deinit();
3030

3131
std.debug.print("Sub id: 0x{x}\n", .{id.response});
32-
// There is currently a bug on the tls client that will cause index out of bound errors
33-
// https://github.com/ziglang/zig/issues/15226
34-
// Make sure that for now the data you are using is not big enough to cause these crashes.
3532
while (true) {
3633
const event = try socket.getPendingTransactionsSubEvent();
3734
defer event.deinit();

0 commit comments

Comments
 (0)