-
Notifications
You must be signed in to change notification settings - Fork 0
/
netobj.cpp
567 lines (525 loc) · 17.5 KB
/
netobj.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//
// Copyright (c) 2004 K. Wilkins
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from
// the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
//////////////////////////////////////////////////////////////////////////////
// Handy - An Atari Lynx Emulator //
// Copyright (c) 1996,1997 //
// K. Wilkins //
//////////////////////////////////////////////////////////////////////////////
// Network Object Class //
//////////////////////////////////////////////////////////////////////////////
// //
// This class provides a minimal UDP connection service between two PCs //
// to allow Lynx networking to be implemented. //
// //
// K. Wilkins //
// March 2000 //
// //
//////////////////////////////////////////////////////////////////////////////
// Revision History: //
// ----------------- //
// //
// 21Mar2000 KW Document header added & class created //
// //
//////////////////////////////////////////////////////////////////////////////
#define _NETOBJ_C
#include "netobj.h"
class CNetAddrDlg : public CDialog
{
public:
CNetAddrDlg(CString netadr)
:CDialog(IDD_NETADDRESS,AfxGetMainWnd())
{
//{{AFX_DATA_INIT(CNetAddrDlg)
mNetAddress = netadr;
//}}AFX_DATA_INIT
}
BOOL OnInitDialog(void)
{
BOOL retval=CDialog::OnInitDialog();
CEdit *tmp;
tmp=(CEdit*)GetDlgItem(IDC_NETWORK_ADDRESS);
tmp->SetWindowText(mNetAddress);
return retval;
}
void DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDumpWinDlg)
DDX_Text(pDX, IDC_NETWORK_ADDRESS, mNetAddress);
//}}AFX_DATA_MAP
}
//{{AFX_DATA(CNetAddrDlg)
CString mNetAddress;
//}}AFX_DATA
};
CNetObj::CNetObj()
{
int error;
struct protoent *ppe;
mState=NETOBJ_STOP;
mRxSocket=0;
mTxSocket=0;
mTimerID=0;
mSyncCounter=-1;
mRXcount=0;
mTXcount=0;
mLastRxData=0;
TRACE_NETOBJ0("CNetObj()");
// Blank the dest socket
memset(&mDestSockAddr, 0, sizeof(mDestSockAddr));
// Initialise the dialog for later, its not visible by default
mpWaitForSync= new CNetWaitDlg;
// Initialise WinSock
// if((error=WSAStartup(MAKEWORD(2,0),&mWsadata)))
if((error=WSAStartup(MAKEWORD(1,1),&mWsadata)))
{
CString errmsg;
errmsg.Format("CNetObj::CNetObj() - Error %05d Unable to start winsock support",error);
::MessageBox((AfxGetMainWnd())->m_hWnd,errmsg,NETOBJ_CAPTION,MB_ICONEXCLAMATION);
TRACE_NETOBJ0("CNetObj() - Failed to start WinSock");
mState=NETOBJ_DEAD;
}
else
{
// Lookup protocol
if((ppe=getprotobyname("udp"))==0)
{
::MessageBox((AfxGetMainWnd())->m_hWnd,"CNetObj::CNetObj() - GetProtoByname() - Can't get UDP protocol entry.\nOnly TCP/IP(UDP) is supported by Handy.",NETOBJ_CAPTION,MB_ICONEXCLAMATION);
mState=NETOBJ_DEAD;
}
else
{
TRACE_NETOBJ0("CNetObj() - WinSock started");
// now open our socket
if ((mRxSocket=socket(PF_INET, SOCK_DGRAM, ppe->p_proto))==INVALID_SOCKET || (mTxSocket=socket(PF_INET, SOCK_DGRAM, ppe->p_proto))==INVALID_SOCKET)
{
::MessageBox((AfxGetMainWnd())->m_hWnd,"CNetObj::CNetObj() - Request to create Rx/Tx sockets failed.\nOnly TCP/IP(UDP) is supported by Handy.",NETOBJ_CAPTION,MB_ICONEXCLAMATION);
mState=NETOBJ_DEAD;
}
else
{
TRACE_NETOBJ0("CNetObj() - Rx/Tx Sockets Created");
// Success, now bind to the socket we will listen on
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=htonl(INADDR_ANY);
sin.sin_port=htons(HANDY_PORT_NUMBER);
if(bind(mRxSocket,(struct sockaddr *)&sin, sizeof(sin))==SOCKET_ERROR)
{
::MessageBox((AfxGetMainWnd())->m_hWnd,"CNetObj::CNetObj() - Could not bind to socket.\nMay be already in use ??.",NETOBJ_CAPTION,MB_ICONEXCLAMATION);
mState=NETOBJ_DEAD;
}
else
{
TRACE_NETOBJ0("CNetObj() - Bound to socket");
DWORD param=1;
if(ioctlsocket(mRxSocket,FIONBIO,¶m)==SOCKET_ERROR || ioctlsocket(mTxSocket,FIONBIO,¶m)==SOCKET_ERROR)
{
::MessageBox((AfxGetMainWnd())->m_hWnd,"CNetObj::CNetObj() - Could set sockets to non-blocking.\nMay be already in use ??.",NETOBJ_CAPTION,MB_ICONEXCLAMATION);
mState=NETOBJ_DEAD;
}
else
{
param=1;
if(setsockopt(mTxSocket,IPPROTO_UDP,TCP_NODELAY,(char*)¶m,sizeof(param))==SOCKET_ERROR)
{
// int error=WSAGetLastError();
// CString errmsg;
// errmsg.Format("CNetObj::CNetObj() - Error %05d could not setsockopt to TCP_NODELAY.\nMay be already in use ??.",error);
// ::MessageBox((AfxGetMainWnd())->m_hWnd,errmsg,NETOBJ_CAPTION,MB_ICONEXCLAMATION);
// mState=NETOBJ_DEAD;
mState=NETOBJ_IDLE;
}
else
{
TRACE_NETOBJ0("CNetObj() - Socket set to TCP_NODELAY");
mState=NETOBJ_IDLE;
}
}
}
}
}
}
}
CNetObj::~CNetObj(void)
{
// Kill the timer if its active
if(mTimerID!=NULL) timeKillEvent(mTimerID);
// Destroy the wait dialog
if(mpWaitForSync!=NULL)
{
mpWaitForSync->DestroyWindow();
delete mpWaitForSync;
}
mpWaitForSync=NULL;
// Close any open sockets
if(mRxSocket) closesocket(mRxSocket);
if(mTxSocket) closesocket(mTxSocket);
}
void CALLBACK CNetObj::SyncCallback(UINT uID,UINT uMsg,DWORD dwUser,DWORD dw1,DWORD dw2)
{
(AfxGetMainWnd())->PostMessage(WM_NETOBJ_UPDATE);
}
void CNetObj::Update()
{
static int twiddle=0x00000001;
// Refreference the objects as this is a static member
// CNetObj *netobj = (CNetObj*) dwUser;
// Only do anything if in the sync state
TRACE_NETOBJ0("SyncCallback()");
if(mState==NETOBJ_SYNC)
{
// We're uninitialised
if(mSyncCounter==-1)
{
// Setup the count
mSyncCounter=HANDY_SYNCUP_COUNT;
}
// Check for an abort or a sync packet
if(mpWaitForSync->mAbort)
{
TRACE_NETOBJ0("SyncCallback() - Synchronisation aborted");
Stop();
}
else
{
// Once we get the first Sync message we start to auto decrement
// the counter, the first sync tells that both machines are in
// Sync mode, we then do HANDY_SYNCUP_COUNT transmits, one every 100ms.
// when count reaches 0 we assume SYNCHRONISATION.
//
// This isnt perfect but it IS simple.
//
if(mSyncCounter==HANDY_SYNCUP_COUNT)
{
if(Receive()==HANDY_SYNC_MESSAGE)
{
mpWaitForSync->SetString("Sync complete, please wait...");
TRACE_NETOBJ0("SyncCallback() - Received a HANDY_SYNC_MESSAGE, starting countdown.");
mSyncCounter--;
}
else
{
// Do the night rider bit...
CString waitstr;
waitstr.Format("Waiting for sync message... %07x",twiddle&0x7ffffff);
mpWaitForSync->SetString(waitstr);
if(twiddle==0x01000000) twiddle=0x11000000;
if(twiddle==0x10000001) twiddle=0x00000001;
if(twiddle&0x10000000) twiddle=0x10000000+((twiddle&0x7ffffff)>>4); else twiddle=0x00000000+((twiddle&0x7ffffff)<<4);
}
}
else if(!mSyncCounter)
{
TRACE_NETOBJ0("SyncCallback() - Synchronisation complete, flushing net buffers, mState=NETOBJ_ACTIVE");
mpWaitForSync->ShowWindow(SW_HIDE);
mState=NETOBJ_ACTIVE;
// Halt the multimedia timer
if(mTimerID!=NULL)
{
timeKillEvent(mTimerID);
mTimerID=NULL;
}
// Flush any incoming SYNC messages
while(Receive()==HANDY_SYNC_MESSAGE);
// Make winsock send messages to the main window
if(WSAAsyncSelect(mRxSocket,(AfxGetMainWnd())->m_hWnd,WM_NETOBJ_SELECT,FD_READ)==SOCKET_ERROR)
{
// A bad thing has occured, what to do...
Stop();
TRACE_NETOBJ0("SyncCallback() - WSAAsyncSelect failed - Set mState=NETOBJ_DEAD");
mState=NETOBJ_DEAD;
}
// Reset our internal state
mSyncCounter=-1;
mRXcount=0;
mTXcount=0;
}
else
{
mSyncCounter--;
}
// Transmit the Sync to the destination
Transmit(HANDY_SYNC_MESSAGE);
}
}
else
{
// Usually net connection has stopped and we're just displaying a message
// We're uninitialised
if(mSyncCounter==-1) mSyncCounter=20;
if(!mSyncCounter)
{
TRACE_NETOBJ0("SyncCallback() - Shutdown complete, mState=NETOBJ_IDLE");
mpWaitForSync->ShowWindow(SW_HIDE);
// Halt the multimedia timer
if(mTimerID!=NULL)
{
timeKillEvent(mTimerID);
mTimerID=NULL;
}
// Reset our internal state
mSyncCounter=-1;
}
else
{
mSyncCounter--;
}
}
// else
// {
// Stop();
// TRACE_NETOBJ1("SyncCallback() - Illegal mState value %d",mState);
// mState=NETOBJ_DEAD;
// }
}
int CNetObj::Start(CString netadr)
{
mRXcount=0;
mTXcount=0;
if(mState==NETOBJ_IDLE)
{
// Fetch the network address
struct hostent *hostentry;
CString tmpstr;
CNetAddrDlg *netdlg= new CNetAddrDlg(netadr);
int result=netdlg->DoModal();
tmpstr=netdlg->mNetAddress;
delete netdlg;
if(result==IDOK && !tmpstr.IsEmpty())
{
ULONG inetaddr;
mNetAddress=tmpstr;
TRACE_NETOBJ1("Start() - Got Address : %s",mNetAddress);
mNetAddress.TrimLeft();
mNetAddress.MakeLower();
TRACE_NETOBJ1("Start() - Processed to: %s",mNetAddress);
inetaddr=INADDR_NONE;
// Lets try to resolve the hostname/address to an IP addr
if((inetaddr=inet_addr(mNetAddress))==INADDR_NONE)
{
// Wasnt a raw IP address, try resolution
if((hostentry=gethostbyname(mNetAddress))!=NULL)
{
memset(&mDestSockAddr, 0, sizeof(mDestSockAddr));
memcpy((char*)&mDestSockAddr.sin_addr,hostentry->h_addr,hostentry->h_length);
mDestSockAddr.sin_family=AF_INET;
mDestSockAddr.sin_port=htons(HANDY_PORT_NUMBER);
// Go to sync mode
TRACE_NETOBJ0("Start() - Set mDestSockAddr, mState=NETOBJ_SYNC");
mState=NETOBJ_SYNC;
}
else
{
// Failed to resolve
CString message;
message.Format("CNetObj::Start() - Failed to resolve the network name you provided ( %s ), connect failed.",mNetAddress);
::MessageBox((AfxGetMainWnd())->m_hWnd,message,NETOBJ_CAPTION,MB_ICONEXCLAMATION);
}
}
else
{
memset(&mDestSockAddr, 0, sizeof(mDestSockAddr));
mDestSockAddr.sin_family=AF_INET;
mDestSockAddr.sin_addr.s_addr=inetaddr;
mDestSockAddr.sin_port=htons(HANDY_PORT_NUMBER);
// Go to sync mode
TRACE_NETOBJ0("Start() - Set mDestSockAddr, mState=NETOBJ_SYNC");
mState=NETOBJ_SYNC;
}
// Now we've entered SYNC stage start up the callback to perform SYNC
if(mState==NETOBJ_SYNC)
{
// Reset the sync engine state
mSyncCounter=-1;
mpWaitForSync->mAbort=FALSE;
// Display the synchro dialog
mpWaitForSync->ShowWindow(SW_SHOW);
// If the timer is not running then start it
if(mTimerID==NULL)
{
// Initialise the timer callback
if((mTimerID=timeSetEvent(100,0,(LPTIMECALLBACK)SyncCallback,(DWORD)this,TIME_PERIODIC))==NULL)
{
TRACE_NETOBJ0("Start() - Multimedia timer failed");
::MessageBox((AfxGetMainWnd())->m_hWnd,"CNetObj::Start() - Couldn't initialise Multimedia timer, aborting ???",NETOBJ_CAPTION,MB_ICONEXCLAMATION);
Stop();
TRACE_NETOBJ0("Start() - Set mState=NETOBJ_DEAD");
mState=NETOBJ_DEAD;
}
}
}
}
else
{
TRACE_NETOBJ0("Start() - NetAddress Dialog, cancelled or empty.");
}
}
else if(mState==NETOBJ_SYNC)
{
// Dont allow re-start if we are already in Sync mode
::MessageBox((AfxGetMainWnd())->m_hWnd,"CNetObj::Start() - Already in network sync mode.",NETOBJ_CAPTION,MB_ICONEXCLAMATION);
}
else
{
if(mState!=NETOBJ_DEAD) TRACE_NETOBJ1("Start() - mState!=NETOBJ_IDLE/SYNC, mState=%d",mState);
TRACE_NETOBJ0("Start() - Set mState=NETOBJ_DEAD");
mState=NETOBJ_DEAD;
}
// Return sucess indicator
if(mState==NETOBJ_SYNC) return TRUE; else return FALSE;
}
int CNetObj::Stop(void)
{
if(mState==NETOBJ_ACTIVE || mState==NETOBJ_SYNC)
{
// Alert user that network is stopping
mpWaitForSync->SetString("Closing connection, please wait...");
mpWaitForSync->ShowWindow(SW_SHOW);
// Reset the update counter index
mSyncCounter=-1;
// If the timer is not running then start it
if(mTimerID==NULL)
{
// Initialise the timer callback
if((mTimerID=timeSetEvent(100,0,(LPTIMECALLBACK)SyncCallback,(DWORD)this,TIME_PERIODIC))==NULL)
{
TRACE_NETOBJ0("Start() - Multimedia timer failed");
::MessageBox((AfxGetMainWnd())->m_hWnd,"CNetObj::Stop() - Couldn't initialise Multimedia timer, aborting ???",NETOBJ_CAPTION,MB_ICONEXCLAMATION);
TRACE_NETOBJ0("Start() - Set mState=NETOBJ_DEAD");
mState=NETOBJ_DEAD;
}
}
// Make winsock send messages to the main window
if(WSAAsyncSelect(mRxSocket,(AfxGetMainWnd())->m_hWnd,0,0)==SOCKET_ERROR)
{
// A bad thing has occured, what to do...
::MessageBox((AfxGetMainWnd())->m_hWnd,"CNetObj::Stop() - WSAASyncSelect failes",NETOBJ_CAPTION,MB_ICONEXCLAMATION);
TRACE_NETOBJ0("Start() - WSAAsyncSelect failed - Set mState=NETOBJ_DEAD");
mState=NETOBJ_DEAD;
}
else
{
TRACE_NETOBJ0("Stop() - Set mState=NETOBJ_IDLE");
mState=NETOBJ_IDLE;
}
// Send a message back to force the window to close
}
else
{
if(mState!=NETOBJ_DEAD) TRACE_NETOBJ1("Stop() - mState!=NETOBJ_ACTIVE/SYNC, mState=%d",mState);
TRACE_NETOBJ0("Stop() - Set mState=NETOBJ_DEAD");
mState=NETOBJ_DEAD;
}
if(mState==NETOBJ_IDLE) return TRUE; else return FALSE;
}
int CNetObj::Receive(void)
{
int retval=0;
if(mState==NETOBJ_ACTIVE || mState==NETOBJ_SYNC)
{
unsigned char rxbuf[10];
int rxcount;
// If there is any to get then get it
rxcount=recv(mRxSocket,(char*)rxbuf,2,0);
if(rxcount==2)
{
retval=rxbuf[0]+(rxbuf[1]<<8);
TRACE_NETOBJ1("Reveive() - Data received = %04x",retval);
mRXcount++;
mLastRxData=retval;
}
else if(rxcount==-1)
{
TRACE_NETOBJ0("Reveive() - No Data Waiting");
retval=HANDY_NODATA_MESSAGE;
}
else
{
TRACE_NETOBJ1("Reveive() - Wrong number of bytes waiting = %d",rxcount);
retval=HANDY_NODATA_MESSAGE;
}
}
else
{
if(mState!=NETOBJ_DEAD) TRACE_NETOBJ1("Reveive() - mState!=NETOBJ_ACTIVE/SYNC, mState=%d",mState);
TRACE_NETOBJ0("Receive() - Set mState=NETOBJ_DEAD");
mState=NETOBJ_DEAD;
retval=HANDY_NODATA_MESSAGE;
}
return retval;
}
void CNetObj::Transmit(int data)
{
// Only sync messages can be sent in sync state
if(mState==NETOBJ_SYNC && data!=HANDY_SYNC_MESSAGE)
{
TRACE_NETOBJ0("Transmit() - mState=NETOBJ_SYNC, Lynx data discarded");
return;
}
mTXcount++;
if(mState==NETOBJ_ACTIVE || mState==NETOBJ_SYNC)
{
unsigned char txbuf[10];
int txcount;
txbuf[0]=(unsigned char) data&0xff;
txbuf[1]=(unsigned char) (data>>8)&0xff;
if((txcount=sendto(mTxSocket,(char*)txbuf,2,0,(struct sockaddr *)&mDestSockAddr,sizeof(mDestSockAddr)))!=SOCKET_ERROR)
{
TRACE_NETOBJ1("Transmit() - Data = %04x",data);
}
else
{
TRACE_NETOBJ2("Transmit() - Failed sendto call (Error=%d, txcount=%d)",WSAGetLastError(),txcount);
// Process error in a sensible manner
switch(WSAGetLastError())
{
case WSAENETUNREACH:
case WSAEHOSTUNREACH:
{
CString message;
Stop();
message.Format("CNetObj::Transmit() - Host \"%s\" is unreachable.",mNetAddress);
::MessageBox((AfxGetMainWnd())->m_hWnd,message,NETOBJ_CAPTION,MB_ICONEXCLAMATION);
}
break;
default:
Stop();
::MessageBox((AfxGetMainWnd())->m_hWnd,"CNetObj::Transmit() - Winsock sendto call has failed, disabling networking.",NETOBJ_CAPTION,MB_ICONEXCLAMATION);
TRACE_NETOBJ0("Transmit() - Set mState=NETOBJ_DEAD");
mState=NETOBJ_DEAD;
break;
}
}
}
else if(mState==NETOBJ_IDLE)
{
TRACE_NETOBJ0("Transmit() - mState=NETOBJ_IDLE, your wasting your time");
}
else
{
if(mState!=NETOBJ_DEAD) TRACE_NETOBJ1("Transmit() - mState!=NETOBJ_ACTIVE/SYNC, mState=%d",mState);
TRACE_NETOBJ0("Transmit() - Set mState=NETOBJ_DEAD");
mState=NETOBJ_DEAD;
}
}