public static void OnJoinAnnounce(int hostId, int connectionId, JoinAnnounceMessage message) { Debug.Log("OnJoinAnnounce received"); P2PConnection connection = P2PConnectionManager.GetConnection(hostId, connectionId); connection.SuccessfullyConnect(); }
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 static void ConnectEvent(int hostId, int connectionId) { int port; string ip; UnityEngine.Networking.Types.NetworkID netId; UnityEngine.Networking.Types.NodeID nodeId; NetworkTransport.GetConnectionInfo(hostId, connectionId, out ip, out port, out netId, out nodeId, out P2PController.error); P2PConnection connection = P2PConnectionManager.GetConnection(ip, port); if (connection == null) { //new connection from targeted ip or new player connection = new P2PConnection(hostId, connectionId); connection.ip = ip; connection.port = port; connections.Add(connection); Debug.Log("New connection with " + connection); if (!JoinRequestSend) //I'm wanting to join { Debug.Log("Sending Join Request"); JoinRequestSend = true; JoinRequestMessage message = new JoinRequestMessage(); P2PSender.Send(hostId, connectionId, P2PChannels.ReliableChannelId, message, MessageTypes.JoinRequest); } else if (JoinAnswerReceived && !p2PController.GameStarted()) { connection.SuccessfullyConnect(); } } else if (!connection.ConnectionSuccessful()) { //successfully connect to an existing player. Connection requested previously connection.hostId = hostId; connection.connectionId = connectionId; connection.SuccessfullyConnect(); JoinAnnounceMessage announceMessage = new JoinAnnounceMessage(); P2PSender.Send(hostId, connectionId, P2PChannels.ReliableChannelId, announceMessage, MessageTypes.JoinAnnounce); } if (!p2PController.GameStarted()) { if (JoinAnswerReceived) { CheckConnectionsStatus(); } } }