feature: Add client protocol option TCP/UDP

This commit is contained in:
yuanyuanxiang
2025-07-03 04:30:25 +08:00
parent 848f4089bb
commit 64bebb1b86
12 changed files with 107 additions and 52 deletions

View File

@@ -13,7 +13,7 @@
// Զ<>̵<EFBFBD>ַ // Զ<>̵<EFBFBD>ַ
CONNECT_ADDRESS g_SETTINGS = { CONNECT_ADDRESS g_SETTINGS = {
FLAG_GHOST, "127.0.0.1", "6543", CLIENT_TYPE_DLL, false, DLL_VERSION, FLAG_GHOST, "127.0.0.1", "6543", CLIENT_TYPE_DLL, false, DLL_VERSION,
FALSE, Startup_DLL, NULL, PROTO_TCP, RUNNING_RANDOM, FALSE, Startup_DLL, NULL, PROTO_UDP, RUNNING_RANDOM,
}; };
// <20><><EFBFBD>տͻ<D5BF><CDBB><EFBFBD>ֻ<EFBFBD><D6BB>2<EFBFBD><32>ȫ<EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD>: g_SETTINGS<47><53>g_MyApp<70><70><EFBFBD><EFBFBD>g_SETTINGS<47><53>Ϊg_MyApp<70>ij<EFBFBD>Ա. // <20><><EFBFBD>տͻ<D5BF><CDBB><EFBFBD>ֻ<EFBFBD><D6BB>2<EFBFBD><32>ȫ<EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD>: g_SETTINGS<47><53>g_MyApp<70><70><EFBFBD><EFBFBD>g_SETTINGS<47><53>Ϊg_MyApp<70>ij<EFBFBD>Ա.

Binary file not shown.

View File

