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 / Example8 / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-30  |  3.7 KB  |  152 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 the use of networked member functions in classes
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string>
  17. #include "RNReplicaNet/Inc/ReplicaNet.h"
  18. #include "Test1.h"
  19. #include "RNPlatform/Inc/ThreadClass.h"
  20.  
  21. using namespace RNReplicaNet;
  22.  
  23. /**
  24.  * Create our simple derived class to detect session joiners.
  25.  */
  26. class MyReplicaNet : public ReplicaNet
  27. {
  28. public:
  29.     MyReplicaNet() : mJoined(0) {}
  30.  
  31.     /**
  32.      * Access method for the number of joined sessions.
  33.      */
  34.     int GetNumJoined(void)
  35.     {
  36.         // We use a mutex because we are multi-threaded and the variable may change due to this thread.
  37.         THREADSAFELOCKCLASS(mMutex);
  38.         return mJoined;
  39.     }
  40.  
  41.     /**
  42.      * Access method to get the session ID based on an index.
  43.      */
  44.     int GetSessionIDAt(const int index)
  45.     {
  46.         // We use a mutex because we are multi-threaded and the variable may change due to this thread.
  47.         THREADSAFELOCKCLASS(mMutex);
  48.         return mSessionIDs[index];
  49.     }
  50.  
  51. private:
  52.     void JoinerSessionIDPost(const int sessionID)
  53.     {
  54.         // We use a mutex because we are multi-threaded and the variable may change due to this thread.
  55.         THREADSAFELOCKCLASS(mMutex);
  56.         mSessionIDs.push_back(sessionID);
  57.         mJoined++;
  58.     }
  59.  
  60.     int mJoined;
  61.  
  62.     std::vector<int> mSessionIDs;
  63.  
  64.     MutexClass mMutex;
  65. };
  66.  
  67.  
  68. int main(int argc,char **argv)
  69. {
  70.     MyReplicaNet *network;
  71.  
  72.     // Alloocate a ReplicaNet session
  73.     network = new MyReplicaNet();
  74.  
  75.     // Start to find a session
  76.     printf("Searching for a game...\n");
  77.     network->SessionFind();
  78.  
  79.     // Sleep for half a second to wait for replies
  80.     CurrentThread::Sleep(500);
  81.  
  82.     // Enumerate the first found session
  83.     std::string found = network->SessionEnumerateFound();
  84.  
  85.     if (found != "")
  86.     {
  87.         // Check for the "Example8" string in the URL and set the found URL to be "" if it does not exist
  88.         if (found.find("Example8") == std::string::npos)
  89.         {
  90.             found = "";
  91.         }
  92.     }
  93.  
  94.     // If no matching session name is found then...
  95.     if (found == "")
  96.     {
  97.         // Create a new session
  98.         network->SessionCreate("Example8");
  99.         printf("New game started '%s'\n",network->SessionExportURL().c_str());
  100.     }
  101.     else
  102.     {
  103.         // Join the found session
  104.         printf("Joining session '%s'\n",found.c_str());
  105.         network->SessionJoin(found);
  106.     }
  107.  
  108.     // Wait for the session to become stable
  109.     while (!network->IsStable())
  110.     {
  111.         CurrentThread::Sleep(100);
  112.     }
  113.  
  114.     Test1 *object = 0;
  115.     // If the session is our master session then create an object
  116.     if (network->IsMaster())
  117.     {
  118.         // Now allocate our test object
  119.         object = new Test1();
  120.         // Publish the object on to the network
  121.         object->Publish();
  122.  
  123.         printf("Waiting for the other three sessions to join\n");
  124.     }
  125.  
  126.     while (network->GetNumJoined() < 3)
  127.     {
  128.         CurrentThread::Sleep(200);
  129.     }
  130.  
  131.     // Go around in an endless loop
  132.     while(1)
  133.     {
  134.         // If this session has an allocated object then we know we just allocated it above
  135.         if (object)
  136.         {
  137.             int twoSessionIDs[2];
  138.             // Get the first and third (0 and 2) session IDs to join the session from our list
  139.             twoSessionIDs[0] = network->GetSessionIDAt(0);
  140.             twoSessionIDs[1] = network->GetSessionIDAt(2);
  141.             // RunTest() is the member function that shows how to call the network shared member functions
  142.             // This time we nominate the two session IDs to call
  143.             object->RunTest(2,twoSessionIDs);
  144.         }
  145.  
  146.         // Sleep for five seconds
  147.         CurrentThread::Sleep(5000);
  148.     }
  149.  
  150.     return 0;
  151. }
  152.