summaryrefslogtreecommitdiffstats
path: root/private/sdktools/zd/zdsim.c
blob: 571df10f107e0679f1d3a6c5014641c539a9b1ec (plain) (blame)
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
#include "zdsimp.h"

int
APIENTRY
WinMain (
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpszCmdLine,
    int nCmdShow
    )
{

    DialogBox(hInstance, MAKEINTRESOURCE(ID_ZDSIM_DLG),NULL, ZdDlgProc);
    return(0);
}


BOOL
CALLBACK
ZdDlgProc(
    HWND hDlg,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
    )
{

    BOOL ReturnValue;

    ReturnValue = TRUE;

    HwndDlg = hDlg;
    switch (uMsg) {
        HANDLE_MSG(hDlg, WM_INITDIALOG,  ZdDlgInit);
        HANDLE_MSG(hDlg, WM_COMMAND,  ZdDlgCommand);
        HANDLE_MSG(hDlg, WM_HSCROLL,  ZdDlgParamChange);

       default:
          ReturnValue = FALSE;
          break;
     }
    return ReturnValue;
}


BOOL
ZdDlgInit(
    HWND hwnd,
    HWND hwndFocus,
    LPARAM lParam
    )
{

    HWND hwndSB;


    //
    // Get output window handle
    //

    hwndOutput = GetDlgItem(hwnd, ID_OUTPUT_WINDOW);

    SelNumberOfClients = DEF_CLIENTS;
    fSimulationStarted = FALSE;

    //
    // Setup simulation parameters
    //

    hwndSB = GetDlgItem(hwnd, ID_NUM_CLIENTS);
    SetScrollRange(hwndSB, SB_CTL, MIN_CLIENTS, MAX_CLIENTS, TRUE);
    SendMessage(hwnd,WM_HSCROLL,MAKEWPARAM(SB_THUMBTRACK,DEF_CLIENTS),(LPARAM)hwndSB);

    CheckRadioButton(hwnd,ID_RAD_TCP,ID_RAD_SPX,ID_RAD_TCP);
    fTcp = TRUE;
    fSpx = FALSE;

    CheckRadioButton(hwnd,ID_RAD_THREAD_PER_CLIENT,ID_RAD_DYNAMIC_THREADS,ID_RAD_DYNAMIC_THREADS);
    fDynamicThreadMode = TRUE;
    fOneThreadPerClient = FALSE;
    SelThreadsPerQueue = 2;
    InitializeCriticalSection(&DynamicCritSect);


    EnableWindow(GetDlgItem(hwnd, ID_END_SIM),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_1),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_2),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_3),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_4),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_5),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_6),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_7),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_8),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_9),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_10),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_11),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_12),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_13),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_14),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_15),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_16),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_17),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_18),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_19),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_20),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_21),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_22),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_23),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_24),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_25),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_26),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_27),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_28),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_29),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_30),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_31),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_32),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_33),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_34),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_35),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_36),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_37),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_38),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_39),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_40),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_41),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_42),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_43),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_44),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_45),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_46),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_47),FALSE);
    EnableWindow(GetDlgItem(hwnd, THREAD_48),FALSE);

    return(TRUE);
}


void
ZdDlgParamChange(
    HWND hwnd,
    HWND hwndCtl,
    UINT code,
    int pos
    )
{

    CHAR szBuf[10];
    int nPosCrnt, nPosMin, nPosMax;
    DWORD ControlId;
    HWND hwndText;

    ControlId = GetWindowLong(hwndCtl,GWL_ID);

    //
    // Get the current position and the legal range for the
    // scrollbar that the user is changing.
    //

    nPosCrnt = GetScrollPos(hwndCtl,SB_CTL);
    GetScrollRange(hwndCtl, SB_CTL, &nPosMin, &nPosMax);

    switch (code) {
        case SB_LINELEFT:
            nPosCrnt--;
            break;

        case SB_LINERIGHT:
            nPosCrnt++;
            break;

        case SB_PAGELEFT:
            nPosCrnt += (nPosMax - nPosMin + 1) / 10;
            break;

        case SB_PAGERIGHT:
            nPosCrnt -= (nPosMax - nPosMin + 1) / 10;
            break;

        case SB_THUMBTRACK:
            nPosCrnt = pos;
            break;

        case SB_LEFT:
            nPosCrnt = nPosMin;
            break;

        case SB_RIGHT:
            nPosCrnt = nPosMax;
            break;
    }

    //
    // Make sure that the new scroll position
    // is within the legal range.
    //

    if (nPosCrnt < nPosMin) {
        nPosCrnt = nPosMin;
        }

    if (nPosCrnt > nPosMax) {
        nPosCrnt = nPosMax;
        }

    switch (ControlId) {
        case ID_NUM_CLIENTS:
            hwndText = GetDlgItem(hwnd,ID_NNOC);
            SelNumberOfClients = nPosCrnt;
            _stprintf(szBuf, "%d", nPosCrnt);
            break;

        default:
            return;

        }

    //
    // Set the new scroll position.
    //

    SetScrollPos(hwndCtl, SB_CTL, nPosCrnt, TRUE);

    //
    // Change the value displayed in the text box to
    // reflect the value in the scrollbar.
    //


    SetWindowText(hwndText, szBuf);
}


