fix: Limit the online host number which uses UDP

This commit is contained in:
yuanyuanxiang
2025-07-20 01:18:16 +08:00
parent 1b1ce090c8
commit 07f6c92306
2 changed files with 15 additions and 0 deletions

View File

@@ -51,6 +51,11 @@ void IOCPUDPServer::PostRecv() {
if (!m_running) return; if (!m_running) return;
IO_CONTEXT* ioCtx = AddCount(); IO_CONTEXT* ioCtx = AddCount();
if (ioCtx == nullptr) {
Mprintf("IOCPUDPServer max connection number reached.\n");
return;
}
CONTEXT_UDP* ctx = ioCtx->pContext; CONTEXT_UDP* ctx = ioCtx->pContext;
ctx->wsaInBuf.buf = ctx->szBuffer; ctx->wsaInBuf.buf = ctx->szBuffer;
ctx->wsaInBuf.len = sizeof(ctx->szBuffer); ctx->wsaInBuf.len = sizeof(ctx->szBuffer);

View File

@@ -25,12 +25,21 @@ public:
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;
virtual void UpdateMaxConnection(int maxConn) override {
m_locker.lock();
m_maxConn = maxConn;
m_locker.unlock();
}
private: private:
void WorkerThread(); void WorkerThread();
void PostRecv(); void PostRecv();
IO_CONTEXT* AddCount(){ IO_CONTEXT* AddCount(){
m_locker.lock(); m_locker.lock();
if (m_count > m_maxConn) {
m_locker.unlock();
return nullptr;
}
IO_CONTEXT* ioCtx = new IO_CONTEXT(); IO_CONTEXT* ioCtx = new IO_CONTEXT();
ioCtx->pContext->InitMember(m_socket, this); ioCtx->pContext->InitMember(m_socket, this);
m_count++; m_count++;
@@ -49,6 +58,7 @@ private:
return n; return n;
} }
private: private:
int m_maxConn = 10000;
int m_port = 6543; int m_port = 6543;
int m_count = 0; int m_count = 0;
CLocker m_locker; CLocker m_locker;