private Message readServerMessage(BitBuffer bf) { MessageType messageType = bf.GetEnum <MessageType> ((int)MessageType.TOTAL); Message serverMessage = null; switch (messageType) { case MessageType.PLAYER_CONNECTED: serverMessage = PlayerConnectedMessage.CreatePlayerConnectedMessageToReceive(); break; case MessageType.PLAYER_DISCONNECTED: serverMessage = PlayerDisconnectedMessage.CreatePlayerDisconnectedMessageToReceive(); break; case MessageType.SNAPSHOT: serverMessage = new SnapshotMessage(); break; case MessageType.ACK_RELIABLE_MAX_WAIT_TIME: serverMessage = AckReliableMessage.CreateAckReliableMessageMessageToReceive(); break; case MessageType.ACK_RELIABLE_SEND_EVERY_PACKET: serverMessage = AckReliableSendEveryFrameMessage.CreateAckReliableSendEveryFrameMessageMessageToReceive(); break; default: Debug.LogError("Got a Server message that cannot be understood"); return(null); } serverMessage.Load(bf); return(serverMessage); }
// ====================================== // Server message reception // ====================================== private static Message ReceiveServerMessage(BitBuffer bitBuffer) { var messageType = bitBuffer.GetEnum <MessageType>((int)MessageType.TOTAL); Message message; switch (messageType) { case MessageType.PLAYER_CONNECTED: message = PlayerConnectedMessage.CreatePlayerConnectedMessageToReceive(); break; case MessageType.PLAYER_DISCONNECTED: message = PlayerDisconnectedMessage.CreatePlayerDisconnectedMessageToReceive(); break; case MessageType.SNAPSHOT: message = new SnapshotMessage(); break; case MessageType.ACK_RELIABLE_MAX_WAIT_TIME: message = AckReliableMessage.CreateAckReliableMessageMessageToReceive(); break; case MessageType.ACK_RELIABLE_SEND_EVERY_PACKET: message = AckReliableSendEveryFrameMessage.CreateAckReliableSendEveryFrameMessageMessageToReceive(); break; default: return(null); // Return null if the message is not a "from server" type } message.Load(bitBuffer); return(message); }
Message ReadServerMessage(BitBuffer bitBuffer) { MessageType messageType = bitBuffer.GetEnum <MessageType>((int)MessageType.TOTAL); Message serverMessage = null; switch (messageType) { case MessageType.PLAYER_CONNECTED: serverMessage = PlayerConnectedMessage.CreatePCMToReceive(); break; case MessageType.PLAYER_DISCONNECTED: serverMessage = PlayerDisconnectedMessage.CreatePDMToReceive(); break; case MessageType.SNAPSHOT: serverMessage = new SnapshotMessage(); break; case MessageType.ACK_RELIABLE_MAX_WAIT_TIME: serverMessage = AckReliableMessage.CreateAckReliableMessageToReceive(); break; case MessageType.ACK_RELIABLE_SEND_EVERY_PACKET: serverMessage = AckReliableSendEveryFrameMessage.CreateAckReliableSEFMToReceive(); break; default: Debug.LogError("Unknown server message received."); return(null); } serverMessage.Load(bitBuffer); return(serverMessage); }
public void ProcessSnapshot(SnapshotMessage snapshot) { GameData gameData = snapshot.GameSnapshot; simIniTime = simIniTime == 0 ? gameData.Time : simIniTime; snapshots.Add(gameData); snapshots.OrderBy(snap => snap.Time).ToList(); }
private void ProcessSnapshotMessage(SnapshotMessage message) { Debug.Log(message.GameSnapshot.Players[0].Position); var snapshot = message.GameSnapshot; _simulationStartingTime = 0.0.Equals(_simulationStartingTime) ? snapshot.Time : _simulationStartingTime; if (!_snapshots.Any() || _snapshots.Last().Time < snapshot.Time) { _snapshots.Add(snapshot); } }
public override byte[] ToBinary(object obj) { if (obj is SelectedSnapshot snapshot) { SnapshotMessage snapshotMessage = new SnapshotMessage(); snapshotMessage.PersistenceId = snapshot.Metadata.PersistenceId; snapshotMessage.SequenceNr = snapshot.Metadata.SequenceNr; snapshotMessage.TimeStamp = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(snapshot.Metadata.Timestamp); snapshotMessage.Payload = GetPersistentPayload(snapshot.Snapshot); return(snapshotMessage.ToByteArray()); } throw new ArgumentException($"Can't serialize object of type [{obj.GetType()}] in [{GetType()}]"); }
public void ProcessSnapshot(SnapshotMessage snapshotMessage) { List <PlayerData> playersData = snapshotMessage.GameSnapshot.Players; for (int i = 0; i < playersData.Count; i++) { PlayerData playerData = playersData[i]; PlayerNetworkView playerNetworkView = GetPlayerWithId(playerData.PlayerId); if (playerNetworkView != null) { playerNetworkView.Load(playerData); } } }
public void ApplySnapshot(SnapshotMessage msg) { if (!IsClient) { return; } if (m_gameBall != null) { m_gameBall.OnNewSnapshot(msg.BallPos, msg.TS); } (m_playerA as PlayerInterpolated).OnNewSnapshot(msg.RacketAXPos, msg.TS); (m_playerB as PlayerInterpolated).OnNewSnapshot(msg.RacketBXPos, msg.TS); m_playerA.Score = msg.ScoreA; m_playerB.Score = msg.ScoreB; }
private void SendSnapshot() { float currentTime = Time.realtimeSinceStartup; float ttsSnapshot = 1.0f / snapshotSendRate; if (currentTime - lastSnapshotSentTime >= ttsSnapshot) { SnapshotMessage snapshotMessage = new SnapshotMessage(-1, BuildGameData()) { TimeToSend = fakeDelay }; for (int i = 0; i < players.Count; i++) { players[i].CommunicationManager.SendMessage(snapshotMessage); } lastSnapshotSentTime = currentTime; } }
public void Simulate(uint tickIndex) { if (_playerRegistry.Players.Count == 0) { return; } _playersData.Clear(); foreach (ControlledPlayer player in _playerRegistry.ControlledPlayers) { var playerData = new PlayerSnapshotData(player.PlayerId, player.transform.position, player.Team); _playersData.Add(playerData); } var message = new SnapshotMessage(_playersData, _ball.transform.position); _server.SendToAll(message); }
public virtual void onSnapshot(SnapshotMessage message) { }
void Update() { Packet inPacket = channel.GetPacket(); if (inPacket != null) { //read it! BitBuffer bitBuffer = inPacket.buffer; int messageCount = bitBuffer.GetInt(); for (int i = 0; i < messageCount; i++) { //parse message Message clientMessage = ReadClientMessage(bitBuffer, inPacket.fromEndPoint); if (clientMessage != null) { if (clientMessage.Type == MessageType.CONNECT_PLAYER) { ProcessConnectPlayer(clientMessage as ConnectPlayerMessage); } else if (clientMessage.Type == MessageType.DISCONNECT_PLAYER) { ProcessDisconnectPlayer(clientMessage as DisconnectPlayerMessage); } else { Player player = GetPlayerWithEndPoint(inPacket.fromEndPoint); if (player != null) { player.CommunicationManager.ReceiveMessage(clientMessage); } } } } } float currentTime = Time.realtimeSinceStartup; float timeToSendSnapshot = 1.0f / snapshotSendRate; if (currentTime - lastSnapshotSentTime >= timeToSendSnapshot) { SnapshotMessage snapshotMessage = new SnapshotMessage(-1, BuildGameData()); snapshotMessage.TimeToSend = fakeDelay; for (int i = 0; i < players.Count; i++) { players[i].CommunicationManager.SendMessage(snapshotMessage); } lastSnapshotSentTime = currentTime; } for (int playerIdx = 0; playerIdx < players.Count; playerIdx++) { Player player = players[playerIdx]; Packet p = player.CommunicationManager.BuildPacket(); if (p != null) { bool shouldDropPacket = Random.Range(0.0001f, 100.0f) < fakePacketLoss; if (!shouldDropPacket) { channel.Send(p, player.endPoint); } } } }
Message ReadServerMessage(BitBuffer bitBuffer) { MessageType messageType = bitBuffer.GetEnum <MessageType> ((int)MessageType.TOTAL); Message serverMessage = null; switch (messageType) { case MessageType.PLAYER_CONNECTED: serverMessage = PlayerConnectedMessage.CreatePlayerConnectedMessageToReceive(); break; case MessageType.PLAYER_DISCONNECTED: serverMessage = PlayerDisconnectedMessage.CreatePlayerDisconnectedMessageToReceive(); break; case MessageType.SNAPSHOT: serverMessage = new SnapshotMessage(); break; case MessageType.ACK_RELIABLE_MAX_WAIT_TIME: Debug.Log("Client::got ack reliable wait time "); serverMessage = AckReliableMessage.CreateAckReliableMessageMessageToReceive(); break; case MessageType.ACK_RELIABLE_SEND_EVERY_PACKET: serverMessage = AckReliableSendEveryFrameMessage.CreateAckReliableSendEveryFrameMessageMessageToReceive(); break; default: Debug.LogError("Got a server message that cannot be understood"); return(null); } serverMessage.Load(bitBuffer); if (serverMessage.Reliability == ReliabilityType.RELIABLE_MAX_WAIT_TIME) { if (serverMessage.ReliabilityId == (LastReceivedReliableMessageId + 1)) { //accept it... valid message since its +1 since the last received //send to the sender that its reliable message has been received outMessages.Add(AckReliableMessage.CreateAckReliableMessageMessageToSend(serverMessage.ReliabilityId)); LastReceivedReliableMessageId = serverMessage.ReliabilityId; } else { //we need to discard it... either its been already processed or out of order return(null); } } else if (serverMessage.Reliability == ReliabilityType.RELIABLE_SEND_EVERY_PACKET) { if (serverMessage.ReliabilityId > LastReceivedSendInEveryFramePacketMessageId) { //set the last "reliable send in every packet" message id.. it will get sent to the server on the next packet LastReceivedSendInEveryFramePacketMessageId = serverMessage.ReliabilityId; Debug.Log("Client::lastReceivedSendInEveryFramePacketMessageId = " + lastReceivedSendInEveryFramePacketMessageId); } else { //discard return(null); } } return(serverMessage); }
public override void onSnapshot(SnapshotMessage snapshot) { SetPosition(snapshot.playerPositions [id]); playerGo.transform.position = position.toVector3(); }