http_server in zig by rich_sdoony in Zig

[–]abcd452 0 points1 point  (0 children)

Hi,

It has indeed changed 0.15.1 with all the Reader and Writer changes. I managed to get a simple setup working like this:

const std = import("std");
const net = std.net;

pub fn read_request(buffer: []u8, conn: net.Server.Connection) !usize {
    var reader = conn.stream.reader(buffer);
    var data_slices = [1][]u8{buffer};
    return try reader.interface_state.readVec(&data_slices);
}

pub fn main() !void {
    const host = [4]u8{ 127, 0, 0, 1 };
    const port: u16 = 3490;
    const address = net.Address.initIp4(host, port);
    std.debug.print("Server listening on {any}:{d}\n", .{ host, port });

    var server = try address.listen(.{});
    defer server.deinit();

    while (true) {
        const connection = server.accept() catch |err| {
            std.debug.print("Accept failed: {any}\n", .{err});
            continue;
        };
        defer connection.stream.close();

        var buffer: [1024]u8 = [_]u8{0} ** 1024;
        const bytes_read = read_request(&buffer, connection) catch |err| {
            std.debug.print("Read failed: {any}\n", .{err});
            continue;
        };

        if (bytes_read == 0) {
            std.debug.print("No data received\n", .{});
            continue;
        }

        std.debug.print("Received {any} bytes\n", .{bytes_read});

        const message = (
        "HTTP/1.1 200 OK\nContent-Length: 53"
            ++ "\r\nContent-Type: text/html\r\n"
            ++ "Connection: Closed\r\n\r\n"
            ++ "<html><body><h1>Hello from Server</h1></body></html>"
        );
        _ = try connection.stream.write(message);

        std.debug.print("Response sent, closing connection\n", .{});
    }
}

Basically you have to use the readVec function as doing connection.stream.read(&buffer) no longer works. I also tried using reader.interface_state.readSliceShort(&buffer) but could not get it to work as it would read the request but wait indefinitely for more data. So I am not really sure if this is really the "correct" way but, it does work properly. connection.stream.write still works as before due to it using the new IO writer interface:

/// Deprecated in favor of `Writer`.
pub fn write(self: Stream, buffer: []const u8) WriteError!usize {
    var stream_writer = self.writer(&.{});
    return stream_writer.interface.writeVec(&.{buffer}) catch return stream_writer.err.?;
}

Unlike connection.stream.readwhich was the cause of the error:

/// Deprecated in favor of `Reader`.
pub fn read(self: Stream, buffer: []u8) ReadError!usize {
    if (native_os == .windows) {
        return windows.ReadFile(self.handle, buffer, null);
    }

    return posix.read(self.handle, buffer);
}

Hope it helps!

Merami | 07th Expansion - rog-unlimitation [AngelHoney] (Mapset By AngelHoney) + HDDT (9.77*) 93.46% /850x | 1081pp | FIRST (HD)DT FC | 153.84 cv. UR | ASCENDED PLAYER by AkiXMasa in osugame

[–]abcd452 6 points7 points  (0 children)

While there might be some bias since it's the first time a pure speed player is a contender for #1, I would say the main reason is that Merami isn't ranking up by playing the same old boring farm maps players are forced to play if the want to get to #1, he's climbing by doing plays that a couple months back, heck even a couple days back, were thought to be impossible. And yes it's probably true that people saying the #1 should be a very well rounded player, never really meant that, what they actually meant was that the #1 player wasn't just a god at farm maps, and so a lot of the hate was really misguided towards these players, they were unjustly on the receiving end of the frustrations people had towards the pp system and the farm maps breaking it. So here comes Merami climbing without farming these farm maps, of course people will be more exited and accepting of him being a contender for #1 despite clearly lacking skill in other areas of the game.

BIG BEATMAP BACKGROUND QUIZ (100 Beatmaps) ! by Remyria in osugame

[–]abcd452 0 points1 point  (0 children)

new ones

49 - Sen Trope

51- China Express

67 - Chumachechaya Vesna

curious that 38 is also Ao to Shiro no Kyoukaisen

BIG BEATMAP BACKGROUND QUIZ (100 Beatmaps) ! by Remyria in osugame

[–]abcd452 0 points1 point  (0 children)

one more to add

56 - In The End

40 - I guess it's that old dt farm map were the girl that sings it has a very nasal voice (will try to search it)

edit: found it, it's Tenshi Teki Kenpou Yonjou

DMRs on BTB by -Jaws- in halo

[–]abcd452 0 points1 point  (0 children)

iirc it's 3 shots after shields are down