summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client/main.ha1
-rw-r--r--drawing/op.ha21
-rw-r--r--server/main.ha98
-rw-r--r--server/packet.ha2
4 files changed, 72 insertions, 50 deletions
diff --git a/client/main.ha b/client/main.ha
index 7bbb706..6c9208a 100644
--- a/client/main.ha
+++ b/client/main.ha
@@ -81,6 +81,7 @@ export fn main() void = {
case void => yield;
case let op: drawing::op =>
drawing::perform(pictures, op);
+ drawing::send_op(conn, op)!;
};
for (let i = 0z; i < len(pictures); i += 1) {
diff --git a/drawing/op.ha b/drawing/op.ha
index ef39161..deda81c 100644
--- a/drawing/op.ha
+++ b/drawing/op.ha
@@ -1,4 +1,25 @@
+use io;
+use endian;
+
export type op_circle = pos;
export type op_other = void;
export type op = (op_circle| op_other);
+
+
+
+export fn send_op(file: io::handle, op: op) (void | io::error) = {
+ static let buf: [8]u8 = [0...];
+ const opc = op as op_circle;
+ endian::leputu32(buf[0..4], opc.0: u32);
+ endian::leputu32(buf[4..8], opc.1: u32);
+ io::writeall(file, buf)?;
+};
+
+export fn deser_op(bytes: []u8) op = {
+ assert(len(bytes) == 8, "wrong length somehow");
+ return (
+ endian::legetu32(bytes[0..4]): i32,
+ endian::legetu32(bytes[4..8]): i32,
+ ) : op_circle;
+};
diff --git a/server/main.ha b/server/main.ha
index 4476221..4223b77 100644
--- a/server/main.ha
+++ b/server/main.ha
@@ -6,6 +6,7 @@ use io;
use os;
use unix::poll;
use bufio;
+use drawing;
def PORT: u16 = 41460;
@@ -14,56 +15,55 @@ type conn = net::socket;
export fn main() void = {
const listener = tcp::listen(ip::ANY_V4, PORT)!;
- const conn = tcp::accept(listener)!;
-
- const pr = packet_reader_new();
- for :mainloop (true) {
- if (packet_reader_read(&pr, conn)! is io::EOF)
- fmt::fatal("disconnected");
- let i = 0;
- for (const packet => packet_reader_next(&pr)) {
- fmt::printf("#{} got {}:",i,len(packet))!;
- i+=1;
- for (let byte .. packet) fmt::printf(" {:x}",byte)!;
- fmt::println("")!;
- };
- };
-
+ loop(listener);
};
+fn loop(listener: net::socket) void = {
+ // pollfds[0] is the listener
+ // pollfds[n>0] corresponds to packet_readers[n-1]
+ let pollfds: []poll::pollfd = alloc([ poll::pollfd {
+ fd = listener, events = poll::event::POLLIN, revents = 0
+ }]);
+ let packet_readers: []packet_reader = [];
-// fn loop(listener: net::socket) void = {
-// const buf: [256]u8 = [0...];
-// let pollfds: []poll::pollfd = alloc([ poll::pollfd {
-// fd = listener, events = poll::event::POLLIN, revents = 0
-// }]);
-
-// for (true) {
-// poll::poll(pollfds, poll::INDEF)!;
-// if (0 != pollfds[0].revents & poll::event::POLLIN) {
-// fmt::println("new conn")!;
-// append(pollfds, poll::pollfd {
-// fd = tcp::accept(pollfds[0].fd)!,
-// events = poll::event::POLLIN, revents = 0
-// });
-// };
-// for (let i = 1z; i < len(pollfds); i += 1) {
-// if (0 != pollfds[i].revents & poll::event::POLLIN) {
-// match (io::read(pollfds[i].fd, buf)!) {
-// case io::EOF =>
-// fmt::println("disconnect", i)!;
-// delete(pollfds[i]);
-// i -= 1;
-// case let n: size =>
-// io::write(os::stdout_file, buf[..n])!;
-// for (let j = 1z; j < len(pollfds); j += 1) {
-// if (i != j) {
-// io::writeall(pollfds[j].fd, buf[..n])!;
-// };
-// };
-// };
-// };
-// };
-// };
-// };
+ for (true) {
+ fmt::println("poll.")!;
+ poll::poll(pollfds, poll::INDEF)!;
+ if (0 != pollfds[0].revents & poll::event::POLLIN) {
+ fmt::println("new conn")!;
+ const new_conn = tcp::accept(pollfds[0].fd)!;
+ fmt::println("a")!;
+ append(pollfds, poll::pollfd {
+ fd = new_conn,
+ events = poll::event::POLLIN, revents = 0
+ });
+ append(packet_readers, packet_reader_new());
+ fmt::printfln("there are now {},{} conns.",len(pollfds),len(packet_readers))!;
+ };
+ for (let i = 1z; i < len(pollfds); i += 1) {
+ if (0 != pollfds[i].revents & poll::event::POLLIN) {
+ match (packet_reader_read(&packet_readers[i-1], pollfds[i].fd)) {
+ case void =>
+ let j = 1;
+ for (const packet_bytes => packet_reader_next(&packet_readers[i-1])) {
+ fmt::printf("#{}:{} got {:2}",i,j,len(packet_bytes))!;
+ for (let byte .. packet_bytes) fmt::printf(" {:x}",byte)!;
+ const op = drawing::deser_op(packet_bytes) as drawing::op_circle;
+ fmt::printfln("\t({:+6},{:+6})",op.0,op.1)!;
+ };
+ case let err: io::error =>
+ fmt::printfln("#{} error: {}",i,io::strerror(err))!;
+ delete(pollfds[i]);
+ delete(packet_readers[i-1]);
+ i -= 1;
+ case io::EOF =>
+ fmt::printfln("#{} disconnect",i)!;
+ delete(pollfds[i]);
+ delete(packet_readers[i-1]);
+ i -= 1;
+ };
+ };
+ };
+ };
+};
diff --git a/server/packet.ha b/server/packet.ha
index b12d121..813ee6f 100644
--- a/server/packet.ha
+++ b/server/packet.ha
@@ -2,7 +2,7 @@ use io;
use fmt;
type packet = []u8;
-def PCKSZ: size = 4;
+def PCKSZ: size = 8;
type packet_reader = struct {
buf: [256]u8,