arzmq-sys 0.6.3

Low-level bindings to the zeromq library
Documentation
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
#ifdef SIMULATE
#include "nsProtoSimAgent.h"
#else
#include "protoApp.h"
#include "protoNet.h"
#include "protoRouteMgr.h"
#endif  // if/else SIMULATE

#include <stdlib.h>  // for atoi()
#include <stdio.h>   // for stdout/stderr printouts
#include <string.h>

class ProtoExample :
#ifdef SIMULATE
                     public NsProtoSimAgent
#else
                     public ProtoApp
#endif  // if/else SIMULATE
                     
{
    public:
        ProtoExample();
        ~ProtoExample();

        /**
        * Override from ProtoApp or NsProtoSimAgent base
        */
        bool OnStartup(int argc, const char*const* argv);
        /**
        * Override from ProtoApp or NsProtoSimAgent base
        */
        bool ProcessCommands(int argc, const char*const* argv);
        /**
        * Override from ProtoApp or NsProtoSimAgent base
        */
        void OnShutdown();
        /**
        * Override from ProtoApp or NsProtoSimAgent base
        */
        virtual bool HandleMessage(unsigned int len, const char* txBuffer,const ProtoAddress& srcAddr) {return true;}

    private:
        enum CmdType {CMD_INVALID, CMD_ARG, CMD_NOARG};
        static CmdType GetCmdType(const char* string);
        bool OnCommand(const char* cmd, const char* val);        
        void Usage();

        void OnTxTimeout(ProtoTimer& theTimer);
        void OnUdpSocketEvent(ProtoSocket&       theSocket, 
                            ProtoSocket::Event theEvent);
        void OnClientSocketEvent(ProtoSocket&       theSocket, 
                               ProtoSocket::Event theEvent);
        void OnServerSocketEvent(ProtoSocket&       theSocket, 
                               ProtoSocket::Event theEvent);
        static const char* const CMD_LIST[];
        static void SignalHandler(int sigNum);

        // ProtoTimer/ UDP socket demo members
        ProtoTimer          tx_timer;
        ProtoAddress        dst_addr;

        ProtoSocket         udp_tx_socket;
        ProtoSocket         udp_rx_socket;

        // TCP socket demo members
        ProtoSocket         server_socket;
        ProtoSocket         client_socket;
        unsigned int        client_msg_count;
        ProtoSocket::List   connection_list;
        bool                use_html;

}; // end class ProtoExample


// (TBD) Note this #if/else code could be replaced with something like
// a PROTO_INSTANTIATE(ProtoExample) macro defined differently
// in "protoApp.h" and "nsProtoSimAgent.h"
#ifdef SIMULATE
#ifdef NS2
static class NsProtoExampleClass : public TclClass
{
	public:
		NsProtoExampleClass() : TclClass("Agent/ProtoExample") {}
	 	TclObject *create(int argc, const char*const* argv) 
			{return (new ProtoExample());}
} class_proto_example;	
#endif // NS2


#else

// Our application instance 
PROTO_INSTANTIATE_APP(ProtoExample) 

#endif  // SIMULATE

ProtoExample::ProtoExample()
: udp_tx_socket(ProtoSocket::UDP),
  udp_rx_socket(ProtoSocket::UDP),
  server_socket(ProtoSocket::TCP), 
  client_socket(ProtoSocket::TCP), 
  client_msg_count(0), use_html(false)
{    
    tx_timer.SetListener(this, &ProtoExample::OnTxTimeout);
    udp_tx_socket.SetNotifier(&GetSocketNotifier());
    udp_tx_socket.SetListener(this, &ProtoExample::OnUdpSocketEvent);
    udp_rx_socket.SetNotifier(&GetSocketNotifier());
    udp_rx_socket.SetListener(this, &ProtoExample::OnUdpSocketEvent);
    client_socket.SetNotifier(&GetSocketNotifier());
    client_socket.SetListener(this, &ProtoExample::OnClientSocketEvent);
    server_socket.SetNotifier(&GetSocketNotifier());
    server_socket.SetListener(this, &ProtoExample::OnServerSocketEvent);
}

ProtoExample::~ProtoExample()
{
    
}

void ProtoExample::Usage()
{
    fprintf(stderr, "protoExample [send <host>/<port>] [recv [<group>/]<port>]\n"
                    "             connect <host>/<port>] [listen <port>][html]\n");
}  // end ProtoExample::Usage()


