Skip to content

Commit

Permalink
improving network resiliency
Browse files Browse the repository at this point in the history
  • Loading branch information
Mag-nus committed Dec 25, 2023
1 parent c0a1793 commit b7216d0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 4 additions & 2 deletions Source/ACE.Server/Network/ClientMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ namespace ACE.Server.Network
{
public class ClientMessage
{
public BinaryReader Payload { get; }

public MemoryStream Data { get; }

public BinaryReader Payload { get; }

public uint Opcode { get; }

/// <exception cref="EndOfStreamException">stream must be at least 4 bytes in length remaining to read</exception>
public ClientMessage(MemoryStream stream)
{
Data = stream;
Payload = new BinaryReader(Data);
Opcode = Payload.ReadUInt32();
}

/// <exception cref="EndOfStreamException">data must be at least 4 bytes in length</exception>
public ClientMessage(byte[] data)
{
Data = new MemoryStream(data);
Expand Down
11 changes: 10 additions & 1 deletion Source/ACE.Server/Network/MessageBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,25 @@ public void AddFragment(ClientPacketFragment fragment)
}
}

public ClientMessage GetMessage()
/// <summary>
/// This will return null if not enough data exists to create a valid ClientMessage (4 bytes minimum are required)
/// </summary>
public ClientMessage TryGetMessage()
{
fragments.Sort(delegate (ClientPacketFragment x, ClientPacketFragment y) { return x.Header.Index - y.Header.Index; });

MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
foreach (ClientPacketFragment fragment in fragments)
{
writer.Write(fragment.Data);
}

stream.Seek(0, SeekOrigin.Begin);

if (stream.Length < 4) // ClientMessage must be a minimum of 4 bytes in length
return null;

return new ClientMessage(stream);
}
}
Expand Down
6 changes: 4 additions & 2 deletions Source/ACE.Server/Network/NetworkSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ private void ProcessFragment(ClientPacketFragment fragment)
{
// The buffer is complete, so we can go ahead and handle
packetLog.DebugFormat("[{0}] Buffer {1} is complete", session.LoggingIdentifier, buffer.Sequence);
message = buffer.GetMessage();
message = buffer.TryGetMessage();
MessageBuffer removed = null;
partialFragments.TryRemove(fragment.Header.Sequence, out removed);
}
Expand All @@ -495,7 +495,9 @@ private void ProcessFragment(ClientPacketFragment fragment)
{
// Packet is not split, proceed with handling it.
packetLog.DebugFormat("[{0}] Fragment {1} is not split", session.LoggingIdentifier, fragment.Header.Sequence);
message = new ClientMessage(fragment.Data);

if (fragment.Data.Length >= 4) // ClientMessage must be a minimum of 4 bytes in length
message = new ClientMessage(fragment.Data);
}

// If message is not null, we have a complete message to handle
Expand Down

0 comments on commit b7216d0

Please sign in to comment.