static void CreateNetworkReader(byte[] data) { //https://docs.unity3d.com/ScriptReference/Networking.NetworkReader.html NetworkReader networkReader = new NetworkReader(data); // The first two bytes in the buffer represent the size of the message. This is equal to the NetworkReader.Length // minus the size of the prefix. networkReader.ReadBytes(2); //short readerMsgSize = (short)((readerMsgSizeData[1] << 8) + readerMsgSizeData[0]); // The message type added in NetworkWriter.StartMessage is to be read now. It is a short and so consists of // two bytes. It is the second two bytes on the buffer. byte[] readerMsgTypeData = networkReader.ReadBytes(2); short readerMsgType = (short)((readerMsgTypeData[1] << 8) + readerMsgTypeData[0]); //Debug.Log("Message of type " + readerMsgType + " received"); if (readerMsgType == MessageTypes.JoinRequest) { JoinRequestMessage message = new JoinRequestMessage(); message.Deserialize(networkReader); P2PConnectionManager.OnJoinRequest(recHostId, connectionId, message); } else if (readerMsgType == MessageTypes.JoinAnswer) { JoinAnswerMessage message = new JoinAnswerMessage(); message.Deserialize(networkReader); P2PConnectionManager.OnJoinAnswer(recHostId, connectionId, message); } else if (readerMsgType == MessageTypes.JoinAnnounce) { JoinAnnounceMessage message = new JoinAnnounceMessage(); message.Deserialize(networkReader); P2PConnectionManager.OnJoinAnnounce(recHostId, connectionId, message); } else if (readerMsgType == MessageTypes.Position) { PositionMessage message = new PositionMessage(); message.Deserialize(networkReader); p2PController.ReceivePositionInformation(recHostId, connectionId, message); } else if (readerMsgType == MessageTypes.AskConsent) { AskConsentMessage message = new AskConsentMessage(); message.Deserialize(networkReader); p2PController.OnAskForConsentMsg(recHostId, connectionId, message); } else if (readerMsgType == MessageTypes.AnswerConsent) { AnswerConsentMessage message = new AnswerConsentMessage(); message.Deserialize(networkReader); P2PConsentManager.ReceiveAnswerConsent(message); } else if (readerMsgType == MessageTypes.ApplyConsent) { ApplyConsentMessage message = new ApplyConsentMessage(); message.Deserialize(networkReader); p2PController.ApplyConsent(message); } }
public void AskForConsent(ConsentMessage consentMessage) { AskConsentMessage message = new AskConsentMessage(); message.consentId = P2PConsentManager.GetNextConsentIdAndIncrement(); message.consentAction = consentMessage.consentAction; message.result = consentMessage.result; message.parameters.AddRange(consentMessage.parameters); if (consensusAlgorithm && P2PConnectionManager.SuccessfulConnectionsCount() > 0) { Debug.Log("P2P: Asking consent for: " + consentMessage.consentAction); P2PConsentManager.AddPendingConsent(message); P2PSender.SendToAll(P2PChannels.ReliableChannelId, message, MessageTypes.AskConsent); } else { Debug.Log("P2P: Imposing consent for: " + consentMessage.consentAction); P2PConsentManager.ApplyAndSpreadConsentResult(OnAskForConsentMsg(-1, -1, message)); } }
void Update() { if (initialized && !GameStarted()) { float currentTime = Time.time; if (currentTime - P2PConnectionManager.connectionRequestSentTime > connectionRequestTimeoutTimeS) { Debug.Log("Connection request timeout"); myLane = -1; DisplayError("Connection request timeout"); } } if (initialized) { SendPositionInformation(); P2PConsentManager.CheckForTimeoutPendingConsents(); P2PListener.Listen(); } }