const char* const ProtoExample::CMD_LIST[] =
{
    "+send",       // Send UDP packets to destination host/port
    "+recv",       // Recv UDP packets on <port> (<group> addr optional)
    "+connect",    // TCP connect and send to destination host/port 
    "+listen",     // Listen for TCP connections on <port>
    "-html",       // listening TCP server provides canned HTML response to client(s) (use with "listen" option)
    NULL
};

ProtoExample::CmdType ProtoExample::GetCmdType(const char* cmd)
{
    if (!cmd) return CMD_INVALID;
    unsigned int len = strlen(cmd);
    bool matched = false;
    CmdType type = CMD_INVALID;
    const char* const* nextCmd = CMD_LIST;
    while (*nextCmd)
    {
        if (!strncmp(cmd, *nextCmd+1, len))
        {
            if (matched)
            {
                // ambiguous command (command should match only once)
                return CMD_INVALID;
            }
            else
            {
                matched = true;   
                if ('+' == *nextCmd[0])
                    type = CMD_ARG;
                else
                    type = CMD_NOARG;
            }
        }
        nextCmd++;
    }
    return type; 
}  // end ProtoExample::GetCmdType()

bool ProtoExample::OnStartup(int argc, const char*const* argv)
{
#ifdef _WIN32_WCE
    if (!OnCommand("recv", "224.225.1.2/5002"))
    {
        TRACE("Error with \"recv\" command ...\n");
    }
    if (!OnCommand("send", "224.225.1.2/5002"))
    {
        TRACE("Error with \"send\" command ...\n");
    }
#endif // _WIN32_WCE
    
    if (!ProcessCommands(argc, argv))
    {
        PLOG(PL_ERROR, "protoExample: Error! bad command line\n");
        return false;
    }  
    
#ifndef SIMULATE     

    // Here's some code to test the ProtoSocket routines for network interface info       
    ProtoAddress localAddress;
    char nameBuffer[256];
    nameBuffer[255] = '\0';

    // ljt test
    ProtoAddress dst;
    dst.ResolveFromString("fe80::426c:8fff:fe31:4f8e");
    if (dst.ResolveToName(nameBuffer,255))
        TRACE("protoExample: LJT ipv6 host name: %s\n", nameBuffer);

    
    if (localAddress.ResolveLocalAddress())
    {
        TRACE("protoExample: local default IP address: %s\n", localAddress.GetHostString());
        if (localAddress.ResolveToName(nameBuffer, 255))
            TRACE("protoExample: local default host name: %s\n", nameBuffer);
        else
            TRACE("protoExample: unable to resolve local default IP address to name\n");
    }      
    else
    {
        TRACE("protoExample: unable to determine local default IP address\n");
    }
    
    // Get all of our ifaces and dump some info (name, MAC addr, IP addrs, etc)
    unsigned int ifaceCount = ProtoNet::GetInterfaceCount();
    if (ifaceCount > 0)
    {
        // Allocate array to hold the indices
        unsigned int* indexArray = new unsigned int[ifaceCount];
        if (NULL == indexArray)
        {
            PLOG(PL_ERROR, "protoExample: new indexArray[%u] error: %s\n", ifaceCount, GetErrorString());
            return false;
        }
        if (ProtoNet::GetInterfaceIndices(indexArray, ifaceCount) != ifaceCount)
        {
            PLOG(PL_ERROR, "protoExample: GetInterfaceIndices() error?!\n");
            delete[] indexArray;
            return false;
        }
        for (unsigned int i = 0; i < ifaceCount; i++)
        {
            unsigned int index = indexArray[i];
            TRACE("protoExample: interface index %u ", index);
            // Get the iface name
            if (ProtoNet::GetInterfaceName(index, nameBuffer, 255))
                TRACE("has name \"%s\" ", nameBuffer);
            else
                TRACE("name \"unknown\" ");
            // Get MAC addr for the iface
            ProtoAddress ifaceAddr;
            if (ProtoNet::GetInterfaceAddress(nameBuffer, ProtoAddress::ETH, ifaceAddr))
                TRACE("with MAC addr %s\n", ifaceAddr.GetHostString());
            else
                TRACE("with no MAC addr\n");
            
            ProtoAddressList addrList;
            // Get IPv4 addrs for the iface
            if (ProtoNet::GetInterfaceAddressList(nameBuffer, ProtoAddress::IPv4, addrList))
            {
                TRACE("          IPv4 addresses:");
                ProtoAddressList::Iterator iterator(addrList);
                while (iterator.GetNextAddress(ifaceAddr))
                {
					TRACE(" %s/%d", ifaceAddr.GetHostString(),ProtoNet::GetInterfaceAddressMask(nameBuffer, ifaceAddr));
                    if (ifaceAddr.IsLinkLocal()) TRACE(" (link local)");
                }
                TRACE("\n");
            }
            addrList.Destroy();
            // Get IPv6 addrs for the iface
            if (ProtoNet::GetInterfaceAddressList(nameBuffer, ProtoAddress::IPv6, addrList))
            {
                TRACE("          IPv6 addresses:");
                ProtoAddressList::Iterator iterator(addrList);
                while (iterator.GetNextAddress(ifaceAddr))
                {

                    TRACE(" %s/%d", ifaceAddr.GetHostString(), ProtoNet::GetInterfaceAddressMask(nameBuffer, ifaceAddr));

					if (ifaceAddr.IsLinkLocal()) TRACE(" (link local)");
                }
                TRACE("\n");
            }
            addrList.Destroy();

			// Test our interface add routine
            /*
			char * ifaceName = "192.168.1.6";
			ProtoAddress tmpAddr;
			tmpAddr.ConvertFromString("192.168.1.6");
			int maskLen = 24;
			bool result = ProtoNet::AddInterfaceAddress(ifaceName, tmpAddr, maskLen);
            */


#ifndef WIN32
            // This code should work for Linux and BSD (incl. Mac OSX)
            // Get IPv4 group memberships for interfaces
            if (ProtoNet::GetGroupMemberships(nameBuffer, ProtoAddress::IPv4, addrList))
            {
                TRACE("          IPv4 memberships:");
                ProtoAddressList::Iterator iterator(addrList);
                ProtoAddress groupAddr;
                while (iterator.GetNextAddress(groupAddr))
                {
                    TRACE(" %s", groupAddr.GetHostString());
                }       
                TRACE("\n");
            }
            addrList.Destroy();
            if (ProtoNet::GetGroupMemberships(nameBuffer, ProtoAddress::IPv6, addrList))
            {
                TRACE("          IPv6 memberships:");
                ProtoAddressList::Iterator iterator(addrList);
                ProtoAddress groupAddr;
                while (iterator.GetNextAddress(groupAddr))
                {
                    TRACE(" %s", groupAddr.GetHostString());
                }       
                TRACE("\n");
            }
            addrList.Destroy();
#endif // WIN32
        }
        delete[] indexArray;
    }
    else
    {
        TRACE("protoExample: host has no network interfaces?!\n");
    }  
    // Here's some code to get the system routing table
    ProtoRouteTable routeTable;
    ProtoRouteMgr* routeMgr = ProtoRouteMgr::Create();
    if (NULL == routeMgr)
    {
        PLOG(PL_ERROR, "ProtoExample::OnStartup() error creating route manager\n");
        return false;
    }
    if (!routeMgr->Open())
    {
        PLOG(PL_ERROR, "ProtoExample::OnStartup() error opening route manager\n");
        return false;    
    }
    if (!routeMgr->GetAllRoutes(ProtoAddress::IPv4, routeTable))
        PLOG(PL_ERROR, "ProtoExample::OnStartup() warning getting system routes\n");     
    // Display whatever routes we got
    ProtoRouteTable::Iterator iterator(routeTable);
    ProtoRouteTable::Entry* entry;
    PLOG(PL_ALWAYS, "IPv4 Routing Table:\n");
    PLOG(PL_ALWAYS, "%16s/Prefix %-12s   ifIndex   Metric\n", "Destination", "Gateway");
    while (NULL != (entry = iterator.GetNextEntry()))
    {
        PLOG(PL_ALWAYS, "%16s/%-3u     ",
                entry->GetDestination().GetHostString(), entry->GetPrefixSize());
        const ProtoAddress& gw = entry->GetGateway();
        PLOG(PL_ALWAYS, "%-16s %-02u     %d\n",
                gw.IsValid() ? gw.GetHostString() : "0.0.0.0", 
                entry->GetInterfaceIndex(),
                entry->GetMetric());
    }
    //ProtoAddress dst;
    //dst.ResolveFromString("10.1.2.3");
    //ProtoAddress gw;
    //routeMgr->SetRoute(dst, 32, gw, 4, 0);
    routeMgr->Close();
            
    fprintf(stderr, "\nprotoExample: entering main loop (<CTRL-C> to exit)...\n");
#endif // !SIMULATE
    return true;
}  // end ProtoExample::OnStartup()

