-
Notifications
You must be signed in to change notification settings - Fork 0
/
SOCKET.CPP
471 lines (340 loc) · 11.1 KB
/
SOCKET.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Socket.cpp : implementation of the CSocketWnd class
//
// Copyright (C) 1993-1994 George Mills and Softronics, Inc. Corporation
// All rights reserved.
//
#include "stdafx.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
#define MAX_PENDING_CONNECTS 4 /* The backlog allowed for listen() */
#define MAX_PACKET_SIZE 1024
/////////////////////////////////////////////////////////////////////////////
// CSocketWnd
IMPLEMENT_DYNCREATE(CSocketWnd, CWnd)
BEGIN_MESSAGE_MAP(CSocketWnd, CWnd)
//{{AFX_MSG_MAP(CSocketWnd)
ON_MESSAGE(ID_NET_ACCEPTFINISH, OnNetAcceptfinish)
ON_MESSAGE(ID_NET_CONNECTACK, OnNetConnectack)
ON_MESSAGE(ID_NET_CONNECTFINISH, OnNetConnectfinish)
//}}AFX_MSG_MAP
// Standard printing commands
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSocketWnd construction/destruction
CSocketWnd::CSocketWnd()
{
bConnected = FALSE; // Flag that send socket is connected
bBusy = FALSE; // Flag that send socket is busy
bNetworkOn = FALSE; // flag for send enabled (enables message processing)
phe = NULL; // Pointer to Host Entry for send
}
CSocketWnd::~CSocketWnd()
{
}
// void CSocketWnd::SetView(CLogiView *pview)
// {
// pView = pview;
// }
BOOL CSocketWnd::ConnectOn(int iport, const char *networkaddress, CLogiDoc* pdoc, CLogiNetworkGate *pobj)
{
long ConnectMessage;
char szThisHost[80];
pObj = pobj;
pDoc = pdoc;
// start network in case it's not up
NetworkStartup();
// sanity check first
if (hWinSockDLL == NULL)
{
return(FALSE);
}
if (bNetworkOn)
{
return(FALSE);
}
bNetworkOn = TRUE;
// bring up hidden window to receive messages for "this" connection
// if (!Create(NULL, "Socket", WS_CHILD, CRect(0,0,0,0), pView, 99))
if (!Create(NULL, "Socket", WS_CHILD, CRect(0,0,0,0), AfxGetMainWnd(), IDR_MAINFRAME))
{
return(FALSE);
}
// save the port
iPort = iport;
// get sockets
if ((iSocket = lpsocket( AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
MessageBox( WSAGetLastErrorString(0), "socket(iSocketet)");
return(FALSE);
}
// if no address then listen
if (networkaddress == NULL)
{
lpgethostname(szThisHost, sizeof(szThisHost));
ConnectMessage = ID_NET_ACCEPTFINISH;
}
// else call
else
{
strncpy(szThisHost, networkaddress, sizeof(szThisHost));
ConnectMessage = ID_NET_CONNECTFINISH;
}
if (phe == NULL)
{
phe = (PHOSTENT) malloc(MAXGETHOSTSTRUCT);
memset(phe, 0, MAXGETHOSTSTRUCT);
}
unsigned long sockAddr;
// if not IP address then must IP name
if ((sockAddr = lpinet_addr(szThisHost)) == INADDR_NONE)
{
// get address of remote machine
if (!lpWSAAsyncGetHostByName ( m_hWnd, ConnectMessage, szThisHost, (LPSTR) phe, MAXGETHOSTSTRUCT ))
{
MessageBox( WSAGetLastErrorString(0), "WSAAsyncGetHostByName()");
return(FALSE);
}
}
// else build a fake HOSTENT
else
{
phe->h_length = sizeof(sockAddr);
phe->h_addr_list = (char **) (phe + sizeof(HOSTENT));
phe->h_addr_list[0] = (char *) (phe->h_addr_list + sizeof(void *));
memcpy(phe->h_addr, (LPCSTR) &sockAddr, sizeof(sockAddr));
// now fake success
PostMessage(ConnectMessage);
// I don't know why this won't work, also it still needs DNS.
// if (!lpWSAAsyncGetHostByAddr ( m_hWnd, ConnectMessage, (LPCSTR) &sockAddr, 4, PF_INET, (LPSTR) phe, MAXGETHOSTSTRUCT ))
// {
// MessageBox( WSAGetLastErrorString(0), "WSAAsyncGetHostByAddr()");
// return(FALSE);
// }
}
// wait for callback
return(TRUE);
}
void CSocketWnd::ConnectOff(void)
{
// tell handler not to do anything with messages for network send
if (bNetworkOn)
{
bNetworkOn = FALSE;
bConnected = FALSE; // Flag that send socket is connected
bBusy = FALSE; // Flag that send socket is busy
if (lpshutdown(iSocket,SD_BOTH) == SOCKET_ERROR)
{
MessageBox( WSAGetLastErrorString(0), "shutdown()");
}
if (lpclosesocket(iSocket) == SOCKET_ERROR)
{
MessageBox( WSAGetLastErrorString(0), "closesocket()");
}
if (phe)
{
free(phe);
phe = NULL; // Pointer to Host Entry for send
}
// HWND hWnd = Detach();
// if (IsWindow(hWnd))
// {
// ::DestroyWindow(hWnd);
// }
DestroyWindow();
// ::DestroyWindow(Detach());
}
}
BOOL CSocketWnd::SendData(char *Data)
{
if (bConnected && !bBusy)
{
// send the data
if ((lpsend( iSocket, Data, 1, 0 )) == SOCKET_ERROR)
{
if (lpWSAGetLastError() != WSAEWOULDBLOCK)
{
MessageBox(WSAGetLastErrorString(0), "send(iSocket)");
return(FALSE);
}
// Idle Until it's ok again.
MessageBox("UtOh1", "Unexpected Programing error");
bBusy = TRUE;
return(TRUE);
}
}
else
{
return(FALSE);
}
return(TRUE);
}
LRESULT CSocketWnd::OnNetConnectack( WPARAM /*wParam*/, LPARAM lParam)
{
// SOCKADDR_IN acc_sin; /* Accept socket address - internet style */
// int acc_sin_len; /* Accept socket address length */
char Buffer[MAX_PACKET_SIZE];
int status;
if (WSAGETASYNCERROR(lParam) != 0)
{
MessageBox(WSAGetLastErrorString(WSAGETASYNCERROR(lParam)), "WSAAsyncGetHostByNameCallBack()");
return 0L;
}
// queue message if enabled
if (bNetworkOn)
{
// update flags based on event type
switch (WSAGETSELECTEVENT(lParam))
{
case FD_READ:
{
memset(Buffer, 0, MAX_PACKET_SIZE);
// get the data
if ((status = lprecv( iSocket, Buffer, MAX_PACKET_SIZE-1, 0)) == SOCKET_ERROR)
{
int iErrorCode;
// if block wait til we get called again
if ((iErrorCode = lpWSAGetLastError()) == WSAEWOULDBLOCK)
{
MessageBox("UtOh2", "Unexpected Programing error");
return 0L;
}
MessageBox(WSAGetLastErrorString(0), "recv(iSocket)");
return 0L;
}
// if something is there (better be) then process it
if (status != 0)
{
// Queue up each char
for (int i=0;i<status;i++)
{
pDoc->NetworkMessageQueue.AddTail(new ANetworkMessage(pObj, pDoc, WSAGETSELECTEVENT(lParam), Buffer[i]));
pDoc->NetworkMessageQueue.AddTail(new ANetworkMessage(pObj, pDoc, 0L, 0));
}
}
return 0L;
}
case FD_WRITE:
{
// allow another frame to go out.
bBusy = FALSE;
break;
}
case FD_CONNECT:
{
// flag it's ok to start firing
bConnected = TRUE;
pDoc->NetworkMessageQueue.AddTail(new ANetworkMessage(pObj, pDoc, WSAGETSELECTEVENT(lParam), 0));
break;
}
case FD_ACCEPT:
{
bConnected = TRUE;
// acc_sin_len = sizeof(acc_sin);
// if ((iSocket = lpaccept( iSocket,(struct sockaddr FAR *) &acc_sin, (int FAR *) &acc_sin_len )) == INVALID_SOCKET)
if ((iSocket = lpaccept( iSocket, NULL, NULL )) == INVALID_SOCKET)
{
MessageBox(WSAGetLastErrorString(0), "accept(receivesock)");
}
pDoc->NetworkMessageQueue.AddTail(new ANetworkMessage(pObj, pDoc, WSAGETSELECTEVENT(lParam), 0));
break;
}
case FD_CLOSE:
{
// done
bConnected = FALSE;
pDoc->NetworkMessageQueue.AddTail(new ANetworkMessage(pObj, pDoc, WSAGETSELECTEVENT(lParam), 0));
break;
}
}
}
return 0L;
}
LRESULT CSocketWnd::OnNetConnectfinish( WPARAM /*wParam*/, LPARAM lParam)
{
// should these not be automatic?
SOCKADDR_IN sockAddr;
if (WSAGETASYNCERROR(lParam) != 0)
{
MessageBox(WSAGetLastErrorString(WSAGETASYNCERROR(lParam)), "WSAAsyncGetHostByNameCallBack()");
return 0L;
}
// always start clean
memset(&sockAddr, 0, sizeof(SOCKADDR_IN));
// what else is there
sockAddr.sin_family = AF_INET;
// I wonder if this in network order
memcpy((char FAR *)&(sockAddr.sin_addr), phe->h_addr, phe->h_length);
// set ports
sockAddr.sin_port = lphtons((unsigned short) iPort); /* Convert to network ordering */
// Associate an address with a socket. (bind)
// if (lpbind( iSocket, (struct sockaddr FAR *) &sockAddr, sizeof(sockAddr)) == SOCKET_ERROR)
// {
// MessageBox(WSAGetLastErrorString(0), "bind(sendsock)");
// return 0L;
// }
// watch for connect
if (lpWSAAsyncSelect( iSocket, m_hWnd, ID_NET_CONNECTACK, FD_ACCEPT | FD_CONNECT | FD_WRITE | FD_READ | FD_CLOSE) == SOCKET_ERROR)
{
MessageBox(WSAGetLastErrorString(0), "WSAAsyncSelect(iSocket) FD_CONNECT");
}
// lets try now
if (lpconnect( iSocket, (PSOCKADDR) &sockAddr, sizeof( sockAddr)) == SOCKET_ERROR)
{
int iErrorCode;
if ((iErrorCode = lpWSAGetLastError()) != WSAEWOULDBLOCK)
{
MessageBox(WSAGetLastErrorString(0), "connect(iSocket)");
return 0L;
}
}
// fire event that connection is made
if (bNetworkOn)
{
// xxxx
}
return 0L;
}
LRESULT CSocketWnd::OnNetAcceptfinish( WPARAM /*wParam*/, LPARAM lParam)
{
SOCKADDR_IN sockAddr; /* Local socket - internet style */
if (WSAGETASYNCERROR(lParam) != 0)
{
MessageBox(WSAGetLastErrorString(WSAGETASYNCERROR(lParam)), "WSAAsyncGetHostByNameCallBack()");
return 0L;
}
// always start clean
memset(&sockAddr, 0, sizeof(SOCKADDR_IN));
// what else is there
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = lphtonl(INADDR_ANY);
// memcpy((char FAR *)&(sockAddr.sin_addr), phe->h_addr, phe->h_length);
// set ports
sockAddr.sin_port = lphtons((unsigned short) iPort); /* Convert to network ordering */
// int flag = 1;
// lpsetsockopt(iSocket, SOL_SOCKET, SO_REUSEADDR, (LPCSTR) &flag, sizeof(int));
// Associate an address with a socket. (bind)
if (lpbind( iSocket, (struct sockaddr FAR *) &sockAddr, sizeof(sockAddr)) == SOCKET_ERROR)
{
MessageBox(WSAGetLastErrorString(0), "bind(receivesock)");
return 0L;
}
// listen for connect
if (lplisten( iSocket, MAX_PENDING_CONNECTS ) == SOCKET_ERROR)
{
MessageBox(WSAGetLastErrorString(0), "listen(receivesock)");
return 0L;
}
// watch for when connect happens
if (lpWSAAsyncSelect( iSocket, m_hWnd, ID_NET_CONNECTACK, FD_ACCEPT | FD_CONNECT | FD_READ | FD_WRITE | FD_CLOSE) == SOCKET_ERROR)
{
MessageBox(WSAGetLastErrorString(0), "WSAAsyncSelect(receivesock) FD_ACCEPT");
}
// queue this event
if (bNetworkOn)
{
// xxx
}
return 0L;
}