feat: add ListServerResponseMessage, refactor

This commit is contained in:
Aaron Yarborough 2024-05-19 22:33:10 +01:00
parent 1ef186c46c
commit 861dc7ab01
8 changed files with 60 additions and 43 deletions

View file

@ -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;
}
}

View file

@ -1,13 +1,16 @@
namespace GServer.Common.Networking.Enums
{
public enum ClientPacketIn : byte
{
/// <summary>
/// Represents an auth result from the server.
/// Format: {1(success) | 2(error)}{error msg length}{error msg}
/// </summary>
AUTH_RESPONSE = 1,
namespace GServer.Common.Networking.Enums;
UNKNOWN = 255
}
public enum ClientPacketIn : byte
{
/// <summary>
/// Represents an auth result from the server.
/// </summary>
AUTH_RESPONSE = 1,
/// <summary>
/// Contains a list of server listings.
/// </summary>
LIST_SERVERS_RESPONSE = 2,
UNKNOWN = 255
}

View file

@ -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
}

View file

@ -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
{

View file

@ -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<ListServersResponseMessage>
{
public IEnumerable<ServerListing> ServerListings { get; set; }
public ListServersResponseMessage(IEnumerable<ServerListing> 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();
}
}

View file

@ -16,16 +16,6 @@ public class AuthMessage : BaseMessage, IMessage<AuthMessage>
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);

View file

@ -0,0 +1,15 @@
using GServer.Common.Networking.Enums;
namespace GServer.Common.Networking.Messages.Server;
public class ListServersMessage : BaseMessage, IMessage<ListServersMessage>
{
public ListServersMessage() : base((byte)ServerPacketIn.LIST_SERVERS)
{
}
public byte[] Serialize()
{
return [];
}
}

View file

@ -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")