void ProtoExample::OnShutdown()
{
    if (tx_timer.IsActive()) tx_timer.Deactivate();
    if (udp_tx_socket.IsOpen()) udp_tx_socket.Close();
    if (udp_rx_socket.IsOpen()) udp_rx_socket.Close();
    if (server_socket.IsOpen()) server_socket.Close();
    if (client_socket.IsOpen()) client_socket.Close();
    connection_list.Destroy();   
#ifndef SIMULATE
    PLOG(PL_ERROR, "protoExample: Done.\n");
#endif // SIMULATE
    CloseDebugLog();
}  // end ProtoExample::OnShutdown()

bool ProtoExample::ProcessCommands(int argc, const char*const* argv)
{
    // Dispatch command-line commands to our OnCommand() method
    int i = 1;
    while ( i < argc)
    {
        // Is it a class ProtoExample command?
        switch (GetCmdType(argv[i]))
        {
            case CMD_INVALID:
            {
#ifndef SIMULATE
                PLOG(PL_ERROR, "ProtoExample::ProcessCommands() Invalid command:%s\n", 
                        argv[i]);
#endif // SIMULATE
                return false;
            }
            case CMD_NOARG:
                if (!OnCommand(argv[i], NULL))
                {
                    PLOG(PL_ERROR, "ProtoExample::ProcessCommands() ProcessCommand(%s) error\n", 
                            argv[i]);
                    return false;
                }
                i++;
                break;
            case CMD_ARG:
                if (!OnCommand(argv[i], argv[i+1]))
                {
                    PLOG(PL_ERROR, "ProtoExample::ProcessCommands() ProcessCommand(%s, %s) error\n", 
                            argv[i], argv[i+1]);
                    return false;
                }
                i += 2;
                break;
        }
    }
    return true;  
}  // end ProtoExample::ProcessCommands()

