void PingServer() { for (int i = 0; i < NetworkServer.connections.Count; ++i) { int rtt; NetworkConnection c = NetworkServer.connections[i]; if (c == null) { continue; } byte error; if (c.hostId < 0) { rtt = 0; } else { rtt = NetworkTransport.GetCurrentRtt(c.hostId, c.connectionId, out error); } //Debug.Log(c.playerControllers[0].gameObject.GetComponent<CharacterControl>().NameText.text + " " + rtt); } //EmptyMessage msg = new EmptyMessage();420 //NetworkServer.SendToAll(Score, msg); /* * if (ServerIP != null && (serverPing.isDone || serverPing == null)) * { * serverPing = new Ping(ServerIP); * } */ }
void Update() { Player p = this; if (this.isServer) { // check if player should be disconnected // if (p.status == PlayerStatus.ShouldDisconnect) { if (p.timeUntilDisconnect > 0) { p.timeUntilDisconnect -= Time.deltaTime; if (p.timeUntilDisconnect <= 0) { this.DisconnectPlayer(0, ""); } } } // update player ping if (p.conn != null) { if (p.conn.hostId >= 0) // this is not host { byte error = 0; p.ping = (short)NetworkTransport.GetCurrentRtt(p.conn.hostId, p.conn.connectionId, out error); } } }
// Update is called once per frame void Update() { //Init some values (first frame only) if (isServer && !isInit) { NetworkIdentity identity = GetComponent <NetworkIdentity>(); if (identity.connectionToClient != null) { Debug.Log("isinit"); hostID = identity.connectionToClient.hostId; connID = identity.connectionToClient.connectionId; isInit = true; } } else { isInit = true; } //Update player ping if (isServer && !isLocalPlayer && Time.time > nextUpdate) { nextUpdate = Time.time + GetNetworkSendInterval(); byte error; this.ping = (short)NetworkTransport.GetCurrentRtt(hostID, connID, out error); } if (Input.GetKeyUp(KeyCode.K)) { CmdSpawnPlayer(); } }
public override void GetPeerStats2(out string stat2) { if (this.isServer) { stat2 = "<server>"; } else { byte num; stat2 = string.Format("rtt: {0,5:0000.00}", NetworkTransport.GetCurrentRtt(this._hostID, this._selfConnID, out num)); } }
void Update() { if (state > InternetState.None) { RTT = NetworkTransport.GetCurrentRtt(HostID, ConnectionID, out error); NetworkEventType recData = NetworkTransport.Receive(out RecHostID, out RecConnectionID, out RecChannelID, Buffer, BufferSize, out DataSize, out error); switch (recData) { case NetworkEventType.Nothing: break; case NetworkEventType.ConnectEvent: if (ConnectionID == RecConnectionID) { Log.Server("Connected to server as " + RecConnectionID.ToString()); } else { Log.Server(RecConnectionID.ToString() + " Connected"); } break; case NetworkEventType.DataEvent: char[] chars = new char[Buffer.Length / sizeof(char)]; System.Buffer.BlockCopy(Buffer, 0, chars, 0, DataSize); Log.Line(new string (chars), new Color(0.7f, 0.2f, 1f)); break; case NetworkEventType.DisconnectEvent: if (ConnectionID == RecConnectionID) { Log.Error("Connection Failed (" + ((NetworkError)error).ToString() + ")"); } else { Log.Server(RecConnectionID.ToString() + " Disconnected"); } break; } } }
private void UpdateLockStepIntervalDynamic() { if (!DynamicLockStepIntervalEnabled || !IsHost() || !IsNetworkEnabled()) { return; } if (NetworkServer.connections.Count < 2) { return; } double avgRtt = 0; int rttCount = 0; foreach (NetworkConnection conn in NetworkServer.connections) { // if conn.hostId == -1 it is the connection to ourself if (conn == null || conn.hostId == -1) { continue; } byte error; int rtt = NetworkTransport.GetCurrentRtt(conn.hostId, conn.connectionId, out error); avgRtt += rtt; rttCount++; PrintDebug(DebugLevel.OFF, "rtt={0}", rtt); } avgRtt /= rttCount; PrintDebug(DebugLevel.DEBUG, "avgRtt={0}", avgRtt); int newInterval = (int)(GetCurrentLockStepInterval() + avgRtt) / 2; if (newInterval > 0) { PrintDebug(DebugLevel.DEBUG, "newInterval={0}", newInterval); foreach (H2LogPlayer nwPlayer in H2LogPlayer.instances) { nwPlayer.AdjustedLockStepInterval = newInterval; } } }
static public float GetLatency(GameObject player) { float latency = 0.0f; PlayerIdentity playerIdentity = player.GetComponent <PlayerIdentity>(); if (playerIdentity != null) { byte error = 1; NetworkConnection connection = null; //exclude host setup (both isClient and isServer set) if (playerIdentity.isServer && !playerIdentity.isClient) { connection = playerIdentity.connectionToClient; } if (playerIdentity.isClient && !playerIdentity.isServer) { connection = playerIdentity.connectionToServer; } if (connection != null) { int rtt = NetworkTransport.GetCurrentRtt(connection.hostId, connection.connectionId, out error); if (error == 0) { latency = Convert.ToSingle(rtt) * 0.001f; } } } else { Debug.LogWarning("PlayerManager.GetLatency: " + player.name + " does not have a PlayerIdentity."); } return(latency); }
static void DrawConsole() { int consoleWidth = Screen.width; int consoleHeight = Screen.height / 2; // Draw rectangle in background. GUI.Box(new Rect(0, 0, consoleWidth, consoleHeight), ""); // Draw some statistics above console GUILayout.BeginArea(new Rect(0, 0, Screen.width, 30)); GUILayout.BeginHorizontal(); // display fps GUILayout.Label("FPS: " + GameManager.GetAverageFps()); // display uptime if (NetworkStatus.IsServerStarted()) { GUILayout.Label(" uptime: " + Utilities.Utilities.FormatElapsedTime(Time.realtimeSinceStartup)); } // let anybody else display their own stats try { onDrawStats(); } catch (System.Exception ex) { Debug.LogException(ex); } // Display network statistics for client if (NetworkStatus.IsClientConnected() && !NetworkStatus.IsHost()) { NetworkConnection conn = NetManager.GetClient().connection; byte error = 0; GUILayout.Label(" ping: " + NetworkTransport.GetCurrentRtt(conn.hostId, conn.connectionId, out error) + " ms"); GUILayout.Label(" lost_packets: " + NetworkTransport.GetNetworkLostPacketNum(conn.hostId, conn.connectionId, out error)); GUILayout.Label(" received_rate: " + NetworkTransport.GetPacketReceivedRate(conn.hostId, conn.connectionId, out error)); GUILayout.Label(" sent_rate: " + NetworkTransport.GetPacketSentRate(conn.hostId, conn.connectionId, out error)); } GUILayout.EndHorizontal(); GUILayout.EndArea(); m_consoleScrollPosition = GUILayout.BeginScrollView( m_consoleScrollPosition, GUILayout.Width(consoleWidth), GUILayout.Height(consoleHeight)); /* * // Display player information * if( networkManager.IsServer() ) { * * GUILayout.Label("Players"); * GUILayout.Label("name\t\t | health\t | kills\t | deaths\t | ping"); * foreach ( Player player in networkManager.players ) { * * string s = ""; * s += player.playerName + "\t "; * if (player.mainNetworkScript != null) { * s += player.mainNetworkScript.health + "\t " + player.mainNetworkScript.numKills + "\t " + player.mainNetworkScript.numDeaths ; * } * s += "\t 0 ms"; * * GUILayout.Label( s ); * } * } */ // Draw log string // if( logString != "" ) { { /* * GUIStyle style = new GUIStyle( GUI.skin.textArea ); * // style.wordWrap = true; * style.richText = true ; * GUILayout.Space(25); * GUILayout.TextArea( logString, style, GUILayout.MinHeight (consoleHeight - 30) ); */ GUILayout.Space(30); GUILayout.Label(m_logString); } GUILayout.EndScrollView(); GUILayout.BeginHorizontal(); string textToProcess = ""; // Edit box for commands input. GUI.SetNextControlName("commands_input"); m_consoleCommandText = GUILayout.TextField(m_consoleCommandText, 1000, GUILayout.Width(Screen.width / 4), GUILayout.Height(40)); if (Event.current.isKey && GUI.GetNameOfFocusedControl() == "commands_input") { if (Event.current.keyCode == KeyCode.UpArrow) { // up arrow pressed and edit box is in focus BrowseHistoryBackwards(); } else if (Event.current.keyCode == KeyCode.DownArrow) { // down arrow pressed and edit box is in focus BrowseHistoryForwards(); } else if (Event.current.keyCode == KeyCode.Return) { // enter pressed textToProcess = m_consoleCommandText; } } // submit button // bool submited = GUILayout.Button( "Submit", GUILayout.Width(60), GUILayout.Height(40) ); bool submited = GameManager.DrawButtonWithCalculatedSize("Submit"); if (submited) { textToProcess = m_consoleCommandText; } GUILayout.EndHorizontal(); if (textToProcess != "") { SubmittedText(textToProcess); // clear input text box SetInputBoxText(""); } }
void OnGUI() { //if(!menuActive) if (networkManager.isNetworkActive) { int connections = 0;// = networkManager.connections.Count; int bytesIn = 0; int bytesOut = 0; int totalBytesIn = 0; int totalBytesOut = 0; int msg; string ping = ""; string button = ""; byte error; /*if (!host) * { * foreach (NetworkConnection conn in networkManager.connections) * { * bytesIn += NetworkTransport.GetPacketReceivedRate(conn.hostId, conn.connectionId, out error); * bytesOut += NetworkTransport.GetPacketSentRate(conn.hostId, conn.connectionId, out error); * ping += (NetworkTransport.GetCurrentRtt(conn.hostId, conn.connectionId, out error) / 2) + "ms "; * } * }*/ if (server || host) { //connections = NetworkServer.connections.Count; foreach (NetworkConnection conn in NetworkServer.connections) // networkManager.connections) { if (conn != null && conn.hostId != -1) //dozey - is this "own" connection? { //Debug.Log("hostId: "+conn.hostId); connections++; bytesIn += NetworkTransport.GetPacketReceivedRate(conn.hostId, conn.connectionId, out error); bytesOut += NetworkTransport.GetPacketSentRate(conn.hostId, conn.connectionId, out error); ping += (NetworkTransport.GetCurrentRtt(conn.hostId, conn.connectionId, out error) / 2) + "ms "; } } NetworkServer.GetStatsIn(out msg, out totalBytesIn); NetworkServer.GetStatsOut(out msg, out msg, out totalBytesOut, out msg); button = "Stop Server"; } else { NetworkConnection conn = ncl.connection; connections = 1; bytesIn += NetworkTransport.GetPacketReceivedRate(conn.hostId, conn.connectionId, out error); bytesOut += NetworkTransport.GetPacketSentRate(conn.hostId, conn.connectionId, out error); ping += (NetworkTransport.GetCurrentRtt(conn.hostId, conn.connectionId, out error) / 2) + "ms "; ncl.GetStatsIn(out msg, out totalBytesIn); ncl.GetStatsOut(out msg, out msg, out totalBytesOut, out msg); button = "Disconnect"; } GUI.Label(new Rect(10, 10, 200, 70), "conn: " + connections + "\nin: " + bytesIn + "/s total: " + totalBytesIn + "\nout: " + bytesOut + "/s total: " + totalBytesOut + "\nping: " + ping ); if (GUI.Button(new Rect(10, 80, 100, 25), button)) { if (server) { networkManager.StopServer(); server = false; } if (client) { networkManager.StopClient(); client = false; } if (host) { networkManager.StopHost(); host = false; } } return; } else { if (GUI.Button(new Rect(10, 10, 100, 25), "Start a Server")) { //menuActive = false; Settings.gameMode = GameMode.network; //Network.InitializeServer(maxplayers, port, !Network.HavePublicAddress()); //ConnectionConfig connectionCfg = new ConnectionConfig(); // networkManager.StartServer(connectionCfg, Settings.maxPlayers); networkManager.maxConnections = Settings.maxPlayers; networkManager.StartServer(); server = true; } if (GUI.Button(new Rect(10, 50, 150, 25), "Connect to a Server ...")) { //menuActive = false; Settings.gameMode = GameMode.network; Settings.localPlayers = 1; //Network.Connect(ip, port); networkManager.maxConnections = 1; ncl = networkManager.StartClient(); client = true; } string old = splayers; splayers = GUI.TextField(new Rect(170, 90, 30, 25), splayers, 1); if (splayers.Length > 0) { int x; if (!Int32.TryParse(splayers, out x)) { splayers = old; } else { players = Math.Min(x, Settings.maxPlayers); players = Math.Max(players, 1); splayers = players.ToString(); } } if (GUI.Button(new Rect(10, 90, 150, 25), "Local Multiplayer")) { //menuActive = false; Settings.gameMode = GameMode.local; Settings.localPlayers = players; //Settings.maxPlayers = 4; //EventHandler.TriggerLocalGameInitialized(); //ConnectionConfig connectionCfg = new ConnectionConfig(); //networkManager.StartHost(connectionCfg, 1); ncl = networkManager.StartHost(); host = true; } } }