From fb1059e3782a3e6d8f183899f7445f94593a7441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Thu, 22 May 2025 15:39:29 +0200 Subject: [PATCH] chore: refactor --- src/httpClient.zig | 70 ++++++++++++++++++++++++++-------------------- src/main.zig | 1 - 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/httpClient.zig b/src/httpClient.zig index 8f3ce36..1c75297 100644 --- a/src/httpClient.zig +++ b/src/httpClient.zig @@ -37,7 +37,12 @@ pub const HttpHeader = struct { 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 { if (p1 == null) { @@ -164,40 +169,43 @@ pub const Client = struct { print("Client.sendRequest"); 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_string = switch (host) { - .raw => |v| v, - .percent_encoded => |v| v, - }; + const host = if (request.url.host) |host| host else unreachable; + const host_string = switch (host) { + .raw => |v| v, + .percent_encoded => |v| v, + }; - const host_c_string = try client.allocator.alloc(u8, host_string.len + 1); - defer client.allocator.free(host_c_string); - host_c_string[host_c_string.len - 1] = 0; - for (0..host_string.len) |i| { - host_c_string[i] = host_string[i]; + const host_c_string = try client.allocator.alloc(u8, host_string.len + 1); + defer client.allocator.free(host_c_string); + host_c_string[host_c_string.len - 1] = 0; + for (0..host_string.len) |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..."); while (!context.finished) { diff --git a/src/main.zig b/src/main.zig index c883df5..e0bc919 100644 --- a/src/main.zig +++ b/src/main.zig @@ -40,7 +40,6 @@ const appSettings: AppSettings = x: { }; }; -// Basically the pico_w blink sample export fn main() c_int { _ = p.stdio_init_all(); p.sleep_ms(2000);