beterraba

commit fda4deb0d475cb8c368d2ae936277e3b5c57ec87

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

server: read client messages

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


diff --git a/cmd/beterrabad/socket2.ha b/cmd/beterrabad/socket2.ha
index 8b78ab54969d44a52aae5cecf2fb31005f8fba2b..a3ae8bda17696242bdb8c9ded295531d79762d67 100644
--- a/cmd/beterrabad/socket2.ha
+++ b/cmd/beterrabad/socket2.ha
@@ -10,6 +10,9 @@ use unix::poll;
 use unix::signal;
 use unix::poll::{event};
 use path;
+use bufio;
+use strings;
+use encoding::utf8;
 
 type server2 = struct {
 	sock: net::socket,
@@ -75,12 +78,50 @@ 		if (s.pollfd[1].revents & event::POLLIN != 0) {
 			signal::read(s.signalfd)!;
 			return false;
 		};
+
+		for (let i = 2z; i < len(s.pollfd); i += 1) {
+			dispatch_client2(s, &s.clients[i - 2]);
+		};
 	};
 	case let err: errors::error =>
 		log::fatal("poll:", errors::strerror(err));
 	};
 
 	return true;
+};
+
+fn dispatch_client2(s: *server2, client: *client2) void = {
+	let cpollfd = client.pollfd;
+
+	if (cpollfd.revents & event::POLLIN != 0) {
+		read_client(s, client);
+	};
+
+	if (cpollfd.revents & event::POLLOUT != 0) {
+		log::println("Write to client");
+	};
+};
+
+fn read_client(s: *server2, client: *client2) void = {
+	let bufline = match (bufio::scanline(client.sock)) {
+	case let l: []u8 =>
+		yield l;
+	case io::EOF =>
+		// disconnect
+		log::println("EOF");
+	case io::error =>
+		// disconnect
+		log::fatalf("io error");
+	};
+
+	let line = match (strings::try_fromutf8(bufline as []u8)) {
+	case let s: str =>
+		yield s;
+	case utf8::invalid =>
+		log::fatal("invalid utf-8");
+	};
+
+	log::printfln("Line: {}", line);
 };
 
 // TODO: Check if we reached max client connections first