From d577a3f874a3fc2cb71708f400482ca817abc33e Mon Sep 17 00:00:00 2001 From: troido Date: Thu, 23 Apr 2020 17:01:39 +0200 Subject: added option to join the server without joining the game (just for the chat) --- client.py | 19 +++++++++++++------ clientinet.py | 57 ------------------------------------------------------- src/gameserver.rs | 17 ++++++++++++----- 3 files changed, 25 insertions(+), 68 deletions(-) delete mode 100755 clientinet.py diff --git a/client.py b/client.py index 2e4a4c4..bf908d6 100755 --- a/client.py +++ b/client.py @@ -6,6 +6,8 @@ import threading import json import getpass +# A simple asciifarm client that can only join the chat. + def send(sock, msg): length = len(msg) @@ -14,7 +16,7 @@ def send(sock, msg): sock.sendall(totalmsg) def receive(sock): - header = recvall(sock, 4) #sock.recv(4) + header = recvall(sock, 4) length = int.from_bytes(header, byteorder="big") return recvall(sock, length) @@ -30,10 +32,15 @@ def recvall(sock, length): bytes_recd = bytes_recd + len(chunk) return b''.join(chunks) +inet = "inet" in sys.argv +join = "join" in sys.argv - -sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) -sock.connect("\0rustifarm")#("localhost", 1234)) +if inet: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect(("localhost", 9021)) +else: + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + sock.connect("\0rustifarm") def listen(): while True: @@ -48,10 +55,10 @@ threading.Thread(target=listen, daemon=True).start() if len(sys.argv) >= 2: name = sys.argv[1] else: - name = "~" + getpass.getuser() + name = getpass.getuser() print(name) -send(sock, bytes(json.dumps(["name", name]), "utf-8")) +send(sock, bytes(json.dumps(["auth", {"name": name, "join": join, "type": "guest"}]), "utf-8")) for line in sys.stdin: send(sock, bytes(json.dumps(["chat", line.strip()]), "utf-8")) diff --git a/clientinet.py b/clientinet.py deleted file mode 100755 index f4f4887..0000000 --- a/clientinet.py +++ /dev/null @@ -1,57 +0,0 @@ -#!/usr/bin/env python3 - -import socket -import sys -import threading -import json -import getpass - - -def send(sock, msg): - length = len(msg) - header = length.to_bytes(4, byteorder="big") - totalmsg = header + msg - sock.sendall(totalmsg) - -def receive(sock): - header = recvall(sock, 4) #sock.recv(4) - length = int.from_bytes(header, byteorder="big") - return recvall(sock, length) - -def recvall(sock, length): - chunks = [] - bytes_recd = 0 - while bytes_recd < length: - chunk = sock.recv(min(length - bytes_recd, 4096)) - if chunk == b'': - break - #raise RuntimeError("socket connection broken") - chunks.append(chunk) - bytes_recd = bytes_recd + len(chunk) - return b''.join(chunks) - - - -sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -sock.connect(("localhost", 1234)) - -def listen(): - while True: - d = receive(sock) - if len(d) == 0: - print("Connection closed by server", file=sys.stdout) - return - print(str(d, "utf-8")) - -threading.Thread(target=listen, daemon=True).start() - -if len(sys.argv) >= 2: - name = sys.argv[1] -else: - name = getpass.getuser() -print(name) - -send(sock, bytes(json.dumps(["name", name]), "utf-8")) - -for line in sys.stdin: - send(sock, bytes(json.dumps(["chat", line.strip()]), "utf-8")) diff --git a/src/gameserver.rs b/src/gameserver.rs index 709ee9d..37b889f 100644 --- a/src/gameserver.rs +++ b/src/gameserver.rs @@ -22,7 +22,7 @@ enum Authentication { #[derive(Debug)] enum Message { - Auth(String, Authentication), + Auth(String, Authentication, bool), Chat(String), Input(Value) } @@ -131,7 +131,7 @@ impl GameServer { fn handle_message(&mut self, (serverid, connectionid): (usize, usize), msg: Message) -> Result, MessageError> { let id = (serverid, connectionid); match msg { - Message::Auth(name, auth) => { + Message::Auth(name, auth, join) => { if name.len() > 99 { return Err(merr!(name, "A name can not be longer than 99 bytes")); } @@ -166,7 +166,11 @@ impl GameServer { "server" ])); } - Ok(Some(Action::Join(player))) + Ok(if join { + Some(Action::Join(player)) + } else { + None + }) } Message::Chat(text) => { let player = self.players.get(&id).ok_or(merr!(action, "Set a valid name before you send any other messages"))?; @@ -243,7 +247,8 @@ fn parse_message(msg: &str) -> Result { Authentication::Tilde } else { Authentication::Guest - } + }, + true ) } "chat" => { @@ -256,6 +261,7 @@ fn parse_message(msg: &str) -> Result { "auth" => { let name = arg.get("name").ok_or(merr!(msg, "auth message does not have name"))?.as_str().ok_or(merr!(msg, "auth name not a string"))?.to_string(); let typ = arg.get("type").ok_or(merr!(msg, "auth message does not have type"))?.as_str().ok_or(merr!(msg, "auth type not a string"))?; + let join = arg.get("join").unwrap_or(&json!(true)).as_bool().ok_or(merr!(msg, "join flag not a bool"))?; Message::Auth( name, match typ { @@ -270,7 +276,8 @@ fn parse_message(msg: &str) -> Result { .to_string() ), _ => {return Err(merr!(msg, "invalid authentication type"))} - } + }, + join ) } _ => { -- cgit