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 / Example7 / Example7.cpp next >
Encoding:
C/C++ Source or Header  |  2005-10-30  |  5.1 KB  |  194 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. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <Windows.h>
  14.  
  15. #include "RNXPSession/Inc/XPSession.h"
  16. #include "RNPlatform/Inc/ThreadClass.h"
  17. #include "RNPlatform/Inc/PlatformInfo.h"
  18. #include "RNXPURL/Inc/XPURL.h"
  19.  
  20. using namespace RNReplicaNet;
  21.  
  22. char buffer_classcreate[]="This is a typcial class create message which can be quite large and contain a number of bytes";
  23. char buffer_datablock[]="A data block message which is small";
  24.  
  25. // This forces the session to be polled a few times if the program is not operating with multi-threading
  26. // If multi-threading is active then it is not absolutely needed to force a poll
  27. static void ForcePoll(XPSession *aSession)
  28. {
  29.     // If the platform is not multithreaded then call the XPSession::Poll() function a few times
  30.     if (!PlatformInfo::IsThreaded())
  31.     {
  32.         int i;
  33.         for (i=0;i<20;i++)
  34.         {
  35.             aSession->Poll();
  36.         }
  37.     }
  38. }
  39.  
  40. // The example starts here
  41. int main(int argc,char **argv)
  42. {
  43.     XPSession *aSession;
  44.  
  45.     aSession = XPSession::Allocate();
  46.  
  47.     aSession->SetCanSpider(true);
  48.     aSession->SetCanBecomeMaster(true);
  49.     aSession->SetEncryption(true);
  50.  
  51.     printf("Searching for a session\n");
  52.  
  53.     int normalsessid = aSession->GetSessionID();
  54.  
  55.     aSession->Find();
  56.  
  57.     ForcePoll(aSession);
  58.  
  59.     /* Pause for a while for any other sessions to respond. A game can do anything while waiting for replies. */
  60.     CurrentThread::Sleep(2000);
  61.  
  62.     ForcePoll(aSession);
  63.  
  64.     std::string resfind;
  65.  
  66.     do
  67.     {
  68.         resfind = aSession->EnumerateFound();
  69.         if (resfind=="")
  70.         {
  71.             break;
  72.         }
  73.         printf("Found a session '%s'\n",resfind.c_str());
  74.         // If a found session string contains 'Example7' then we have found a session to join.
  75.         if (resfind.find("Example7") != std::string::npos)
  76.         {
  77.             break;
  78.         }
  79.     } while (resfind != "");
  80.  
  81.     if (resfind != "")
  82.     {
  83.         printf("Trying to join the session '%s'\n",resfind.c_str());
  84.         aSession->Join(resfind);
  85.     }
  86.     else
  87.     {
  88.         printf("Find session failed to find a suitable session\n");
  89.         aSession->Create("Example7");
  90.     }
  91.  
  92.     aSession->Poll();
  93.  
  94.     std::string url = aSession->ExportURL();
  95.  
  96.     printf("My URL is '%s'\n",url.c_str());
  97.  
  98.     aSession->Poll();
  99.  
  100.     while(aSession->GetStatus() == XPSession::kXPSession_EOK)
  101.     {
  102.         aSession->Poll();
  103.  
  104.         if (normalsessid != aSession->GetSessionID())
  105.         {
  106.             normalsessid = aSession->GetSessionID();
  107.             printf("\nI am now session ID %d\n",normalsessid);
  108.             aSession->DataSendToSession(0,buffer_classcreate,sizeof(buffer_classcreate),true);
  109.         }
  110.  
  111.         if (aSession->IsMaster())
  112.         {
  113.             printf("MasterSession ID = %d time %f Latency %f packets resent %d\r",aSession->GetSessionID(),aSession->GetTime(),aSession->GetLatencyToMasterSession(),aSession->GetNetworkPacketsLost());
  114.         }
  115.         else
  116.         {
  117.             printf("Session ID = %d time %f Latency %f packets resent %d\r",aSession->GetSessionID(),aSession->GetTime(),aSession->GetLatencyToMasterSession(),aSession->GetNetworkPacketsLost());
  118.         }
  119.  
  120.         // Now send some packets randomly
  121.         // With the 'unreliable' method
  122.         if ( (rand() & 3) == 0)
  123.         {
  124.             aSession->DataSendToSessionUnreliable(rand()&3,buffer_classcreate,sizeof(buffer_classcreate));
  125.         }
  126.         if ( (rand() & 3) == 0)
  127.         {
  128.             aSession->DataSendToSessionUnreliable(rand()&3,buffer_datablock,sizeof(buffer_datablock));
  129.         }
  130.  
  131.         // With the 'certain' method
  132.         if ( (rand() & 3) == 0)
  133.         {
  134.             aSession->DataSendToSessionCertain(rand()&3,buffer_classcreate,sizeof(buffer_classcreate));
  135.         }
  136.         if ( (rand() & 3) == 0)
  137.         {
  138.             aSession->DataSendToSessionCertain(rand()&3,buffer_datablock,sizeof(buffer_datablock));
  139.         }
  140.  
  141.         // With the 'reliable' method
  142.         if ( (rand() & 3) == 0)
  143.         {
  144.             aSession->DataSendToSessionReliable(rand()&3,buffer_classcreate,sizeof(buffer_classcreate));
  145.         }
  146.         if ( (rand() & 3) == 0)
  147.         {
  148.             aSession->DataSendToSessionReliable(rand()&3,buffer_datablock,sizeof(buffer_datablock));
  149.         }
  150.  
  151.         // Now check for packet data to arrive
  152.         char data[512];
  153.         int len;
  154.         int sessionid;
  155.         unsigned char type;
  156.         bool gotPacket = false;
  157.         while(aSession->DataReceive(&sessionid,data,&len,&type))
  158.         {
  159.             data[len] = 0;
  160.             printf("\nGot %d bytes from %d with type %d",len,sessionid,(int)type);
  161.             gotPacket = true;
  162.         }
  163.         // Produce a tidy output if we got a packet this time
  164.         if (gotPacket)
  165.         {
  166.             printf("\n");
  167.         }
  168.  
  169.         // Also check for session ID numbers joining or leaving the session
  170.         int sessid;
  171.         while ( (sessid = aSession->GetJoiner()) != kXPSessionUnknownID)
  172.         {
  173.             printf("\nSession %d Joined\n\n",sessid);
  174.         }
  175.         while ( (sessid = aSession->GetLeaver()) != kXPSessionUnknownID)
  176.         {
  177.             printf("\nSession %d Left\n\n",sessid);
  178.         }
  179.  
  180.         CurrentThread::Sleep(100);
  181.  
  182.         // Quit the session after 20 seconds local time
  183.         if (aSession->GetLocalTime() > 20.0f)
  184.         {
  185.             break;
  186.         }
  187.     }
  188.  
  189.     delete aSession;
  190.     printf("\nQuit the test\n");
  191.  
  192.     return 0;
  193. }
  194.