Fix ring buffer
This commit is contained in:
parent
e70a04ac16
commit
c9c2b45b2a
17 changed files with 330 additions and 135 deletions
|
@ -0,0 +1,50 @@
|
|||
const std = @import("std");
|
||||
|
||||
const msg_queue = @import("../msg_queue/root.zig");
|
||||
const models = @import("../models/root.zig");
|
||||
|
||||
const Self = @This();
|
||||
|
||||
const log = std.log.scoped(.block_update_dispatcher);
|
||||
|
||||
pub const Config = struct {
|
||||
capacity: usize,
|
||||
};
|
||||
|
||||
allocator: std.mem.Allocator,
|
||||
|
||||
config: *const Config,
|
||||
queue: *msg_queue.MsgQueueUnmanaged(models.BlockUpdateMsg),
|
||||
stream: ?std.net.Stream = null,
|
||||
|
||||
pub fn init(
|
||||
allocator: std.mem.Allocator,
|
||||
config: *const Config,
|
||||
queue: *msg_queue.MsgQueueUnmanaged(models.BlockUpdateMsg),
|
||||
stream: std.net.Stream,
|
||||
) Self {
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.config = config,
|
||||
.queue = queue,
|
||||
.stream = stream,
|
||||
};
|
||||
}
|
||||
|
||||
fn send(self: *Self, update: *const models.BlockUpdateMsg) !void {
|
||||
var buf: [models.BlockUpdateMsg.csv_max_len]u8 = undefined;
|
||||
const stream = self.stream.?;
|
||||
const csv = try update.toCsv(&buf);
|
||||
try stream.writeAll(csv);
|
||||
try stream.writeAll(&.{'\n'});
|
||||
}
|
||||
|
||||
pub fn start(self: *Self) !void {
|
||||
const buf = try self.allocator.alloc(models.BlockUpdateMsg, self.queue.capacity());
|
||||
defer self.allocator.free(buf);
|
||||
|
||||
while (true) {
|
||||
const updates = try self.queue.tryDequeueAll(buf);
|
||||
for (updates) |*update| try self.send(update);
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
pub const BlockUpdate = @import("BlockUpdate.zig");
|
Loading…
Add table
Add a link
Reference in a new issue