private void WaitForResponse(IntPtr serverBrowserInstance)
 {
     responseReceived = false;
     // think while the update is in progress
     while (!responseReceived)
     {
         ServerBrowserError nError = gamespyServerBrowser.ServerBrowserThink(serverBrowserInstance);
         if (nError != ServerBrowserError.sbe_noerror)
         {
             Console.WriteLine("ServerBrowserThink Error 0x%x\n", nError);
             return;
         }
         Thread.Sleep(new TimeSpan(100000));  // think should be called every 10-100ms; quicker calls produce more accurate ping measurements
     }
 }
        public void Run()
        {
            IntPtr updateServer = IntPtr.Zero; // this is the server we will call the Aux update for.

            string gamename = "gmtest";

            Console.WriteLine("\n------------ Server Browser C# sample ------------------------\n");

            if (CheckServices(gamename) != GSIACResult.GSIACAvailable)
            {
                Console.WriteLine("{0}: Online services are unavailable now.", gamename);
                return;
            }

#if GSI_COMMON_DEBUG
            StringBuilder debugFileName = new StringBuilder("Debug_ServerBrowser");
            debugFileName.Append(".log");

            gamespyCommonDebug.gsOpenDebugFile(debugFileName.ToString());
            gamespyCommonDebug.gsSetDebugLevel(GSIDebugCategory.GSIDebugCat_All, GSIDebugType.GSIDebugType_All, GSIDebugLevel.Hardcore);
#endif
            IntPtr serverBrowserInstance = gamespyServerBrowser.ServerBrowserNewW(gamename,
                                                                                  gamename,
                                                                                  SECRET_KEY,
                                                                                  VERSION,
                                                                                  MAX_QUERY,
                                                                                  gamespyServerBrowser.QVERSION_QR2,
                                                                                  LAN_BROWSE,
                                                                                  serverBrowserCallback,
                                                                                  IntPtr.Zero
                                                                                  );


            // Now get all the servers listed with a list of keys
            IntPtr basicFieldsPtr = Marshal.AllocHGlobal(basicFields.Length * Marshal.SizeOf(typeof(byte)));
            Marshal.Copy(basicFields, 0, basicFieldsPtr, basicFields.Length);

            Console.WriteLine("\nGet Server List form Backend\n");
            ServerBrowserError nError = gamespyServerBrowser.ServerBrowserUpdateW(serverBrowserInstance,    // server browser instance
                                                                                  ASYNC,                    // asynchronous request
                                                                                  DISC_ON_COMPLETE,         // diconnect when complete
                                                                                  basicFieldsPtr,           // key list
                                                                                  basicFields.Length,       // number of keys
                                                                                  string.Empty              // server filter
                                                                                  );
            if (nError != ServerBrowserError.sbe_noerror)
            {
                Console.WriteLine("ServerBrowserUpdate Error 0x{0}", nError);
                return;
            }
            WaitForResponse(serverBrowserInstance);


            // Sort the server list (local call) according to a given criteri on a certain field
            gamespyServerBrowser.ServerBrowserSortW(serverBrowserInstance,
                                                    true, // ascending
                                                    "ping",
                                                    gamespyServerBrowser.ServerBrowserCompareMode.sbcm_int);

            // Retrieve the number of servers from the local list
            int serverListCount = gamespyServerBrowser.ServerBrowserCount(serverBrowserInstance);
            Console.WriteLine("\nNumber of Servers : {0}", serverListCount);

            if (serverListCount > 0)
            {
                Console.WriteLine("Sorted Servers in ascending order according to ping time:");
            }

            for (int i = 0; i < serverListCount; i++)
            {
                IntPtr aServer = gamespyServerBrowser.ServerBrowserGetServer(serverBrowserInstance, i);
                Console.WriteLine("\tServer Address {0}:{1}", Marshal.PtrToStringAnsi(gamespyServerBrowser.SBServerGetPublicAddress(aServer)),
                                  gamespyServerBrowser.SBServerGetPublicQueryPort(aServer));

                // check if this is our resident sample server
                if (Marshal.PtrToStringUni(gamespyServerBrowser.SBServerGetStringValueW(aServer, "hostname", "No Value")) ==
                    "GameSpy QR2 Sample")
                {
                    updateServer = aServer;
                }
            }

            // If we found the resident running QR2 server
            // Get all the keys of specific server
            if (updateServer != IntPtr.Zero)
            {
                Console.WriteLine("\nGet Server Keys for {0}:{1}",
                                  Marshal.PtrToStringAnsi(gamespyServerBrowser.SBServerGetPublicAddress(updateServer)),
                                  gamespyServerBrowser.SBServerGetPublicQueryPort(updateServer));

                gamespyServerBrowser.ServerBrowserAuxUpdateServer(serverBrowserInstance,
                                                                  updateServer,
                                                                  ASYNC,
                                                                  true      /*fullUpdate*/
                                                                  );
                WaitForResponse(serverBrowserInstance);
            }

            // Clear server list. This is a local call resets the server browser list.
            gamespyServerBrowser.ServerBrowserClear(serverBrowserInstance);

            Console.WriteLine("\n------------ Server Browser C# Sample Completed------------------------\n");
            Console.WriteLine("\nPress Enter to Continue....");
            Console.ReadLine();
        }
Пример #3
0
 public static extern string ServerBrowserErrorDesc
 (
     IntPtr serverBrowserInstance, // ServerBrowser
     ServerBrowserError error
 );