Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #136 from 0blu/delay_when_not_able_to_send
Browse files Browse the repository at this point in the history
Queue packets when "not connected" to instance
  • Loading branch information
0blu authored Nov 29, 2022
2 parents b27b7bb + 604adaf commit 4e03ecb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions HermesProxy/GlobalSessionData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class GameSessionData
public bool IsWaitingForNewWorld;
public bool IsFirstEnterWorld;
public bool IsConnectedToInstance;
public Queue<ServerPacket> PendingUninstancedPackets = new(); // Here packets are queued while IsConnectedToInstance = false;
public bool IsInWorld;
public uint? CurrentMapId;
public uint CurrentZoneId;
Expand Down
30 changes: 26 additions & 4 deletions HermesProxy/World/Client/WorldClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ public void SendPacketToClient(ServerPacket packet, Opcode delayUntilOpcode = Op

private void SendPacketToClientDirect(ServerPacket packet)
{
var pendingPackets = GetSession().GameState.PendingUninstancedPackets;
if (packet.GetConnection() == ConnectionType.Realm)
{
GetSession().RealmSocket.SendPacket(packet);
Expand All @@ -265,17 +266,38 @@ private void SendPacketToClientDirect(ServerPacket packet)
if (GetSession().InstanceSocket == null &&
!GetSession().GameState.IsConnectedToInstance)
{
Log.PrintNet(LogType.Error, LogNetDir.P2C, $"Can't send opcode {packet.GetUniversalOpcode()} ({packet.GetOpcode()}) before entering world!");
return;
lock (pendingPackets)
{
if (GetSession().InstanceSocket == null &&
!GetSession().GameState.IsConnectedToInstance)
{
pendingPackets.Enqueue(packet);
Log.PrintNet(LogType.Warn, LogNetDir.P2C, $"Can't send opcode {packet.GetUniversalOpcode()} ({packet.GetOpcode()}) before entering world! Queue");
return;
}
}
}

// block these packets until connected to instance
while (GetSession().InstanceSocket == null)
{
Log.PrintNet(LogType.Network, LogNetDir.P2C, $"Waiting to send {packet.GetUniversalOpcode()} ({packet.GetOpcode()}).");
System.Threading.Thread.Sleep(200);
};
GetSession().InstanceSocket.SendPacket(packet);
}

var socket = GetSession().InstanceSocket;
if (pendingPackets.Count > 0)
{
lock (pendingPackets)
{
while (pendingPackets.TryDequeue(out var oldPacket))
{
socket.SendPacket(oldPacket);
}
}
}

socket.SendPacket(packet);
}
}

Expand Down

0 comments on commit 4e03ecb

Please sign in to comment.