home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / ReplicaNetFreewareV5_4.exe / data1.cab / Program_Executable_Files / Example3 / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-30  |  3.6 KB  |  162 lines

  1. /* START_LICENSE_HEADER
  2.  
  3. Copyright (C) 2000 Martin Piper, original design and program code
  4. Copyright (C) 2001-2005 Replica Software
  5.  
  6. This program file is copyright (C) Replica Software and can only be used under license.
  7. For more information visit: http://www.replicanet.com/
  8. Or email: info@replicanet.com
  9.  
  10. END_LICENSE_HEADER */
  11. /*
  12. This example shows how to use the reliable UDP transport on its own
  13. */
  14. #include <windows.h>
  15. #include "RNXPURL/Inc/XPURL.h"
  16.  
  17. using namespace RNReplicaNet;
  18.  
  19. int main(int argc,char **argv)
  20. {
  21.     // Make sure the default transports are registered otherwise FindTransport may not find the transport we are looking for
  22.     XPURL::RegisterDefaultTransports();
  23.  
  24.     // The XPURL class enables us to find the transport we want
  25.     XPURL xpurl;
  26.  
  27.     // Some transport pointers
  28.     Transport *transa,*transb;
  29.  
  30.     // Test to make sure we have the UDP transport loaded
  31.     if (!xpurl.FindTransport("UDP@"))
  32.     {
  33.         // If not loaded then report an error
  34.         printf("Cannot find a transport to use\n");
  35.         exit(-1);
  36.     }
  37.  
  38.     // Create our reliable UDP listen transport
  39.     transa = xpurl.FindTransport("UDP@")->Allocate();
  40.     transa->Listen();        // We could force a port numebr to be used here e.g. transa->Listen(4444); but we let the transport pick a port number
  41.     // Query what the transport is listening as
  42.     std::string url = transa->ExportURL();
  43.     printf("The listening transport URL is '%s'\n",url.c_str());
  44.  
  45.     // Create our reliable UDP transport to connect with
  46.     transb = xpurl.FindTransport("UDP@")->Allocate();
  47.  
  48.     // Try to connect to our listen transport
  49.     transb->Connect(url);
  50.  
  51.     // Sleep a short while to make sure the connection is established
  52.     Sleep(50);
  53.  
  54.     // A buffer for the data we are going to send
  55.     char buffer[256];
  56.  
  57.     // Accept the transport that connected
  58.     Transport *ret = transa->Accept();
  59.  
  60.     // Sleep for a little while
  61.     Sleep(50);
  62.  
  63.     // If we couldn't accept the connection then report an error
  64.     if (!ret)
  65.     {
  66.         printf("UDP transa couldn't accept\n");
  67.         exit(-1);
  68.     }
  69.  
  70.     int i;
  71.     // Send 100 packets on both transports with the reliable method
  72.     for (i=0;i<100;i++)
  73.     {
  74.         ret->SendReliable(buffer,8);
  75.         transb->SendReliable(buffer,8);
  76.     }
  77.  
  78.     // Sleep to give the packets time to get there...
  79.     Sleep(50);
  80.  
  81.     int gota=0,gotb=0;
  82.  
  83.     // Total up the number of packet received from each transport
  84.     while(ret->Recv(buffer,256) > 0)
  85.     {
  86.         gota++;
  87.     }
  88.  
  89.     while(transb->Recv(buffer,256) > 0)
  90.     {
  91.         gotb++;
  92.     }
  93.  
  94.     // Print our results
  95.     printf("Reliable gota = %d   gotb = %d\n",gota,gotb);
  96.  
  97.  
  98.     // Send 100 packets on both transports with the certain method
  99.     for (i=0;i<100;i++)
  100.     {
  101.         ret->SendCertain(buffer,8);
  102.         transb->SendCertain(buffer,8);
  103.     }
  104.  
  105.     // Sleep to give the packets time to get there...
  106.     Sleep(50);
  107.  
  108.     int cgota=0,cgotb=0;
  109.  
  110.     // Total up the number of packet received from each transport
  111.     while(ret->Recv(buffer,256) > 0)
  112.     {
  113.         cgota++;
  114.     }
  115.  
  116.     while(transb->Recv(buffer,256) > 0)
  117.     {
  118.         cgotb++;
  119.     }
  120.  
  121.     // Print our results
  122.     printf("Certain gota = %d   gotb = %d\n",gota,gotb);
  123.  
  124.  
  125.  
  126.     // Now send 100 packets as before but using the unreliable method
  127.     for (i=0;i<100;i++)
  128.     {
  129.         ret->Send(buffer,8);
  130.         transb->Send(buffer,8);
  131.     }
  132.  
  133.     Sleep(50);
  134.  
  135.     int ugota=0,ugotb=0;
  136.  
  137.     while( ret->Recv(buffer,256) > 0)
  138.     {
  139.         ugota++;
  140.     }
  141.  
  142.     while( transb->Recv(buffer,256) > 0)
  143.     {
  144.         ugotb++;
  145.     }
  146.  
  147.     // Print out the results
  148.     printf("Unreliable gota = %d   gotb = %d\n",ugota,ugotb);
  149.  
  150.     // Finish the example
  151.     if (gota != 100 || gotb != 100 || cgota != 100 || cgotb != 100 || ugota != 100 || ugotb != 100)
  152.     {
  153.         printf("Somewhere some packets got lost\n");
  154.     }
  155.     else
  156.     {
  157.         printf("All packets were received\n");
  158.     }
  159.     return 0;
  160. }
  161.  
  162.