diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/main.ha | 15 | ||||
-rw-r--r-- | server/packet.ha | 52 |
2 files changed, 11 insertions, 56 deletions
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))!; diff --git a/server/packet.ha b/server/packet.ha deleted file mode 100644 index 813ee6f..0000000 --- a/server/packet.ha +++ /dev/null @@ -1,52 +0,0 @@ -use io; -use fmt; - -type packet = []u8; -def PCKSZ: size = 8; - -type packet_reader = struct { - buf: [256]u8, - good: []u8, -}; - -fn packet_reader_new() packet_reader = { - let pr = packet_reader { - buf = [0...], - ... - }; - pr.good = pr.buf[0..0]; - return pr; - -}; - -// call when input is ready. could block otherwise -fn packet_reader_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 - // move unconsumed stuff to start of buffer - fmt::printfln("moving {} remaining bytes",remaining_amt)!; - pr.buf[0..remaining_amt] = pr.good; - yield remaining_amt; - } else 0z; - const nread = match(io::read(sock, pr.buf[read_pos..])?) { - case io::EOF => return io::EOF; - case let n: size => yield n; - }; - fmt::printfln("read {} bytes",nread)!; - const total_amt = read_pos + nread; - pr.good = pr.buf[0..total_amt]; - fmt::printfln("now {} bytes in buffer",total_amt)!; -}; - - -fn packet_reader_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, - // or, ascertain there is no full packet, and return done - if (len(pr.good) < PCKSZ) return done; - const packet_bytes = pr.good[..PCKSZ]; - pr.good = pr.good[PCKSZ..]; - return packet_bytes; -}; |