-
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
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
378
-
379
-
380
-
381
-
382
-
383
-
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
-
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
-
418
-
419
-
420
-
421
-
422
-
423
-
424
-
425
-
426
-
427
-
428
-
429
-
430
-
431
-
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
-
440
-
441
-
442
-
443
-
444
-
445
-
446
-
447
-
448
-
449
-
450
-
451
-
452
-
453
-
454
-
455
-
456
-
457
-
458
-
459
-
460
-
461
-
462
-
463
-
464
-
465
-
466
-
467
-
468
-
469
-
470
-
471
-
472
-
473
-
474
-
475
-
476
-
477
-
478
-
479
-
480
-
481
-
482
-
483
-
484
-
485
-
486
-
487
-
488
-
489
-
490
-
491
-
492
-
493
-
494
-
495
-
496
-
497
-
498
-
499
-
500
-
501
-
502
-
503
-
504
-
505
-
506
-
507
-
508
-
509
-
510
-
511
-
512
-
513
-
514
-
515
-
516
-
517
-
518
-
519
-
520
-
521
-
522
-
523
-
524
-
525
-
526
-
527
-
528
-
529
-
530
-
531
-
532
-
533
-
534
-
535
-
536
-
537
-
538
-
539
-
540
-
541
-
542
-
543
-
544
-
545
-
546
-
547
-
548
-
549
-
550
-
551
-
552
-
553
-
554
-
555
-
556
-
557
-
558
-
559
-
560
-
561
-
562
-
563
-
564
-
565
-
566
-
567
-
568
-
569
-
570
-
571
-
572
-
573
-
574
-
575
-
576
-
577
-
578
-
579
-
580
-
581
-
582
-
583
-
584
-
585
-
586
-
587
-
588
-
589
-
590
-
591
-
592
-
593
-
594
-
595
-
596
-
597
-
598
-
599
-
600
-
601
-
602
-
603
-
604
-
605
-
606
-
607
-
608
-
609
-
610
-
611
-
612
-
613
-
614
-
615
-
616
-
617
-
618
-
619
-
620
-
621
-
622
-
623
-
624
-
625
-
626
-
627
-
628
-
629
// Copyright 2025 Shota FUJI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
const std = @import("std");
const sood = @import("sood");
const Connection = @import("./roon/connection.zig").Connection;
const discovery = @import("./roon/discovery.zig");
const Extension = @import("./roon/extension.zig").Extension;
const PingService = @import("./roon/services/ping.zig").PingService;
const RegistryService = @import("./roon/services/registry.zig").RegistryService;
const TransportService = @import("./roon/services/transport.zig").TransportService;
const callback = @import("./App/callback.zig");
pub const Server = @import("./App/Server.zig");
pub const ServerSelector = @import("./App/ServerSelector.zig");
const State = @import("./App/State.zig");
const App = @This();
const AppExtension = Extension(.{
.id = "jp.pocka.plac",
.display_name = "Plac",
.version = "0.0.0-dev",
.publisher = "Shota FUJI",
.email = "pockawoooh@gmail.com",
.required_services = &.{TransportService},
.optional_services = &.{},
.provided_services = &.{PingService},
});
const OnServerChange = callback.Callback(struct {});
const OnConnectionChange = callback.Callback(struct {});
const OnRestoreComplete = callback.Callback(struct {});
const ServerConnInfo = struct {
allocator: std.mem.Allocator,
addr: std.net.Address,
id: []const u8,
name: []const u8,
version: []const u8,
pub const FromSoodResponseError = std.mem.Allocator.Error;
pub fn fromSoodResponse(
allocator: std.mem.Allocator,
addr: std.net.Address,
resp: *const sood.discovery.Response,
) FromSoodResponseError!@This() {
var ws_addr = addr;
ws_addr.setPort(resp.http_port);
const id = try allocator.dupe(u8, resp.unique_id);
errdefer allocator.free(id);
const name = try allocator.dupe(u8, resp.name);
errdefer allocator.free(name);
const version = try allocator.dupe(u8, resp.display_version);
errdefer allocator.free(version);
return .{
.allocator = allocator,
.addr = ws_addr,
.id = id,
.name = name,
.version = version,
};
}
pub fn deinit(self: *const @This()) void {
self.allocator.free(self.version);
self.allocator.free(self.name);
self.allocator.free(self.id);
}
pub fn getKey(self: *const @This()) []const u8 {
return self.id;
}
};
allocator: std.mem.Allocator,
pool: std.Thread.Pool = undefined,
on_server_change: OnServerChange.Store,
on_connection_change: OnConnectionChange.Store,
on_restore_complete: OnRestoreComplete.Store,
capi_lock: std.Thread.Mutex,
state_file_path: ?[]const u8 = null,
pub fn init(allocator: std.mem.Allocator) App {
return .{
.allocator = allocator,
.on_server_change = OnServerChange.Store.init(allocator),
.on_connection_change = OnConnectionChange.Store.init(allocator),
.on_restore_complete = OnRestoreComplete.Store.init(allocator),
.capi_lock = std.Thread.Mutex{},
};
}
pub fn deinit(self: *App) void {
if (self.state_file_path) |path| {
self.allocator.free(path);
}
self.on_restore_complete.deinit();
self.on_connection_change.deinit();
self.on_server_change.deinit();
self.pool.deinit();
}
pub const CApi = extern struct {
internal: *App,
server_selector: *ServerSelector.CApi,
connection: ConnectionState = .idle,
server: ?*Server.CApi = null,
pub const ConnectionState = enum(c_int) {
idle = 0,
busy = 1,
err_unexpected = 2,
err_network_unavailable = 3,
err_socket_permission = 4,
err_out_of_memory = 5,
err_socket = 6,
err_registry_down = 7,
err_failed_to_register = 8,
err_thread_spawn = 9,
err_websocket = 10,
err_not_found = 11,
};
pub const InitError = error{
PoolInitError,
} || std.mem.Allocator.Error;
pub fn init(allocator: std.mem.Allocator) InitError!CApi {
const internal = try allocator.create(App);
errdefer allocator.destroy(internal);
internal.* = App.init(allocator);
internal.pool.init(.{ .allocator = allocator, .n_jobs = 4 }) catch |err| {
std.log.err("Unable to initialize thread pool: {s}", .{@errorName(err)});
return InitError.PoolInitError;
};
errdefer internal.deinit();
const server_selector = try allocator.create(ServerSelector.CApi);
errdefer allocator.destroy(server_selector);
server_selector.* = try ServerSelector.CApi.init(allocator, &internal.pool);
errdefer server_selector.deinit(allocator);
return .{
.internal = internal,
.server_selector = server_selector,
};
}
pub fn new() callconv(.C) ?*CApi {
const capi = std.heap.c_allocator.create(CApi) catch return null;
capi.* = CApi.init(std.heap.c_allocator) catch {
std.heap.c_allocator.destroy(capi);
return null;
};
return capi;
}
pub fn deinit(self: *CApi, allocator: std.mem.Allocator) void {
if (self.server) |server| {
server.internal.conn.deinit();
self.internal.allocator.destroy(server.internal.conn);
server.deinit();
self.internal.allocator.destroy(server);
}
self.server_selector.deinit(allocator);
allocator.destroy(self.server_selector);
self.internal.deinit();
allocator.destroy(self.internal);
self.internal = undefined;
}
pub fn destroy(self_ptr: ?*CApi) callconv(.C) void {
const self = self_ptr orelse return;
std.log.debug("Destroying application...", .{});
self.deinit(std.heap.c_allocator);
std.heap.c_allocator.destroy(self);
}
pub fn setStatePath(self_ptr: ?*CApi, path_ptr: ?[*:0]const u8, path_len: usize) callconv(.C) void {
const self = self_ptr orelse return;
const path = if (path_ptr) |ptr| ptr[0..path_len] else {
std.log.warn("A path given to plac_app_set_state_path is NULL", .{});
return;
};
std.log.debug("Setting state path to {s}...", .{path});
self.internal.state_file_path = self.internal.allocator.dupe(u8, path) catch {
std.log.err("Unable to clone state file path: out of memory", .{});
return;
};
}
pub fn restoreState(self_ptr: ?*CApi) callconv(.C) void {
const self = self_ptr orelse return;
const file_path = self.internal.state_file_path orelse {
std.log.debug("No state file is configured, skipping restore phase", .{});
self.internal.on_restore_complete.runAll(.{});
return;
};
if (!std.fs.path.isAbsolute(file_path)) {
std.log.err("State file path is not absolute: {s}", .{file_path});
self.internal.on_restore_complete.runAll(.{});
return;
}
self.internal.pool.spawn(restoreWorker, .{ self, file_path }) catch |err| {
std.log.err("Unable to spawn thread for restore job: {s}", .{@errorName(err)});
self.internal.on_restore_complete.runAll(.{});
return;
};
}
fn restoreWorker(self: *CApi, file_path: []const u8) void {
defer self.internal.on_restore_complete.runAll(.{});
const file = std.fs.openFileAbsolute(file_path, .{}) catch |err| {
switch (err) {
error.FileNotFound => return,
else => {
std.log.err("Unable to open state file: {s}", .{@errorName(err)});
return;
},
}
};
var reader = std.json.reader(self.internal.allocator, file.reader());
defer reader.deinit();
const parsed = std.json.parseFromTokenSource(
State,
self.internal.allocator,
&reader,
.{},
) catch |err| {
std.log.err("Failed to parse state file: {s}", .{@errorName(err)});
return;
};
defer parsed.deinit();
const conn = parsed.value.connection orelse {
std.log.debug("No connection stored on state file, skipping", .{});
return;
};
std.log.debug("Loaded state, connecting to {s}", .{conn.server_id});
self.connectInner(conn.server_id, conn.token);
}
fn saveState(self: *CApi, state: State) void {
const file_path = self.internal.state_file_path orelse {
std.log.debug("State file path is not set, skipping", .{});
return;
};
std.log.debug("Saving state to {s}...", .{file_path});
const file = std.fs.createFileAbsolute(file_path, .{}) catch |err| {
std.log.err("Unable to create or open {s}: {s}", .{ file_path, @errorName(err) });
return;
};
defer file.close();
std.json.stringify(state, .{ .whitespace = .indent_tab }, file.writer()) catch |err| {
std.log.err("Unable to write state to {s}: {s}", .{ file_path, @errorName(err) });
return;
};
}
pub fn connect(
self_ptr: ?*CApi,
server_id_ptr: [*:0]const u8,
server_id_len: usize,
saved_token_ptr: ?[*:0]const u8,
saved_token_len: usize,
) callconv(.C) void {
const self = self_ptr orelse return;
const server_id = server_id_ptr[0..server_id_len];
const saved_token = if (saved_token_ptr) |tok| tok[0..saved_token_len] else null;
self.connectInner(server_id, saved_token);
}
fn connectInner(self: *CApi, server_id: []const u8, saved_token: ?[]const u8) void {
if (self.connection == .busy) {
return;
}
if (self.server) |server| {
if (std.mem.eql(u8, server.id[0..server.id_len], server_id)) {
return;
}
}
{
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.connection = .busy;
self.internal.on_connection_change.runAll(.{});
}
const server_id_t = self.internal.allocator.dupe(u8, server_id) catch {
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.connection = .err_out_of_memory;
self.internal.on_connection_change.runAll(.{});
return;
};
const saved_token_t = if (saved_token) |t| self.internal.allocator.dupe(u8, t) catch {
self.internal.allocator.free(server_id_t);
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.connection = .err_out_of_memory;
self.internal.on_connection_change.runAll(.{});
return;
} else null;
self.internal.pool.spawn(connectWorker, .{ self, server_id_t, saved_token_t }) catch {
self.internal.allocator.free(server_id_t);
if (saved_token_t) |t| {
self.internal.allocator.free(t);
}
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.connection = .err_thread_spawn;
self.internal.on_connection_change.runAll(.{});
return;
};
}
pub fn lock(capi_ptr: ?*CApi) callconv(.C) void {
const capi = capi_ptr orelse return;
capi.internal.capi_lock.lock();
}
pub fn unlock(capi_ptr: ?*CApi) callconv(.C) void {
const capi = capi_ptr orelse return;
capi.internal.capi_lock.unlock();
}
pub fn onServerChange(capi_ptr: ?*CApi, cb: OnServerChange.Fn, userdata: callback.UserData) callconv(.C) void {
const capi = capi_ptr orelse return;
// TODO: Notify error to caller
capi.internal.on_server_change.add(OnServerChange.init(cb, userdata)) catch {};
}
pub fn onServerChangeDisarm(capi_ptr: ?*CApi, cb: OnServerChange.Fn, userdata: callback.UserData) callconv(.C) void {
const capi = capi_ptr orelse return;
capi.internal.on_server_change.remove(OnServerChange.init(cb, userdata));
}
pub fn onConnectionChange(capi_ptr: ?*CApi, cb: OnConnectionChange.Fn, userdata: callback.UserData) callconv(.C) void {
const capi = capi_ptr orelse return;
// TODO: Notify error to caller
capi.internal.on_connection_change.add(OnConnectionChange.init(cb, userdata)) catch {};
}
pub fn onConnectionChangeDisarm(capi_ptr: ?*CApi, cb: OnConnectionChange.Fn, userdata: callback.UserData) callconv(.C) void {
const capi = capi_ptr orelse return;
capi.internal.on_connection_change.remove(OnConnectionChange.init(cb, userdata));
}
pub fn onRestoreComplete(capi_ptr: ?*CApi, cb: OnRestoreComplete.Fn, userdata: callback.UserData) callconv(.C) void {
const capi = capi_ptr orelse return;
// TODO: Notify error to caller
capi.internal.on_restore_complete.add(OnRestoreComplete.init(cb, userdata)) catch {};
}
pub fn onRestoreCompleteDisarm(capi_ptr: ?*CApi, cb: OnRestoreComplete.Fn, userdata: callback.UserData) callconv(.C) void {
const capi = capi_ptr orelse return;
capi.internal.on_restore_complete.remove(OnRestoreComplete.init(cb, userdata));
}
};
fn connectWorker(self: *CApi, server_id: []const u8, saved_token: ?[]const u8) void {
defer {
self.internal.allocator.free(server_id);
if (saved_token) |t| {
self.internal.allocator.free(t);
}
}
if (self.server) |server| {
server.internal.conn.deinit();
self.internal.allocator.destroy(server.internal.conn);
server.deinit();
self.internal.allocator.destroy(server);
self.server = null;
}
for (self.server_selector.entries[0..self.server_selector.entries_len]) |entry| {
if (std.mem.eql(u8, entry.id[0..entry.id_len], server_id)) {
const conn, const token = register(
self.internal.allocator,
entry.getAddr(),
&self.internal.pool,
server_id,
saved_token,
) catch |err| {
std.log.warn("Unable to connect to pre-scanned sever, skipping: {s}", .{
@errorName(err),
});
continue;
};
defer self.internal.allocator.free(token);
self.saveState(.{
.connection = .{
.server_id = server_id,
.token = token,
},
});
const server = self.internal.allocator.create(Server.CApi) catch {
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.connection = .err_out_of_memory;
self.internal.on_connection_change.runAll(.{});
return;
};
server.* = Server.CApi.init(
self.internal.allocator,
entry.getAddr(),
conn,
entry.getId(),
entry.getName(),
entry.getVersion(),
token,
&self.internal.pool,
) catch {
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.connection = .err_out_of_memory;
self.internal.on_connection_change.runAll(.{});
return;
};
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.server = server;
self.connection = .idle;
self.internal.on_server_change.runAll(.{});
self.internal.on_connection_change.runAll(.{});
return;
}
}
const info = discovery.resolve(ServerConnInfo, self.internal.allocator, server_id, .{}) catch |err| {
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.connection = switch (err) {
error.OutOfMemory => App.CApi.ConnectionState.err_out_of_memory,
error.SocketPermissionDenied => App.CApi.ConnectionState.err_socket_permission,
error.SocketCreationError,
error.UDPRecvError,
error.UDPSendError,
=> App.CApi.ConnectionState.err_socket,
error.NetworkUnavailable => App.CApi.ConnectionState.err_network_unavailable,
else => App.CApi.ConnectionState.err_unexpected,
};
self.internal.on_connection_change.runAll(.{});
return;
} orelse {
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.connection = .err_not_found;
self.internal.on_connection_change.runAll(.{});
return;
};
defer info.deinit();
const conn, const token = register(
self.internal.allocator,
info.addr,
&self.internal.pool,
server_id,
saved_token,
) catch |err| {
std.log.err("Unable to connect sever: {s}", .{@errorName(err)});
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.connection = switch (err) {
error.OutOfMemory => App.CApi.ConnectionState.err_out_of_memory,
RegisterError.RegistryInfoError => App.CApi.ConnectionState.err_registry_down,
RegisterError.RegistryRegisterError => App.CApi.ConnectionState.err_failed_to_register,
RegisterError.ListeningThreadSpawnError => App.CApi.ConnectionState.err_thread_spawn,
RegisterError.ServerIdMismatch => App.CApi.ConnectionState.err_not_found,
Connection.InitError.WebSocketClientCreationError,
Connection.InitError.WebSocketHandshakeError,
=> App.CApi.ConnectionState.err_websocket,
else => App.CApi.ConnectionState.err_unexpected,
};
self.internal.on_connection_change.runAll(.{});
return;
};
defer self.internal.allocator.free(token);
self.saveState(.{
.connection = .{
.server_id = info.id,
.token = token,
},
});
const server = self.internal.allocator.create(Server.CApi) catch {
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.connection = .err_out_of_memory;
self.internal.on_connection_change.runAll(.{});
return;
};
server.* = Server.CApi.init(
self.internal.allocator,
info.addr,
conn,
info.id,
info.name,
info.version,
token,
&self.internal.pool,
) catch {
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.connection = .err_out_of_memory;
self.internal.on_connection_change.runAll(.{});
return;
};
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.server = server;
self.connection = .idle;
self.internal.on_server_change.runAll(.{});
self.internal.on_connection_change.runAll(.{});
return;
}
const RegisterError = error{
RegistryInfoError,
RegistryRegisterError,
ListeningThreadSpawnError,
ServerIdMismatch,
} || Connection.InitError;
fn register(
allocator: std.mem.Allocator,
address: std.net.Address,
pool: *std.Thread.Pool,
server_id: []const u8,
saved_token: ?[]const u8,
) !struct { *Connection, []const u8 } {
const conn = try allocator.create(Connection);
errdefer allocator.destroy(conn);
conn.* = try Connection.init(allocator, address, pool);
errdefer conn.deinit();
conn.listen(PingService.handleRequest) catch {
return RegisterError.ListeningThreadSpawnError;
};
std.log.debug("Querying registry status...", .{});
const info = RegistryService.info(allocator, conn) catch |err| {
std.log.err("Failed to get extension registry status: {s}", .{@errorName(err)});
return RegisterError.RegistryInfoError;
};
defer info.deinit();
if (!std.mem.eql(u8, info.value.core_id, server_id)) {
return RegisterError.ServerIdMismatch;
}
const extension = AppExtension{
.token = saved_token,
};
std.log.debug("Registering extension {s} token...", .{if (saved_token) |_| "with" else "without"});
const r = RegistryService.register(AppExtension, allocator, conn, extension) catch |err| {
std.log.err("Failed to register extension: {s}", .{@errorName(err)});
return RegisterError.RegistryRegisterError;
};
defer r.deinit();
std.log.debug("Extension registered, connection is ready", .{});
const token = try allocator.dupe(u8, r.value.token);
return .{ conn, token };
}