SDC C-Project CF Review 프로그램
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// IOCP.cpp: implementation of the CMemoryPool class.
//
//////////////////////////////////////////////////////////////////////
 
#include "stdafx.h"
#include "MemoryPool.h"
 
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
 
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
 
CMemoryPool::CMemoryPool()
{
    m_ListenSocket = NULL;
}
 
CMemoryPool::~CMemoryPool()
{
    if(m_ListenSocket != NULL)
    {
        closesocket(m_ListenSocket);
        m_ListenSocket = NULL;
    }
}
 
/********************************************************************************/
// Á¢¼Ó¿¡ ´ëÇÑ ¸Þ¸ð¸®¸¦ ÇÒ´çÇÏ°í ¿¬°á
/********************************************************************************/
pPerSocketContext CMemoryPool::AllocPerSocketContext(SOCKET clientSocket)
{
    pPerSocketContext pPerSocketCtx = NULL;
 
    if (m_pPerSocketCtxMemPool->GetAllocCount() == m_pPerSocketCtxMemPool->GetBlockCount())
        return NULL;
 
    pPerSocketCtx = m_pPerSocketCtxMemPool->Alloc();
 
    if (!pPerSocketCtx)
        return NULL;
 
    pPerSocketCtx->socket = clientSocket;
    pPerSocketCtx->recvContext = AllocPerIoContextForRecv();
    pPerSocketCtx->sendContext = AllocPerIoContextForSend();
    pPerSocketCtx->recvContext->ReleaseBuffer();
    pPerSocketCtx->sendContext->ReleaseBuffer();
 
    return pPerSocketCtx;
}
 
int CMemoryPool::GetSocketContextAllocCount()
{
    return m_pPerSocketCtxMemPool->GetAllocCount();
}
 
// Per Io Context ¸Þ¸ð¸® ÇÒ´ç
pPerIoContext CMemoryPool::AllocPerIoContextForSend(void)
{
    pPerIoContext pPerSendIoCtx = NULL;
 
    pPerSendIoCtx = m_pSendMemPool->Alloc();
 
    return pPerSendIoCtx;
}
 
// Per Socket Context ¸Þ¸ð¸® ÇÒ´ç
pPerIoContext CMemoryPool::AllocPerIoContextForRecv(void)
{
    pPerIoContext pPerRecvIoCtx = NULL;
 
    pPerRecvIoCtx = m_pRecvMemPool->Alloc();
 
    return pPerRecvIoCtx;
}
 
// PerSocketContext ¸Þ¸ð¸® Á¦°Å
void CMemoryPool::DeallocPerSocketContext(pPerSocketContext pPerSocketCtx)
{
    assert(pPerSocketCtx);
 
    // IO Context ÇÒ´çÇÑ °Í Á¦°Å
    pPerSocketCtx->recvContext->ReleaseBuffer();
    pPerSocketCtx->sendContext->ReleaseBuffer();
    DeallocPerIoContextForRecv(pPerSocketCtx->recvContext);
    DeallocPerIoContextForSend(pPerSocketCtx->sendContext);
 
    // ¼ÒÄÏ ÄÁÅØ½ºÆ® Á¦°Å 
    BOOL bRet=m_pPerSocketCtxMemPool->Free(pPerSocketCtx);
    assert(bRet);
}
 
void CMemoryPool::DeallocPerIoContextForSend(pPerIoContext pPerSendIoCtx)
{
    assert(pPerSendIoCtx);
    BOOL bRet = m_pSendMemPool->Free(pPerSendIoCtx);
    assert(bRet);
    
}
 
// PerIoContext ¸Þ¸ð¸® Á¦°Å
void CMemoryPool::DeallocPerIoContextForRecv(pPerIoContext pPerRecvIoCtx)
{
    assert(pPerRecvIoCtx);
    BOOL bRet = m_pRecvMemPool->Free(pPerRecvIoCtx);
    assert(bRet);
}