void
ZdDlgCommand (
    HWND hwnd,
    int id,
    HWND hwndCtl,
    UINT codeNotify
    )
{

    switch (id) {

        case ID_START_SIM:
        case IDOK:

            EnableWindow(hwndCtl,FALSE);
            EnableWindow(GetDlgItem(hwnd, ID_END_SIM),TRUE);
            EnableWindow(GetDlgItem(hwnd, ID_NUM_CLIENTS),FALSE);
            EnableWindow(GetDlgItem(hwnd, ID_RAD_DYNAMIC_THREADS),FALSE);
            EnableWindow(GetDlgItem(hwnd, ID_RAD_THREAD_PER_CLIENT),FALSE);
            EnableWindow(GetDlgItem(hwnd, ID_RAD_TCP),FALSE);
            EnableWindow(GetDlgItem(hwnd, ID_RAD_SPX),FALSE);

            SelNumberOfClients = GetScrollPos(GetDlgItem(hwnd, ID_NUM_CLIENTS),SB_CTL);
            if ( fOneThreadPerClient ) {
                SelThreadsPerQueue = SelNumberOfClients;
                }

            SendMessage(hwndOutput, LB_RESETCONTENT, 0L, 0L);

            //
            // Disable the StartSimulation button
            //


            if (NULL == GetFocus()) {
                SetFocus(GetDlgItem(hwnd, ID_END_SIM));
                }

            fSimulationStarted = TRUE;

            CreateNetConnections();

            CreateWorkers();

            break;

        case ID_RAD_DYNAMIC_THREADS:
            SelThreadsPerQueue = 2;
            fDynamicThreadMode = TRUE;
            fOneThreadPerClient = FALSE;
            break;

        case ID_RAD_THREAD_PER_CLIENT:
            SelThreadsPerQueue = GetScrollPos(GetDlgItem(hwnd, ID_NUM_CLIENTS),SB_CTL);;
            fDynamicThreadMode = FALSE;
            fOneThreadPerClient = TRUE;
            break;

        case ID_RAD_TCP:
            fTcp = TRUE;
            fSpx = FALSE;
            break;

        case ID_RAD_SPX:
            fTcp = FALSE;
            fSpx = TRUE;
            break;

        case ID_END_SIM:
            EnableWindow(GetDlgItem(hwnd, ID_END_SIM),FALSE);
            fSimulationStarted = FALSE;
            break;

        case IDCANCEL:
           EndDialog(hwnd, id);
           break;
    }
}

//
// This function constructs a string using the format string
// passed and the variable number of arguments and adds the
// string to the output window list box identified by the
// global hwndOutput variable.
//

void
OutputString(
    LPCSTR szFmt,
    ...
    )
{
    CHAR szBuf[150];
    int nIndex;
    va_list va_params;

    //
    // Make va_params point to the first argument after szFmt.
    //

    va_start(va_params, szFmt);

    //
    // Build the string to be displayed.
    //

    _vstprintf(szBuf, szFmt, va_params);
    do {

        //
        // Add the string to the end of the list box.
        //

        nIndex = SendMessage(hwndOutput,LB_ADDSTRING, 0L, (LPARAM)szBuf);

        //
        // If the list box is full, delete the first item in it.
        //

        if (nIndex == LB_ERR) {
            SendMessage(hwndOutput, LB_DELETESTRING, (WPARAM)0, 0);
            }

        } while (nIndex == LB_ERR);

    //
    // Select the newly added item.
    //

    SendMessage(hwndOutput, LB_SETCURSEL, (WPARAM)nIndex, 0);

    //
    // Indicate that we are done referencing
    // the variable arguments.
    //

    va_end(va_params);
}

