-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWinAsyncSocket.cpp
More file actions
277 lines (213 loc) · 7.19 KB
/
WinAsyncSocket.cpp
File metadata and controls
277 lines (213 loc) · 7.19 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*******************************************************************
// Copyright (c) 2000, Robert Umbehant
// mailto:rumbehant@wheresjames.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307 USA
//
*******************************************************************/
// WinAsyncSocket.cpp: implementation of the CWinAsyncSocket class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#ifdef ENABLE_SOCKETS2
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CWinAsyncSocket::CWinAsyncSocket()
{
// Socket is not connected
m_uStatus = eStatusDisconnected;
}
CWinAsyncSocket::~CWinAsyncSocket()
{
// Stop any running thread
Stop();
}
BOOL CWinAsyncSocket::InitThread( LPVOID pData )
{
return TRUE;
}
BOOL CWinAsyncSocket::DoThread( LPVOID pData )
{
HANDLE phEvents[ 4 ];
// Get events
phEvents[ 0 ] = m_hStop;
phEvents[ 1 ] = m_fifo.GetDataReadyHandle();
phEvents[ 2 ] = m_ws.GetEventHandle();
phEvents[ 3 ] = IsConnected() ? GetDataReadyHandle() : NULL;
// How many valid handles
UINT uHandles = !phEvents[ 2 ] ? 2 : !phEvents[ 3 ] ? 3 : 4;
// Wait forever
UINT uRet = WaitForMultipleObjects( uHandles, phEvents, FALSE, INFINITE );
// Stop command?
if ( uRet == WAIT_OBJECT_0 ) return FALSE;
// Command?
else if ( uRet == WAIT_OBJECT_0 + 1 )
// Process the command
OnCommand();
// Write data waiting to go out?
else if ( uRet == WAIT_OBJECT_0 + 2 )
// Process socket event
OnSocketEvent();
// Write data waiting to go out?
else if ( uRet == WAIT_OBJECT_0 + 3 )
// Process socket event
if ( IsConnected() )
OnDataReady();
// Wait connection
else Sleep( 15 );
return TRUE;
}
BOOL CWinAsyncSocket::EndThread( LPVOID pData )
{
m_ws.Destroy();
return TRUE;
}
BOOL CWinAsyncSocket::OnCommand()
{
// How large is the buffer?
DWORD dwCmdLen = 0;
if ( !m_fifo.Read( NULL, 0, &dwCmdLen ) )
return FALSE;
// Allocate space for the command and read it in
TMem< BYTE > mem;
if ( !mem.allocate( dwCmdLen ) ||
!m_fifo.Read( mem.ptr(), dwCmdLen, &dwCmdLen ) )
return FALSE;
// Get a pointer to the command structure
SCmdData *pCd = (SCmdData*)mem.ptr();
char *pExtra = (char*)mem.ptr( sizeof( SCmdData ) );
switch( pCd->uCmd )
{
case eCmdOpen :
// Attempt connection
m_ws.Connect( pExtra, pCd->uParam1 );
break;
case eCmdClose :
m_ws.Destroy();
break;
} // end switch
return TRUE;
}
BOOL CWinAsyncSocket::OnSocketEvent()
{
// Must have a socket handle
if ( !m_ws.IsSocket() ) return FALSE;
// Use local structure if one was not provided
WSANETWORKEVENTS wne;
// Reset the network event
ResetEvent( m_ws.GetEventHandle() );
// Get network events
if ( 0 != WSAEnumNetworkEvents( m_ws, m_ws.GetEventHandle(), &wne ) )
return FALSE;
// *** These messages are sent in a purposeful order
// Ensure connect messages are first
if ( ( wne.lNetworkEvents & FD_CONNECT ) != 0 )
{ if ( wne.iErrorCode ) m_uStatus = eStatusDisconnected;
else m_uStatus = eStatusConnected;
OnConnect( wne.iErrorCode[ FD_CONNECT_BIT ] );
MMessage( (WPARAM)FD_CONNECT, (LPARAM)wne.iErrorCode[ FD_CONNECT_BIT ] );
} // end if
if ( ( wne.lNetworkEvents & FD_ACCEPT ) != 0 )
{ m_uStatus = eStatusConnected;
OnAccept( wne.iErrorCode[ FD_ACCEPT_BIT ] );
MMessage( (WPARAM)FD_ACCEPT, (LPARAM)wne.iErrorCode[ FD_ACCEPT_BIT ] );
} // end if
if ( ( wne.lNetworkEvents & FD_OOB ) != 0 )
{ m_uStatus = eStatusConnected;
OnOOB( wne.iErrorCode[ FD_OOB_BIT ] );
MMessage( (WPARAM)FD_OOB, (LPARAM)wne.iErrorCode[ FD_OOB_BIT ] );
} // end if
if ( ( wne.lNetworkEvents & FD_QOS ) != 0 )
{ m_uStatus = eStatusConnected;
OnQOS( wne.iErrorCode[ FD_QOS_BIT ] );
MMessage( (WPARAM)FD_QOS, (LPARAM)wne.iErrorCode[ FD_QOS_BIT ] );
} // end if
if ( ( wne.lNetworkEvents & FD_GROUP_QOS ) != 0 )
{ m_uStatus = eStatusConnected;
OnGroupQOS( wne.iErrorCode[ FD_GROUP_QOS_BIT ] );
MMessage( (WPARAM)FD_GROUP_QOS, (LPARAM)wne.iErrorCode[ FD_GROUP_QOS_BIT ] );
} // end if
if ( ( wne.lNetworkEvents & FD_ROUTING_INTERFACE_CHANGE ) != 0 )
{ m_uStatus = eStatusConnected;
OnRoutingInterfaceChange( wne.iErrorCode[ FD_ROUTING_INTERFACE_CHANGE_BIT ] );
MMessage( (WPARAM)FD_ROUTING_INTERFACE_CHANGE, (LPARAM)wne.iErrorCode[ FD_ROUTING_INTERFACE_CHANGE_BIT ] );
} // end if
if ( ( wne.lNetworkEvents & FD_WRITE ) != 0 )
{ m_uStatus = eStatusConnected;
OnWrite( wne.iErrorCode[ FD_WRITE_BIT ] );
MMessage( (WPARAM)FD_WRITE, (LPARAM)wne.iErrorCode[ FD_WRITE_BIT ] );
} // end if
if ( ( wne.lNetworkEvents & FD_READ ) != 0 )
{ m_uStatus = eStatusConnected;
OnRead( wne.iErrorCode[ FD_READ_BIT ] );
MMessage( (WPARAM)FD_READ, (LPARAM)wne.iErrorCode[ FD_READ_BIT ] );
} // end if
// Ensure close messages are last
if ( ( wne.lNetworkEvents & FD_CLOSE ) != 0 )
{ m_uStatus = eStatusDisconnected;
OnClose( wne.iErrorCode[ FD_CLOSE_BIT ] );
MMessage( (WPARAM)FD_CLOSE, (LPARAM)wne.iErrorCode[ FD_CLOSE_BIT ] );
} // end if
return TRUE;
}
BOOL CWinAsyncSocket::Connect(LPCTSTR pIpAddress, UINT uPort)
{
if ( !pIpAddress || !*pIpAddress ) return FALSE;
// Lock the fifo buffer
CTlLocalLock ll( m_fifo );
if ( !ll.IsLocked() ) return FALSE;
// Command data
SCmdData cd;
cd.uCmd = eCmdOpen;
cd.uParam1 = uPort;
cd.uDataLen = strlen( pIpAddress ) + 1;
// Initialize fifo write
if ( !m_fifo.InitFifoWrite() ) return FALSE;
// Write the command header
if ( !m_fifo.AddFifo( &cd, sizeof( cd ) ) ) return FALSE;
// Add ip address string
if ( !m_fifo.AddFifo( pIpAddress, cd.uDataLen ) ) return FALSE;
// Finalize the write
if ( !m_fifo.EndFifoWrite() ) return FALSE;
// Start the thread if needed
if ( !IsRunning() ) Start();
return TRUE;
}
BOOL CWinAsyncSocket::Close()
{
// Lock the fifo buffer
CTlLocalLock ll( m_fifo );
if ( !ll.IsLocked() ) return FALSE;
// Command data
SCmdData cd;
cd.uCmd = eCmdClose;
cd.uParam1 = 0;
cd.uDataLen = 0;
// Initialize fifo write
if ( !m_fifo.InitFifoWrite() ) return FALSE;
// Write the command header
if ( !m_fifo.AddFifo( &cd, sizeof( cd ) ) ) return FALSE;
// Finalize the write
return m_fifo.EndFifoWrite();
}
#endif