diff options
author | ubq323 <ubq323@ubq323.website> | 2024-04-10 20:06:49 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2024-04-10 20:06:53 +0100 |
commit | 2677d3ae0f4f5323ca67c6436cd3a55fd4fe8021 (patch) | |
tree | 16532c2cd1b1478f0adc8dd1efc96564ee25c396 /server | |
parent | 40231bd839dbe5b8aa65e8f3bde31bd33b9a9190 (diff) |
send and recv drawing ops c2s
Diffstat (limited to 'server')
-rw-r--r-- | server/main.ha | 98 | ||||
-rw-r--r-- | server/packet.ha | 2 |
2 files changed, 50 insertions, 50 deletions
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, |