diff --git a/GServer.Common/Networking/Core/MessageMemoryStream.cs b/GServer.Common/Networking/Core/MessageMemoryStream.cs index 15204b4..28cdda6 100644 --- a/GServer.Common/Networking/Core/MessageMemoryStream.cs +++ b/GServer.Common/Networking/Core/MessageMemoryStream.cs @@ -43,7 +43,13 @@ public class MessageMemoryStream : MemoryStream WriteByte((byte)(value ? 1 : 0)); } - public void WriteUInt16(short value) + public void WriteInt16(short value) + { + byte[] bytes = BitConverter.GetBytes(value); + Write(bytes, 0, 2); + } + + public void WriteUInt16(ushort value) { byte[] bytes = BitConverter.GetBytes(value); Write(bytes, 0, 2); @@ -54,4 +60,29 @@ public class MessageMemoryStream : MemoryStream byte[] bytes = Encoding.UTF8.GetBytes(value); Write(bytes, 0, bytes.Length); } + + public void WriteUtf8StringOfSize(uint byteLength, string value) + { + byte[] bytes = Encoding.UTF8.GetBytes(value); + Write(bytes, 0, (int)byteLength); + } + + public void WriteInt64(long value) + { + byte[] bytes = BitConverter.GetBytes(value); + Write(bytes, 0, 8); + } + + public void WriteBytes(byte[] data) + { + long offset = 0; + int chunkSize = Int32.MaxValue; // Maximum size for one write operation + + while (offset < data.Length) + { + int remaining = (int)Math.Min(chunkSize, data.Length - offset); + Write(data, (int)offset, remaining); + offset += remaining; + } + } } diff --git a/GServer.Common/Networking/Messages/Server/ListServersMessage.cs b/GServer.Common/Networking/Messages/Server/ListServersMessage.cs index cec5bc5..7c17c02 100644 --- a/GServer.Common/Networking/Messages/Server/ListServersMessage.cs +++ b/GServer.Common/Networking/Messages/Server/ListServersMessage.cs @@ -1,15 +1,42 @@ -using GServer.Common.Networking.Enums; +using GServer.Common.Game.Entities; +using GServer.Common.Networking.Core; +using GServer.Common.Networking.Enums; namespace GServer.Common.Networking.Messages.Server; public class ListServersMessage : BaseMessage, IMessage { - public ListServersMessage() : base((byte)ServerPacketIn.ListServers) + public ServerListing[] ServerListings; + + public const int SERVER_NAME_BYTES = 50; + + public ListServersMessage(ServerListing[] serverListings) : base((byte)ServerPacketIn.ListServers) { + ServerListings = serverListings; } public byte[] Serialize() { - return []; + using MessageMemoryStream stream = new(); + + stream.WriteByte(PacketId); + + foreach (ServerListing listing in ServerListings) + { + using MessageMemoryStream listingStream = new(); + + listingStream.WriteUtf8StringOfSize(50, listing.Name); + listingStream.WriteUtf8StringOfSize(1000, listing.Description); + listingStream.WriteUInt16(listing.Playercount); + listingStream.WriteUtf8StringOfSize(15, listing.IpAddress); + listingStream.WriteUInt16(listing.Port); + listingStream.WriteByte((byte)listing.ServerTier); + + long listingBlockSize = listingStream.Length; + stream.WriteInt64(listingBlockSize); + stream.WriteBytes(listingStream.ToArray()); + } + + return stream.ToArray(); } } diff --git a/docs/architecture.drawio b/docs/architecture.drawio new file mode 100644 index 0000000..44a1b21 --- /dev/null +++ b/docs/architecture.drawio @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file