layout: Refactor the socket server code

This commit is contained in:
yuanyuanxiang
2025-06-30 04:35:38 +08:00
parent 017a8c8fa2
commit 3e5d45df6b
28 changed files with 259 additions and 221 deletions

View File

@@ -57,11 +57,9 @@ enum {
COMPRESS_NONE = 1, // û<><C3BB>ѹ<EFBFBD><D1B9>
};
struct CONTEXT_OBJECT;
// Header parser: parse the data to make sure it's from a supported client.
class HeaderParser {
friend struct CONTEXT_OBJECT;
friend class CONTEXT_OBJECT;
protected:
HeaderParser() {
memset(this, 0, sizeof(HeaderParser));
@@ -229,8 +227,31 @@ public:
}
};
typedef struct CONTEXT_OBJECT
typedef void (CALLBACK* pfnNotifyProc)(CONTEXT_OBJECT* ContextObject);
typedef void (CALLBACK* pfnOfflineProc)(CONTEXT_OBJECT* ContextObject);
class Server
{
public:
friend class CONTEXT_OBJECT;
Server() {}
virtual ~Server() {}
virtual UINT StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort) = 0;
virtual void Send2Client(CONTEXT_OBJECT* ContextObject, PBYTE szBuffer, ULONG ulOriginalLength) = 0;
virtual void UpdateMaxConnection(int maxConn) {}
virtual void Destroy() {}
virtual void Disconnect(CONTEXT_OBJECT* ctx) {}
};
typedef class CONTEXT_OBJECT
{
public:
CString sClientInfo[ONLINELIST_MAX];
CString additonalInfo[RES_MAX];
SOCKET sClientSocket;
@@ -250,10 +271,11 @@ typedef struct CONTEXT_OBJECT
BOOL m_bProxyConnected; // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
BOOL bLogin; // <20>Ƿ<EFBFBD> login
std::string PeerName; // <20>Զ<EFBFBD>IP
Server* server; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int addrLen; // for UDP
sockaddr_in clientAddr; // for UDP
VOID InitMember(SOCKET s)
VOID InitMember(SOCKET s, Server *svr)
{
memset(szBuffer, 0, sizeof(char) * PACKET_LENGTH);
hWnd = NULL;
@@ -273,9 +295,17 @@ typedef struct CONTEXT_OBJECT
Parser.Reset();
bLogin = FALSE;
m_bProxyConnected = FALSE;
server = svr;
clientAddr = {};
addrLen = sizeof(sockaddr_in);
}
Server* GetServer() {
return server;
}
VOID Send2Client(PBYTE szBuffer, ULONG ulOriginalLength) {
if (server)
server->Send2Client(this, szBuffer, ulOriginalLength);
}
VOID SetClientInfo(const CString(&s)[ONLINELIST_MAX], const std::vector<std::string>& a = {}) {
for (int i = 0; i < ONLINELIST_MAX; i++)
{
@@ -396,23 +426,3 @@ typedef struct CONTEXT_OBJECT
}CONTEXT_OBJECT, * PCONTEXT_OBJECT;
typedef CList<PCONTEXT_OBJECT> ContextObjectList;
typedef void (CALLBACK* pfnNotifyProc)(CONTEXT_OBJECT* ContextObject);
typedef void (CALLBACK* pfnOfflineProc)(CONTEXT_OBJECT* ContextObject);
class Server
{
public:
Server(){}
virtual ~Server(){}
virtual UINT StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort) = 0;
virtual void Send2Client(CONTEXT_OBJECT * ContextObject, PBYTE szBuffer, ULONG ulOriginalLength) = 0;
virtual void UpdateMaxConnection(int maxConn){}
virtual void Destroy(){}
virtual void Disconnect(CONTEXT_OBJECT* ctx) {}
};