1
0

ProtoProxy: Added logging for the window contents, item's metadata is saved to a separate file

git-svn-id: http://mc-server.googlecode.com/svn/trunk@1537 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
madmaxoft@gmail.com 2013-06-02 09:56:04 +00:00
parent 7c0be7520a
commit e9042ac098
3 changed files with 38 additions and 15 deletions

View File

@ -191,20 +191,22 @@ enum
// cConnection: // cConnection:
cConnection::cConnection(SOCKET a_ClientSocket, cServer & a_Server) : cConnection::cConnection(SOCKET a_ClientSocket, cServer & a_Server) :
m_Server(a_Server), m_ItemIdx(0),
m_LogFile(NULL), m_LogFile(NULL),
m_Server(a_Server),
m_ClientSocket(a_ClientSocket), m_ClientSocket(a_ClientSocket),
m_ServerSocket(-1), m_ServerSocket(-1),
m_BeginTick(clock()), m_BeginTick(clock()),
m_ClientState(csUnencrypted), m_ClientState(csUnencrypted),
m_ServerState(csUnencrypted), m_ServerState(csUnencrypted),
m_Nonce(0),
m_ClientBuffer(1024 KiB), m_ClientBuffer(1024 KiB),
m_ServerBuffer(1024 KiB), m_ServerBuffer(1024 KiB),
m_Nonce(0),
m_HasClientPinged(false) m_HasClientPinged(false)
{ {
AString fnam; Printf(m_LogNameBase, "Log_%d", (int)time(NULL));
Printf(fnam, "Log_%d.log", (int)time(NULL)); AString fnam(m_LogNameBase);
fnam.append(".log");
m_LogFile = fopen(fnam.c_str(), "w"); m_LogFile = fopen(fnam.c_str(), "w");
Log("Log file created"); Log("Log file created");
} }
@ -2001,6 +2003,9 @@ bool cConnection::HandleServerWindowContents(void)
{ {
HANDLE_SERVER_PACKET_READ(ReadChar, char, WindowID); HANDLE_SERVER_PACKET_READ(ReadChar, char, WindowID);
HANDLE_SERVER_PACKET_READ(ReadBEShort, short, NumSlots); HANDLE_SERVER_PACKET_READ(ReadBEShort, short, NumSlots);
Log("Received a PACKET_WINDOW_CONTENTS from the server:");
Log(" WindowID = %d", WindowID);
Log(" NumSlots = %d", NumSlots);
AStringVector Items; AStringVector Items;
for (short i = 0; i < NumSlots; i++) for (short i = 0; i < NumSlots; i++)
{ {
@ -2009,14 +2014,9 @@ bool cConnection::HandleServerWindowContents(void)
{ {
return false; return false;
} }
Items.push_back(Item); Log(" %d: %s", i, Item.c_str());
} }
Log("Received a PACKET_WINDOW_CONTENTS from the server:");
Log(" WindowID = %d", WindowID);
Log(" NumSlots = %d", NumSlots);
// TODO: list items
COPY_TO_CLIENT(); COPY_TO_CLIENT();
return true; return true;
} }
@ -2031,10 +2031,11 @@ bool cConnection::HandleServerWindowOpen(void)
HANDLE_SERVER_PACKET_READ(ReadChar, char, WindowType); HANDLE_SERVER_PACKET_READ(ReadChar, char, WindowType);
HANDLE_SERVER_PACKET_READ(ReadBEUTF16String16, AString, Title); HANDLE_SERVER_PACKET_READ(ReadBEUTF16String16, AString, Title);
HANDLE_SERVER_PACKET_READ(ReadByte, Byte, NumSlots); HANDLE_SERVER_PACKET_READ(ReadByte, Byte, NumSlots);
HANDLE_SERVER_PACKET_READ(ReadByte, Byte, UseProvidedTitle);
Log("Received a PACKET_WINDOW_OPEN from the server:"); Log("Received a PACKET_WINDOW_OPEN from the server:");
Log(" WindowID = %d", WindowID); Log(" WindowID = %d", WindowID);
Log(" WindowType = %d", WindowType); Log(" WindowType = %d", WindowType);
Log(" Title = \"%s\"", Title.c_str()); Log(" Title = \"%s\", Use = %d", Title.c_str(), UseProvidedTitle);
Log(" NumSlots = %d", NumSlots); Log(" NumSlots = %d", NumSlots);
COPY_TO_CLIENT(); COPY_TO_CLIENT();
return true; return true;
@ -2071,11 +2072,27 @@ bool cConnection::ParseSlot(cByteBuffer & a_Buffer, AString & a_ItemDesc)
{ {
return true; return true;
} }
AppendPrintf(a_ItemDesc, " (%d bytes of meta)", MetadataLength); AString Metadata;
if (!a_Buffer.SkipRead(MetadataLength)) Metadata.resize(MetadataLength);
if (!a_Buffer.ReadBuf((void *)Metadata.data(), MetadataLength))
{ {
return false; return false;
} }
AString MetaHex;
CreateHexDump(MetaHex, Metadata.data(), Metadata.size(), 16);
AppendPrintf(a_ItemDesc, "; %d bytes of meta:\n%s", MetadataLength, MetaHex.c_str());
// Save metadata to a file:
AString fnam;
Printf(fnam, "%s_item_%08x.nbt", m_LogNameBase.c_str(), m_ItemIdx++);
FILE * f = fopen(fnam.c_str(), "wb");
if (f != NULL)
{
fwrite(Metadata.data(), 1, Metadata.size(), f);
fclose(f);
AppendPrintf(a_ItemDesc, "\n (saved to file \"%s\")", fnam.c_str());
}
return true; return true;
} }

View File

@ -24,6 +24,10 @@ class cServer;
class cConnection class cConnection
{ {
AString m_LogNameBase; ///< Base for the log filename and all files connected to this log
int m_ItemIdx; ///< Index for the next file into which item metadata should be written (ParseSlot() function)
cCriticalSection m_CSLog; cCriticalSection m_CSLog;
FILE * m_LogFile; FILE * m_LogFile;

View File

@ -58,7 +58,7 @@ int cServer::Init(short a_ListenPort, short a_ConnectPort)
void cServer::Run(void) void cServer::Run(void)
{ {
printf("Server running\n"); printf("Server running.\n");
while (true) while (true)
{ {
sockaddr_in Addr; sockaddr_in Addr;
@ -67,11 +67,13 @@ void cServer::Run(void)
SOCKET client = accept(m_ListenSocket, (sockaddr *)&Addr, &AddrSize); SOCKET client = accept(m_ListenSocket, (sockaddr *)&Addr, &AddrSize);
if (client == INVALID_SOCKET) if (client == INVALID_SOCKET)
{ {
printf("accept returned an error: %d; bailing out", WSAGetLastError()); printf("accept returned an error: %d; bailing out.\n", WSAGetLastError());
return; return;
} }
printf("Client connected, proxying...\n");
cConnection Connection(client, *this); cConnection Connection(client, *this);
Connection.Run(); Connection.Run();
printf("Client disconnected. Ready for another connection.\n");
} }
} }