Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
server: rig up writing to clients
cmd/beterrabad/socket2.ha | 36 +++++++++++++++++++++++++++++++++---
diff --git a/cmd/beterrabad/socket2.ha b/cmd/beterrabad/socket2.ha index 4a07c208df42e6c333964bbb12b5b550952f7ca5..c5796abebf1509c4396e2b3fd552a54d5b39736f 100644 --- a/cmd/beterrabad/socket2.ha +++ b/cmd/beterrabad/socket2.ha @@ -1,4 +1,5 @@ use io; +use fmt; use dirs; use fs; use errors; @@ -13,6 +14,8 @@ use path; use bufio; use strings; use encoding::utf8; + +type servererror2 = !(io::error | fs::error); type server2 = struct { sock: net::socket, @@ -148,7 +151,7 @@ case state2::WRITE => client.state = state2::READ; client.pollfd.events = event::POLLIN | event::POLLHUP; case state2::WRITE_ERROR => - disconnect(client); + disconnect_client(client); case => abort(); }; }; @@ -170,11 +173,38 @@ case utf8::invalid => log::fatal("invalid utf-8"); }; - exec2(line); + match (exec2(line, client)) { + case void => + void; + case servererror2 => + log::fatal("Fudeu"); + }; }; -fn exec2(command: str) void = { +fn exec2(command: str, client: *client2) (servererror2 | void) = { + let buf = bufio::dynamic(io::mode::WRITE); + // TODO: Flesh out command execution here + if (command == "ping") { + fmt::fprintf(&buf, "pong\n")?; + }; + + fmt::fprintln(&buf, "end")?; + writebuf2(client, bufio::buffer(&buf)); +}; + +// Writes data to the client. Takes ownership over the buffer. +fn commit_write(client: *client2, buf: []u8) void = { + assert(client.state != state2::WRITE + && client.state != state2::WRITE_ERROR); + client.wbuf = buf; + client.state = state2::WRITE; + client.pollfd.events = event::POLLOUT | event::POLLHUP; +}; + +// Writes data to the client. Duplicates the buffer. +fn writebuf2(client: *client2, buf: []u8) void = { + commit_write(client, alloc(buf...)); }; // TODO: Check if we reached max client connections first