DWORD
WINAPI
NetWorkThread(
    LPVOID WorkContext
    )
{
    PZD_WORK_QUEUE MyQueue;
    PZD_THREAD Me;
    DWORD WorkIndex;
    INT err;
    DWORD ResponseLength;
    BOOL b;
    LPOVERLAPPED lpo;
    DWORD nbytes;

    Me = (PZD_THREAD)WorkContext;
    MyQueue = Me->WorkQueue;

    for(;;){

        b = GetQueuedCompletionStatus(
                CompletionPort,
                &nbytes,
                &WorkIndex,
                &lpo,
                INFINITE
                );

        if ( b || lpo ) {

            WorkerStartWork(Me);

            //
            // Don't send a response if we received 0 bytes (end of 
            // transmission).  
            //

            if ( b && nbytes == 4 ) {

                //
                // Determine how long a response was desired by the client.
                // The first four bytes of the received packet contain the
                // number of bytes desired by the client.
                //
    
                ResponseLength = *(PDWORD)(ZdWorkQueue.IoBuffer[WorkIndex]);
    
                //
                // Send a response and post another asynchronous read on the
                // socket.
                //
    
                err = send( MyQueue->Sockets[WorkIndex], 
                            MyQueue->IoBuffer[WorkIndex], ResponseLength, 0 );
                if ( err == SOCKET_ERROR ) {
                    MessageBox(HwndDlg,"Send Failed. Simulation Should Stop","Send Failed",MB_ICONSTOP|MB_OK);
                }
    
                ZdWorkQueue.Overlapped[WorkIndex].hEvent = NULL;
    
                err = ReadFile( (HANDLE)ZdWorkQueue.Sockets[WorkIndex],
                                ZdWorkQueue.IoBuffer[WorkIndex], 4,
                                &ZdWorkQueue.Overlapped[WorkIndex].InternalHigh,
                                &ZdWorkQueue.Overlapped[WorkIndex] );
                if ( !err && GetLastError( ) != ERROR_IO_PENDING ) {
                    MessageBox(HwndDlg,"ReadFile Failed. Simulation Should Stop","ReadFile Failed",MB_ICONSTOP|MB_OK);
                }
            }

            WorkerEndWork(Me);

        } else {

            MessageBox(HwndDlg,"netWorkThread Wait Failed. Simulation Should Stop","Wait Failed",MB_ICONSTOP|MB_OK);
        }
    }
}

DWORD
WINAPI
NetWorkThread2(
    LPVOID WorkContext
    )
{
    PZD_WORK_QUEUE MyQueue;
    PZD_THREAD Me;
    DWORD WorkIndex;
    INT err;
    DWORD ResponseLength;

    Me = (PZD_THREAD)WorkContext;
    MyQueue = Me->WorkQueue;
    WorkIndex = Me->ThreadIndex;

    for(;;){

        err = recv( MyQueue->Sockets[WorkIndex], 
                    MyQueue->IoBuffer[WorkIndex], 4, 0 );
        if ( err == SOCKET_ERROR ) {
            MessageBox(HwndDlg,"Recv Failed. Simulation Should Stop","Recv Failed",MB_ICONSTOP|MB_OK);
            break;
        }

        if ( err < 4 ) {
            return;
        }

        WorkerStartWork(Me);

        //
        // Determine how long a response was desired by the client.  The 
        // first four bytes of the received packet contain the number of 
        // bytes desired by the client.  
        //

        ResponseLength = *(PDWORD)(ZdWorkQueue.IoBuffer[WorkIndex]);

        //
        // Send a response and post another asynchronous read on the
        // socket.
        //

        err = send( MyQueue->Sockets[WorkIndex],
                    MyQueue->IoBuffer[WorkIndex], ResponseLength, 0 );
                    
        if ( err == SOCKET_ERROR ) {
            MessageBox(HwndDlg,"Send Failed. Simulation Should Stop","Send Failed",MB_ICONSTOP|MB_OK);
            break;
        }

        WorkerEndWork(Me);

    }
}

