From 276be748ce942f66c4ae9433132af2ce108d54d6 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Sat, 30 Jul 2011 18:16:06 -0700 Subject: [PATCH 1/3] Add network debug logging instrumentation --- .../Plasma/NucleusLib/pnNetCli/pnNcCli.cpp | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp b/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp index 2b0056d8..393b55f0 100644 --- a/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp +++ b/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp @@ -42,6 +42,19 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com //#define NO_ENCRYPTION +struct NetLogMessage_Header +{ + unsigned m_protocol; + int m_direction; + FILETIME m_time; + unsigned m_size; +}; + +#define HURU_PIPE_NAME "\\\\.\\pipe\\H-Uru_NetLog" + +static CRITICAL_SECTION s_pipeCritical; +static HANDLE s_netlog = 0; + namespace pnNetCli { /***************************************************************************** @@ -128,6 +141,21 @@ static void PutBufferOnWire (NetCli * cli, void * data, unsigned bytes) { byte * temp, * heap = NULL; + // Write to the netlog + if (s_netlog) { + NetLogMessage_Header header; + header.m_protocol = cli->protocol; + header.m_direction = 0; // kCli2Srv + GetSystemTimeAsFileTime(&header.m_time); + header.m_size = bytes; + + EnterCriticalSection(&s_pipeCritical); + DWORD bytesWritten; + WriteFile(s_netlog, &header, sizeof(header), &bytesWritten, NULL); + WriteFile(s_netlog, data, bytes, &bytesWritten, NULL); + LeaveCriticalSection(&s_pipeCritical); + } + if (cli->mode == kNetCliModeEncrypted) { // Encrypt data... #ifndef NO_ENCRYPTION @@ -822,6 +850,21 @@ static NetCli * ConnCreate ( cli->mode = mode; cli->SetValue(kNilGuid); + // Network debug pipe + if (!s_netlog) { + InitializeCriticalSection(&s_pipeCritical); + WaitNamedPipe(HURU_PIPE_NAME, NMPWAIT_WAIT_FOREVER); + s_netlog = CreateFileA( + HURU_PIPE_NAME, + GENERIC_READ | GENERIC_WRITE, + 0, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL + ); + } + ResetSendRecv(cli); return cli; @@ -1009,6 +1052,21 @@ bool NetCliDispatch ( cli->input.Add(bytes, data); bool result = DispatchData(cli, param); + // Write to the netlog + if (s_netlog) { + NetLogMessage_Header header; + header.m_protocol = cli->protocol; + header.m_direction = 1; // kSrv2Cli + GetSystemTimeAsFileTime(&header.m_time); + header.m_size = bytes; + + EnterCriticalSection(&s_pipeCritical); + DWORD bytesWritten; + WriteFile(s_netlog, &header, sizeof(header), &bytesWritten, NULL); + WriteFile(s_netlog, data, bytes, &bytesWritten, NULL); + LeaveCriticalSection(&s_pipeCritical); + } + #ifdef SERVER cli->recvDispatch = result; #endif From 1da8eb7202ae6f19b1455ca1b56c51d35ab018d7 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Sat, 30 Jul 2011 19:10:35 -0700 Subject: [PATCH 2/3] Use pre-adjusted time --- .../Plasma/NucleusLib/pnNetCli/pnNcCli.cpp | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp b/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp index 393b55f0..01e3fca7 100644 --- a/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp +++ b/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp @@ -46,7 +46,7 @@ struct NetLogMessage_Header { unsigned m_protocol; int m_direction; - FILETIME m_time; + unsigned m_time; unsigned m_size; }; @@ -54,6 +54,18 @@ struct NetLogMessage_Header static CRITICAL_SECTION s_pipeCritical; static HANDLE s_netlog = 0; +static ULARGE_INTEGER s_timeOffset; + +static unsigned GetAdjustedTimer() +{ + FILETIME time; + ULARGE_INTEGER maths; + GetSystemTimeAsFileTime(&time); + maths.HighPart = time.dwHighDateTime; + maths.LowPart = time.dwLowDateTime; + maths.QuadPart -= s_timeOffset.QuadPart; + return maths.LowPart % 864000000; +} namespace pnNetCli { @@ -146,7 +158,7 @@ static void PutBufferOnWire (NetCli * cli, void * data, unsigned bytes) { NetLogMessage_Header header; header.m_protocol = cli->protocol; header.m_direction = 0; // kCli2Srv - GetSystemTimeAsFileTime(&header.m_time); + header.m_time = GetAdjustedTimer(); header.m_size = bytes; EnterCriticalSection(&s_pipeCritical); @@ -863,6 +875,12 @@ static NetCli * ConnCreate ( FILE_ATTRIBUTE_NORMAL, NULL ); + + // Not exactly the start, but close enough ;) + FILETIME timeBase; + GetSystemTimeAsFileTime(&timeBase); + s_timeOffset.HighPart = timeBase.dwHighDateTime; + s_timeOffset.LowPart = timeBase.dwLowDateTime; } ResetSendRecv(cli); @@ -1057,7 +1075,7 @@ bool NetCliDispatch ( NetLogMessage_Header header; header.m_protocol = cli->protocol; header.m_direction = 1; // kSrv2Cli - GetSystemTimeAsFileTime(&header.m_time); + header.m_time = GetAdjustedTimer(); header.m_size = bytes; EnterCriticalSection(&s_pipeCritical); From 7cbcc8a6fc89f99d54fe69a49198a27eeb611186 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Tue, 2 Aug 2011 20:58:53 -0700 Subject: [PATCH 3/3] Wrap NetLog stuff in !defined(PLASMA_EXTERNAL_RELEASE) --- .../Plasma/NucleusLib/pnNetCli/pnNcCli.cpp | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp b/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp index 01e3fca7..5bd8d392 100644 --- a/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp +++ b/Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp @@ -42,6 +42,8 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com //#define NO_ENCRYPTION +#ifndef PLASMA_EXTERNAL_RELEASE + struct NetLogMessage_Header { unsigned m_protocol; @@ -50,22 +52,24 @@ struct NetLogMessage_Header unsigned m_size; }; -#define HURU_PIPE_NAME "\\\\.\\pipe\\H-Uru_NetLog" - -static CRITICAL_SECTION s_pipeCritical; -static HANDLE s_netlog = 0; -static ULARGE_INTEGER s_timeOffset; - -static unsigned GetAdjustedTimer() -{ - FILETIME time; - ULARGE_INTEGER maths; - GetSystemTimeAsFileTime(&time); - maths.HighPart = time.dwHighDateTime; - maths.LowPart = time.dwLowDateTime; - maths.QuadPart -= s_timeOffset.QuadPart; - return maths.LowPart % 864000000; -} +#define HURU_PIPE_NAME "\\\\.\\pipe\\H-Uru_NetLog" + +static CRITICAL_SECTION s_pipeCritical; +static HANDLE s_netlog = 0; +static ULARGE_INTEGER s_timeOffset; + +static unsigned GetAdjustedTimer() +{ + FILETIME time; + ULARGE_INTEGER maths; + GetSystemTimeAsFileTime(&time); + maths.HighPart = time.dwHighDateTime; + maths.LowPart = time.dwLowDateTime; + maths.QuadPart -= s_timeOffset.QuadPart; + return maths.LowPart % 864000000; +} + +#endif // PLASMA_EXTERNAL_RELEASE namespace pnNetCli { @@ -153,6 +157,7 @@ static void PutBufferOnWire (NetCli * cli, void * data, unsigned bytes) { byte * temp, * heap = NULL; +#ifndef PLASMA_EXTERNAL_RELEASE // Write to the netlog if (s_netlog) { NetLogMessage_Header header; @@ -167,6 +172,7 @@ static void PutBufferOnWire (NetCli * cli, void * data, unsigned bytes) { WriteFile(s_netlog, data, bytes, &bytesWritten, NULL); LeaveCriticalSection(&s_pipeCritical); } +#endif // PLASMA_EXTERNAL_RELEASE if (cli->mode == kNetCliModeEncrypted) { // Encrypt data... @@ -862,6 +868,7 @@ static NetCli * ConnCreate ( cli->mode = mode; cli->SetValue(kNilGuid); +#ifndef PLASMA_EXTERNAL_RELEASE // Network debug pipe if (!s_netlog) { InitializeCriticalSection(&s_pipeCritical); @@ -882,6 +889,7 @@ static NetCli * ConnCreate ( s_timeOffset.HighPart = timeBase.dwHighDateTime; s_timeOffset.LowPart = timeBase.dwLowDateTime; } +#endif // PLASMA_EXTERNAL_RELEASE ResetSendRecv(cli); @@ -1070,6 +1078,7 @@ bool NetCliDispatch ( cli->input.Add(bytes, data); bool result = DispatchData(cli, param); +#ifndef PLASMA_EXTERNAL_RELEASE // Write to the netlog if (s_netlog) { NetLogMessage_Header header; @@ -1077,13 +1086,14 @@ bool NetCliDispatch ( header.m_direction = 1; // kSrv2Cli header.m_time = GetAdjustedTimer(); header.m_size = bytes; - + EnterCriticalSection(&s_pipeCritical); DWORD bytesWritten; WriteFile(s_netlog, &header, sizeof(header), &bytesWritten, NULL); WriteFile(s_netlog, data, bytes, &bytesWritten, NULL); LeaveCriticalSection(&s_pipeCritical); } +#endif // PLASMA_EXTERNAL_RELEASE #ifdef SERVER cli->recvDispatch = result;