// Check if a packet already exists in the incomingPackets log public static bool checkPacketLog(FilePacket packet) { foreach (FilePacket ipacket in incomingPacketsLog) { if (ipacket.transfer_uid.Equals(packet.transfer_uid) && ipacket.packet_number == packet.packet_number && ipacket.added) { return(true); } } return(false); }
public static bool receiveFileData(byte[] data, byte[] sender) { Logging.info("Received File Data"); byte[] file_data = null; try { using (MemoryStream m = new MemoryStream(data)) { using (BinaryReader reader = new BinaryReader(m)) { string uid = reader.ReadString(); ulong packet_number = reader.ReadUInt64(); FilePacket packet = new FilePacket(uid, packet_number); if (checkPacketLog(packet)) { return(false); } int data_length = reader.ReadInt32(); if (data_length > 0) { file_data = reader.ReadBytes(data_length); } Logging.info("File Uid: {0} Packet #{1}", uid, packet_number); FileTransfer transfer = TransferManager.getIncomingTransfer(uid); if (transfer == null) { return(false); } // Check if the transfer is already completed if (transfer.completed) { return(false); } // Check if this is the next packet to process if (transfer.lastPacket != packet_number) { return(false); } incomingPacketsLog.Add(packet); transfer.fileStream.Seek(Config.packetDataSize * (int)packet_number, SeekOrigin.Begin); transfer.fileStream.Write(file_data, 0, file_data.Length); transfer.updateActivity(packet_number + 1); ulong new_packet_number = packet_number + 1; if (new_packet_number * (ulong)Config.packetDataSize > transfer.fileSize + (ulong)Config.packetDataSize) { completeFileTransfer(sender, uid); sendFileTransferCompleted(sender, uid); return(true); } requestFileData(sender, uid, new_packet_number); } } } catch (Exception e) { Logging.error("Exception occured while receiving file data from bytes: " + e); } return(true); }