VOID
CreateNetConnections(
    void
    )
{
    DWORD i;
    SOCKET listener;
    INT err;
    WSADATA WsaData;

    err = WSAStartup( 0x0101, &WsaData );
    if ( err == SOCKET_ERROR ) {
        MessageBox(HwndDlg,"WSAStartup Failed. Simulation Should Stop","WSAStartup Failed",MB_ICONSTOP|MB_OK);
        return;
    }

    //
    // Open a socket to listen for incoming connections.
    //

    if ( fTcp ) {

        SOCKADDR_IN localAddr;
        
        listener = socket( AF_INET, SOCK_STREAM, 0 );
        if ( listener == INVALID_SOCKET ) {
            MessageBox(HwndDlg,"Socket Create Failed. Simulation Should Stop","Socket Create Failed",MB_ICONSTOP|MB_OK);
            return;
        }
    
        RtlZeroMemory( &localAddr, sizeof(localAddr) );
        localAddr.sin_port = htons( 7 );
        localAddr.sin_family = AF_INET;
    
        err = bind( listener, (PSOCKADDR)&localAddr, sizeof(localAddr) );
        if ( err ==SOCKET_ERROR ) {
            MessageBox(HwndDlg,"Socket Bind Failed. Simulation Should Stop","Socket Bind Failed",MB_ICONSTOP|MB_OK);
            return;
        }

    } else {

        SOCKADDR_IPX localAddr;

        listener = socket( AF_IPX, SOCK_STREAM, NSPROTO_SPX );
        if ( listener == INVALID_SOCKET ) {
            MessageBox(HwndDlg,"Socket Create Failed. Simulation Should Stop","Socket Create Failed",MB_ICONSTOP|MB_OK);
            return;
        }
    
        RtlZeroMemory( &localAddr, sizeof(localAddr) );
        localAddr.sa_socket = htons( 7 );
        localAddr.sa_family = AF_IPX;
    
        err = bind( listener, (PSOCKADDR)&localAddr, sizeof(localAddr) );
        if ( err ==SOCKET_ERROR ) {
            MessageBox(HwndDlg,"Socket Bind Failed. Simulation Should Stop","Socket Bind Failed",MB_ICONSTOP|MB_OK);
            return;
        }
    }

    err = listen( listener, 5 );
    if ( err == SOCKET_ERROR ) {
        MessageBox(HwndDlg,"Socket Listen Failed. Simulation Should Stop","Socket Listen Failed",MB_ICONSTOP|MB_OK);
        return;
    }


    //
    // Only Handle a single Queue
    //

    for(i=0;i<SelNumberOfClients;i++) {

        //
        // Accept incoming connect requests and create wait events for each.
        //

        ZdWorkQueue.Sockets[i] = accept( listener, NULL, NULL );
        if ( ZdWorkQueue.Sockets[i] == INVALID_SOCKET ) {
            MessageBox(HwndDlg,"Accept Failed. Simulation Should Stop","Accept Failed",MB_ICONSTOP|MB_OK);
        }

        //
        // note the 16 says how many concurrent cpu bound threads to allow thru
        // this should be tunable based on the requests. CPU bound requests will
        // really really honor this.
        //

        CompletionPort = CreateIoCompletionPort(
                            (HANDLE)ZdWorkQueue.Sockets[i],
                            CompletionPort,
                            (DWORD)i,
                            16
                            );

        if ( !CompletionPort ) {
            MessageBox(HwndDlg,"CompletionPort Create Failed. Simulation Should Stop","Completion Port Create Failed",MB_ICONSTOP|MB_OK);
        }

        //
        // Start off an asynchronous read on the socket.
        //

        if (!fOneThreadPerClient) {
            ZdWorkQueue.Overlapped[i].hEvent = NULL;
            err = ReadFile( (HANDLE)ZdWorkQueue.Sockets[i],
                            ZdWorkQueue.IoBuffer[i], 4,
                            &ZdWorkQueue.Overlapped[i].InternalHigh,
                            &ZdWorkQueue.Overlapped[i] );
            if ( !err && GetLastError( ) != ERROR_IO_PENDING ) {
                MessageBox(HwndDlg,"ReadFile Failed. Simulation Should Stop","ReadFile Failed",MB_ICONSTOP|MB_OK);
            }
        }

        ZdWorkQueue.NumberOfConnections++;
    }

    //
    // Tell the clients to proceed with the test.
    //

    for(i=0;i<SelNumberOfClients;i++) {
        err = send( ZdWorkQueue.Sockets[i], "go", 2, 0 );
        if ( err != 2 ) {
            MessageBox(HwndDlg,"send() Failed. Simulation Should Stop","send() Failed",MB_ICONSTOP|MB_OK);
        }
    }

    //closesocket( listener );
}