bool ProtoExample::OnCommand(const char* cmd, const char* val)
{

    // (TBD) move command processing into Mgen class ???
    CmdType type = GetCmdType(cmd);
    ASSERT(CMD_INVALID != type);
    unsigned int len = strlen(cmd);
    if ((CMD_ARG == type) && !val)
    {
        PLOG(PL_ERROR, "ProtoExample::ProcessCommand(%s) missing argument\n", cmd);
        return false;
    }
    else if (!strncmp("send", cmd, len))
    {
        if (!udp_tx_socket.Open(0, ProtoAddress::IPv4, false))
        {
            PLOG(PL_ERROR, "ProtoExample::ProcessCommand(send) error opening udp_tx_socket\n");
            return false;    
        }    
        char string[256];
        strncpy(string, val, 256);
        string[255] ='\0';
        char* ptr = strchr(string, '/');
        if (ptr) 
        {
            *ptr++ = '\0';
            if (!dst_addr.ResolveFromString(string))
            {
                PLOG(PL_ERROR, "ProtoExample::ProcessCommand(send) error: invalid <host>\n");
                return false;
            }
            dst_addr.SetPort(atoi(ptr));
            udp_tx_socket.Connect(dst_addr);
            tx_timer.SetInterval(1.0);
            tx_timer.SetRepeat(-1);
            OnTxTimeout(tx_timer);
            if (!tx_timer.IsActive()) ActivateTimer(tx_timer);
        }
        else
        {
            PLOG(PL_ERROR, "ProtoExample::ProcessCommand(send) error: <port> not specified\n");
            return false;   
        } 
    }
    else if (!strncmp("recv", cmd, len))
    {     
        char string[256];
        strncpy(string, val, 256);
        string[255] ='\0';
        char* portPtr = strchr(string, '/');
        ProtoAddress groupAddr;
        if (portPtr) 
        {
            *portPtr++ = '\0';
            if (!groupAddr.ResolveFromString(string))
            {
                PLOG(PL_ERROR, "ProtoExample::ProcessCommand(recv) error: invalid <groupAddr>\n");
                return false;
            }
        }
        else
        {
            portPtr = string; 
        } 
        UINT16 thePort;
        if (1 == sscanf(portPtr, "%hu", &thePort))
        {
            
            if (!udp_rx_socket.Open(0, ProtoAddress::IPv4, false))
            {
                PLOG(PL_ERROR, "ProtoExample::ProcessCommand(recv) error opening udp_rx_socket\n");
                return false;   
            }
            udp_rx_socket.EnableRecvDstAddr();  // for testing this feature
            if (groupAddr.IsValid() && groupAddr.IsMulticast())
            {
                
                bool result = udp_rx_socket.SetReuse(true);
                TRACE("set port reuse result %d...\n", result);
            }
            if (!udp_rx_socket.Bind(thePort))
            {
                PLOG(PL_ERROR, "ProtoExample::ProcessCommand(recv) error binding udp_rx_socket\n");
                return false;   
            }
            if (groupAddr.IsValid() && groupAddr.IsMulticast())
            {
                if (!(udp_rx_socket.JoinGroup(groupAddr)))
                {
                    PLOG(PL_ERROR, "ProtoExample::ProcessCommand(recv) error joining group\n");
                    udp_rx_socket.Close();
                    return false;  
                }
            }            
        }
        else
        {
            PLOG(PL_ERROR, "ProtoExample::ProcessCommand(recv) error: <port> not specified\n");
            return false;   
        } 
    }
    else if (!strncmp("connect", cmd, len))
    {
        char string[256];
        strncpy(string, val, 256);
        string[255] ='\0';
        char* ptr = strchr(string, '/');
        if (ptr) 
        {
            *ptr++ = '\0';
            ProtoAddress server;
            if (!server.ResolveFromString(string))
            {
                PLOG(PL_ERROR, "ProtoExample::ProcessCommand(connect) error: invalid <host>\n");
                return false;
            }
            server.SetPort(atoi(ptr));
            TRACE("calling connect ...\n");
            if (!client_socket.Connect(server))
            {
                PLOG(PL_ERROR, "ProtoExample::ProcessCommand(connect) error connecting\n");
                return false;
            }
            client_msg_count = 5;  // (Send 5 messages, then disconnect)
            TRACE("   (completed. starting output notification ...\n");
            client_socket.StartOutputNotification();
        }
        else
        {
            PLOG(PL_ERROR, "ProtoExample::ProcessCommand(connect) error: <port> not specified\n");
            return false;   
        } 
    }
    else if (!strncmp("listen", cmd, len))
    {
        UINT16 thePort;
        if (1 == sscanf(val, "%hu", &thePort))
        {
            if (!server_socket.Listen(thePort))
            {
                PLOG(PL_ERROR, "ProtoExample::ProcessCommand(listen) error listening\n");
                return false;   
            }            
        }
        else
        {
            PLOG(PL_ERROR, "ProtoExample::ProcessCommand(connect) error: <port> not specified\n");
            return false;   
        } 
    }
    else if (!strncmp("html", cmd, len))
    {
        use_html = true;
    }
    else if (!strncmp("background", cmd, len))
    {
        // do nothing (this command was scanned immediately at startup)
    }
    return true;
}  // end ProtoExample::OnCommand()

