-
Notifications
You must be signed in to change notification settings - Fork 46
/
SocketClientImpl.h
248 lines (218 loc) · 6.44 KB
/
SocketClientImpl.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#pragma once
#pragma warning(push)
#pragma warning(disable:4995)
#include <windows.h>
#include <vector>
#include <crtdbg.h>
#pragma warning(pop)
#include "SocketHandle.h"
class ISocketClientHandler
{
public:
virtual void OnThreadBegin(CSocketHandle* ) {}
virtual void OnThreadExit(CSocketHandle* ) {}
virtual void OnDataReceived(CSocketHandle* , const BYTE* , DWORD , const SockAddrIn& ) {}
virtual void OnConnectionDropped(CSocketHandle* ) {}
virtual void OnConnectionError(CSocketHandle* , DWORD ) {}
};
template <typename T, size_t tBufferSize = 1440>
class SocketClientImpl
{
typedef SocketClientImpl<T, tBufferSize> thisClass;
public:
SocketClientImpl()
: _pInterface(0)
, _thread(0)
{
}
void SetInterface(T* pInterface)
{
InterlockedExchangePointer(reinterpret_cast<void**>(&_pInterface), pInterface);
}
operator CSocketHandle*() throw()
{
return( &_socket );
}
CSocketHandle* operator->() throw()
{
return( &_socket );
}
HANDLE GetHandle() const
{
return _thread;
}
bool IsOpen() const
{
return _socket.IsOpen();
}
bool CreateSocket(LPCTSTR pszHost, LPCTSTR pszServiceName, int nFamily, int nType, UINT uOptions = 0)
{
return _socket.CreateSocket(pszHost, pszServiceName, nFamily, nType, uOptions);
}
bool ConnectTo(LPCTSTR pszHostName, LPCTSTR pszRemote, LPCTSTR pszServiceName, int nFamily, int nType)
{
return _socket.ConnectTo(pszHostName, pszRemote, pszServiceName, nFamily, nType);
}
void Close()
{
_socket.Close();
}
DWORD Read(LPBYTE lpBuffer, DWORD dwSize, LPSOCKADDR lpAddrIn = NULL, DWORD dwTimeout = INFINITE)
{
return _socket.Read(lpBuffer, dwSize, lpAddrIn, dwTimeout);
}
DWORD Write(const LPBYTE lpBuffer, DWORD dwCount, const LPSOCKADDR lpAddrIn = NULL, DWORD dwTimeout = INFINITE)
{
return _socket.Write(lpBuffer, dwCount, lpAddrIn, dwTimeout);
}
bool StartClient(LPCTSTR pszHost, LPCTSTR pszRemote, LPCTSTR pszServiceName, int nFamily, int nType, UINT uOptions = 0);
void Terminate(DWORD dwTimeout = INFINITE);
static bool IsConnectionDropped(DWORD dwError);
protected:
void Run();
static DWORD WINAPI SocketClientProc(thisClass* _this);
T* _pInterface;
HANDLE _thread;
CSocketHandle _socket;
};
template <typename T, size_t tBufferSize>
bool SocketClientImpl<T, tBufferSize>::StartClient(LPCTSTR pszHost, LPCTSTR pszRemote, LPCTSTR pszServiceName, int nFamily, int nType, UINT uOptions)
{
// must be closed first...
if ( IsOpen() ) return false;
bool result = false;
if ( nType == SOCK_STREAM )
{
result = _socket.ConnectTo(pszHost, pszRemote, pszServiceName, nFamily, nType); // TCP
}
else
{
// let provider select a port for us
result = _socket.CreateSocket(pszHost, TEXT("0"), nFamily, nType, uOptions); // UDP
}
if ( result )
{
_thread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)SocketClientProc, this,NULL,NULL);
if ( _thread == NULL )
{
DWORD dwError = GetLastError();
_socket.Close();
SetLastError(dwError);
result = false;
}
}
return result;
}
template <typename T, size_t tBufferSize>
void SocketClientImpl<T, tBufferSize>::Run()
{
SockAddrIn addrIn;
std::vector<unsigned char> data;
data.resize( tBufferSize );
DWORD dwRead;
DWORD dwError;
_ASSERTE( _pInterface != NULL && "Need an interface to pass events");
// Notification: OnThreadBegin
if ( _pInterface != NULL ) {
_pInterface->OnThreadBegin(*this);
}
int type = _socket.GetSocketType();
if (type == SOCK_DGRAM) {
_socket.GetPeerName( addrIn );
}
while ( _socket.IsOpen() )
{
if (type == SOCK_STREAM)
{
dwRead = _socket.Read(&data[0], tBufferSize, NULL, INFINITE);
}
else
{
dwRead = _socket.Read(&data[0], tBufferSize, addrIn, INFINITE);
}
if ( (dwRead != -1L) && (dwRead > 0))
{
// Notification: OnDataReceived
if ( _pInterface != NULL ) {
_pInterface->OnDataReceived(*this, &data[0], dwRead, addrIn);
}
}
else if (type == SOCK_STREAM && dwRead == 0L )
{
// connection broken
if ( _pInterface != NULL )
{
_pInterface->OnConnectionDropped(*this);
}
break;
}
else if ( dwRead == -1L )
{
dwError = GetLastError();
if ( _pInterface != NULL )
{
if (IsConnectionDropped( dwError) ) {
// Notification: OnConnectionDropped
if (type == SOCK_STREAM || (dwError == WSAENOTSOCK || dwError == WSAENETDOWN))
{
_pInterface->OnConnectionDropped(*this);
break;
}
}
// Notification: OnConnectionError
_pInterface->OnConnectionError(*this, dwError);
}
else {
break;
}
}
}
data.clear();
// Notification: OnThreadExit
if ( _pInterface != NULL ) {
_pInterface->OnThreadExit(*this);
}
}
template <typename T, size_t tBufferSize>
void SocketClientImpl<T, tBufferSize>::Terminate(DWORD dwTimeout /*= INFINITE*/)
{
_socket.Close();
if ( _thread != NULL )
{
if ( WaitForSingleObject(_thread, dwTimeout) == WAIT_TIMEOUT ) {
TerminateThread(_thread, 1);
}
CloseHandle(_thread);
_thread = NULL;
}
}
template <typename T, size_t tBufferSize>
DWORD WINAPI SocketClientImpl<T, tBufferSize>::SocketClientProc(thisClass* _this)
{
if ( _this != NULL )
{
_this->Run();
}
return 0;
}
template <typename T, size_t tBufferSize>
bool SocketClientImpl<T, tBufferSize>::IsConnectionDropped(DWORD dwError)
{
// see: winerror.h for definition
switch( dwError )
{
case WSAENOTSOCK:
case WSAENETDOWN:
case WSAENETUNREACH:
case WSAENETRESET:
case WSAECONNABORTED:
case WSAECONNRESET:
case WSAESHUTDOWN:
case WSAEHOSTDOWN:
case WSAEHOSTUNREACH:
return true;
default:
break;
}
return false;
}