Skip to content

Commit d97007b

Browse files
authored
Merge 324fca2 into 8e55f0e
2 parents 8e55f0e + 324fca2 commit d97007b

7 files changed

Lines changed: 32 additions & 19 deletions

File tree

src/game/Server/WorldGateway.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ bool WorldGateway::FilterAuthPacket(WorldPacket& packet)
8585
return true;
8686
}
8787

88-
void WorldGateway::TracePacket(WorldPacket const& packet, bool incoming)
88+
void WorldGateway::TracePacket(proto::SessionId session, WorldPacket const& packet, bool incoming)
8989
{
9090
if (sLog.IsPacketLoggingEnabled())
9191
{
92-
sLog.outWorldPacketDump(0, packet.GetOpcode(),
92+
sLog.outWorldPacketDump(session, packet.GetOpcode(),
9393
LookupOpcodeName(packet.GetOpcode()), &packet, incoming);
9494
}
9595
}

src/game/Server/WorldGateway.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class WorldGateway final : public proto::IWorldGateway
3838
{
3939
public:
4040
bool FilterAuthPacket(WorldPacket& packet) override;
41-
void TracePacket(WorldPacket const& packet, bool incoming) override;
41+
void TracePacket(proto::SessionId session, WorldPacket const& packet, bool incoming) override;
4242
proto::AuthLookup LookupAccount(proto::AuthRequest const& request) override;
4343
proto::SessionId Attach(proto::AuthRequest const& request,
4444
std::shared_ptr<proto::IClientLink> const& link,

src/proto/ClientConnection.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ std::vector<uint8_t> ClientConnection::onConnect()
6262
{
6363
WorldPacket challenge(SMSG_AUTH_CHALLENGE, 4);
6464
challenge << m_seed;
65-
m_gateway.TracePacket(challenge, false);
65+
m_gateway.TracePacket(INVALID_SESSION_ID, challenge, false);
6666
return EncodePacket(challenge);
6767
}
6868
catch (...)
@@ -99,7 +99,8 @@ std::vector<uint8_t> ClientConnection::onData(
9999

100100
for (size_t i = 0; i < packets.size() && !m_closed.load(); ++i)
101101
{
102-
m_gateway.TracePacket(packets[i], true);
102+
m_gateway.TracePacket(m_traceSession.load(std::memory_order_relaxed),
103+
packets[i], true);
103104
if (!HandlePacket(packets[i]))
104105
{
105106
Close();
@@ -126,6 +127,7 @@ void ClientConnection::onClose()
126127
session = m_session;
127128
m_session = INVALID_SESSION_ID;
128129
}
130+
m_traceSession.store(INVALID_SESSION_ID, std::memory_order_relaxed);
129131
if (session != INVALID_SESSION_ID)
130132
{
131133
try
@@ -153,7 +155,7 @@ void ClientConnection::SendPacket(WorldPacket const& packet)
153155
return;
154156
}
155157

156-
m_gateway.TracePacket(packet, false);
158+
m_gateway.TracePacket(m_traceSession.load(std::memory_order_relaxed), packet, false);
157159
std::vector<uint8> const frame = PacketCodec::Encode(packet,
158160
[this](uint8* header, std::size_t len)
159161
{
@@ -290,6 +292,7 @@ bool ClientConnection::HandleAuthSession(WorldPacket& packet)
290292
m_gateway.Detach(session);
291293
return false;
292294
}
295+
m_traceSession.store(session, std::memory_order_relaxed);
293296

294297
return true;
295298
}

src/proto/ClientConnection.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ class ClientConnection final : public net::ISession, public IClientLink
7979
std::mutex m_sessionLock;
8080
uint32 m_seed;
8181
SessionId m_session = INVALID_SESSION_ID;
82+
83+
/// The same id, readable without m_sessionLock: SendPacket traces while holding
84+
/// m_sendOrderLock, and taking m_sessionLock under it would invert the order
85+
/// HandleAuthSession uses. Tracing only -- m_session stays the authority.
86+
std::atomic<SessionId> m_traceSession{INVALID_SESSION_ID};
8287
bool m_authStarted = false;
8388
std::atomic<bool> m_closed{false};
8489
net::Sender m_sender;

src/proto/IWorldGateway.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ class IWorldGateway
9595
public:
9696
virtual ~IWorldGateway() = default;
9797
virtual bool FilterAuthPacket(WorldPacket& packet) = 0;
98-
virtual void TracePacket(WorldPacket const& packet, bool incoming) = 0;
98+
99+
/// `session` is INVALID_SESSION_ID for the pre-auth handshake only. Without it a dump
100+
/// cannot say which of several connected clients a packet belongs to, which is most of
101+
/// what a packet dump is for.
102+
virtual void TracePacket(SessionId session, WorldPacket const& packet, bool incoming) = 0;
99103
virtual AuthLookup LookupAccount(AuthRequest const& request) = 0;
100104
virtual SessionId Attach(AuthRequest const& request,
101105
std::shared_ptr<IClientLink> const& link,

src/shared/Log/Log.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ void Log::outErrorScriptLib(const char* err, ...)
12251225
}
12261226
}
12271227

1228-
void Log::outWorldPacketDump(uint32 socket, uint32 opcode, char const* opcodeName, ByteBuffer const* packet, bool incoming)
1228+
void Log::outWorldPacketDump(uint32 session, uint32 opcode, char const* opcodeName, ByteBuffer const* packet, bool incoming)
12291229
{
12301230
if (!worldLogfile)
12311231
{
@@ -1237,18 +1237,19 @@ void Log::outWorldPacketDump(uint32 socket, uint32 opcode, char const* opcodeNam
12371237
outTimestamp(worldLogfile);
12381238

12391239
// Build the whole hex dump into one buffer and emit it with a single
1240-
// fwrite, instead of one fprintf PER BYTE (16+ stdio calls per row). Output
1241-
// is byte-identical to the previous format. Durability via Flush()/shutdown.
1240+
// fwrite, instead of one fprintf PER BYTE (16+ stdio calls per row). The hex
1241+
// body is byte-identical to that format. Durability via Flush()/shutdown.
12421242
std::string out;
12431243
out.reserve(packet->size() * 3 + packet->size() / 16 + 128);
12441244

1245-
// header[512] is ample for the fixed text plus a (short, compile-time)
1246-
// opcode-name constant; snprintf is bounded, so output stays byte-identical
1247-
// to the previous fprintf for every real opcode name.
1245+
// SESSION, not SOCKET: the field carried a hardcoded 0 for every packet of every
1246+
// client, so two clients logged as one interleaved stream that could not be taken
1247+
// apart. header[512] is ample for the fixed text plus a (short, compile-time)
1248+
// opcode-name constant, and snprintf is bounded.
12481249
char header[512];
1249-
snprintf(header, sizeof(header), "\n%s:\nSOCKET: %u\nLENGTH: %zu\nOPCODE: %s (0x%.4X)\nDATA:\n",
1250+
snprintf(header, sizeof(header), "\n%s:\nSESSION: %u\nLENGTH: %zu\nOPCODE: %s (0x%.4X)\nDATA:\n",
12501251
incoming ? "CLIENT" : "SERVER",
1251-
socket, packet->size(), opcodeName, opcode);
1252+
session, packet->size(), opcodeName, opcode);
12521253
out += header;
12531254

12541255
char hexbuf[4];

src/shared/Log/Log.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,16 @@ class Log : public MaNGOS::Singleton<Log>
311311
/**
312312
* @brief any log level
313313
*
314-
* Called from WorldGateway::Deliver (incoming) and WorldSession::SendPacket
315-
* (outgoing) -- see IsPacketLoggingEnabled()'s comment below.
314+
* Called from WorldGateway::TracePacket, i.e. from ClientConnection on the
315+
* network thread in both directions -- see IsPacketLoggingEnabled() below.
316316
*
317-
* @param socket
317+
* @param session which client stream the packet belongs to; 0 pre-auth
318318
* @param opcode
319319
* @param opcodeName
320320
* @param packet
321321
* @param incoming
322322
*/
323-
void outWorldPacketDump(uint32 socket, uint32 opcode, char const* opcodeName, ByteBuffer const* packet, bool incoming);
323+
void outWorldPacketDump(uint32 session, uint32 opcode, char const* opcodeName, ByteBuffer const* packet, bool incoming);
324324
/**
325325
* @brief any log level
326326
*

0 commit comments

Comments
 (0)