void ProtoExample::OnTxTimeout(ProtoTimer& /*theTimer*/)
{
    const char* string = "Hello there UDP peer, how are you doing?";
    unsigned int len = strlen(string) + 1;
    unsigned int numBytes = len;
    
    if (!udp_tx_socket.SendTo(string, numBytes, dst_addr))
    {
        PLOG(PL_ERROR, "ProtoExample::OnTxTimeout() error sending to %s/%hu ...\n",
                dst_addr.GetHostString(), dst_addr.GetPort());   
    }
    else if (len != numBytes)
    {
        PLOG(PL_ERROR, "ProtoExample::OnTxTimeout() incomplete SendTo()\n");                
        TRACE("   (only sent %lu of %lu bytes)\n", numBytes, len);
    }

}  // end ProtoExample::OnTxTimeout()

void ProtoExample::OnUdpSocketEvent(ProtoSocket&       theSocket, 
                                    ProtoSocket::Event theEvent)
{
    if (&theSocket == &udp_tx_socket)
        TRACE("ProtoExample::OnUdpTxSocketEvent(");
    else
        TRACE("ProtoExample::OnUdpRxSocketEvent(");
    switch (theEvent)
    {
        case ProtoSocket::INVALID_EVENT:
            TRACE("ERROR) ...\n");
            break;
        case ProtoSocket::CONNECT:
            TRACE("CONNECT) ...\n");
            break;  
        case ProtoSocket::ACCEPT:
            TRACE("ACCEPT) ...\n");
            break; 
        case ProtoSocket::SEND:
        {
            TRACE("SEND) ...\n");
            break; 
        }
        case ProtoSocket::RECV:
        {
            static unsigned long count = 0;
            TRACE("RECV) ...\n");
            ProtoAddress srcAddr, dstAddr;
            char buffer[1024];
            unsigned int len = 1024;
            // This RecvFrom() call w/ dstAddr may not be suppoted on WIN32 yet
            if (theSocket.RecvFrom(buffer, len, srcAddr, dstAddr))
            {
                buffer[len] = '\0';
                if (len)
                {
                    char dstAddrString[32];
                    dstAddr.GetHostString(dstAddrString, 32);
                    TRACE("ProtoExample::OnUdpSocketEvent(%lu) received \"%s\" from %s to dest %s\n", 
                           count++, buffer, srcAddr.GetHostString(), dstAddrString);
                }
                else
                {
                    TRACE("ProtoExample::OnUdpSocketEvent() received 0 bytes\n");
                }
            }
            else
            {
                PLOG(PL_ERROR, "ProtoExample::OnUdpSocketEvent() error receiving!\n");
            }
            break; 
        }
        case ProtoSocket::DISCONNECT:
            TRACE("DISCONNECT) ...\n");
            break;   
        
        case ProtoSocket::EXCEPTION:
            TRACE("EXCEPTION) ...\n");
            break;  
        
        case ProtoSocket::ERROR_:
            TRACE("ERROR_) ...\n");
            break;  
    }
}  // end ProtoExample::OnUdpSocketEvent()


