chore: refactor
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user