public static void Init(Action callback) { CombatFrequeny = GlobalData.dataMap[0].CombatFrequeny; CombatNumberInSecond = (1 / CombatFrequeny); BuildingReversionFrequency = GlobalData.dataMap[0].BuildingReversionFrequency; BuildingReversionEnergy = GlobalData.dataMap[0].BuildingReversionEnergy; AttackStarFrequeny = GlobalData.dataMap[0].AttackStarFrequeny; //AttackStarEnergy = GlobalData.dataMap[0].AttackStarEnergy; GameTime = GlobalData.dataMap[0].GameTime; m_sceneManager = new ScenesManager(); m_findServer = new FindServer(); m_uiManager = new MogoUIManager(); billboardManager = new BillboardManager(); m_dataMapManager = new MapDataManager(); SoundManager.Init(); ServerProxy.InitServerProxy(new RemoteProxy()); ServerProxy.Instance.Init(callback); }
/// <summary> /// Finds the StoMoHab Server by listening out for its UDP Multicast message. /// If it can't find the server it gives the user to option to try and find the server again /// or to manually enter the IP address. /// /// The servers multicast message has a TTL of 0 so it will only function on the LAN and will not /// extend past a router, this is where the manual IP entry option is useful. If needed change the /// server code to so that its TTL is >0 so that the message can be recived outside of the LAN. /// </summary> /// <returns>The servers IP Address</returns> private static string FindServerIPAddress() { bool foundServer = false; bool timeOut = false; const int receiveTimeout = 3000; // Time to wait until giving up (ms) string ip = null; // Place to store the server ip once its been found _defaultIP = LoadDefaultIP(); // The default IP to fill the input box when the server can't be found automatically // set up the socket and ip end point Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050); EndPoint ep = (EndPoint)iep; sock.Bind(iep); sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.100.0.1"))); sock.ReceiveTimeout = receiveTimeout; // prepare the data structures byte[] data = new byte[1024]; int recv = 0; // While the server isn't found while (!foundServer) { timeOut = false; try { // try and find the data recv = sock.ReceiveFrom(data, ref ep); } catch (SocketException se) { if (se.ErrorCode == 10060) // Connection Timed out { Console.WriteLine("Time out"); timeOut = true; if (_firstFailedFindAttempt && FindServer.IsValidIP(_defaultIP)) // on the first fail try the defaul IP if its valid { ip = _defaultIP; foundServer = true; _firstFailedFindAttempt = false; } else // after that fails wait for input { //present the user with a gui to help resolve the problem and wait FindServer findservergui = new FindServer(_defaultIP); DialogResult result = findservergui.ShowDialog(); if (result == DialogResult.OK) // Manually entered a valid ip string { ip = findservergui.ServerIP; foundServer = true; DefaultIP = ip; // save it as the new default } if (result == DialogResult.Cancel) // Chose to exit { //in debug mode don't exit just return null to allow a failed connect #if !DEBUG Environment.Exit(0); #endif #if DEBUG return(null); #endif } // otherwise they wanted to retry findservergui.Dispose(); } } } if (!timeOut) //if all went well and there wasn't a time out then process the udp packet { string stringData = Encoding.ASCII.GetString(data, 0, recv); if (stringData.CompareTo("StroMoHab Server") == 0) { foundServer = true; // If the packet came from the server then you have found it (very very unlikely it wouldn't have) } // Split the ip data from ip:port to just get ip char[] splitChar = { ':' }; string[] ipdata = ep.ToString().Split(splitChar); ip = ipdata[0]; } } sock.Close(); // All done close the socket if (foundServer) { Console.WriteLine("Found Server at " + ip); return(ip); } else // Shouldn't ever get here { return(null); } }