void ProtoExample::OnClientSocketEvent(ProtoSocket&       theSocket, 
                                       ProtoSocket::Event theEvent)
{
    TRACE("ProtoExample::OnClientSocketEvent(");
    switch (theEvent)
    {
        case ProtoSocket::INVALID_EVENT:
            TRACE("ERROR) ...\n");
            break;
        case ProtoSocket::CONNECT:
            TRACE("CONNECT) ...\n");
            break;  
        case ProtoSocket::ACCEPT:
            TRACE("ACCEPT) ...\n");
            break; 
        case ProtoSocket::SEND:
        {
            TRACE("SEND) ...\n");
            if (0 == client_msg_count)
            {
                TRACE("protoExample: client message transmission completed.\n");
                client_socket.StopOutputNotification();  // don't send any more
                // Uncomment this to have the client cleanly disconnect after transmission
                // (otherwise, linger for server response until user "Ctrl-C" exit)
                client_socket.Shutdown();
				return;
            }         
            const char* string = "Hello there ProtoServer, how are you doing?";
            unsigned int len = strlen(string) + 1;
            unsigned int numBytes = len;
			client_msg_count--;
            if (!client_socket.Send(string, numBytes))
            {
                PLOG(PL_ERROR, "ProtoExample::OnClientSocketEvent() error sending\n");   
            }
            else if (len != numBytes)
            {
                PLOG(PL_WARN, "ProtoExample::OnClientSocketEvent() incomplete Send()\n");                
				TRACE("   (only sent %lu of %lu bytes (msgCount:%d))\n", numBytes, len, client_msg_count);
			} 
            else
            {
                TRACE("sent %u bytes to ProtoServer ...\n", len);
            }  
			
            break; 
        }
        case ProtoSocket::RECV:
        {
            TRACE("RECV) ...\n");
            char buffer[1024];
            unsigned int len = 1024;
            theSocket.Recv(buffer, len);
            if (0 != len)
            {
                buffer[len-1] = '\0';
                TRACE("ProtoExample::OnClientSocketEvent() received %u bytes \"%s\" from %s\n", 
                       len, buffer, theSocket.GetDestination().GetHostString());
            }
            else
            {
                TRACE("ProtoExample::OnClientSocketEvent() received 0 bytes\n");
            }
            break;  
        }
        case ProtoSocket::DISCONNECT:
            TRACE("DISCONNECT) ...\n");
            client_socket.Close();
            break;    
        
        case ProtoSocket::EXCEPTION:
            TRACE("EXCEPTION) ...\n");
            break;  
        
        case ProtoSocket::ERROR_:
            TRACE("ERROR_) ...\n");
            break;   
    }
}  // end ProtoExample::OnClientSocketEvent()

