Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
zig source #2
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); const c = @cImport(@cInclude("mosquitto.h")); var run = true; fn concat(s1: []const u8, s2: []const u8) anyerror![]const u8 { return try std.mem.concat(std.heap.direct_allocator, u8, [_][]const u8{ s1, s2 }); } fn concat3(s1: []const u8, s2: []const u8, s3: []const u8) anyerror![]const u8 { return try std.mem.concat(std.heap.direct_allocator, u8, [_][]const u8{ s1, s2, s3 }); } extern fn intHandler(sig: i32, info: *std.os.siginfo_t, ctx_ptr: *c_void) void { std.debug.warn("\nShutting down...\n"); run = false; } pub fn main() anyerror!void { // Signal handling const act = std.os.Sigaction{ .sigaction = intHandler, .mask = std.os.empty_sigset, .flags = 0, }; std.os.sigaction(std.os.SIGINT, &act, null); // Setup var hostnameBuf: [64]u8 = undefined; const hostname: []const u8 = try std.os.gethostname(&hostnameBuf); const statusTopic = try concat3("server/", hostname, "/status"); std.debug.warn("Init status: {}\n", c.mosquitto_lib_init()); defer std.debug.warn("Deinit: {}\n", c.mosquitto_lib_cleanup()); var mos = c.mosquitto_new(hostname.ptr, false, null); defer c.mosquitto_destroy(mos); // Will std.debug.warn("Will: {}\n", c.mosquitto_will_set(mos, statusTopic.ptr, 7, c"offline", 2, true)); std.debug.warn("Connect: {}\n", c.mosquitto_connect(mos, c"127.0.0.1", 1883, 30)); defer std.debug.warn("Disconnect: {}\n", c.mosquitto_disconnect(mos)); std.debug.warn("Publish: {}\n", c.mosquitto_publish(mos, null, statusTopic.ptr, 6, c"online", 1, true)); // Loop while (run) { std.debug.warn("Looping: {}\n", errorCode(c.mosquitto_loop(mos, 10000, 1))); std.os.nanosleep(1, 0); } // Set unknown status on exit std.debug.warn("Publish: {}\n", c.mosquitto_publish(mos, null, statusTopic.ptr, 17, c"client-disconnect", 1, true)); } fn errorCode(errcode: c_int) []const u8 { const result = switch (@intToEnum(c.mosq_err_t, errcode)) { .MOSQ_ERR_AUTH_CONTINUE => "-4", .MOSQ_ERR_NO_SUBSCRIBERS => "No subscribers", .MOSQ_ERR_SUB_EXISTS => "Subscriber exists", .MOSQ_ERR_CONN_PENDING => "Connection pending", .MOSQ_ERR_SUCCESS => "OK", .MOSQ_ERR_NOMEM => "Out of memory", .MOSQ_ERR_PROTOCOL => "Protocol error", .MOSQ_ERR_INVAL => "Invalid parameter", .MOSQ_ERR_NO_CONN => "No connection", .MOSQ_ERR_CONN_REFUSED => "Connection refused", .MOSQ_ERR_NOT_FOUND => "Not found", .MOSQ_ERR_CONN_LOST => "Connection lost", .MOSQ_ERR_TLS => "TLS error", .MOSQ_ERR_PAYLOAD_SIZE => "Payload size", .MOSQ_ERR_NOT_SUPPORTED => "Not supported", .MOSQ_ERR_AUTH => "Auth error", .MOSQ_ERR_ACL_DENIED => "Denied by ACL", .MOSQ_ERR_UNKNOWN => "Unknown topic", .MOSQ_ERR_ERRNO => "ERRNO", .MOSQ_ERR_EAI => "15", .MOSQ_ERR_PROXY => "16", .MOSQ_ERR_PLUGIN_DEFER => "17", .MOSQ_ERR_MALFORMED_UTF8 => "Malformed UTF8", .MOSQ_ERR_KEEPALIVE => "19", .MOSQ_ERR_LOOKUP => "20", .MOSQ_ERR_MALFORMED_PACKET => "Malformed packet", .MOSQ_ERR_DUPLICATE_PROPERTY => "Duplicate property", .MOSQ_ERR_TLS_HANDSHAKE => "TLS handshake error", .MOSQ_ERR_QOS_NOT_SUPPORTED => "QOS not supported", .MOSQ_ERR_OVERSIZE_PACKET => "Oversize packet", .MOSQ_ERR_OCSP => "OSCP error", else => "Unknown", }; return result; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree