void FixedUpdate() { // Update the ping client UI with the ping statistics computed by teh job scheduled previous frame since that // is now guaranteed to have completed PingClientUIBehaviour.UpdateStats(m_numPingsSent, m_lastPingTime); // Update the NetworkDriver. It schedules a job so we must wait for that job with Complete m_ClientDriver.ScheduleUpdate().Complete(); // If the client ui indicates we should be sending pings but we do not have an active connection we create one if (PingClientUIBehaviour.ServerEndPoint.IsValid && !m_clientToServerConnection.IsCreated) { m_clientToServerConnection = m_ClientDriver.Connect(PingClientUIBehaviour.ServerEndPoint); } // If the client ui indicates we should not be sending pings but we do have a connection we close that connection if (!PingClientUIBehaviour.ServerEndPoint.IsValid && m_clientToServerConnection.IsCreated) { m_clientToServerConnection.Disconnect(m_ClientDriver); m_clientToServerConnection = default(NetworkConnection); } DataStreamReader strm; NetworkEvent.Type cmd; // Process all events on the connection. If the connection is invalid it will return Empty immediately while ((cmd = m_clientToServerConnection.PopEvent(m_ClientDriver, out strm)) != NetworkEvent.Type.Empty) { if (cmd == NetworkEvent.Type.Connect) { // When we get the connect message we can start sending data to the server // Set the ping id to a sequence number for the new ping we are about to send m_pendingPing = new PendingPing { id = m_numPingsSent, time = Time.fixedTime }; // Create a 4 byte data stream which we can store our ping sequence number in var pingData = new DataStreamWriter(4, Allocator.Temp); pingData.Write(m_numPingsSent); m_clientToServerConnection.Send(m_ClientDriver, pingData); pingData.Dispose(); // Update the number of sent pings ++m_numPingsSent; } else if (cmd == NetworkEvent.Type.Data) { // When the pong message is received we calculate the ping time and disconnect m_lastPingTime = (int)((Time.fixedTime - m_pendingPing.time) * 1000); m_clientToServerConnection.Disconnect(m_ClientDriver); m_clientToServerConnection = default(NetworkConnection); } else if (cmd == NetworkEvent.Type.Disconnect) { // If the server disconnected us we clear out connection m_clientToServerConnection = default(NetworkConnection); } } }
// Update is called once per frame private void Update() { m_Driver.ScheduleUpdate().Complete(); if (!m_Connection.IsCreated) { if (!Done) { Debug.Log("Something went wrong during connection"); } return; } NetworkEvent.Type cmd; while ((cmd = m_Connection.PopEvent(m_Driver, out DataStreamReader stream)) != NetworkEvent.Type.Empty) { switch (cmd) { case NetworkEvent.Type.Connect: Debug.Log("We are now connected to the server."); var value = 1; using (var writer = new DataStreamWriter(4, Allocator.Temp)) { writer.Write(value); m_Connection.Send(m_Driver, writer); } break; case NetworkEvent.Type.Data: var readerCtx = default(DataStreamReader.Context); uint streamValue = stream.ReadUInt(ref readerCtx); Debug.Log("Got the value = " + streamValue + " back from the server."); Done = true; m_Connection.Disconnect(m_Driver); m_Connection = default; break; case NetworkEvent.Type.Disconnect: Debug.Log("Client got disconnected from the server."); m_Connection = default; break; case NetworkEvent.Type.Empty: break; } } }