beterraba

commit da47988b322358832547351cd1492c03a8904981

Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>

server: write to clients

 cmd/beterrabad/socket2.ha | 41 +++++++++++++++++++++++++++++++++++++++--


diff --git a/cmd/beterrabad/socket2.ha b/cmd/beterrabad/socket2.ha
index b4103a249fe406ce9c104be0bbe42cacf5770029..4a07c208df42e6c333964bbb12b5b550952f7ca5 100644
--- a/cmd/beterrabad/socket2.ha
+++ b/cmd/beterrabad/socket2.ha
@@ -26,8 +26,15 @@ type client2 = struct {
 	server: *server2,
 	sock: io::file,
 	pollfd: *poll::pollfd,
+	state: state2,
 	wbuf: []u8,
 	rbuf: []u8
+};
+
+type state2 = enum {
+	READ,
+	WRITE,
+	WRITE_ERROR,
 };
 
 fn bind2(fd: io::file) server2 = {
@@ -116,7 +123,33 @@ 		read_client(s, client);
 	};
 
 	if (cpollfd.revents & event::POLLOUT != 0) {
-		log::println("Write to client");
+		write_client(s, client);
+	};
+};
+
+fn write_client(s: *server2, client: *client2) void = {
+	let sz = match (io::write(client.sock, client.wbuf)) {
+	case let z: size =>
+		yield z;
+	case errors::again =>
+		return;
+	case let err: io::error =>
+		log::printfln("Couldn't write to client sock due to {}", io::strerror(err));
+		disconnect_client(client);
+
+		return;
+	};
+
+	// Clean up the buffer
+	delete(client.wbuf[..sz]);
+
+	switch (client.state) {
+	case state2::WRITE =>
+		client.state = state2::READ;
+		client.pollfd.events = event::POLLIN | event::POLLHUP;
+	case state2::WRITE_ERROR =>
+		disconnect(client);
+	case => abort();
 	};
 };
 
@@ -137,7 +170,11 @@ 	case utf8::invalid =>
 		log::fatal("invalid utf-8");
 	};
 
-	log::printfln("Line: {}", line);
+	exec2(line);
+};
+
+fn exec2(command: str) void = {
+	// TODO: Flesh out command execution here
 };
 
 // TODO: Check if we reached max client connections first