chore: refactor

This commit is contained in:
2025-05-22 15:39:29 +02:00
parent d92ee9c10c
commit fb1059e378
2 changed files with 39 additions and 32 deletions

View File

@@ -37,7 +37,12 @@ pub const HttpHeader = struct {
value: []const u8, value: []const u8,
}; };
pub const HttpContext = struct { client: *const Client, request: *const HttpRequest, response: ?*const HttpResponse = null, finished: bool = false }; pub const HttpContext = struct {
client: *const Client,
request: *const HttpRequest,
response: ?*const HttpResponse = null,
finished: bool = false,
};
fn tcp_recv_callback(context: ?*anyopaque, pcb: ?*cNet.tcp_pcb, p1: ?*cNet.pbuf, _: cNet.err_t) callconv(.C) cNet.err_t { fn tcp_recv_callback(context: ?*anyopaque, pcb: ?*cNet.tcp_pcb, p1: ?*cNet.pbuf, _: cNet.err_t) callconv(.C) cNet.err_t {
if (p1 == null) { if (p1 == null) {
@@ -164,40 +169,43 @@ pub const Client = struct {
print("Client.sendRequest"); print("Client.sendRequest");
var context: HttpContext = .{ .client = client, .request = request }; var context: HttpContext = .{ .client = client, .request = request };
cNet.cyw43_arch_lwip_begin(); {
var cached_address = cNet.ip_addr_t{}; cNet.cyw43_arch_lwip_begin();
defer cNet.cyw43_arch_lwip_end();
var cached_address = cNet.ip_addr_t{};
const host = if (request.url.host) |host| host else unreachable; const host = if (request.url.host) |host| host else unreachable;
const host_string = switch (host) { const host_string = switch (host) {
.raw => |v| v, .raw => |v| v,
.percent_encoded => |v| v, .percent_encoded => |v| v,
}; };
const host_c_string = try client.allocator.alloc(u8, host_string.len + 1); const host_c_string = try client.allocator.alloc(u8, host_string.len + 1);
defer client.allocator.free(host_c_string); defer client.allocator.free(host_c_string);
host_c_string[host_c_string.len - 1] = 0; host_c_string[host_c_string.len - 1] = 0;
for (0..host_string.len) |i| { for (0..host_string.len) |i| {
host_c_string[i] = host_string[i]; host_c_string[i] = host_string[i];
}
print("Sending DNS resolution for host");
print(host_string);
const result = cNet.dns_gethostbyname(@ptrCast(host_c_string), &cached_address, dns_callback, @ptrCast(@alignCast(&context)));
if (result == cNet.ERR_OK) {
print("DNS resolution returned with OK");
cNet.sleep_ms(10000);
open_tcp_connection(&cached_address, &context, @ptrCast(@alignCast(&context)));
context.finished = true;
} else if (result == cNet.ERR_INPROGRESS) {
print("DNS resolution returned with INPROGRESS");
} else if (result == cNet.ERR_ARG) {
print("DNS resolution returned with ERR_ARG");
context.finished = true;
}
print("Calling cyw43_arch_lwip_end");
} }
print("Sending DNS resolution for host");
print(host_string);
const result = cNet.dns_gethostbyname(@ptrCast(host_c_string), &cached_address, dns_callback, @ptrCast(@alignCast(&context)));
if (result == cNet.ERR_OK) {
print("DNS resolution returned with OK");
cNet.sleep_ms(10000);
open_tcp_connection(&cached_address, &context, @ptrCast(@alignCast(&context)));
context.finished = true;
} else if (result == cNet.ERR_INPROGRESS) {
print("DNS resolution returned with INPROGRESS");
} else if (result == cNet.ERR_ARG) {
print("DNS resolution returned with ERR_ARG");
context.finished = true;
}
print("Calling cyw43_arch_lwip_end");
cNet.cyw43_arch_lwip_end();
print("Ending sendRequest..."); print("Ending sendRequest...");
while (!context.finished) { while (!context.finished) {

View File

@@ -40,7 +40,6 @@ const appSettings: AppSettings = x: {
}; };
}; };
// Basically the pico_w blink sample
export fn main() c_int { export fn main() c_int {
_ = p.stdio_init_all(); _ = p.stdio_init_all();
p.sleep_ms(2000); p.sleep_ms(2000);