-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBaseIOCPServer.h
134 lines (108 loc) · 4.18 KB
/
BaseIOCPServer.h
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
#pragma once
typedef enum _IO_OPERATION {
ClientIoAccept,
ClientIoRead,
ClientIoWrite
} IO_OPERATION, *PIO_OPERATION;
typedef enum _IO_POST_RESULT {
PostIoSuccess,
PostIoInvalicSocket,
PostIoFailed
}IO_POST_RESULT,*PIO_POST_RESULT;
typedef struct _PER_IO_CONTEXT {
WSAOVERLAPPED Overlapped;
WSABUF wsabuf;
IO_OPERATION IOOperation;
SOCKET SocketAccept;
CIOCPBuffer* IOCPBuffer;
CIOCPBufferReader* BufferReader;
_PER_IO_CONTEXT();
} PER_IO_CONTEXT, *PPER_IO_CONTEXT;
class _PER_SOCKET_CONTEXT
{
public:
SOCKET m_Socket;
IOCPMutex m_Lock;
LONGLONG m_guid;
ULONG m_NumberOfPendingIO;
PER_IO_CONTEXT m_SendContext;
PER_IO_CONTEXT m_RecvContext;
IOCPQueue<CIOCPBuffer*> m_SendBufferList;
//std::list<_PER_SOCKET_CONTEXT*>::iterator pos;
_PER_SOCKET_CONTEXT();
virtual ~_PER_SOCKET_CONTEXT();
};
typedef _PER_SOCKET_CONTEXT PER_SOCKET_CONTEXT;
typedef PER_SOCKET_CONTEXT* PPER_SOCKET_CONTEXT;
extern "C"
{
#include "table.h"
};
class CBaseIOCPServer;
class _PER_SOCKET_CONTEXT_LIST
{
friend class CBaseIOCPServer;
public:
_PER_SOCKET_CONTEXT_LIST();
virtual ~_PER_SOCKET_CONTEXT_LIST();
IOCPMutex m_ContextLock;
//std::map<LONGLONG,PPER_SOCKET_CONTEXT> m_vContextMap;
//std::list<PPER_SOCKET_CONTEXT> m_vContextList;
void ClearAll();
void AddContext(PPER_SOCKET_CONTEXT lpPerSocketContext);
void DeleteContext(PPER_SOCKET_CONTEXT lpPerSocketContext);
PPER_SOCKET_CONTEXT GetContext(LONGLONG guid); //使用这个函数需要手动锁定 ***且不会增加引用计数器***
private:
struct pt_table *m_table;
};
typedef _PER_SOCKET_CONTEXT_LIST PER_SOCKET_CONTEXT_LIST;
typedef PER_SOCKET_CONTEXT_LIST* PPER_SOCKET_CONTEXT_LIST;
class CBaseIOCPServer
{
private:
WSADATA m_WSAData;
LPFN_ACCEPTEX m_lpfnAcceptEx;
SOCKET m_sdListen;
HANDLE m_hIOCP;
HANDLE* m_pvThreadHandles;
DWORD m_nThreadHandleCount;
BOOL m_bIsShutdown;
ULONG m_nCurrentConnectCount;
ULONG m_nLimitConnectCount;
PER_SOCKET_CONTEXT_LIST m_vContextList;
std::vector <PPER_IO_CONTEXT> m_vAcceptIOContext;
private:
SOCKET CreateSocket(void);
BOOL CreateListenSocket(USHORT nPort);
BOOL CreateAcceptSocket(PPER_IO_CONTEXT lpPerIOContext = NULL);
FARPROC GetExtensionProcAddress(GUID& Guid);
VOID UpdateCompletionPort(SOCKET hSocket, DWORD_PTR lpCompletionKey = NULL);
VOID UpdateSocket(SOCKET sd);
PPER_SOCKET_CONTEXT AllocateSocketContext();
VOID ReleaseSocketContext(PPER_SOCKET_CONTEXT lpPerSocketContext);
ULONG EnterIOLoop(PPER_SOCKET_CONTEXT lpPerSocketContext);
ULONG ExitIOLoop(PPER_SOCKET_CONTEXT lpPerSocketContext);
VOID PostClientIoRead(PPER_SOCKET_CONTEXT lpPerSocketContext,PPER_IO_CONTEXT lpPerIOContext,IO_POST_RESULT& PostResult);
VOID PostClientIoWrite(PPER_SOCKET_CONTEXT lpPerSocketContext,PPER_IO_CONTEXT lpPerIOContext,IO_POST_RESULT& PostResult);
VOID OnClientIoAccept(PPER_SOCKET_CONTEXT lpPerSocketContext,PPER_IO_CONTEXT lpPerIOContext,DWORD dwIoSize);
VOID OnClientIoRead(PPER_SOCKET_CONTEXT lpPerSocketContext,PPER_IO_CONTEXT lpPerIOContext,DWORD dwIoSize);
VOID OnClientIoWrite(PPER_SOCKET_CONTEXT lpPerSocketContext,PPER_IO_CONTEXT lpPerIOContext,DWORD dwIoSize);
static DWORD WINAPI WorkerThread(LPVOID Param);
public:
VOID CloseClient (PPER_SOCKET_CONTEXT lpPerSocketContext);
public:
BOOL Startup(USHORT nPort,DWORD dwWorkerThreadCount,DWORD dwMaxConnection);
BOOL Shutdown();
BOOL Send(PPER_SOCKET_CONTEXT lpPerSocketContext,CIOCPBuffer* lpIOCPBuffer);
BOOL Send(LONGLONG guid,CIOCPBuffer* lpIOCPBuffer);
CBaseIOCPServer(void);
~CBaseIOCPServer(void);
protected:
static void OnPtTableCloseCallback(struct pt_table* ptable, uint64_t id, void *ptr, void* user_arg);
virtual VOID NotifyNewConnection(PPER_SOCKET_CONTEXT lpPerSocketContext);
virtual VOID NotifyDisconnectedClient(PPER_SOCKET_CONTEXT lpPerSocketContext);
virtual VOID NotifyWriteCompleted(PPER_SOCKET_CONTEXT lpPerSocketContext, CIOCPBuffer* lpIOCPBuffer);
virtual VOID NotifyWritePackage(PPER_SOCKET_CONTEXT lpPerSocketContext, CIOCPBuffer* lpIOCPBuffer);
virtual VOID NotifyReceivedPackage(PPER_SOCKET_CONTEXT lpPerSocketContext, CIOCPBuffer* pBuffer);
};
int myprintf(const char *lpFormat, ...);