-
Notifications
You must be signed in to change notification settings - Fork 14
/
TextIOCPServer.cpp
163 lines (127 loc) · 3.88 KB
/
TextIOCPServer.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "IOCPCommon.h"
#include "IOCPBuffer.h"
#include "IOCPBufferWriter.h"
#include "IOCPBufferReader.h"
#include "BaseIOCPServer.h"
#include "TextIOCPServer.h"
UserBufferObject::UserBufferObject()
{
m_nRefs = 0;
}
VOID UserBufferList::AllocateBufferObject(LONGLONG guid)
{
UserBufferObject* object = new UserBufferObject();
object->m_ulGuid = guid;
object->m_nRefs++;
m_stBufferLock.Lock();
m_vBufferMap[guid] = object;
m_stBufferLock.UnLock();
}
VOID UserBufferList::ReleaseBufferObject(LONGLONG guid)
{
UserBufferObject* object = NULL;
std::map <LONGLONG,UserBufferObject*>::iterator iter;
m_stBufferLock.Lock();
iter = m_vBufferMap.find(guid);
if(iter != m_vBufferMap.end()){
object = iter->second;
}
m_stBufferLock.UnLock();
if(object){
ReleaseBufferObject(object);
}
}
VOID UserBufferList::ReleaseBufferObject(UserBufferObject* object)
{
std::map <LONGLONG,UserBufferObject*>::iterator iter;
if(InterlockedDecrement(&object->m_nRefs) <= 0){
m_stBufferLock.Lock();
iter = m_vBufferMap.find(object->m_ulGuid);
if(iter != m_vBufferMap.end()){
m_vBufferMap.erase(iter);
}
m_stBufferLock.UnLock();
delete object;
}
}
UserBufferObject* UserBufferList::GetBufferObject(LONGLONG guid)
{
UserBufferObject* object = NULL;
m_stBufferLock.Lock();
object = m_vBufferMap[guid];
if(object){
InterlockedIncrement(&object->m_nRefs);
}
m_stBufferLock.UnLock();
return object;
}
CTextIOCPServer::CTextIOCPServer(void)
{
}
CTextIOCPServer::~CTextIOCPServer(void)
{
}
DWORD CTextIOCPServer::GetStringLen(CONST BYTE* lpBuffer,DWORD dwBufferSize)
{
for(DWORD i=0;i<dwBufferSize;i++)
{
if(lpBuffer[i] == 0){
return (i + 1);
}
}
return -1;
}
VOID CTextIOCPServer::NotifyNewConnection(PPER_SOCKET_CONTEXT lpPerSocketContext)
{
m_stUserBufferMap.AllocateBufferObject(lpPerSocketContext->m_guid);
}
VOID CTextIOCPServer::NotifyDisconnectedClient(PPER_SOCKET_CONTEXT lpPerSocketContext)
{
m_stUserBufferMap.ReleaseBufferObject(lpPerSocketContext->m_guid);
}
VOID CTextIOCPServer::NotifyReceivedPackage(PPER_SOCKET_CONTEXT lpPerSocketContext, CIOCPBuffer* pBuffer)
{
UserBufferObject* object = m_stUserBufferMap.GetBufferObject(lpPerSocketContext->m_guid);
if(object){
DWORD dwNewBufferLength = pBuffer->GetLength() + object->m_stBufferWriter.GetBuffer()->GetLength();
if(dwNewBufferLength > 0x20000){ //´óÓÚ128KBµÄÎı¾¾ÍÌß³öÓû§
CloseClient(lpPerSocketContext);
m_stUserBufferMap.ReleaseBufferObject(object);
return;
}
object->m_stBufferWriter.Write(pBuffer);
DWORD dwBufferLength = object->m_stBufferWriter.GetBuffer()->GetLength();
BYTE* lpBuffer = object->m_stBufferWriter.GetBuffer()->GetBytes();
BYTE* lpStartPos = lpBuffer;
DWORD dwReadingLength = 0;
DWORD dwLastLength = dwBufferLength;
DWORD dwStringLength = GetStringLen(lpStartPos,dwLastLength);
if(dwStringLength != -1){
do{
char* lpszText = new char[dwStringLength];
strncpy_s(lpszText,dwStringLength,(const char*)lpStartPos,dwStringLength);
NotifyReceivedFormatPackage(lpPerSocketContext,lpszText);
delete [] lpszText;
dwReadingLength += dwStringLength;
lpStartPos += dwStringLength;
dwLastLength -= dwStringLength;
dwStringLength = GetStringLen(lpStartPos,dwLastLength);
}while(dwStringLength != -1);
}
if(dwReadingLength > 0){
memcpy((BYTE*)object->m_stBufferWriter.GetBuffer()->GetBytes(),
&object->m_stBufferWriter.GetBuffer()->GetBytes()[dwReadingLength],dwLastLength);
object->m_stBufferWriter.GetBuffer()->m_pData->m_dwDataLength = dwLastLength;
}
m_stUserBufferMap.ReleaseBufferObject(object);
}
}
VOID CTextIOCPServer::SendEx(PPER_SOCKET_CONTEXT lpPerSocketContext,LPCSTR lpszText)
{
CIOCPBufferWriter Writer;
Writer.Write((BYTE*)lpszText,strlen(lpszText) + sizeof(char));
Send(lpPerSocketContext,Writer.GetBuffer());
}
VOID CTextIOCPServer::NotifyReceivedFormatPackage(PPER_SOCKET_CONTEXT lpPerSocketContext, LPCSTR lpszText)
{
}