private void OnReceiveData(List <Packet.Packet> packets) { // We received packets from the server, which means the server is still alive OnHeartBeat?.Invoke(); foreach (var packet in packets) { // Create a ClientUpdatePacket from the raw packet instance, // and read the values into it var clientUpdatePacket = new ClientUpdatePacket(packet); clientUpdatePacket.ReadPacket(); UpdateManager.OnReceivePacket(clientUpdatePacket); _packetManager.HandleClientPacket(clientUpdatePacket); } }
protected void Heartbeat() { while (true) { Thread.Sleep(5000); try { //Log.Info(string.Format("Heartbeat:{0}", IsAlive)); PrintPeerInfo(); Ping(true); OnHeartBeat?.Invoke(); } catch (Exception ex) { Log.Error(ex); } } }
/** * Callback for when UDP traffic is received */ private void OnUdpReceive(IAsyncResult result) { // Initialize default IPEndPoint for reference in data receive method var endPoint = new IPEndPoint(IPAddress.Any, 0); byte[] receivedData = { }; try { receivedData = _udpClient.EndReceive(result, ref endPoint); } catch (Exception e) { Logger.Get().Warn(this, $"UDP Receive exception: {e.Message}"); } // Immediately start receiving data again _udpClient.BeginReceive(OnUdpReceive, null); // Figure out which client ID this data is from ushort id = 0; var idFound = false; foreach (var client in _clients.GetCopy().Values) { if (client.HasAddress(endPoint)) { id = client.GetId(); idFound = true; break; } } if (!idFound) { Logger.Get().Warn(this, $"Received UDP data from {endPoint.Address}, but there was no matching known client"); return; } List <Packet> packets; // Lock the leftover data array for synchronous data handling // This makes sure that from another asynchronous receive callback we don't // read/write to it in different places lock (_lock) { packets = PacketManager.HandleReceivedData(receivedData, ref _leftoverData); } // We received packets from this client, which means they are still alive OnHeartBeat?.Invoke(id); foreach (var packet in packets) { // Create a server update packet from the raw packet instance var serverUpdatePacket = new ServerUpdatePacket(packet); serverUpdatePacket.ReadPacket(); _clients[id].UpdateManager.OnReceivePacket(serverUpdatePacket); // Let the packet manager handle the received data _packetManager.HandleServerPacket(id, serverUpdatePacket); } }
/// <summary> /// Heart beat event /// </summary> private void HeartBeatEvent() => OnHeartBeat?.Invoke();