const char* PROTO_HTML = 
    "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n"
    "<html>\n"
    "<head>\n"
    "<title>protoExample</title>\n"
    "</head>\n"
    "<body>\n"
    "<p>This is a canned response from your friendly, neighborhood <b>protoExample</b> server.</p>\n"
    "</body>\n"
    "</html>\n";
  
void ProtoExample::OnServerSocketEvent(ProtoSocket&       theSocket, 
                                       ProtoSocket::Event theEvent)
{
    TRACE("ProtoExample::OnServerSocketEvent(");
    switch (theEvent)
    {
        case ProtoSocket::INVALID_EVENT:
            TRACE("INVALID_EVENT) ...\n");
            break;
        case ProtoSocket::CONNECT:
            TRACE("CONNECT) ...\n");
            break;  
        case ProtoSocket::ACCEPT:
        {
            TRACE("ACCEPT) ...\n");
            ProtoSocket* connectedSocket = new ProtoSocket(ProtoSocket::TCP);
			if (NULL == connectedSocket)
            {
                PLOG(PL_ERROR, "ProtoExample::OnServerSocketEvent(ACCEPT) new ProtoSocket error\n");
                break;
            }
            connectedSocket->SetListener(this, &ProtoExample::OnServerSocketEvent); 

            if (!server_socket.Accept(connectedSocket))
            {
                PLOG(PL_ERROR, "ProtoExample::OnServerSocketEvent(ACCEPT) error accepting connection\n");
                delete connectedSocket;
                break;
            }

            if (!connection_list.AddSocket(*connectedSocket))
            {
                PLOG(PL_ERROR, "ProtoExample::OnServerSocketEvent(ACCEPT) error adding client to list\n");
                delete connectedSocket;
            }
            PLOG(PL_ERROR, "protoExample: accepted connection from %s\n",
                    connectedSocket->GetDestination().GetHostString());
            break; 
        }
        case ProtoSocket::SEND:
            TRACE("SEND) ...\n");
            break; 
        case ProtoSocket::RECV:
        {
            TRACE("RECV) ...\n");
            char buffer[1024];
            unsigned int len = 1024;
            if (theSocket.Recv(buffer, len))
            {
                if (0 != len)
                {
                    buffer[len-1] = '\0';
                    TRACE("ProtoExample::OnServerSocketEvent() received %u bytes \"%s\" from %s\n", 
                           len, buffer, theSocket.GetDestination().GetHostString());
                    
                    // Talk back to client
                    const char* string = "Hi ProtoClient, I'm fine.  How about you?";
                    if (use_html) string = PROTO_HTML;
                    len = strlen(string) + 1;
                    unsigned int numBytes = len;
                    if (!theSocket.Send(string, numBytes))
                    {
                        PLOG(PL_ERROR, "ProtoExample::OnServerSocketEvent() error sending\n");   
                    }
                    else if (len != numBytes)
                    {
                        PLOG(PL_ERROR, "ProtoExample::OnServerSocketEvent() incomplete Send()\n");                
                        TRACE("   (only sent %lu of %lu bytes)\n", numBytes, len);
                    } 
                    else
                    {
                        TRACE("sent %u bytes back to ProtoClient ...\n", len);
                    }  
                    if (use_html) theSocket.Shutdown();
                }
                else
                {
                    TRACE("ProtoExample::OnServerSocketEvent() received 0 bytes\n");
                }
                
            }
            else
            {
                PLOG(PL_ERROR, "ProtoExample::OnServerSocketEvent() error receiving\n");
            }
            break; 
        }
        case ProtoSocket::DISCONNECT:
            TRACE("DISCONNECT) ...\n");
            if(&theSocket == &server_socket)
            {
                TRACE("server_socket disconnected!\n");
                break;
            }
            theSocket.Close();
            connection_list.RemoveSocket(theSocket);
            break;   
        
        case ProtoSocket::EXCEPTION:
            TRACE("EXCEPTION) ...\n");
            break;  
        
        case ProtoSocket::ERROR_:
            TRACE("ERROR_) ...\n");
            break;  
    }
}  // end ProtoExample::OnServerSocketEvent()