diff options
Diffstat (limited to 'client/src')
-rw-r--r-- | client/src/client/mod.rs | 15 | ||||
-rw-r--r-- | client/src/client/run.rs | 20 | ||||
-rw-r--r-- | client/src/main.rs | 6 | ||||
-rw-r--r-- | client/src/server_connection/mod.rs | 50 |
4 files changed, 90 insertions, 1 deletions
diff --git a/client/src/client/mod.rs b/client/src/client/mod.rs new file mode 100644 index 0000000..5694b6a --- /dev/null +++ b/client/src/client/mod.rs @@ -0,0 +1,15 @@ +// Copyright 2022-2024 Gabriel Bjørnager Jensen. + +mod run; + +pub struct Client; + +impl Client { + #[must_use] + pub fn new() -> Self { Self } +} + +impl Default for Client { + #[inline(always)] + fn default() -> Self { Self::new() } +} diff --git a/client/src/client/run.rs b/client/src/client/run.rs new file mode 100644 index 0000000..d955cea --- /dev/null +++ b/client/src/client/run.rs @@ -0,0 +1,20 @@ +// Copyright 2022-2024 Gabriel Bjørnager Jensen. + +use crate::{Client, ServerConnection}; + +use bowshock::net::{DEFAULT_SERVER_PORT, Request}; +use bzipper::FixedString; + +impl Client { + pub fn run(self) -> Result<(), i32> { + let server_addr = format!("127.0.0.1:{DEFAULT_SERVER_PORT}"); + eprintln!("connecting to server at {server_addr}"); + + let mut connection = ServerConnection::new(server_addr); + + let join_request = Request::PlayerJoin { username: FixedString::try_from("delta").unwrap() }; + connection.send_request(&join_request); + + Ok(()) + } +} diff --git a/client/src/main.rs b/client/src/main.rs index 2235f75..93ce825 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -1,3 +1,7 @@ // Copyright 2022-2024 Gabriel Bjørnager Jensen. -fn main() { } +use bowshock::use_mod; +use_mod!(pub client); +use_mod!(pub server_connection); + +fn main() -> Result<(), i32> { Client::new().run() } diff --git a/client/src/server_connection/mod.rs b/client/src/server_connection/mod.rs new file mode 100644 index 0000000..c69f3da --- /dev/null +++ b/client/src/server_connection/mod.rs @@ -0,0 +1,50 @@ +// Copyright 2022-2024 Gabriel Bjørnager Jensen. + +use bowshock::net::{Request, Response}; +use bzipper::Buffer; +use std::net::{SocketAddr, ToSocketAddrs, UdpSocket}; + +pub struct ServerConnection { + socket: UdpSocket, + + request_buf: Buffer<Request>, + response_buf: Buffer<Response>, + + #[expect(unused)] + addr: SocketAddr, +} + +impl ServerConnection { + #[must_use] + pub fn new<A: ToSocketAddrs>(addr: A) -> Self { + let addr = addr + .to_socket_addrs() + .unwrap() + .next() + .unwrap(); + + let socket = UdpSocket::bind("0.0.0.0:0").unwrap(); + socket.connect(addr).unwrap(); + + Self { + socket, + + request_buf: Buffer::new(), + response_buf: Buffer::new(), + + addr, + } + } + + pub fn send_request(&mut self, request: &Request) { + self.request_buf.write(request).unwrap(); + self.socket.send(&self.request_buf).unwrap(); + } + + pub fn recieve_response(&mut self) -> Response { + let len = self.socket.recv(&mut self.response_buf).unwrap(); + assert_eq!(len, self.response_buf.len()); + + self.response_buf.read().unwrap() + } +} |