void Start() { Menu.Console.onDrawStats += () => { if (NetworkStatus.IsServerStarted()) { GUILayout.Label(GetTextForConsole1()); GUILayout.Label(GetTextForConsole2()); } }; Menu.Console.RegisterStats(() => { if (NetworkStatus.IsServerStarted()) { return(GetTextForConsole1()); } return(""); }); Menu.Console.RegisterStats(() => { if (NetworkStatus.IsServerStarted()) { return(GetTextForConsole2()); } return(""); }); }
public static void CheckIfServerIsStarted() { if (NetworkStatus.IsServerStarted()) { throw new System.Exception("Server already started"); } }
void Update() { if (NetworkStatus.IsServerStarted()) { UpdateBroadcastData(); // assign broadcast data // m_customNetworkDiscovery.broadcastData = ConvertDictionaryToString (m_dataForBroadcasting); } }
public static void MarkPlayerForSpawning(Player p) { if (!NetworkStatus.IsServerStarted()) { return; } if (!m_playersMarkedForSpawning.Contains(p)) { m_playersMarkedForSpawning.Add(p); } }
// Update is called once per frame void Update() { if (!NetworkStatus.IsServerStarted()) { return; } // spawn all players which are marked for spawning m_playersMarkedForSpawning.RemoveAll(delegate(Player p) { if (null == p) { return(true); } if (!p.IsLoggedIn()) { return(true); } if (null == p.GetControllingGameObject()) { Vector3 pos = new Vector3(); Quaternion rot = Quaternion.identity; if (CanPlayerBeSpawnedAtAnySpawnPosition(p, ref pos, ref rot)) { if (p.CreateGameObjectForPlayer(pos, rot) != null) { Debug.Log("Spawned game object for " + p.playerName); } else { Debug.LogError("Failed to create game object for " + p.playerName); } return(true); } else { // failed to find spawn location for player's game object // try next time } } return(false); }); }
private static System.Collections.IEnumerator BroadcastCoroutine() { while (true) { yield return(new WaitForSecondsRealtime(1.5f)); if (!m_isBroadcasting) { continue; } if (!NetworkStatus.IsServerStarted()) { continue; } if (null == m_serverUdpCl) { continue; } // TODO: data should be broadcasted to internal networks only ? e.g. those that start with 192 // TODO: should we send to every local IP, or just to broadcast IP (255.255.255.255) // measured time: average 0.8 ms for 2 IP addresses // var stopwatch = System.Diagnostics.Stopwatch.StartNew (); Profiler.BeginSample("Broadcast"); Utilities.Utilities.RunExceptionSafe(() => { Profiler.BeginSample("GetLocalIPv4Addresses"); var localAddresses = GetLocalIPv4Addresses(); Profiler.EndSample(); // Debug.Log("local addresses: \n" + string.Join("\n", localAddresses.Select( ip => ip.ToString() ).ToArray() ) ); Profiler.BeginSample("ConvertDictionaryToByteArray"); byte[] buffer = ConvertDictionaryToByteArray(m_dataForBroadcasting); Profiler.EndSample(); IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, m_clientPort); foreach (var address in localAddresses) { // convert it to broadcast address byte[] addressBytes = address.GetAddressBytes(); addressBytes[3] = 255; endPoint.Address = new IPAddress(addressBytes); Profiler.BeginSample("UdpClient.Send"); try { m_serverUdpCl.Send(buffer, buffer.Length, endPoint); } catch (SocketException ex) { if (ex.ErrorCode == 10051) { // Network is unreachable // ignore this error } else { throw; } } Profiler.EndSample(); } }); Profiler.EndSample(); // Debug.Log ("Broadcast send time: " + stopwatch.GetElapsedMicroSeconds() + " us"); } }
/// <summary> /// Is server active ? /// </summary> public static bool IsServer() { return(NetworkStatus.IsServerStarted()); }
// Update is called once per frame void Update() { if (!NetworkStatus.IsServerStarted() && !NetworkStatus.IsClientConnected()) { m_currentlySpectatedObject = null; return; } if (NetworkStatus.IsClient()) { if (Player.local != null) { if (Player.local.GetControllingGameObject() != null) { // controlling object is alive // set watched object to null m_currentlySpectatedObject = null; } else { // controlling object is not alive if (null == m_currentlySpectatedObject || null == m_currentlySpectatedObject.GetControllingGameObject()) { // we are not spectating anyone // find object for spectating FindObjectForSpectating(0); } else { // we are spectating someone } } } else { // we are on client, and there is no local player m_currentlySpectatedObject = null; } } else if (NetworkStatus.IsServer()) { // we are on dedicated server if (null == m_currentlySpectatedObject || null == m_currentlySpectatedObject.GetControllingGameObject()) { // we are not spectating anyone // find object for spectating FindObjectForSpectating(0); } } // just in case if (m_currentlySpectatedObject) { if (!m_currentlySpectatedObject.GetControllingGameObject()) { // controlling game object of spectated player is dead m_currentlySpectatedObject = null; } } }
public static bool IsServer(this NetworkManager netMgr) { return(NetworkStatus.IsServerStarted()); }