2019-01-05 20:21:43 +08:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
2025-06-08 15:38:41 +08:00
|
|
|
|
#include "StdAfx.h"
|
2019-01-05 20:21:43 +08:00
|
|
|
|
#include <WinSock2.h>
|
|
|
|
|
|
#pragma comment(lib,"ws2_32.lib")
|
2025-06-29 21:25:59 +08:00
|
|
|
|
#include "Server.h"
|
2025-05-03 20:57:22 +08:00
|
|
|
|
|
2025-02-07 18:59:15 +08:00
|
|
|
|
#if USING_CTX
|
|
|
|
|
|
#include "zstd/zstd.h"
|
|
|
|
|
|
#endif
|
2019-01-05 20:21:43 +08:00
|
|
|
|
|
|
|
|
|
|
#include <Mstcpip.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define NC_CLIENT_CONNECT 0x0001
|
|
|
|
|
|
#define NC_RECEIVE 0x0004
|
|
|
|
|
|
#define NC_RECEIVE_COMPLETE 0x0005 // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
2025-06-29 21:25:59 +08:00
|
|
|
|
// ZLIB ѹ<><D1B9><EFBFBD><EFBFBD>
|
|
|
|
|
|
#include "zlib/zlib.h"
|
|
|
|
|
|
|
2025-08-10 17:15:32 +08:00
|
|
|
|
// ZSTD
|
2025-06-29 21:25:59 +08:00
|
|
|
|
#include "zstd/zstd.h"
|
|
|
|
|
|
#ifdef _WIN64
|
|
|
|
|
|
#pragma comment(lib, "zstd/zstd_x64.lib")
|
|
|
|
|
|
#else
|
|
|
|
|
|
#pragma comment(lib, "zstd/zstd.lib")
|
|
|
|
|
|
#endif
|
|
|
|
|
|
#define C_FAILED(p) ZSTD_isError(p)
|
|
|
|
|
|
#define C_SUCCESS(p) (!C_FAILED(p))
|
|
|
|
|
|
#define ZSTD_CLEVEL 5
|
|
|
|
|
|
#if USING_CTX
|
|
|
|
|
|
#define Mcompress(dest, destLen, source, sourceLen) ZSTD_compress2(m_Cctx, dest, *(destLen), source, sourceLen)
|
|
|
|
|
|
#define Muncompress(dest, destLen, source, sourceLen) ZSTD_decompressDCtx(m_Dctx, dest, *(destLen), source, sourceLen)
|
|
|
|
|
|
#else
|
|
|
|
|
|
#define Mcompress(dest, destLen, source, sourceLen) ZSTD_compress(dest, *(destLen), source, sourceLen, ZSTD_CLEVEL_DEFAULT)
|
|
|
|
|
|
#define Muncompress(dest, destLen, source, sourceLen) ZSTD_decompress(dest, *(destLen), source, sourceLen)
|
|
|
|
|
|
#endif
|
2025-01-31 22:22:16 +08:00
|
|
|
|
|
2025-05-03 20:57:22 +08:00
|
|
|
|
|
2025-06-29 21:25:59 +08:00
|
|
|
|
class IOCPServer : public Server
|
2025-05-03 20:57:22 +08:00
|
|
|
|
{
|
2025-04-03 03:48:48 +08:00
|
|
|
|
protected:
|
2025-07-03 04:30:25 +08:00
|
|
|
|
int m_nPort;
|
2025-06-29 21:25:59 +08:00
|
|
|
|
SOCKET m_sListenSocket;
|
|
|
|
|
|
HANDLE m_hCompletionPort;
|
|
|
|
|
|
UINT m_ulMaxConnections;
|
|
|
|
|
|
HANDLE m_hListenEvent;
|
|
|
|
|
|
HANDLE m_hListenThread;
|
|
|
|
|
|
BOOL m_bTimeToKill;
|
|
|
|
|
|
HANDLE m_hKillEvent;
|
|
|
|
|
|
ULONG m_ulThreadPoolMin;
|
|
|
|
|
|
ULONG m_ulThreadPoolMax;
|
|
|
|
|
|
ULONG m_ulCPULowThreadsHold;
|
|
|
|
|
|
ULONG m_ulCPUHighThreadsHold;
|
|
|
|
|
|
ULONG m_ulCurrentThread;
|
|
|
|
|
|
ULONG m_ulBusyThread;
|
2019-01-05 20:21:43 +08:00
|
|
|
|
|
2025-02-07 18:59:15 +08:00
|
|
|
|
#if USING_CTX
|
2025-06-29 21:25:59 +08:00
|
|
|
|
ZSTD_CCtx* m_Cctx; // ѹ<><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
ZSTD_DCtx* m_Dctx; // <20><>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2025-02-07 18:59:15 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2025-06-29 21:25:59 +08:00
|
|
|
|
ULONG m_ulKeepLiveTime;
|
|
|
|
|
|
pfnNotifyProc m_NotifyProc;
|
|
|
|
|
|
pfnOfflineProc m_OfflineProc;
|
|
|
|
|
|
ULONG m_ulWorkThreadCount;
|
|
|
|
|
|
CRITICAL_SECTION m_cs;
|
|
|
|
|
|
ContextObjectList m_ContextConnectionList;
|
|
|
|
|
|
ContextObjectList m_ContextFreePoolList;
|
2019-01-05 20:21:43 +08:00
|
|
|
|
|
2025-06-29 21:25:59 +08:00
|
|
|
|
private:
|
2019-01-05 20:21:43 +08:00
|
|
|
|
static DWORD WINAPI ListenThreadProc(LPVOID lParam);
|
|
|
|
|
|
static DWORD WINAPI WorkThreadProc(LPVOID lParam);
|
|
|
|
|
|
|
2025-06-29 21:25:59 +08:00
|
|
|
|
BOOL InitializeIOCP(VOID);
|
|
|
|
|
|
VOID OnAccept();
|
2025-06-08 15:38:41 +08:00
|
|
|
|
PCONTEXT_OBJECT AllocateContext(SOCKET s);
|
2019-01-05 20:21:43 +08:00
|
|
|
|
VOID RemoveStaleContext(CONTEXT_OBJECT* ContextObject);
|
2019-01-06 21:18:26 +08:00
|
|
|
|
VOID MoveContextToFreePoolList(CONTEXT_OBJECT* ContextObject);
|
|
|
|
|
|
VOID PostRecv(CONTEXT_OBJECT* ContextObject);
|
2025-06-29 21:25:59 +08:00
|
|
|
|
BOOL HandleIO(IOType PacketFlags, PCONTEXT_OBJECT ContextObject, DWORD dwTrans);
|
|
|
|
|
|
BOOL OnClientInitializing(PCONTEXT_OBJECT ContextObject, DWORD dwTrans);
|
|
|
|
|
|
BOOL OnClientReceiving(PCONTEXT_OBJECT ContextObject, DWORD dwTrans);
|
|
|
|
|
|
VOID OnClientPreSending(CONTEXT_OBJECT* ContextObject, PBYTE szBuffer, size_t ulOriginalLength);
|
|
|
|
|
|
BOOL OnClientPostSending(CONTEXT_OBJECT* ContextObject, ULONG ulCompressedLength);
|
|
|
|
|
|
int AddWorkThread(int n) {
|
|
|
|
|
|
EnterCriticalSection(&m_cs);
|
2025-04-07 18:18:36 +08:00
|
|
|
|
m_ulWorkThreadCount += n;
|
|
|
|
|
|
int ret = m_ulWorkThreadCount;
|
|
|
|
|
|
LeaveCriticalSection(&m_cs);
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
2019-01-10 19:35:03 +08:00
|
|
|
|
|
2025-06-29 21:25:59 +08:00
|
|
|
|
public:
|
|
|
|
|
|
IOCPServer(void);
|
|
|
|
|
|
~IOCPServer(void);
|
2025-07-03 04:30:25 +08:00
|
|
|
|
int GetPort() const override {
|
|
|
|
|
|
return m_nPort;
|
|
|
|
|
|
}
|
2025-06-29 21:25:59 +08:00
|
|
|
|
|
|
|
|
|
|
UINT StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort);
|
|
|
|
|
|
|
2025-06-08 15:38:41 +08:00
|
|
|
|
VOID Send2Client(CONTEXT_OBJECT* ContextObject, PBYTE szBuffer, ULONG ulOriginalLength) {
|
|
|
|
|
|
OnClientPreSending(ContextObject, szBuffer, ulOriginalLength);
|
|
|
|
|
|
}
|
2025-06-29 21:25:59 +08:00
|
|
|
|
|
2024-12-31 03:11:26 +08:00
|
|
|
|
void UpdateMaxConnection(int maxConn);
|
2025-06-29 21:25:59 +08:00
|
|
|
|
|
2025-04-07 18:18:36 +08:00
|
|
|
|
void Destroy();
|
2025-05-03 20:57:22 +08:00
|
|
|
|
void Disconnect(CONTEXT_OBJECT *ctx){}
|
2019-01-05 20:21:43 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-05-03 20:57:22 +08:00
|
|
|
|
typedef IOCPServer ISocketBase;
|
|
|
|
|
|
|
2025-01-31 22:22:16 +08:00
|
|
|
|
typedef IOCPServer CIOCPServer;
|
|
|
|
|
|
|
|
|
|
|
|
typedef CONTEXT_OBJECT ClientContext;
|
|
|
|
|
|
|
|
|
|
|
|
#define m_Socket sClientSocket
|
|
|
|
|
|
#define m_DeCompressionBuffer InDeCompressedBuffer
|
2025-06-08 15:38:41 +08:00
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>ж<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>ĶԻ<C4B6><D4BB><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD>
|
|
|
|
|
|
class CDialogBase : public CDialog {
|
|
|
|
|
|
public:
|
|
|
|
|
|
CONTEXT_OBJECT* m_ContextObject;
|
2025-06-29 21:25:59 +08:00
|
|
|
|
Server* m_iocpServer;
|
2025-06-08 15:38:41 +08:00
|
|
|
|
CString m_IPAddress;
|
|
|
|
|
|
bool m_bIsClosed;
|
2025-06-29 03:29:35 +08:00
|
|
|
|
bool m_bIsProcessing;
|
2025-06-08 15:38:41 +08:00
|
|
|
|
HICON m_hIcon;
|
2025-06-29 21:25:59 +08:00
|
|
|
|
CDialogBase(UINT nIDTemplate, CWnd* pParent, Server* pIOCPServer, CONTEXT_OBJECT* pContext, int nIcon) :
|
2025-06-29 03:29:35 +08:00
|
|
|
|
m_bIsClosed(false), m_bIsProcessing(false),
|
2025-06-08 15:38:41 +08:00
|
|
|
|
m_ContextObject(pContext),
|
|
|
|
|
|
m_iocpServer(pIOCPServer),
|
|
|
|
|
|
CDialog(nIDTemplate, pParent) {
|
|
|
|
|
|
|
2025-08-10 17:15:32 +08:00
|
|
|
|
m_IPAddress = pContext->GetPeerName().c_str();
|
2025-06-08 15:38:41 +08:00
|
|
|
|
m_hIcon = nIcon > 0 ? LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(nIcon)) : NULL;
|
|
|
|
|
|
}
|
2025-06-28 23:52:26 +08:00
|
|
|
|
virtual ~CDialogBase(){}
|
2025-06-08 15:38:41 +08:00
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
virtual void OnReceiveComplete(void) = 0;
|
2025-06-29 03:29:35 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
void MarkReceiving(bool recv = true) {
|
|
|
|
|
|
m_bIsProcessing = recv;
|
|
|
|
|
|
}
|
|
|
|
|
|
bool IsProcessing() const {
|
|
|
|
|
|
return m_bIsProcessing;
|
|
|
|
|
|
}
|
2025-06-08 15:38:41 +08:00
|
|
|
|
void OnClose() {
|
2025-06-29 03:29:35 +08:00
|
|
|
|
m_bIsClosed = true;
|
|
|
|
|
|
while (m_bIsProcessing)
|
|
|
|
|
|
Sleep(200);
|
2025-06-28 23:52:26 +08:00
|
|
|
|
if(m_hIcon) DestroyIcon(m_hIcon);
|
|
|
|
|
|
m_hIcon = NULL;
|
2025-06-08 15:38:41 +08:00
|
|
|
|
CDialog::OnClose();
|
2025-06-28 23:52:26 +08:00
|
|
|
|
|
|
|
|
|
|
if (GetSafeHwnd())
|
|
|
|
|
|
DestroyWindow();
|
|
|
|
|
|
}
|
|
|
|
|
|
virtual void PostNcDestroy() override
|
|
|
|
|
|
{
|
2025-06-08 15:38:41 +08:00
|
|
|
|
delete this;
|
2025-06-28 23:52:26 +08:00
|
|
|
|
}
|
2025-06-29 03:29:35 +08:00
|
|
|
|
// ȡ<><C8A1> SOCKET <20><>ȡ<EFBFBD><C8A1><EFBFBD>ú<EFBFBD><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD>ε<EFBFBD><CEB5><EFBFBD>
|
2025-06-28 23:52:26 +08:00
|
|
|
|
void CancelIO(){
|
|
|
|
|
|
m_bIsClosed = TRUE;
|
|
|
|
|
|
|
|
|
|
|
|
m_ContextObject->CancelIO();
|
2025-06-08 15:38:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef CDialogBase DialogBase;
|
2025-07-02 03:53:21 +08:00
|
|
|
|
|
|
|
|
|
|
BOOL ParseReceivedData(CONTEXT_OBJECT* ContextObject, DWORD dwTrans, pfnNotifyProc m_NotifyProc);
|
|
|
|
|
|
|
|
|
|
|
|
BOOL WriteContextData(CONTEXT_OBJECT* ContextObject, PBYTE szBuffer, size_t ulOriginalLength);
|