internal void ServerConnection(NetworkMessage message) { if (!client.ClientStatus.Connected || Server == null || !Server.EndPoint.Equals(message.From)) { //If the client is not connected to a server or if the message sender is not the server then ignore the message return; } InternalNetworkClient serverNetworkClient = Server as InternalNetworkClient; serverNetworkClient.LastReceivedConnectionTime = DateTime.Now; switch (message.Type) { case NetworkMessage.MessageType.Ack: if (message.Data.FirstOrDefault(d => d.Object == LiNGSMarkers.Id) != null && message.Data.FirstOrDefault(d => d.Object == LiNGSMarkers.Id).Value != null) { int messageId = int.Parse(message.Data.FirstOrDefault(d => d.Object == LiNGSMarkers.Id).Value); IEnumerable <ImportantMessageWrapper> messages; lock (this.importantMessages) { messages = this.importantMessages.Where(im => im.Message.MessageId == messageId).ToList(); } foreach (var item in messages) { item.ConfirmationReceived = true; item.Message.ConfirmReception(); //Calculate Latency based on message travel time serverNetworkClient.Latency = TimeSpan.FromTicks((DateTime.Now - item.SentDate).Ticks / 2); //Reset heartbeat sent flag if (item.Message.Type == NetworkMessage.MessageType.Heartbeat) { serverNetworkClient.HeartbeatSent = false; } lock (this.importantMessages) { this.importantMessages.Remove(item); } } } break; } if (message.NeedsAck) { client.NetworkManager.SendMessage(message.From, new NetworkMessage(NetworkMessage.MessageType.Ack, new MessageData() { Object = LiNGSMarkers.Id, Value = message.MessageId.ToString() })); } }
internal void ClientConnection(NetworkMessage message) { GameClient client = GetClient(message.From); if (client != null) { InternalNetworkClient internalNetworkClient = client.NetworkClient as InternalNetworkClient; internalNetworkClient.LastReceivedConnectionTime = DateTime.Now; switch (message.Type) { case NetworkMessage.MessageType.Ack: if (message.Data.FirstOrDefault(d => d.Object == LiNGSMarkers.Id) != null && message.Data.FirstOrDefault(d => d.Object == LiNGSMarkers.Id).Value != null) { int messageId = int.Parse(message.Data.FirstOrDefault(d => d.Object == LiNGSMarkers.Id).Value); IEnumerable <ImportantMessageWrapper> messages; lock (this.importantMessages) { messages = this.importantMessages.Where(im => im.Client == client.NetworkClient && im.Message.MessageId == messageId).ToList(); } foreach (var item in messages) { item.ConfirmationReceived = true; item.Message.ConfirmReception(); //Calculate Latency based on message travel time internalNetworkClient.Latency = TimeSpan.FromTicks((DateTime.Now - item.SentDate).Ticks / 2); //Reset heartbeat sent flag if (item.Message.Type == NetworkMessage.MessageType.Heartbeat) { internalNetworkClient.HeartbeatSent = false; } lock (this.importantMessages) { this.importantMessages.Remove(item); } } } break; } if (message.NeedsAck) { server.NetworkManager.SendMessage(message.From, new NetworkMessage(NetworkMessage.MessageType.Ack, new MessageData() { Object = LiNGSMarkers.Id, Value = message.MessageId.ToString() })); } } }
public void Update(TimeSpan timeSinceLastUpdate) { if (client.ClientStatus.Connected && client.Manager.Server != null) { InternalNetworkClient serverNetworkClient = client.Manager.Server as InternalNetworkClient; TimeSpan time = DateTime.Now - serverNetworkClient.LastReceivedConnectionTime; if (!serverNetworkClient.HeartbeatSent && time.TotalMilliseconds > client.ClientProperties.MaxServerBlackoutTime) { serverNetworkClient.HeartbeatSent = true; NetworkMessage message = new NetworkMessage(NetworkMessage.MessageType.Heartbeat); message.NeedsAck = true; client.Manager.SendMessage(message); } } }
public void Update(TimeSpan timeSinceLastUpdate) { List <GameClient> clients = manager.ConnectedClients.ToList(); foreach (var item in clients) { InternalNetworkClient client = item.NetworkClient as InternalNetworkClient; TimeSpan time = DateTime.Now - client.LastReceivedConnectionTime; if (!client.HeartbeatSent && time.TotalMilliseconds > server.ServerProperties.MaxClientBlackoutTime) { //Sent a heartbeat to the client and set a flag indicating that a network heartbeat was sent client.HeartbeatSent = true; NetworkMessage message = new NetworkMessage(NetworkMessage.MessageType.Heartbeat); message.NeedsAck = true; server.Manager.SendMessage(client, message); } } }