VOID
CreateWorkers(
    void
    )
{
    DWORD ThreadId;
    HANDLE ThreadHandle;
    DWORD i;
    CHAR szBuf[10];
    LPTHREAD_START_ROUTINE workRoutine;

    if ( fDynamicThreadMode ) {
        _stprintf(szBuf, "%d", SelThreadsPerQueue);
        SetWindowText(GetDlgItem(HwndDlg,ID_NUMBER_OF_THREADS), szBuf);
        }


    for (i=0;i<SelThreadsPerQueue;i++) {
        if ( ZdThreads[i].WorkQueue ) {
            continue;
            }
        else {
            ZdThreads[i].WorkQueue = &ZdWorkQueue;
            switch (i) {
                case 0:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_1);
                    break;

                case 1:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_2);
                    break;

                case 2:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_3);
                    break;

                case 3:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_4);
                    break;

                case 4:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_5);
                    break;

                case 5:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_6);
                    break;

                case 6:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_7);
                    break;

                case 7:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_8);
                    break;

                case 8:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_9);
                    break;

                case 9:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_10);
                    break;

                case 10:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_11);
                    break;

                case 11:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_12);
                    break;

                case 12:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_13);
                    break;

                case 13:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_14);
                    break;

                case 14:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_15);
                    break;

                case 15:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_16);
                    break;

                case 16:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_17);
                    break;

                case 17:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_18);
                    break;

                case 18:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_19);
                    break;

                case 19:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_20);
                    break;

                case 20:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_21);
                    break;

                case 21:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_22);
                    break;

                case 22:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_23);
                    break;

                case 23:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_24);
                    break;

                case 24:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_25);
                    break;

                case 25:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_26);
                    break;

                case 26:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_27);
                    break;

                case 27:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_28);
                    break;

                case 28:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_29);
                    break;

                case 29:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_30);
                    break;

                case 30:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_31);
                    break;

                case 31:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_32);
                    break;

                case 32:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_33);
                    break;

                case 33:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_34);
                    break;

                case 34:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_35);
                    break;

                case 35:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_36);
                    break;

                case 36:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_37);
                    break;

                case 37:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_38);
                    break;

                case 38:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_39);
                    break;

                case 39:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_40);
                    break;

                case 40:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_41);
                    break;

                case 41:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_42);
                    break;

                case 42:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_43);
                    break;

                case 43:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_44);
                    break;

                case 44:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_45);
                    break;

                case 45:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_46);
                    break;

                case 46:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_47);
                    break;

                case 47:
                    ZdThreads[i].YourControl = GetDlgItem(HwndDlg,THREAD_48);
                    break;
                }

            if ( fOneThreadPerClient ) {
                workRoutine = NetWorkThread2;
                }
            else {
                workRoutine = NetWorkThread;
                }

            EnableWindow(ZdThreads[i].YourControl,TRUE);
            ZdThreads[i].DynamicMode = fDynamicThreadMode;
            ZdThreads[i].ThreadIndex = i;

            ThreadHandle = CreateThread(
                                NULL,
                                0,
                                workRoutine,
                                &ZdThreads[i],
                                0,
                                &ThreadId
                                );
            if ( !ThreadHandle ) {
                MessageBox(HwndDlg,"Create Worker Thread Failed. Simulation Should Stop","Thread Create Failed",MB_ICONSTOP|MB_OK);
                }

            CloseHandle(ThreadHandle);
            }
        }

}

VOID
WorkerStartWork(
    PZD_THREAD WorkThread
    )
{
    SendMessage(WorkThread->YourControl, BM_SETCHECK, 1, 0);

    SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_HIGHEST);
    if ( WorkThread->DynamicMode ) {
        EnterCriticalSection(&DynamicCritSect);
        ActiveThreadCount++;
        if ( ActiveThreadCount == SelThreadsPerQueue ) {
            ThreadsLoaded++;
            if ( ThreadsLoaded > ZdThreadAddThreshold ) {
                ThreadsLoaded = 0;
                if ( SelThreadsPerQueue < MAX_THREADS_PER_QUEUE ) {
                    SelThreadsPerQueue++;
                    OutputString("Threads Oveloaded. New Thread Added");
                    if ((SelThreadsPerQueue & 1) == 0) {
                        ZdThreadAddThreshold++;
                        OutputString("New threshold %d", ZdThreadAddThreshold);
                    }
                    CreateWorkers();
                    }
                }
            }
        else {
            ThreadsLoaded = 0;
            }
        LeaveCriticalSection(&DynamicCritSect);
        }

}

VOID
WorkerEndWork(
    PZD_THREAD WorkThread
    )
{
    SendMessage(WorkThread->YourControl, BM_SETCHECK, 0, 0);

    SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_NORMAL);
    if ( WorkThread->DynamicMode ) {
        EnterCriticalSection(&DynamicCritSect);
        ActiveThreadCount--;
        LeaveCriticalSection(&DynamicCritSect);
        }

}