/// <summary> /// This method is responsible for sychronize the players positions. /// </summary> public void SynchronizePlayersPositions() { try { var sendPosDict = new Dictionary <IClient, NetworkPlayer>(_networkPlayersDictionary); foreach (var sendToPlayer in sendPosDict) { if (sendToPlayer.Value == null) { continue; } using (var kingBuffer = KingBufferWriter.Create()) { kingBuffer.Write(MyPackets.PlayerPositionsArray); kingBuffer.Write(sendPosDict.Count(c => c.Key.Id != sendToPlayer.Key.Id && c.Value.Moved)); int amountPlayersMoved = 0; foreach (var posPlayers in sendPosDict) { if (sendToPlayer.Key.Id == posPlayers.Key.Id) { continue; } if (!posPlayers.Value.Moved) { continue; } kingBuffer.Write(posPlayers.Key.Id); kingBuffer.Write(posPlayers.Value.X); kingBuffer.Write(posPlayers.Value.Y); kingBuffer.Write(posPlayers.Value.Z); amountPlayersMoved++; } if (amountPlayersMoved > 0) { _server.SendMessage(sendToPlayer.Key, kingBuffer); } } } foreach (var player in _networkPlayersDictionary) { player.Value.Moved = false; } } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } }
/// <summary> /// Method responsible for execute the callback of message received from client in server. /// </summary> /// <param name="client">The client instance.</param> /// <param name="kingBuffer">The king buffer from received message.</param> private static void OnMessageReceived(IClient client, IKingBuffer kingBuffer) { try { _server.SendMessage(client, kingBuffer); _messagesReceived++; _dataReceived += kingBuffer.Length(); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}."); } }
/// <summary> /// Method responsible for execute the callback of message received from client in server. /// </summary> /// <param name="client">The client instance.</param> /// <param name="kingBuffer">The king buffer from received message.</param> private static void OnMessageReceived(IClient client, IKingBufferReader reader) { try { if (_networkListenerType == NetworkListenerType.WSText) { Console.WriteLine($"OnMessageReceived: {reader.ReadString()}"); } else { var buffer = KingBufferWriter.Create(); buffer.Write(reader.ReadString()); _server.SendMessage(client, buffer); _messagesReceived++; _dataReceived += reader.Length; } } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}."); } }