@@ -1241,8 +1241,10 @@ void CMy2015RemoteDlg::OnOnlineDelete()
{ {
POSITION Pos = m_CList_Online.GetFirstSelectedItemPosition(); POSITION Pos = m_CList_Online.GetFirstSelectedItemPosition();
int iItem = m_CList_Online.GetNextSelectedItem(Pos); int iItem = m_CList_Online.GetNextSelectedItem(Pos);
CString strIP = m_CList_Online.GetItemText(iItem,ONLINELIST_IP); CString strIP = m_CList_Online.GetItemText(iItem,ONLINELIST_IP);
context* ctx = (context*)m_CList_Online.GetItemData(iItem);
m_CList_Online.DeleteItem(iItem); m_CList_Online.DeleteItem(iItem);
ctx->Destroy();
strIP+="断开连接"; strIP+="断开连接";
ShowMessage("操作成功",strIP); ShowMessage("操作成功",strIP);
} }
@@ -1631,11 +1633,10 @@ BOOL CMy2015RemoteDlg::Activate(const std::string& nPort,int nMaxConnection)
} }
VOID CALLBACK CMy2015RemoteDlg::NotifyProc(CONTEXT_OBJECT* ContextObject) BOOL CALLBACK CMy2015RemoteDlg::NotifyProc(CONTEXT_OBJECT* ContextObject)
{ {
if (!g_2015RemoteDlg || g_2015RemoteDlg->isClosed) { if (!g_2015RemoteDlg || g_2015RemoteDlg->isClosed) {
ContextObject->Destroy(); return FALSE;
return;
} }
AUTO_TICK(50); AUTO_TICK(50);
@@ -1649,16 +1650,17 @@ VOID CALLBACK CMy2015RemoteDlg::NotifyProc(CONTEXT_OBJECT* ContextObject)
HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hEvent == NULL) { if (hEvent == NULL) {
Mprintf("===> NotifyProc CreateEvent FAILED: %p <===\n", ContextObject); Mprintf("===> NotifyProc CreateEvent FAILED: %p <===\n", ContextObject);
return; return FALSE;
} }
if (!g_2015RemoteDlg->PostMessage(WM_HANDLEMESSAGE, (WPARAM)hEvent, (LPARAM)ContextObject)) { if (!g_2015RemoteDlg->PostMessage(WM_HANDLEMESSAGE, (WPARAM)hEvent, (LPARAM)ContextObject)) {
Mprintf("===> NotifyProc PostMessage FAILED: %p <===\n", ContextObject); Mprintf("===> NotifyProc PostMessage FAILED: %p <===\n", ContextObject);
CloseHandle(hEvent); CloseHandle(hEvent);
return; return FALSE;
} }
HANDLE handles[2] = { hEvent, g_2015RemoteDlg->m_hExit }; HANDLE handles[2] = { hEvent, g_2015RemoteDlg->m_hExit };
DWORD result = WaitForMultipleObjects(2, handles, FALSE, INFINITE); DWORD result = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
} }
return TRUE;
} }
// 对话框指针及对话框句柄 // 对话框指针及对话框句柄
@@ -1669,10 +1671,10 @@ struct dlgInfo
dlgInfo(HANDLE h, HWND type) : hDlg(h), hWnd(type) { } dlgInfo(HANDLE h, HWND type) : hDlg(h), hWnd(type) { }
}; };
VOID CALLBACK CMy2015RemoteDlg::OfflineProc(CONTEXT_OBJECT* ContextObject) BOOL CALLBACK CMy2015RemoteDlg::OfflineProc(CONTEXT_OBJECT* ContextObject)
{ {
if (!g_2015RemoteDlg) if (!g_2015RemoteDlg || g_2015RemoteDlg->isClosed)
return; return FALSE;
dlgInfo* dlg = ContextObject->hWnd ? new dlgInfo(ContextObject->hDlg, ContextObject->hWnd) : NULL; dlgInfo* dlg = ContextObject->hWnd ? new dlgInfo(ContextObject->hDlg, ContextObject->hWnd) : NULL;
SOCKET nSocket = ContextObject->sClientSocket; SOCKET nSocket = ContextObject->sClientSocket;
@@ -1680,6 +1682,8 @@ VOID CALLBACK CMy2015RemoteDlg::OfflineProc(CONTEXT_OBJECT* ContextObject)
g_2015RemoteDlg->PostMessage(WM_USEROFFLINEMSG, (WPARAM)dlg, (LPARAM)nSocket); g_2015RemoteDlg->PostMessage(WM_USEROFFLINEMSG, (WPARAM)dlg, (LPARAM)nSocket);
ContextObject->hWnd = NULL; ContextObject->hWnd = NULL;
return TRUE;
} }
@@ -2629,9 +2633,10 @@ void CMy2015RemoteDlg::OnListClick(NMHDR* pNMHDR, LRESULT* pResult)
CString strText; CString strText;
std::string expired = res[RES_EXPIRED_DATE]; std::string expired = res[RES_EXPIRED_DATE];
expired = expired.empty() ? "" : " Expired on " + expired; expired = expired.empty() ? "" : " Expired on " + expired;
strText.Format(_T("文件路径: %s%s\r\n系统信息: %s 位 %s 核心 %s GB\r\n启动信息: %s %s"), strText.Format(_T("文件路径: %s%s\r\n系统信息: %s 位 %s 核心 %s GB\r\n启动信息: %s %s\r\n上线信息: %s %d"),
res[RES_PROGRAM_BITS].IsEmpty() ? "" : res[RES_PROGRAM_BITS] + "", res[RES_FILE_PATH], res[RES_PROGRAM_BITS].IsEmpty() ? "" : res[RES_PROGRAM_BITS] + "", res[RES_FILE_PATH],
res[RES_SYSTEM_BITS], res[RES_SYSTEM_CPU], res[RES_SYSTEM_MEM], startTime, expired.c_str()); res[RES_SYSTEM_BITS], res[RES_SYSTEM_CPU], res[RES_SYSTEM_MEM], startTime, expired.c_str(),
ctx->GetProtocol().c_str(), ctx->GetServerPort());
// 获取鼠标位置 // 获取鼠标位置
CPoint pt; CPoint pt;
@@ -2644,7 +2649,7 @@ void CMy2015RemoteDlg::OnListClick(NMHDR* pNMHDR, LRESULT* pResult)
m_pFloatingTip = new CWnd(); m_pFloatingTip = new CWnd();
int width = res[RES_FILE_PATH].GetLength() * 10; int width = res[RES_FILE_PATH].GetLength() * 10;
width = min(max(width, 360), 800); width = min(max(width, 360), 800);
CRect rect(pt.x, pt.y, pt.x + width, pt.y + 50); // 宽度、高度 CRect rect(pt.x, pt.y, pt.x + width, pt.y + 60); // 宽度、高度
BOOL bOk = m_pFloatingTip->CreateEx( BOOL bOk = m_pFloatingTip->CreateEx(
WS_EX_TOPMOST | WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE, WS_EX_TOPMOST | WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE,

View File

@@ -176,8 +176,8 @@ public:
Buffer* m_ServerDLL[PAYLOAD_MAXTYPE]; Buffer* m_ServerDLL[PAYLOAD_MAXTYPE];
Buffer* m_ServerBin[PAYLOAD_MAXTYPE]; Buffer* m_ServerBin[PAYLOAD_MAXTYPE];
MasterSettings m_settings; MasterSettings m_settings;
static VOID CALLBACK NotifyProc(CONTEXT_OBJECT* ContextObject); static BOOL CALLBACK NotifyProc(CONTEXT_OBJECT* ContextObject);
static VOID CALLBACK OfflineProc(CONTEXT_OBJECT* ContextObject); static BOOL CALLBACK OfflineProc(CONTEXT_OBJECT* ContextObject);
VOID MessageHandle(CONTEXT_OBJECT* ContextObject); VOID MessageHandle(CONTEXT_OBJECT* ContextObject);
VOID SendSelectedCommand(PBYTE szBuffer, ULONG ulLength); VOID SendSelectedCommand(PBYTE szBuffer, ULONG ulLength);
// <20><>ʾ<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ // <20><>ʾ<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ

View File

@@ -73,6 +73,7 @@ void CBuildDlg::DoDataExchange(CDataExchange* pDX)
DDX_Control(pDX, IDC_STATIC_OTHER_ITEM, m_OtherItem); DDX_Control(pDX, IDC_STATIC_OTHER_ITEM, m_OtherItem);
DDX_Control(pDX, IDC_COMBO_BITS, m_ComboBits); DDX_Control(pDX, IDC_COMBO_BITS, m_ComboBits);
DDX_Control(pDX, IDC_COMBO_RUNTYPE, m_ComboRunType); DDX_Control(pDX, IDC_COMBO_RUNTYPE, m_ComboRunType);
DDX_Control(pDX, IDC_COMBO_PROTO, m_ComboProto);
} }
@@ -148,6 +149,7 @@ void CBuildDlg::OnBnClickedOk()
CONNECT_ADDRESS g_ConnectAddress = { FLAG_FINDEN, "127.0.0.1", "", typ, false, DLL_VERSION, 0, startup, HeaderEncV1 }; CONNECT_ADDRESS g_ConnectAddress = { FLAG_FINDEN, "127.0.0.1", "", typ, false, DLL_VERSION, 0, startup, HeaderEncV1 };
g_ConnectAddress.SetServer(m_strIP, atoi(m_strPort)); g_ConnectAddress.SetServer(m_strIP, atoi(m_strPort));
g_ConnectAddress.runningType = m_ComboRunType.GetCurSel(); g_ConnectAddress.runningType = m_ComboRunType.GetCurSel();
g_ConnectAddress.protoType = m_ComboProto.GetCurSel();
if (!g_ConnectAddress.IsValid()) { if (!g_ConnectAddress.IsValid()) {
SAFE_DELETE_ARRAY(szBuffer); SAFE_DELETE_ARRAY(szBuffer);
@@ -257,6 +259,10 @@ BOOL CBuildDlg::OnInitDialog()
m_ComboRunType.InsertString(RUNNING_PARALLEL, "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"); m_ComboRunType.InsertString(RUNNING_PARALLEL, "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
m_ComboRunType.SetCurSel(RUNNING_RANDOM); m_ComboRunType.SetCurSel(RUNNING_RANDOM);
m_ComboProto.InsertString(PROTO_TCP, "TCP");
m_ComboProto.InsertString(PROTO_UDP, "UDP");
m_ComboProto.SetCurSel(PROTO_TCP);
m_OtherItem.ShowWindow(SW_HIDE); m_OtherItem.ShowWindow(SW_HIDE);
return TRUE; // return TRUE unless you set the focus to a control return TRUE; // return TRUE unless you set the focus to a control

View File

@@ -33,4 +33,5 @@ public:
CStatic m_OtherItem; CStatic m_OtherItem;
CComboBox m_ComboBits; CComboBox m_ComboBits;
CComboBox m_ComboRunType; CComboBox m_ComboRunType;
CComboBox m_ComboProto;
}; };

View File

@@ -149,6 +149,7 @@ IOCPServer::~IOCPServer(void)
// <20><><EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ. // <20><><EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ.
UINT IOCPServer::StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort) UINT IOCPServer::StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort)
{ {
m_nPort = uPort;
m_NotifyProc = NotifyProc; m_NotifyProc = NotifyProc;
m_OfflineProc = OffProc; m_OfflineProc = OffProc;
m_hKillEvent = CreateEvent(NULL,FALSE,FALSE,NULL); m_hKillEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
@@ -448,6 +449,7 @@ BOOL IOCPServer::OnClientInitializing(PCONTEXT_OBJECT ContextObject, DWORD dwTr
// May be this function should be a member of `CONTEXT_OBJECT`. // May be this function should be a member of `CONTEXT_OBJECT`.
BOOL ParseReceivedData(CONTEXT_OBJECT * ContextObject, DWORD dwTrans, pfnNotifyProc m_NotifyProc) { BOOL ParseReceivedData(CONTEXT_OBJECT * ContextObject, DWORD dwTrans, pfnNotifyProc m_NotifyProc) {
BOOL ret = 1;
try try
{ {
if (dwTrans == 0) //<2F>Է<EFBFBD><D4B7>ر<EFBFBD><D8B1><EFBFBD><EFBFBD>׽<EFBFBD><D7BD><EFBFBD> if (dwTrans == 0) //<2F>Է<EFBFBD><D4B7>ر<EFBFBD><D8B1><EFBFBD><EFBFBD>׽<EFBFBD><D7BD><EFBFBD>
@@ -475,7 +477,8 @@ BOOL ParseReceivedData(CONTEXT_OBJECT * ContextObject, DWORD dwTrans, pfnNotifyP
ULONG ulOriginalLength = 0; ULONG ulOriginalLength = 0;
PBYTE CompressedBuffer = ContextObject->ReadBuffer(ulCompressedLength, ulOriginalLength); PBYTE CompressedBuffer = ContextObject->ReadBuffer(ulCompressedLength, ulOriginalLength);
ContextObject->InDeCompressedBuffer.WriteBuffer(CompressedBuffer, ulCompressedLength); ContextObject->InDeCompressedBuffer.WriteBuffer(CompressedBuffer, ulCompressedLength);
m_NotifyProc(ContextObject); if (m_NotifyProc(ContextObject))
ret = 999;
break; break;
} }
@@ -496,7 +499,8 @@ BOOL ParseReceivedData(CONTEXT_OBJECT * ContextObject, DWORD dwTrans, pfnNotifyP
ContextObject->InDeCompressedBuffer.ClearBuffer(); ContextObject->InDeCompressedBuffer.ClearBuffer();
ContextObject->Decode(CompressedBuffer, ulOriginalLength); ContextObject->Decode(CompressedBuffer, ulOriginalLength);
ContextObject->InDeCompressedBuffer.WriteBuffer(CompressedBuffer, ulOriginalLength); ContextObject->InDeCompressedBuffer.WriteBuffer(CompressedBuffer, ulOriginalLength);
m_NotifyProc(ContextObject); if (m_NotifyProc(ContextObject))
ret = 999;
SAFE_DELETE_ARRAY(CompressedBuffer); SAFE_DELETE_ARRAY(CompressedBuffer);
continue; continue;
} }
@@ -510,7 +514,8 @@ BOOL ParseReceivedData(CONTEXT_OBJECT * ContextObject, DWORD dwTrans, pfnNotifyP
ContextObject->InDeCompressedBuffer.ClearBuffer(); ContextObject->InDeCompressedBuffer.ClearBuffer();
ContextObject->Decode(DeCompressedBuffer, ulOriginalLength); ContextObject->Decode(DeCompressedBuffer, ulOriginalLength);
ContextObject->InDeCompressedBuffer.WriteBuffer(DeCompressedBuffer, ulOriginalLength); ContextObject->InDeCompressedBuffer.WriteBuffer(DeCompressedBuffer, ulOriginalLength);
m_NotifyProc(ContextObject); //֪ͨ<CDA8><D6AA><EFBFBD><EFBFBD> if (m_NotifyProc(ContextObject))
ret = 999;
}else if (usingZstd){ }else if (usingZstd){
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>zlib<69><62>ѹ<EFBFBD><D1B9> // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>zlib<69><62>ѹ<EFBFBD><D1B9>
if (Z_OK == uncompress(DeCompressedBuffer, &ulOriginalLength, CompressedBuffer, ulCompressedLength)) { if (Z_OK == uncompress(DeCompressedBuffer, &ulOriginalLength, CompressedBuffer, ulCompressedLength)) {
@@ -518,7 +523,8 @@ BOOL ParseReceivedData(CONTEXT_OBJECT * ContextObject, DWORD dwTrans, pfnNotifyP
ContextObject->InDeCompressedBuffer.ClearBuffer(); ContextObject->InDeCompressedBuffer.ClearBuffer();
ContextObject->Decode(DeCompressedBuffer, ulOriginalLength); ContextObject->Decode(DeCompressedBuffer, ulOriginalLength);
ContextObject->InDeCompressedBuffer.WriteBuffer(DeCompressedBuffer, ulOriginalLength); ContextObject->InDeCompressedBuffer.WriteBuffer(DeCompressedBuffer, ulOriginalLength);
m_NotifyProc(ContextObject); if (m_NotifyProc(ContextObject))
ret = 999;
} else { } else {
zlibFailed = true; zlibFailed = true;
ContextObject->CompressMethod = COMPRESS_UNKNOWN; ContextObject->CompressMethod = COMPRESS_UNKNOWN;
@@ -542,7 +548,7 @@ BOOL ParseReceivedData(CONTEXT_OBJECT * ContextObject, DWORD dwTrans, pfnNotifyP
ContextObject->InCompressedBuffer.ClearBuffer(); ContextObject->InCompressedBuffer.ClearBuffer();
ContextObject->InDeCompressedBuffer.ClearBuffer(); ContextObject->InDeCompressedBuffer.ClearBuffer();
} }
return TRUE; return ret;
} }
BOOL IOCPServer::OnClientReceiving(PCONTEXT_OBJECT ContextObject, DWORD dwTrans) BOOL IOCPServer::OnClientReceiving(PCONTEXT_OBJECT ContextObject, DWORD dwTrans)

View File

@@ -47,9 +47,8 @@
class IOCPServer : public Server class IOCPServer : public Server
{ {
typedef void (CALLBACK* pfnNotifyProc)(CONTEXT_OBJECT* ContextObject);
typedef void (CALLBACK* pfnOfflineProc)(CONTEXT_OBJECT* ContextObject);
protected: protected:
int m_nPort;
SOCKET m_sListenSocket; SOCKET m_sListenSocket;
HANDLE m_hCompletionPort; HANDLE m_hCompletionPort;
UINT m_ulMaxConnections; UINT m_ulMaxConnections;
@@ -103,6 +102,9 @@ private:
public: public:
IOCPServer(void); IOCPServer(void);
~IOCPServer(void); ~IOCPServer(void);
int GetPort() const override {
return m_nPort;
}
UINT StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort); UINT StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort);

View File

@@ -15,7 +15,7 @@ IOCPUDPServer::~IOCPUDPServer() {
UINT IOCPUDPServer::StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort) { UINT IOCPUDPServer::StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort) {
if (m_running) return 1; if (m_running) return 1;
m_port = uPort;
m_notify = NotifyProc; m_notify = NotifyProc;
m_offline = OffProc; m_offline = OffProc;
@@ -50,14 +50,8 @@ UINT IOCPUDPServer::StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc
void IOCPUDPServer::PostRecv() { void IOCPUDPServer::PostRecv() {
if (!m_running) return; if (!m_running) return;
AddCount(1); IO_CONTEXT* ioCtx = AddCount();
CONTEXT_UDP* ctx = new CONTEXT_UDP(); CONTEXT_UDP* ctx = ioCtx->pContext;
ctx->InitMember(m_socket, this);
IO_CONTEXT* ioCtx = new IO_CONTEXT();
ZeroMemory(&ioCtx->ol, sizeof(OVERLAPPED));
ioCtx->pContext = ctx;
ctx->wsaInBuf.buf = ctx->szBuffer; ctx->wsaInBuf.buf = ctx->szBuffer;
ctx->wsaInBuf.len = sizeof(ctx->szBuffer); ctx->wsaInBuf.len = sizeof(ctx->szBuffer);
@@ -78,8 +72,7 @@ void IOCPUDPServer::PostRecv() {
DWORD err = WSAGetLastError(); DWORD err = WSAGetLastError();
Mprintf("[IOCP] PostRecv error: %d\n", err); Mprintf("[IOCP] PostRecv error: %d\n", err);
delete ioCtx; delete ioCtx;
delete ctx; DelCount();
AddCount(-1);
} }
} }
@@ -95,9 +88,8 @@ void IOCPUDPServer::WorkerThread() {
Mprintf("[IOCP] PostRecv error: %d\n", err); Mprintf("[IOCP] PostRecv error: %d\n", err);
if (pOverlapped) { if (pOverlapped) {
IO_CONTEXT* ioCtx = CONTAINING_RECORD(pOverlapped, IO_CONTEXT, ol); IO_CONTEXT* ioCtx = CONTAINING_RECORD(pOverlapped, IO_CONTEXT, ol);
delete ioCtx->pContext;
delete ioCtx; delete ioCtx;
AddCount(-1); DelCount();
} }
continue; continue;
} }
@@ -105,14 +97,19 @@ void IOCPUDPServer::WorkerThread() {
IO_CONTEXT* ioCtx = CONTAINING_RECORD(pOverlapped, IO_CONTEXT, ol); IO_CONTEXT* ioCtx = CONTAINING_RECORD(pOverlapped, IO_CONTEXT, ol);
CONTEXT_UDP* ctx = ioCtx->pContext; CONTEXT_UDP* ctx = ioCtx->pContext;
ParseReceivedData(ctx, bytes, m_notify); BOOL ret = ParseReceivedData(ctx, bytes, m_notify);
if (999 != ret)
ctx->Destroy();
// <20>ͷ<EFBFBD> // <20>ͷ<EFBFBD>
ioCtx->pContext = NULL;
delete ioCtx; delete ioCtx;
AddCount(-1); DelCount();
PostRecv(); // <20><><EFBFBD><EFBFBD><EFBFBD> PostRecv(); // <20><><EFBFBD><EFBFBD><EFBFBD>
} }
CloseHandle(m_hThread);
m_hThread = NULL;
} }
VOID IOCPUDPServer::Send2Client(CONTEXT_OBJECT* ContextObject, PBYTE szBuffer, ULONG ulOriginalLength) { VOID IOCPUDPServer::Send2Client(CONTEXT_OBJECT* ContextObject, PBYTE szBuffer, ULONG ulOriginalLength) {
@@ -157,8 +154,8 @@ VOID IOCPUDPServer::Destroy() {
if (m_hThread) { if (m_hThread) {
WaitForSingleObject(m_hThread, INFINITE); WaitForSingleObject(m_hThread, INFINITE);
CloseHandle(m_hThread); while (m_hThread)
m_hThread = NULL; Sleep(200);
} }
if (m_hIOCP) { if (m_hIOCP) {

View File

@@ -6,10 +6,21 @@
class IOCPUDPServer : public Server { class IOCPUDPServer : public Server {
struct IO_CONTEXT {
OVERLAPPED ol;
CONTEXT_UDP* pContext;
IO_CONTEXT(CONTEXT_UDP* ctx) : ol({}), pContext(ctx) { }
~IO_CONTEXT() {
SAFE_DELETE(pContext);
}
};
public: public:
IOCPUDPServer(); IOCPUDPServer();
~IOCPUDPServer(); ~IOCPUDPServer();
int GetPort() const override {
return m_port;
}
UINT StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort) override; UINT StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort) override;
VOID Send2Client(CONTEXT_OBJECT* ContextObject, PBYTE szBuffer, ULONG ulOriginalLength) override; VOID Send2Client(CONTEXT_OBJECT* ContextObject, PBYTE szBuffer, ULONG ulOriginalLength) override;
VOID Destroy() override; VOID Destroy() override;
@@ -17,9 +28,17 @@ public:
private: private:
void WorkerThread(); void WorkerThread();
void PostRecv(); void PostRecv();
void AddCount(int n=1){ IO_CONTEXT* AddCount(){
m_locker.lock(); m_locker.lock();
m_count += n; IO_CONTEXT* ioCtx = new IO_CONTEXT(new CONTEXT_UDP());
ioCtx->pContext->InitMember(m_socket, this);
m_count++;
m_locker.unlock();
return ioCtx;
}
void DelCount() {
m_locker.lock();
m_count--;
m_locker.unlock(); m_locker.unlock();
} }
int GetCount() { int GetCount() {
@@ -29,6 +48,7 @@ private:
return n; return n;
} }
private: private:
int m_port = 6543;
int m_count = 0; int m_count = 0;
CLocker m_locker; CLocker m_locker;
SOCKET m_socket = INVALID_SOCKET; SOCKET m_socket = INVALID_SOCKET;
@@ -38,9 +58,4 @@ private:
pfnNotifyProc m_notify = nullptr; pfnNotifyProc m_notify = nullptr;
pfnOfflineProc m_offline = nullptr; pfnOfflineProc m_offline = nullptr;
struct IO_CONTEXT {
OVERLAPPED ol;
CONTEXT_UDP* pContext;
};
}; };

View File

@@ -64,7 +64,12 @@ class HeaderParser {
friend class CONTEXT_OBJECT; friend class CONTEXT_OBJECT;
protected: protected:
HeaderParser() { HeaderParser() {
memset(this, 0, sizeof(HeaderParser)); m_Encoder = nullptr;
m_Encoder2 = nullptr;
m_bParsed = FALSE;
m_nHeaderLen = m_nCompareLen = m_nFlagLen = 0;
m_nFlagType = FLAG_UNKNOWN;
memset(m_szPacketFlag, 0, sizeof(m_szPacketFlag));
} }
virtual ~HeaderParser() { virtual ~HeaderParser() {
Reset(); Reset();
@@ -112,6 +117,8 @@ protected:
m_nFlagLen = m_nCompareLen; m_nFlagLen = m_nCompareLen;
m_nHeaderLen = m_nFlagLen + 8; m_nHeaderLen = m_nFlagLen + 8;
m_bParsed = TRUE; m_bParsed = TRUE;
assert(NULL==m_Encoder);
assert(NULL==m_Encoder2);
m_Encoder = new Encoder(); m_Encoder = new Encoder();
m_Encoder2 = new Encoder(); m_Encoder2 = new Encoder();
break; break;
@@ -156,7 +163,10 @@ protected:
HeaderParser& Reset() { HeaderParser& Reset() {
SAFE_DELETE(m_Encoder); SAFE_DELETE(m_Encoder);
SAFE_DELETE(m_Encoder2); SAFE_DELETE(m_Encoder2);
memset(this, 0, sizeof(HeaderParser)); m_bParsed = FALSE;
m_nHeaderLen = m_nCompareLen = m_nFlagLen = 0;
m_nFlagType = FLAG_UNKNOWN;
memset(m_szPacketFlag, 0, sizeof(m_szPacketFlag));
return *this; return *this;
} }
BOOL IsParsed() const { BOOL IsParsed() const {
@@ -187,8 +197,8 @@ private:
int m_nFlagLen; // <20><>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD> int m_nFlagLen; // <20><>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD>
FlagType m_nFlagType; // <20><>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD> FlagType m_nFlagType; // <20><>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD>
char m_szPacketFlag[32]; // <20>Ա<EFBFBD><D4B1><EFBFBD>Ϣ char m_szPacketFlag[32]; // <20>Ա<EFBFBD><D4B1><EFBFBD>Ϣ
Encoder* m_Encoder; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Encoder* m_Encoder; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Encoder* m_Encoder2; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2 Encoder* m_Encoder2; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2
}; };
enum IOType enum IOType
@@ -229,8 +239,8 @@ public:
} }
}; };
typedef void (CALLBACK* pfnNotifyProc)(CONTEXT_OBJECT* ContextObject); typedef BOOL (CALLBACK* pfnNotifyProc)(CONTEXT_OBJECT* ContextObject);
typedef void (CALLBACK* pfnOfflineProc)(CONTEXT_OBJECT* ContextObject); typedef BOOL (CALLBACK* pfnOfflineProc)(CONTEXT_OBJECT* ContextObject);
class Server class Server
{ {
@@ -240,6 +250,8 @@ public:
Server() {} Server() {}
virtual ~Server() {} virtual ~Server() {}
virtual int GetPort() const = 0;
virtual UINT StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort) = 0; virtual UINT StartServer(pfnNotifyProc NotifyProc, pfnOfflineProc OffProc, USHORT uPort) = 0;
virtual void Send2Client(CONTEXT_OBJECT* ContextObject, PBYTE szBuffer, ULONG ulOriginalLength) = 0; virtual void Send2Client(CONTEXT_OBJECT* ContextObject, PBYTE szBuffer, ULONG ulOriginalLength) = 0;
@@ -261,6 +273,8 @@ public:
virtual uint64_t GetClientID() const = 0; virtual uint64_t GetClientID() const = 0;
virtual std::string GetPeerName() const = 0; virtual std::string GetPeerName() const = 0;
virtual int GetPort() const = 0; virtual int GetPort() const = 0;
virtual std::string GetProtocol() const = 0;
virtual int GetServerPort() const = 0;
public: public:
virtual ~context() {} virtual ~context() {}
@@ -298,6 +312,12 @@ public:
std::string PeerName; // <20>Զ<EFBFBD>IP std::string PeerName; // <20>Զ<EFBFBD>IP
Server* server; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Server* server; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
std::string GetProtocol() const override {
return "TCP";
}
int GetServerPort() const override {
return server->GetPort();
}
VOID InitMember(SOCKET s, Server *svr) VOID InitMember(SOCKET s, Server *svr)
{ {
memset(szBuffer, 0, sizeof(char) * PACKET_LENGTH); memset(szBuffer, 0, sizeof(char) * PACKET_LENGTH);
@@ -462,6 +482,9 @@ public:
int addrLen; int addrLen;
sockaddr_in clientAddr; sockaddr_in clientAddr;
std::string GetProtocol() const override {
return "UDP";
}
VOID InitMember(SOCKET s, Server* svr) override { VOID InitMember(SOCKET s, Server* svr) override {
CONTEXT_OBJECT::InitMember(s, svr); CONTEXT_OBJECT::InitMember(s, svr);
clientAddr = {}; clientAddr = {};

Binary file not shown.