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 / Example1 / Network.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-30  |  4.8 KB  |  200 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 allocates a new ReplicaNet session and also handles the dialog displayed at the start of the application.
  13. The dialog displays a list of found game session for the user to join or allows a new game session to be created.
  14. */
  15. #include <windows.h>
  16. #include "Network.h"
  17. #include "Resource.h"
  18. #include "RNXPURL/Inc/NetworkEmulation.h"
  19.  
  20. using namespace RNReplicaNet;
  21.  
  22. extern HINSTANCE ghInst;
  23. ReplicaNet *gNetwork;
  24.  
  25. static bool failed_to = true;
  26.  
  27. static void CreateGame(void)
  28. {
  29.     std::string url;
  30.  
  31. //    gNetwork->SetGameChannel(2000);
  32. //    gNetwork->SessionCreate("Example1","COMPUDP@");
  33.     gNetwork->SessionCreate("Example1");
  34.     url = gNetwork->SessionExportURL();
  35.     OutputDebugString(url.c_str());
  36.     OutputDebugString("\n");
  37. }
  38.  
  39. void Network_Init(std::string url)
  40. {
  41. //    RNReplicaNet::NetworkEmulation::SetAverageLatency(0.100f);
  42. //    RNReplicaNet::NetworkEmulation::SetJitter(0.030f);
  43. //    RNReplicaNet::NetworkEmulation::SetPacketLoss(11.11f);    // 1 out of 9 packets lost
  44. //    RNReplicaNet::NetworkEmulation::SetEnabled(true);
  45.  
  46.     // Allocate a new ReplicaNet session
  47.     gNetwork = new ReplicaNet;
  48.     // Turn on manual Poll() updates for the session to give us slightly better network prediction performance
  49.     gNetwork->SetManualPoll();
  50.  
  51.  
  52.     // Did we have a URL on the command line?
  53.     if (url != "")
  54.     {
  55.         // Try the URL on the command line
  56.         gNetwork->SessionJoin(url);
  57.     }
  58.     else
  59.     {
  60.         // display the network picking dialog
  61.         Network_Dialog();
  62.  
  63.         if (failed_to)
  64.         {
  65.             gNetwork->SessionFind();
  66.             // Wait a while
  67.             Sleep(500);
  68.             url = gNetwork->SessionEnumerateFound();
  69.  
  70.             if (url.find("Example1") == std::string::npos)
  71.             {
  72.                 url = "";
  73.             }
  74.  
  75.             if (url != "")
  76.             {
  77.                 char buffer[256];
  78.                 sprintf(buffer,"Trying to join %s\n",url.c_str());
  79.                 OutputDebugString(buffer);
  80.                 gNetwork->SessionJoin(url);
  81.             }
  82.             else
  83.             {
  84.                 CreateGame();
  85.             }
  86.         }
  87.     }
  88. }
  89.  
  90. BOOL CALLBACK Dlg_JoinOrNew(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  91. {
  92.     std::string url;
  93.  
  94.     switch (msg)
  95.     {
  96.         case WM_INITDIALOG:
  97.             SetTimer(hwndDlg,1,500,0);
  98.             SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_RESETCONTENT, 0, 0);
  99.  
  100.             SendMessage(hwndDlg,WM_COMMAND,IDC_REFRESH,0);
  101.  
  102. //            SetWindowText (GetDlgItem (hwndDlg, IDC_NEWMAPBLOCKWIDTH), textbuf);
  103.             SetFocus(GetDlgItem(hwndDlg,IDC_NEW));
  104.             return 0;
  105.             break;
  106.         case WM_TIMER:
  107.             do
  108.             {
  109.                 url = gNetwork->SessionEnumerateFound();
  110.                 if (url.find("Example1") == std::string::npos)
  111.                 {
  112.                     url = "";
  113.                 }
  114.  
  115.                 if (url != "")
  116.                 {
  117.                     SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_ADDSTRING, 0, (LPARAM) url.c_str());
  118.                 }
  119.             } while (url != "");
  120.             break;
  121.         case WM_COMMAND:
  122.         switch (LOWORD(wParam))
  123.         {
  124.             case IDC_LIST2:
  125.             {
  126.                 int sel = SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_GETCURSEL,0,0);
  127.                 if (sel != LB_ERR)
  128.                 {
  129.                     char buffer[256];
  130.                     int ret = SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_GETTEXT,sel,(LPARAM) buffer);
  131.                     if (ret != LB_ERR)
  132.                     {
  133.                         SetWindowText(GetDlgItem(hwndDlg,IDC_EDIT1),buffer);
  134.                     }
  135.                     SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_SETCURSEL,sel,0);
  136.  
  137.                 }
  138.                 break;
  139.             }
  140.             case IDC_EDIT1:
  141.                 SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_SETCURSEL, -1, 0);
  142.                 break;
  143.             case IDC_REFRESH:
  144.                 SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_RESETCONTENT, 0, 0);
  145.                 gNetwork->SessionFind();
  146.                 break;
  147.             case IDC_JOIN:
  148.             {
  149.                 int sel = SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_GETCURSEL,0,0);
  150.                 if (sel != LB_ERR)
  151.                 {
  152.                     char buffer[256];
  153.                     int ret = SendMessage(GetDlgItem(hwndDlg, IDC_LIST2), LB_GETTEXT,sel,(LPARAM) buffer);
  154.                     if (ret != LB_ERR)
  155.                     {
  156.                         gNetwork->SessionJoin(std::string(buffer));
  157.                         failed_to = false;
  158.                         SendMessage (hwndDlg, WM_CLOSE, 0, -1L);
  159.                         break;
  160.                     }
  161.                 }
  162.                 char buffer[256];
  163.                 int ret = GetWindowText(GetDlgItem(hwndDlg,IDC_EDIT1),buffer,sizeof(buffer));
  164.                 if (ret > 0)
  165.                 {
  166.                     gNetwork->SessionJoin(std::string(buffer));
  167.                     failed_to = false;
  168.                     SendMessage (hwndDlg, WM_CLOSE, 0, -1L);
  169.                     break;
  170.                 }
  171.                 break;
  172.             }
  173.             case IDC_NEW:
  174.             case IDOK:
  175.                 CreateGame();
  176.  
  177.                 failed_to = false;
  178.                 SendMessage (hwndDlg, WM_CLOSE, 0, -1L);
  179.                 break;
  180.             case IDCANCEL:
  181.                 SendMessage (hwndDlg, WM_CLOSE, 0, -1L);
  182.                 break;
  183.         }
  184.         break;
  185.         case WM_CLOSE:
  186.             EndDialog (hwndDlg, lParam);
  187.             KillTimer(hwndDlg,1);
  188.             break;
  189.         default:
  190.             return 0;
  191.     }
  192.  
  193.     return 1;
  194. }
  195.  
  196. void Network_Dialog(void)
  197. {
  198.     DialogBox(ghInst,MAKEINTRESOURCE(IDD_JOINORNEW),0,&Dlg_JoinOrNew);
  199. }
  200.