Compare commits
2 Commits
2e06f44aa8
...
226d678e68
Author | SHA1 | Date | |
---|---|---|---|
226d678e68 | |||
0dac8402cf |
@ -2,7 +2,7 @@ const std = @import("std");
|
|||||||
const nats = @import("nats");
|
const nats = @import("nats");
|
||||||
|
|
||||||
fn onMessage(
|
fn onMessage(
|
||||||
userdata: *bool,
|
userdata: *u32,
|
||||||
connection: *nats.Connection,
|
connection: *nats.Connection,
|
||||||
subscription: *nats.Subscription,
|
subscription: *nats.Subscription,
|
||||||
message: *nats.Message,
|
message: *nats.Message,
|
||||||
@ -18,18 +18,18 @@ fn onMessage(
|
|||||||
connection.publish(reply, "salutations") catch @panic("HELP");
|
connection.publish(reply, "salutations") catch @panic("HELP");
|
||||||
}
|
}
|
||||||
|
|
||||||
userdata.* = true;
|
userdata.* += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
const connection = try nats.Connection.connectTo(nats.default_server_url);
|
const connection = try nats.Connection.connectTo(nats.default_server_url);
|
||||||
defer connection.destroy();
|
defer connection.destroy();
|
||||||
|
|
||||||
var done = false;
|
var count: u32 = 0;
|
||||||
const subscription = try connection.subscribe(bool, "channel", onMessage, &done);
|
const subscription = try connection.subscribe(u32, "channel", onMessage, &count);
|
||||||
defer subscription.destroy();
|
defer subscription.destroy();
|
||||||
|
|
||||||
while (!done) {
|
while (count < 10) : (nats.sleep(1000)) {
|
||||||
const reply = try connection.request("channel", "greetings", 1000);
|
const reply = try connection.request("channel", "greetings", 1000);
|
||||||
defer reply.destroy();
|
defer reply.destroy();
|
||||||
|
|
||||||
|
@ -893,7 +893,7 @@ pub fn makeConnectionCallbackThunk(
|
|||||||
return struct {
|
return struct {
|
||||||
fn thunk(conn: ?*nats_c.natsConnection, userdata: ?*anyopaque) callconv(.C) void {
|
fn thunk(conn: ?*nats_c.natsConnection, userdata: ?*anyopaque) callconv(.C) void {
|
||||||
const connection: *Connection = if (conn) |c| @ptrCast(c) else unreachable;
|
const connection: *Connection = if (conn) |c| @ptrCast(c) else unreachable;
|
||||||
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
|
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
|
||||||
callback(connection, data);
|
callback(connection, data);
|
||||||
}
|
}
|
||||||
}.thunk;
|
}.thunk;
|
||||||
@ -916,7 +916,7 @@ pub fn makeReconnectDelayCallbackThunk(
|
|||||||
userdata: ?*anyopaque,
|
userdata: ?*anyopaque,
|
||||||
) callconv(.C) i64 {
|
) callconv(.C) i64 {
|
||||||
const connection: *Connection = if (conn) |c| @ptrCast(c) else unreachable;
|
const connection: *Connection = if (conn) |c| @ptrCast(c) else unreachable;
|
||||||
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
|
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
|
||||||
return callback(connection, attempts, data);
|
return callback(connection, attempts, data);
|
||||||
}
|
}
|
||||||
}.thunk;
|
}.thunk;
|
||||||
@ -946,7 +946,7 @@ pub fn makeErrorHandlerCallbackThunk(
|
|||||||
) callconv(.C) void {
|
) callconv(.C) void {
|
||||||
const connection: *Connection = if (conn) |c| @ptrCast(c) else unreachable;
|
const connection: *Connection = if (conn) |c| @ptrCast(c) else unreachable;
|
||||||
const subscription: *Subscription = if (sub) |s| @ptrCast(s) else unreachable;
|
const subscription: *Subscription = if (sub) |s| @ptrCast(s) else unreachable;
|
||||||
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
|
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
|
||||||
|
|
||||||
callback(connection, subscription, Status.fromInt(status), data);
|
callback(connection, subscription, Status.fromInt(status), data);
|
||||||
}
|
}
|
||||||
@ -1005,7 +1005,7 @@ pub fn makeEventLoopAddRemoveCallbackThunk(
|
|||||||
userdata: ?*anyopaque,
|
userdata: ?*anyopaque,
|
||||||
) callconv(.C) nats_c.natsStatus {
|
) callconv(.C) nats_c.natsStatus {
|
||||||
const connection: *Connection = if (conn) |c| @ptrCast(c) else unreachable;
|
const connection: *Connection = if (conn) |c| @ptrCast(c) else unreachable;
|
||||||
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
|
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
|
||||||
callback(connection, attempts, data) catch |err|
|
callback(connection, attempts, data) catch |err|
|
||||||
return Status.fromError(err).toInt();
|
return Status.fromError(err).toInt();
|
||||||
|
|
||||||
@ -1028,7 +1028,7 @@ pub fn makeEventLoopDetachCallbackThunk(
|
|||||||
fn thunk(
|
fn thunk(
|
||||||
userdata: ?*anyopaque,
|
userdata: ?*anyopaque,
|
||||||
) callconv(.C) nats_c.natsStatus {
|
) callconv(.C) nats_c.natsStatus {
|
||||||
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
|
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
|
||||||
callback(data) catch |err| return Status.fromError(err).toInt();
|
callback(data) catch |err| return Status.fromError(err).toInt();
|
||||||
return nats_c.NATS_OK;
|
return nats_c.NATS_OK;
|
||||||
}
|
}
|
||||||
@ -1058,7 +1058,7 @@ pub fn makeJwtHandlerCallbackThunk(
|
|||||||
err_out: *?[*:0]u8,
|
err_out: *?[*:0]u8,
|
||||||
userdata: ?*anyopaque,
|
userdata: ?*anyopaque,
|
||||||
) callconv(.C) nats_c.natsStatus {
|
) callconv(.C) nats_c.natsStatus {
|
||||||
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
|
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
|
||||||
|
|
||||||
switch (callback(data)) {
|
switch (callback(data)) {
|
||||||
.jwt => |jwt| {
|
.jwt => |jwt| {
|
||||||
@ -1099,7 +1099,7 @@ pub fn makeSignatureHandlerCallbackThunk(
|
|||||||
nonce: [*:0]const u8,
|
nonce: [*:0]const u8,
|
||||||
userdata: ?*anyopaque,
|
userdata: ?*anyopaque,
|
||||||
) callconv(.C) nats_c.natsStatus {
|
) callconv(.C) nats_c.natsStatus {
|
||||||
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
|
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
|
||||||
|
|
||||||
switch (callback(std.mem.sliceTo(nonce, 0), data)) {
|
switch (callback(std.mem.sliceTo(nonce, 0), data)) {
|
||||||
.signature => |sig| {
|
.signature => |sig| {
|
||||||
|
@ -197,7 +197,7 @@ pub fn makeSubscriptionCallbackThunk(
|
|||||||
const connection: *Connection = if (conn) |c| @ptrCast(c) else unreachable;
|
const connection: *Connection = if (conn) |c| @ptrCast(c) else unreachable;
|
||||||
const subscription: *Subscription = if (sub) |s| @ptrCast(s) else unreachable;
|
const subscription: *Subscription = if (sub) |s| @ptrCast(s) else unreachable;
|
||||||
|
|
||||||
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
|
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
|
||||||
|
|
||||||
callback(data, connection, subscription, message);
|
callback(data, connection, subscription, message);
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ pub fn makeSimpleCallbackThunk(
|
|||||||
) *const SimpleCallback {
|
) *const SimpleCallback {
|
||||||
return struct {
|
return struct {
|
||||||
fn thunk(userdata: ?*anyopaque) callconv(.C) void {
|
fn thunk(userdata: ?*anyopaque) callconv(.C) void {
|
||||||
const data: *T = if (userdata) |u| @ptrCast(u) else unreachable;
|
const data: *T = if (userdata) |u| @alignCast(@ptrCast(u)) else unreachable;
|
||||||
callback(data);
|
callback(data);
|
||||||
}
|
}
|
||||||
}.thunk;
|
}.thunk;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user