From e19852c958838688481422ed3ddd953d2dcd4309 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 15 Apr 2024 17:13:08 +0100 Subject: version check --- client/main.ha | 21 +++++++++++++++++---- packet_reader/packet_reader.ha | 2 ++ server/main.ha | 22 ++++++++++++++-------- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/client/main.ha b/client/main.ha index 04e6854..4c70892 100644 --- a/client/main.ha +++ b/client/main.ha @@ -1,13 +1,16 @@ use fmt; use sdl2; use math; +use io; use net; use net::dial; +use unix::poll; + use drawing; use drawing::{pos,CHUNKSIZE}; use client::paintui; -use unix::poll; use packet_reader; +use packet_reader::{VERSION}; def NCHUNKS = 16; @@ -19,7 +22,7 @@ export fn main() void = { sdl2::SDL_Init(sdl2::SDL_INIT_VIDEO)!; defer sdl2::SDL_Quit(); - const win = sdl2::SDL_CreateWindow("hi", sdl2::SDL_WINDOWPOS_UNDEFINED, sdl2::SDL_WINDOWPOS_UNDEFINED, 640, 480, sdl2::SDL_WindowFlags::NONE)!; + const win = sdl2::SDL_CreateWindow("garden", sdl2::SDL_WINDOWPOS_UNDEFINED, sdl2::SDL_WINDOWPOS_UNDEFINED, 640, 480, sdl2::SDL_WindowFlags::NONE)!; defer sdl2::SDL_DestroyWindow(win); const wsurf = sdl2::SDL_GetWindowSurface(win)!; @@ -42,11 +45,21 @@ export fn main() void = { }; // connect to server - const conn = match(dial::dial("tcp","localhost","41460")) { + const conn = match(dial::dial("tcp","ubq323.website","41460")) { case let c: net::socket => yield c; case let err: net::error => fmt::fatal("couldn't connect to server:",net::strerror(err)); }; + + let byte = [0u8]; + match (io::read(conn, byte)) { + case let e: io::error => + fmt::fatal("error with server handshake",io::strerror(e)); + case => if (byte[0] != VERSION) + fmt::fatalf("invalid version: got {}, expecting {}", + byte[0], VERSION); + }; + const pollfd: [1]poll::pollfd = [ poll::pollfd { fd = conn, events=poll::event::POLLIN, revents = 0 @@ -150,7 +163,7 @@ export fn main() void = { }; - drawing::clear_picture(&win_pic, 0xff0000); + drawing::clear_picture(&win_pic, 0xdddddd); for (let i = 0z; i < len(pictures); i += 1) { const psurf = picture_surfaces[i]; diff --git a/packet_reader/packet_reader.ha b/packet_reader/packet_reader.ha index df4d417..e4cbcf2 100644 --- a/packet_reader/packet_reader.ha +++ b/packet_reader/packet_reader.ha @@ -4,6 +4,8 @@ use endian; use drawing; use drawing::{pos,CHUNKSIZE}; +export def VERSION: u8 = 1; + export type error = !str; export type packet_reader = struct { diff --git a/server/main.ha b/server/main.ha index 71bb71b..cf8e720 100644 --- a/server/main.ha +++ b/server/main.ha @@ -17,6 +17,7 @@ use unix::poll; use drawing; use drawing::{pos,CHUNKSIZE}; use packet_reader; +use packet_reader::{VERSION}; def PORT: u16 = 41460; @@ -130,7 +131,6 @@ fn loop(state: *server_state, timeout: time::duration) void = { for (let pollfd_idx = 1z: size; pollfd_idx < len(pollfds); pollfd_idx += 1) { const conn_idx = pollfd_idx - 1; if (state.connections[conn_idx].should_delete) continue; - fmt::printfln("checking conn {} with pollfd {}",conn_idx, pollfd_idx)!; if (0 != pollfds[conn_idx+1].revents & poll::event::POLLIN) { read_from_connection(state, conn_idx); }; @@ -153,7 +153,10 @@ fn perform_deletions(state: *server_state) void = { }; fn greet_connection(state: *server_state, conn_idx: size) -(void|io::error) = void; +(void|io::error) = { + const conn = state.connections[conn_idx]; + io::write(conn.sock, [VERSION])?; +}; @@ -195,8 +198,8 @@ fn handle_packet( match (packet) { case let op: packet_reader::packet_drawop => const opc = op as drawing::op_circle; - fmt::printfln("#{}: drawop ({:+6},{:+6}) r{}", - conn_idx,opc.pos.0,opc.pos.1, opc.radius)!; + // fmt::printfln("#{}: drawop ({:+6},{:+6}) r{}", + // conn_idx,opc.pos.0,opc.pos.1, opc.radius)!; drawing::perform(state.pictures, opc); for (let other_idx = 0z; other_idx < len(state.connections); @@ -205,7 +208,7 @@ fn handle_packet( if (other_idx == conn_idx) continue; const other_conn = &state.connections[other_idx]; if (other_conn.should_delete) continue; - fmt::printfln("\t -> #{}",other_idx)!; + // fmt::printfln("\t -> #{}",other_idx)!; match (packet_reader::send(other_conn.sock, packet)) { case void => yield; case let e: io::error => @@ -215,8 +218,8 @@ fn handle_packet( }; }; case let pos: packet_reader::packet_position => - fmt::printfln("pos of #{} is now {},{}", - conn_idx, pos.0, pos.1)!; + // fmt::printfln("pos of #{} is now {},{}", + // conn_idx, pos.0, pos.1)!; state.connections[conn_idx].pos = pos; case let world_pos: packet_reader::packet_reqchunk => // xxx it is probably easy to ddos the server by sending lots of these @@ -268,12 +271,15 @@ fn unload_distant_chunks(state: *server_state) void = { const chunk_in_y = floor_div(conn.pos.1, CHUNKSIZE)*CHUNKSIZE; // pic_chunk_pos - player_chunk_pos must be either 0 or 512 or 1024 // can't be less than 0 or more than 1024 + // + // -CHUNKSIZE because i suspect this still unloads chunks when + // it should't, somehow for (let i = 0z; i < n_pics; i += 1) { const pic = &state.pictures[i]; const dcx = pic.world_pos.0 - chunk_in_x; const dcy = pic.world_pos.1 - chunk_in_y; - if (0 <= dcx && dcx <= 2*CHUNKSIZE && 0 <= dcy && dcy <= 2*CHUNKSIZE) + if (-CHUNKSIZE <= dcx && dcx <= 2*CHUNKSIZE && -CHUNKSIZE <= dcy && dcy <= 2*CHUNKSIZE) should_unload[i] = false; }; }; -- cgit v1.2.3