summaryrefslogtreecommitdiffstats
path: root/ProtoProxy/Connection.cpp
diff options
context:
space:
mode:
authormadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2012-09-02 17:38:28 +0200
committermadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2012-09-02 17:38:28 +0200
commite1c83be32d5435d3c2bbc1468b24ba8c0728bac3 (patch)
treede85217613aa2e95eceadc6452602bf93e07f96b /ProtoProxy/Connection.cpp
parentToLua does not like the override keyword :( (diff)
downloadcuberite-e1c83be32d5435d3c2bbc1468b24ba8c0728bac3.tar
cuberite-e1c83be32d5435d3c2bbc1468b24ba8c0728bac3.tar.gz
cuberite-e1c83be32d5435d3c2bbc1468b24ba8c0728bac3.tar.bz2
cuberite-e1c83be32d5435d3c2bbc1468b24ba8c0728bac3.tar.lz
cuberite-e1c83be32d5435d3c2bbc1468b24ba8c0728bac3.tar.xz
cuberite-e1c83be32d5435d3c2bbc1468b24ba8c0728bac3.tar.zst
cuberite-e1c83be32d5435d3c2bbc1468b24ba8c0728bac3.zip
Diffstat (limited to '')
-rw-r--r--ProtoProxy/Connection.cpp203
1 files changed, 203 insertions, 0 deletions
diff --git a/ProtoProxy/Connection.cpp b/ProtoProxy/Connection.cpp
new file mode 100644
index 000000000..fe37d5003
--- /dev/null
+++ b/ProtoProxy/Connection.cpp
@@ -0,0 +1,203 @@
+
+// Connection.cpp
+
+// Interfaces to the cConnection class representing a single pair of connected sockets
+
+#include "Globals.h"
+#include "Connection.h"
+#include "Server.h"
+
+
+
+
+
+cConnection::cConnection(SOCKET a_ClientSocket, cServer & a_Server) :
+ m_Server(a_Server),
+ m_LogFile(NULL),
+ m_ClientSocket(a_ClientSocket),
+ m_ServerSocket(-1),
+ m_BeginTick(clock())
+{
+ AString fnam;
+ Printf(fnam, "Log_%d.log", (int)time(NULL));
+ m_LogFile = fopen(fnam.c_str(), "w");
+ Log("Log file created");
+}
+
+
+
+
+
+cConnection::~cConnection()
+{
+ fclose(m_LogFile);
+}
+
+
+
+
+
+void cConnection::Run(void)
+{
+ if (!ConnectToServer())
+ {
+ Log("Cannot connect to server; aborting");
+ return;
+ }
+
+ while (true)
+ {
+ fd_set ReadFDs;
+ FD_ZERO(&ReadFDs);
+ FD_SET(m_ServerSocket, &ReadFDs);
+ FD_SET(m_ClientSocket, &ReadFDs);
+ int res = select(2, &ReadFDs, NULL, NULL, NULL);
+ if (res <= 0)
+ {
+ printf("select() failed: %d; aborting client", WSAGetLastError());
+ return;
+ }
+ if (FD_ISSET(m_ServerSocket, &ReadFDs))
+ {
+ if (!RelayFromServer())
+ {
+ return;
+ }
+ }
+ if (FD_ISSET(m_ClientSocket, &ReadFDs))
+ {
+ if (!RelayFromClient())
+ {
+ return;
+ }
+ }
+ }
+}
+
+
+
+
+
+void cConnection::Log(const char * a_Format, ...)
+{
+ va_list args;
+ va_start(args, a_Format);
+ AString msg;
+ AppendVPrintf(msg, a_Format, args);
+ va_end(args);
+ AString FullMsg;
+ Printf(FullMsg, "[%5.3f] %s\n", GetRelativeTime(), msg.c_str());
+
+ cCSLock Lock(m_CSLog);
+ fputs(FullMsg.c_str(), m_LogFile);
+}
+
+
+
+
+
+void cConnection::DataLog(const void * a_Data, int a_Size, const char * a_Format, ...)
+{
+ va_list args;
+ va_start(args, a_Format);
+ AString msg;
+ AppendVPrintf(msg, a_Format, args);
+ va_end(args);
+ AString FullMsg;
+ AString Hex;
+ Printf(FullMsg, "[%5.3f] %s\n%s", GetRelativeTime(), msg.c_str(), CreateHexDump(Hex, a_Data, a_Size, 16).c_str());
+
+ cCSLock Lock(m_CSLog);
+ fputs(FullMsg.c_str(), m_LogFile);
+}
+
+
+
+
+
+bool cConnection::ConnectToServer(void)
+{
+ m_ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (m_ServerSocket == INVALID_SOCKET)
+ {
+ return false;
+ }
+ sockaddr_in localhost;
+ localhost.sin_family = AF_INET;
+ localhost.sin_port = htons(m_Server.GetConnectPort());
+ localhost.sin_addr.s_addr = htonl(0x7f000001); // localhost
+ if (connect(m_ServerSocket, (sockaddr *)&localhost, sizeof(localhost)) != 0)
+ {
+ printf("connection to server failed: %d\n", WSAGetLastError());
+ return false;
+ }
+ return true;
+}
+
+
+
+
+
+bool cConnection::RelayFromServer(void)
+{
+ char Buffer[1024];
+ int res = recv(m_ServerSocket, Buffer, sizeof(Buffer), 0);
+ if (res <= 0)
+ {
+ Log("Server closed the socket: %d; %d; aborting connection", res, WSAGetLastError());
+ return false;
+ }
+
+ DataLog(Buffer, res, "Received %d bytes from the server", res);
+ // TODO: Process the data
+
+ res = send(m_ClientSocket, Buffer, res, 0);
+ if (res <= 0)
+ {
+ Log("Client closed the socket: %d, %d; aborting connection", res, WSAGetLastError());
+ return false;
+ }
+
+ return true;
+}
+
+
+
+
+
+bool cConnection::RelayFromClient(void)
+{
+ char Buffer[1024];
+ int res = recv(m_ClientSocket, Buffer, sizeof(Buffer), 0);
+ if (res <= 0)
+ {
+ Log("Client closed the socket: %d; %d; aborting connection", res, WSAGetLastError());
+ return false;
+ }
+
+ DataLog(Buffer, res, "Received %d bytes from the client", res);
+ // TODO: Process the data
+
+ res = send(m_ServerSocket, Buffer, res, 0);
+ if (res <= 0)
+ {
+ Log("Server closed the socket: %d, %d; aborting connection", res, WSAGetLastError());
+ return false;
+ }
+
+ return true;
+}
+
+
+
+
+
+double cConnection::GetRelativeTime(void)
+{
+ return (double)(clock() - m_BeginTick) / CLOCKS_PER_SEC;
+
+}
+
+
+
+