Skip to content

Commit

Permalink
Add internal methods to client
Browse files Browse the repository at this point in the history
  • Loading branch information
g41797 committed Dec 23, 2024
1 parent 12c3284 commit f2951ce
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions _notes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Client Libraries
https://github.com/bergundy/libbeanstalkclient
https://github.com/deepfryed/beanstalk-client +++
https://github.com/pheanstalk/pheanstalk PHP
https://github.com/pheanstalk/pheanstalk/tree/v5 PHP



Beanstalkd vs Gearman: What are the differences?
Expand Down
67 changes: 65 additions & 2 deletions src/client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
//! - _delete_: remove job from the system

const std = @import("std");
const net = std.net;
const http = std.http;
const Connection = http.Client.Connection;
const Mutex = std.Thread.Mutex;
const Allocator = std.mem.Allocator;

Expand All @@ -25,6 +28,9 @@ const parse = @import("parse.zig");
pub const DefaultAddr = "127.0.0.1";
pub const DafaultPort = 11300;
pub const DafaultTube = "default";
pub const DefaultDelay = 0; // no delay
pub const DefaultPriority = 1024; // most urgent: 0, least urgent: 4294967295
pub const DefaultTTR = 60; // 1 minute

pub const JobState = enum {
DELAYED,
Expand All @@ -36,7 +42,8 @@ pub const JobState = enum {
pub const Client = struct {
ready: bool = false,
mutex: Mutex = .{},
allocator: ?Allocator = null,
allocator: Allocator = undefined,
connection: ?Connection = null,

/// Returns connected to beanstalkd client.
/// Arguments:
Expand All @@ -47,7 +54,7 @@ pub const Client = struct {
/// Returns errors for:
/// - failed connection
/// - already existing connection
pub fn connect(allocator: ?Allocator, addr: ?[]const u8, port: ?u16) !*Client {
pub fn connect(allocator: Allocator, addr: ?[]const u8, port: ?u16) !*Client {
_ = allocator;
_ = addr;
_ = port;
Expand Down Expand Up @@ -163,4 +170,60 @@ pub const Client = struct {
_ = cl;
_ = tname;
}

// Writes the formatted output followed by \r\n to underlying stream.
fn print_line(cl: *Client, comptime fmt: []const u8, args: anytype) !void {
_ = cl;
_ = fmt;
_ = args;
}

// Writes the buffer followed by \r\n to underlying stream.
fn print_buffer(cl: *Client, buffer: []const u8) !void {
_ = cl;
_ = buffer;
}

// Flushes underlying stream.
fn flush(cl: *Client) !void {
_ = cl;
}

// Reads underlying stream till \r\n to the buffer.
// If buffer is small - returns error.
// Returns length of the line without \r\n.
// 0 - for \r\n only.
fn read_line(cl: *Client, buffer: []u8) !usize {
_ = cl;
_ = buffer;
}

// Reads 'len' bytes from underlying stream to the buffer.
// Also reads trailer (\r\n) but not saved it to the buffer.
// If buffer is small - returns error.
// If \r\n were not in the stream - returns error.
fn read_buffer(cl: *Client, buffer: []u8, len: usize) !void {
_ = cl;
_ = buffer;
_ = len;
}

fn connectTcp(client: *Client, host: []const u8, port: u16) !*Connection {
const conn = try client.allocator.create(Connection);
errdefer client.allocator.destroy(conn);

const stream = try net.tcpConnectToHost(client.allocator, host, port);
errdefer stream.close();

conn = .{
.stream = stream,
.tls_client = undefined,
.protocol = Connection.Protocol.plain,
.host = try client.allocator.dupe(u8, host),
.port = port,
};
errdefer client.allocator.free(conn.data.host);

return conn;
}
};

0 comments on commit f2951ce

Please sign in to comment.