From 861dc7ab01af912a2849fe1b408eec38b86c96b0 Mon Sep 17 00:00:00 2001 From: Aaron Yarborough Date: Sun, 19 May 2024 22:33:10 +0100 Subject: [PATCH] feat: add ListServerResponseMessage, refactor --- GServer.Client/Program.cs | 12 +-------- .../Networking/Enums/ClientPacketIn.cs | 25 +++++++++++-------- .../Networking/Enums/ServerPacketIn.cs | 10 ++++---- .../Messages/Client/AuthResponseMessage.cs | 3 +-- .../Client/ListServersResponseMessage.cs | 24 ++++++++++++++++++ .../Networking/Messages/Server/AuthMessage.cs | 10 -------- .../Messages/Server/ListServersMessage.cs | 15 +++++++++++ GServer.Server/Program.cs | 4 --- 8 files changed, 60 insertions(+), 43 deletions(-) create mode 100644 GServer.Common/Networking/Messages/Client/ListServersResponseMessage.cs create mode 100644 GServer.Common/Networking/Messages/Server/ListServersMessage.cs diff --git a/GServer.Client/Program.cs b/GServer.Client/Program.cs index 29b9f2c..869f1ab 100644 --- a/GServer.Client/Program.cs +++ b/GServer.Client/Program.cs @@ -10,20 +10,12 @@ internal class Program private static void Main(string[] args) { - // var sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - - var serverEP = new IPEndPoint(IPAddress.Any, SERVER_PORT); + IPEndPoint serverEP = new(IPAddress.Any, SERVER_PORT); UdpClient udpClient = new(); udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); udpClient.Connect(serverEP); - - // byte[] username = "helloworld123".ToUTF8String(); - // byte[] password = "password&(*$())".ToUTF8String(); - - // byte[] sendBuffer = [(byte)ServerPacketIn.AUTH, (byte)username.Length, .. username, (byte)password.Length, .. password]; - Console.WriteLine("Username..."); string username = Console.ReadLine()!; @@ -58,8 +50,6 @@ internal class Program default: Console.WriteLine($"Received unsupported packet."); - // byte[] response = [(byte)ClientPacketIn.UNKNOWN]; - // listener.Send(response); break; } } diff --git a/GServer.Common/Networking/Enums/ClientPacketIn.cs b/GServer.Common/Networking/Enums/ClientPacketIn.cs index c665ccf..dc27e8c 100644 --- a/GServer.Common/Networking/Enums/ClientPacketIn.cs +++ b/GServer.Common/Networking/Enums/ClientPacketIn.cs @@ -1,13 +1,16 @@ -namespace GServer.Common.Networking.Enums -{ - public enum ClientPacketIn : byte - { - /// - /// Represents an auth result from the server. - /// Format: {1(success) | 2(error)}{error msg length}{error msg} - /// - AUTH_RESPONSE = 1, +namespace GServer.Common.Networking.Enums; - UNKNOWN = 255 - } +public enum ClientPacketIn : byte +{ + /// + /// Represents an auth result from the server. + /// + AUTH_RESPONSE = 1, + + /// + /// Contains a list of server listings. + /// + LIST_SERVERS_RESPONSE = 2, + + UNKNOWN = 255 } \ No newline at end of file diff --git a/GServer.Common/Networking/Enums/ServerPacketIn.cs b/GServer.Common/Networking/Enums/ServerPacketIn.cs index 29fa6ab..2e710a5 100644 --- a/GServer.Common/Networking/Enums/ServerPacketIn.cs +++ b/GServer.Common/Networking/Enums/ServerPacketIn.cs @@ -1,7 +1,7 @@ -namespace GServer.Common.Networking.Enums +namespace GServer.Common.Networking.Enums; + +public enum ServerPacketIn : byte { - public enum ServerPacketIn : byte - { - AUTH = 1 - } + AUTH = 1, + LIST_SERVERS = 2 } \ No newline at end of file diff --git a/GServer.Common/Networking/Messages/Client/AuthResponseMessage.cs b/GServer.Common/Networking/Messages/Client/AuthResponseMessage.cs index e39f44a..ad69d4d 100644 --- a/GServer.Common/Networking/Messages/Client/AuthResponseMessage.cs +++ b/GServer.Common/Networking/Messages/Client/AuthResponseMessage.cs @@ -1,8 +1,7 @@ using System.Text; using GServer.Common.Networking.Enums; -using GServer.Common.Networking.Messages; -namespace GServer.Common; +namespace GServer.Common.Networking.Messages.Client; public enum AuthResponseFailure : byte { diff --git a/GServer.Common/Networking/Messages/Client/ListServersResponseMessage.cs b/GServer.Common/Networking/Messages/Client/ListServersResponseMessage.cs new file mode 100644 index 0000000..f658a9f --- /dev/null +++ b/GServer.Common/Networking/Messages/Client/ListServersResponseMessage.cs @@ -0,0 +1,24 @@ +using GServer.Common.Game.Entities; +using GServer.Common.Networking.Enums; + +namespace GServer.Common.Networking.Messages.Client; + +public class ListServersResponseMessage : BaseMessage, IMessage +{ + public IEnumerable ServerListings { get; set; } + + public ListServersResponseMessage(IEnumerable serverListings) : base((byte)ClientPacketIn.LIST_SERVERS_RESPONSE) + { + ServerListings = serverListings; + } + + public ListServersResponseMessage(MessageMemoryStream stream) : base((byte)ClientPacketIn.LIST_SERVERS_RESPONSE) + { + throw new NotImplementedException(); + } + + public byte[] Serialize() + { + throw new NotImplementedException(); + } +} diff --git a/GServer.Common/Networking/Messages/Server/AuthMessage.cs b/GServer.Common/Networking/Messages/Server/AuthMessage.cs index 76f9fa7..0ad0750 100644 --- a/GServer.Common/Networking/Messages/Server/AuthMessage.cs +++ b/GServer.Common/Networking/Messages/Server/AuthMessage.cs @@ -16,16 +16,6 @@ public class AuthMessage : BaseMessage, IMessage public AuthMessage(MessageMemoryStream stream) : base((byte)ServerPacketIn.AUTH) { - // byte usernameLength = (byte)stream.ReadByte(); - // byte[] usernameBytes = new byte[usernameLength]; - // _ = stream.Read(usernameBytes, 0, usernameLength); - // string username = Encoding.UTF8.GetString(usernameBytes); - - // byte passwordLength = (byte)stream.ReadByte(); - // byte[] passwordBytes = new byte[passwordLength]; - // _ = stream.Read(passwordBytes, 0, passwordLength); - // string password = Encoding.UTF8.GetString(passwordBytes); - byte usernameLen = (byte)stream.ReadByte(); string username = stream.ReadUTF8String(usernameLen); diff --git a/GServer.Common/Networking/Messages/Server/ListServersMessage.cs b/GServer.Common/Networking/Messages/Server/ListServersMessage.cs new file mode 100644 index 0000000..e9f394d --- /dev/null +++ b/GServer.Common/Networking/Messages/Server/ListServersMessage.cs @@ -0,0 +1,15 @@ +using GServer.Common.Networking.Enums; + +namespace GServer.Common.Networking.Messages.Server; + +public class ListServersMessage : BaseMessage, IMessage +{ + public ListServersMessage() : base((byte)ServerPacketIn.LIST_SERVERS) + { + } + + public byte[] Serialize() + { + return []; + } +} diff --git a/GServer.Server/Program.cs b/GServer.Server/Program.cs index b1d440d..8ba3812 100644 --- a/GServer.Server/Program.cs +++ b/GServer.Server/Program.cs @@ -41,10 +41,6 @@ internal class Program case ServerPacketIn.AUTH: var msg = new AuthMessage(stream); - Console.WriteLine($"Username = {msg.Username}, Length = {msg.Username.Length}"); - - Console.WriteLine($"Password = {msg.Password}, Length = {msg.Password.Length}"); - AuthResponseMessage resp; if (msg.Username == "aaronyarbz" && msg.Password == "password123")