| 720 | } |
| 721 | |
| 722 | void NetworkManagerService::Update() |
| 723 | { |
| 724 | const double currentTime = Time::Update.UnscaledTime.GetTotalSeconds(); |
| 725 | const float minDeltaTime = NetworkManager::NetworkFPS > 0 ? 1.0f / NetworkManager::NetworkFPS : 0.0f; |
| 726 | auto peer = NetworkManager::Peer; |
| 727 | if (NetworkManager::Mode == NetworkManagerMode::Offline || (float)(currentTime - LastUpdateTime) < minDeltaTime || !peer) |
| 728 | return; |
| 729 | PROFILE_CPU(); |
| 730 | PROFILE_MEM(Networking); |
| 731 | LastUpdateTime = currentTime; |
| 732 | NetworkManager::Frame++; |
| 733 | NetworkInternal::NetworkReplicatorPreUpdate(); |
| 734 | // TODO: convert into TaskGraphSystems and use async jobs |
| 735 | |
| 736 | // Process network messages |
| 737 | NetworkEvent event; |
| 738 | bool eventIsValid = true; |
| 739 | while (peer->PopEvent(event) && eventIsValid) |
| 740 | { |
| 741 | switch (event.EventType) |
| 742 | { |
| 743 | case NetworkEventType::Connected: |
| 744 | LOG(Info, "Incoming connection with Id={0}", event.Sender.ConnectionId); |
| 745 | if (NetworkManager::IsClient()) |
| 746 | { |
| 747 | // Initialize client connection data |
| 748 | NetworkClientConnectionData connectionData; |
| 749 | connectionData.Client = NetworkManager::LocalClient; |
| 750 | connectionData.Result = 0; |
| 751 | connectionData.Platform = PLATFORM_TYPE; |
| 752 | connectionData.Architecture = PLATFORM_ARCH; |
| 753 | NetworkManager::ClientConnecting(connectionData); // Allow client to validate connection or inject custom connection data |
| 754 | if (connectionData.Result != 0) |
| 755 | { |
| 756 | LOG(Info, "Connection blocked with result {0}.", connectionData.Result); |
| 757 | NetworkManager::Stop(); |
| 758 | break; |
| 759 | } |
| 760 | |
| 761 | // Send initial handshake message from client to server |
| 762 | NetworkMessageHandshake msgData; |
| 763 | msgData.EngineBuild = FLAXENGINE_VERSION_BUILD; |
| 764 | msgData.EngineProtocolVersion = NETWORK_PROTOCOL_VERSION; |
| 765 | msgData.GameProtocolVersion = GameProtocolVersion; |
| 766 | msgData.Platform = (byte)connectionData.Platform; |
| 767 | msgData.Architecture = (byte)connectionData.Architecture; |
| 768 | msgData.PayloadDataSize = (uint16)connectionData.PayloadData.Count(); |
| 769 | NetworkMessage msg = peer->BeginSendMessage(); |
| 770 | msg.WriteStructure(msgData); |
| 771 | msg.WriteBytes(connectionData.PayloadData.Get(), connectionData.PayloadData.Count()); |
| 772 | peer->EndSendMessage(NetworkChannelType::ReliableOrdered, msg); |
| 773 | } |
| 774 | else |
| 775 | { |
| 776 | // Create incoming client |
| 777 | auto client = New<NetworkClient>(NextClientId++, event.Sender); |
| 778 | NetworkManager::Clients.Add(client); |
| 779 | } |
no test coverage detected