diff options
-rw-r--r-- | client/main.ha | 18 | ||||
-rw-r--r-- | packet_reader/packet_reader.ha (renamed from server/packet.ha) | 10 | ||||
-rw-r--r-- | server/main.ha | 15 |
3 files changed, 33 insertions, 10 deletions
diff --git a/client/main.ha b/client/main.ha index 6c9208a..542c8f9 100644 --- a/client/main.ha +++ b/client/main.ha @@ -6,6 +6,8 @@ use net::dial; use drawing; use drawing::{pos}; use client::paintui; +use unix::poll; +use packet_reader; def CHUNKSIZE = 512; def NCHUNKS = 4; @@ -43,7 +45,10 @@ export fn main() void = { case let err: net::error => fmt::fatal("couldn't connect to server:",net::strerror(err)); }; - + const pollfd: [1]poll::pollfd = [ poll::pollfd { + fd = conn, events=poll::event::POLLIN, revents = 0 + }]; + const packet_reader = packet_reader::new(); // paintui state let pstate = paintui::state { ... }; @@ -84,6 +89,17 @@ export fn main() void = { drawing::send_op(conn, op)!; }; + const n = poll::poll(pollfd, poll::NONBLOCK)!; + if (n > 0) { + fmt::println("data available")!; + packet_reader::read(&packet_reader, conn)!; + for (const packet_bytes => packet_reader::next(&packet_reader)) { + const op = drawing::deser_op(packet_bytes); + drawing::perform(pictures, op); + }; + }; + + for (let i = 0z; i < len(pictures); i += 1) { const psurf = picture_surfaces[i]; const pic = &pictures[i]; diff --git a/server/packet.ha b/packet_reader/packet_reader.ha index 813ee6f..43b85d1 100644 --- a/server/packet.ha +++ b/packet_reader/packet_reader.ha @@ -1,15 +1,15 @@ use io; use fmt; -type packet = []u8; +export type packet = []u8; def PCKSZ: size = 8; -type packet_reader = struct { +export type packet_reader = struct { buf: [256]u8, good: []u8, }; -fn packet_reader_new() packet_reader = { +export fn new() packet_reader = { let pr = packet_reader { buf = [0...], ... @@ -20,7 +20,7 @@ fn packet_reader_new() packet_reader = { }; // call when input is ready. could block otherwise -fn packet_reader_read(pr: *packet_reader, sock: io::handle) (void | io::error | io::EOF) = { +export fn read(pr: *packet_reader, sock: io::handle) (void | io::error | io::EOF) = { const remaining_amt = len(pr.good); const read_pos = if (remaining_amt > 0) { // still some unconsumed content in the buffer @@ -40,7 +40,7 @@ fn packet_reader_read(pr: *packet_reader, sock: io::handle) (void | io::error | }; -fn packet_reader_next(pr: *packet_reader) (packet | done) = { +export fn next(pr: *packet_reader) (packet | done) = { // either parse a full packet out of the front of good, // move good along that many bytes, // and return the packet, diff --git a/server/main.ha b/server/main.ha index 4223b77..59e1306 100644 --- a/server/main.ha +++ b/server/main.ha @@ -7,6 +7,7 @@ use os; use unix::poll; use bufio; use drawing; +use packet_reader; def PORT: u16 = 41460; @@ -24,7 +25,7 @@ fn loop(listener: net::socket) void = { let pollfds: []poll::pollfd = alloc([ poll::pollfd { fd = listener, events = poll::event::POLLIN, revents = 0 }]); - let packet_readers: []packet_reader = []; + let packet_readers: []packet_reader::packet_reader = []; for (true) { @@ -38,19 +39,25 @@ fn loop(listener: net::socket) void = { fd = new_conn, events = poll::event::POLLIN, revents = 0 }); - append(packet_readers, packet_reader_new()); + 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)) { + 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])) { + for (const packet_bytes => packet_reader::next(&packet_readers[i-1])) { fmt::printf("#{}:{} got {:2}",i,j,len(packet_bytes))!; + j += 1; 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)!; + for (let k = 1z; k < len(pollfds); k += 1) { + if (k == i) continue; + fmt::printfln("\t -> #{}",k)!; + io::writeall(pollfds[k].fd, packet_bytes)!; + }; }; case let err: io::error => fmt::printfln("#{} error: {}",i,io::strerror(err))!; |