public void OnReceiveData(byte[] data, IPEndPoint iPEndPoint) { MemoryStream memoryStream = new MemoryStream(data); PacketWithCrc packetWithCrc = new PacketWithCrc(); packetWithCrc.Deserialize(memoryStream); memoryStream.Close(); if (crc32.IsDataCorrupted(packetWithCrc.data, packetWithCrc.crc)) { UnityEngine.Debug.LogError("Received corrupted data from " + iPEndPoint); return; } memoryStream = new MemoryStream(packetWithCrc.data); PacketHeader packetHeader = new PacketHeader(); packetHeader.Deserialize(memoryStream); if ((PacketType)packetHeader.packetTypeID == PacketType.User) { UserPacketHeader userPacketHeader = new UserPacketHeader(); userPacketHeader.Deserialize(memoryStream); ReliablePacketHeader reliablePacketHeader = new ReliablePacketHeader(); reliablePacketHeader.Deserialize(memoryStream); ProcessReliablePacketReceived(reliablePacketHeader); InvokeCallback(userPacketHeader, memoryStream); } else { NetworkManager.Instance.OnReceivePacket(iPEndPoint, (PacketType)packetHeader.packetTypeID, memoryStream); } memoryStream.Close(); }
private void DeserializePacket(byte[] data, out MemoryStream stream, out PacketHeader packetHeader, ref UserPacketHeader userPacketHeader) { stream = new MemoryStream(data); packetHeader = new PacketHeader(); packetHeader.Deserialize(stream); if ((PacketType)packetHeader.packetType == PacketType.User) { userPacketHeader = new UserPacketHeader(); userPacketHeader.Deserialize(stream); } }
public void OnReceiveData(byte[] data, IPEndPoint endPoint) { // Debug.Log("data received" + endPoint); PacketHeader header = new PacketHeader(); MemoryStream memoryStream = new MemoryStream(data); header.Deserialize(memoryStream); Debug.Log("Deserialize: " + header.type); if (header.type == PacketType.User) // El packetype no es el correcto { Debug.Log("TyperHeaderAccepted"); UserPacketHeader userHeader = new UserPacketHeader(); userHeader.Deserialize(memoryStream); ReceivingPacket.Invoke(userHeader.objectId, userHeader.userType, memoryStream); } else { ReceivingInternalPacket.Invoke(header.type, memoryStream, endPoint); } memoryStream.Close(); }
bool DeserializePacket(byte[] data, out MemoryStream memoryStream, out PacketHeader packetHeader, ref UserPacketHeader userPacketHeader, ref ReliablePacketHeader reliablePacketHeader) { bool isFaultless; PacketWrapper packetWrapper = new PacketWrapper(); memoryStream = new MemoryStream(data); packetHeader = new PacketHeader(); packetWrapper.Deserialize(memoryStream); memoryStream.Close(); isFaultless = crcCalculator.PerformCrcCheck(packetWrapper.PacketData, packetWrapper.Crc); if (isFaultless) { memoryStream = new MemoryStream(packetWrapper.PacketData); packetHeader.Deserialize(memoryStream); if ((PacketType)packetHeader.PacketTypeIndex == PacketType.User) { userPacketHeader = new UserPacketHeader(); userPacketHeader.Deserialize(memoryStream); if (userPacketHeader.Reliable) { reliablePacketHeader = new ReliablePacketHeader(); reliablePacketHeader.Deserialize(memoryStream); } } } return(isFaultless); }
public void OnReceiveData(byte[] data, IPEndPoint ipEndpoint) { PacketHeader header = new PacketHeader(); MemoryStream stream = new MemoryStream(data); header.Deserialize(stream); if ((PacketType)header.packetType == PacketType.User) { UserPacketHeader userHeader = new UserPacketHeader(); userHeader.Deserialize(stream); if (userHeader.senderId != ConnectionManager.Instance.clientId && onGamePacketReceived.ContainsKey(userHeader.objectId)) { onGamePacketReceived[userHeader.objectId].Invoke(userHeader.packetId, userHeader.packetType, stream); } } else { onInternalPacketReceived.Invoke(header.packetType, ipEndpoint, stream); } stream.Close(); }
public void OnReceiveData(byte[] data, IPEndPoint ipEndpoint) { PacketHeader header = new PacketHeader();//CheckSum: header para leer MemoryStream stream = new MemoryStream(data); BinaryReader binaryReader = new BinaryReader(stream); int hash32 = binaryReader.ReadInt32(); //CheckSum: Se copia solamente el hash para verificar checksum byte[] dataWithoutHash = new byte[data.Length - 4]; Array.Copy(data, 4, dataWithoutHash, 0, data.Length - 4); if (testCheckSum) { if (UnityEngine.Random.Range(0f, 100f) < 1f) { for (int i = 0; i < dataWithoutHash.Length; i++)//CheckSum: trucheo la copia local para que de mal y salte checksum { if (dataWithoutHash[i] != 0) { dataWithoutHash[i] = 0; } } } } int ourHash; using (MD5 md5Hash = MD5.Create()) { byte[] hash = md5Hash.ComputeHash(dataWithoutHash); MemoryStream hashStream = new MemoryStream(hash); BinaryReader hashReader = new BinaryReader(hashStream); ourHash = hashReader.ReadInt32(); hashStream.Close(); //CheckSum: creo un hash igual y tienen que ser iguales para saber que el paquete no esta corrupto } if (hash32 == ourHash)//CheckSum: verifica si son iguales { bool reliability = binaryReader.ReadBoolean(); if (reliability) { if (testReliable) { if (Input.GetKey(KeyCode.A)) { stream.Close(); return; } } uint packageAcknowledge = binaryReader.ReadUInt32();//Si es reliability lee el ack del paquete //este es el que recibo del otro if (ConnectionManager.Instance.isServer) { Client client = ConnectionManager.Instance.clients[ConnectionManager.Instance.ipToId[ipEndpoint]]; client.acknowledgeChecker.RegisterPackageReceived(packageAcknowledge); } else { ConnectionManager.Instance.client.acknowledgeChecker.RegisterPackageReceived(packageAcknowledge); } bool hasAcknowledge = binaryReader.ReadBoolean();// el bool que verifica si habia ack if (hasAcknowledge) { uint lastAcknowledge = binaryReader.ReadUInt32(); // el ultimo que llego siendo confirmado uint prevAcknowledgeArray = binaryReader.ReadUInt32(); // el resto del array para verificar posicion en el mismo if (ConnectionManager.Instance.isServer) { Client client = ConnectionManager.Instance.clients[ConnectionManager.Instance.ipToId[ipEndpoint]]; client.acknowledgeChecker.RegisterPackageReceived(packageAcknowledge); client.acknowledgeChecker.ClearPackets(lastAcknowledge, prevAcknowledgeArray);// limpio los que ya llegaron para que los deje de spamear } else { ConnectionManager.Instance.client.acknowledgeChecker.RegisterPackageReceived(packageAcknowledge); ConnectionManager.Instance.client.acknowledgeChecker.ClearPackets(lastAcknowledge, prevAcknowledgeArray); } } } header.Deserialize(stream); if (header.packetType == PacketType.User) { while (stream.Length - stream.Position > 0) { UserPacketHeader userHeader = new UserPacketHeader(); userHeader.Deserialize(stream); InvokeCallback(userHeader.objectId, userHeader.packetType, stream); } } else { ConnectionManager.Instance.OnReceivePacket(ipEndpoint, header.packetType, stream); } } stream.Close(); }
public void OnReceiveData(byte[] data, IPEndPoint ipEndpoint) { PacketHeader header = new PacketHeader(); MemoryStream stream = new MemoryStream(data); BinaryReader binaryReader = new BinaryReader(stream); int hash32 = binaryReader.ReadInt32(); byte[] dataWithoutHash = new byte[data.Length - 4]; Array.Copy(data, 4, dataWithoutHash, 0, data.Length - 4); #if DEBUG_CHECKSUM if (UnityEngine.Random.Range(0f, 100f) < 0.5f) { if (dataWithoutHash[0] != 0) { dataWithoutHash[0] = 0; } else { dataWithoutHash[0] = 1; } } #endif int ourHash; using (MD5 md5Hash = MD5.Create()) { byte[] hash = md5Hash.ComputeHash(dataWithoutHash); MemoryStream hashStream = new MemoryStream(hash); BinaryReader hashReader = new BinaryReader(hashStream); ourHash = hashReader.ReadInt32(); hashStream.Close(); } if (hash32 == ourHash) { bool reliability = binaryReader.ReadBoolean(); if (reliability) { #if DEBUG_RELIABLE if (UnityEngine.Random.Range(0f, 100f) < 0.6f) { // if (Input.GetKey(KeyCode.A)) { stream.Close(); return; } #endif uint packageAck = binaryReader.ReadUInt32(); if (ConnectionManager.Instance.isServer) { Client client = ConnectionManager.Instance.clients[ConnectionManager.Instance.ipToId[ipEndpoint]]; client.ackChecker.RegisterPackageReceived(packageAck); } else { ConnectionManager.Instance.OwnClient.ackChecker.RegisterPackageReceived(packageAck); } bool hasAck = binaryReader.ReadBoolean(); if (hasAck) { uint lastAck = binaryReader.ReadUInt32(); uint prevAckArray = binaryReader.ReadUInt32(); if (ConnectionManager.Instance.isServer) { Client client = ConnectionManager.Instance.clients[ConnectionManager.Instance.ipToId[ipEndpoint]]; client.ackChecker.ClearPackets(lastAck, prevAckArray); } else { ConnectionManager.Instance.OwnClient.ackChecker.ClearPackets(lastAck, prevAckArray); } } } header.Deserialize(stream); if (header.packetType == PacketType.User) { while (stream.Length - stream.Position > 0) { UserPacketHeader userHeader = new UserPacketHeader(); userHeader.Deserialize(stream); InvokeCallback(userHeader.objectId, userHeader.packetType, stream); } } else { ConnectionManager.Instance.OnReceivePacket(ipEndpoint, header.packetType, stream); } } else { Debug.LogWarning("PACKAGE CORRUPTED"); } stream.Close(); }