-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrpc_client.zig
More file actions
62 lines (52 loc) · 2 KB
/
Copy pathgrpc_client.zig
File metadata and controls
62 lines (52 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! gRPC h2c client example.
//! Demonstrates unary and streaming calls against the grpc_server_1_async example.
//!
//! Run (server must be running on port 9032):
//! zig build example-grpc_client
const std = @import("std");
const zix = @import("zix");
// --------------------------------------------------------- //
pub fn main(process: std.process.Init) !void {
const io = process.io;
std.debug.print("connecting to grpc server at 127.0.0.1:9032\n", .{});
var client = try zix.Grpc.Client.connect(.{ .ip = "127.0.0.1", .port = 9032 }, io);
defer client.deinit();
std.debug.print("connected\n", .{});
// Unary call: SayHello
{
var buf: [256]u8 = undefined;
const resp = client.unary(
"/helloworld.Greeter/SayHello",
"application/grpc+proto",
"world",
&buf,
) catch |e| {
std.debug.print("unary error: {}\n", .{e});
return;
};
std.debug.print("unary response: {s}\n", .{resp});
}
// Server streaming (send 3 messages, expect 3 echoes)
{
const sid = try client.openStream("/helloworld.Greeter/Echo", "application/grpc+proto");
try client.sendMessage(sid, "alpha");
try client.sendMessage(sid, "beta");
try client.sendMessage(sid, "gamma");
try client.endStream(sid);
std.debug.print("streaming echoes:\n", .{});
var buf: [256]u8 = undefined;
var final_status: ?zix.Grpc.Status = null;
while (true) {
const r = client.recvResponse(sid, &buf) catch break;
switch (r) {
.data => |d| std.debug.print(" recv: {s}\n", .{d}),
.status => |stream_status| {
std.debug.print(" status: {d} ({s})\n", .{ @intFromEnum(stream_status), @tagName(stream_status) });
final_status = stream_status;
break;
},
}
}
if (final_status != .OK) return error.StreamFailed;
}
}