/// <summary>
        /// Serializes the packet into a byte array for sending
        /// </summary>
        /// <returns></returns>
        public byte[] ToByteArray()
        {
            PacketBuffer buffer = new PacketBuffer();

            buffer.WriteBytes(Encoding.ASCII.GetBytes(PacketType));
            buffer.WriteBytes(PacketData);
            return(buffer.ToArray());
        }
        public byte[] ToByteArray()
        {
            byte[]       data   = new byte[8];
            PacketBuffer buffer = new PacketBuffer();

            buffer.WriteBytes(Encoding.ASCII.GetBytes(PacketType));
            buffer.WriteBytes(BitConverter.GetBytes(PacketNumber));
            buffer.WriteBytes(BitConverter.GetBytes(TransferID));
            return(buffer.ToArray());
        }
Esempio n. 3
0
        public TextTransferPacket(byte[] data)
        {
            PacketBuffer buffer = new PacketBuffer();

            buffer.WriteBytes(data);
            PacketType = Encoding.ASCII.GetString(buffer.ReadBytes(4));
            PacketText = buffer.ReadString();

            buffer.Dispose();
        }
        /// <summary>
        /// Deserializes a data buffer into a ScoutResponsePacket
        /// </summary>
        /// <param name="data"></param>
        /// <param name="endPoint"></param>
        public ScoutResponsePacket(byte[] data, IPEndPoint endPoint)
        {
            PacketBuffer buffer = new PacketBuffer();

            buffer.WriteBytes(data);
            PacketType     = Encoding.Default.GetString(buffer.ReadBytes(4));
            PacketData     = new byte[data.Length - 4];
            PacketEndPoint = endPoint;
            Array.Copy(data, 4, PacketData, 0, PacketData.Length);
            buffer.Dispose();
        }
        private void ProcessFinishedFile()
        {
            // Create the byte buffer and include it in the OnTransferFinished event
            PacketBuffer buffer = new PacketBuffer();

            for (int i = 0; i < ReceivedChunks.Count; i++)
            {
                buffer.WriteBytes(ReceivedChunks[i].Data);
            }

            byte[] fileBuffer = buffer.ToArray();

            OnTransferFinished?.Invoke(null, new InboundTransferEventArgs(TransferID, true, fileBuffer, IncomingFileStructure));

            fileBuffer     = null;
            ReceivedChunks = null;
        }
        /// <summary>
        /// Returns a NetworkDevice object containing the information included in the packet.
        /// </summary>
        /// <returns></returns>
        public NetworkDevice GetNetworkDevice()
        {
            // The packet data needs to be at least 6 bytes long. Strings in the packetbuffer class are saved with a
            // 4-bytes integer for the lenght, and then the string itself
            if (PacketData == null || PacketData.Length < 6)
            {
                return(null);
            }

            PacketBuffer buffer = new PacketBuffer();

            buffer.WriteBytes(PacketData);

            NetworkDevice device = new NetworkDevice();

            device.Name     = buffer.ReadString();
            device.Type     = (NetworkDeviceType)buffer.ReadByte();
            device.EndPoint = PacketEndPoint;
            buffer.Dispose();

            return(device);
        }
Esempio n. 7
0
        /// <summary>
        /// Returns a FileStructure object containing the information
        /// </summary>
        /// <returns></returns>
        public LocalFileStructure GetFileStructure()
        {
            if (PacketData == null || PacketData.Length < 12)
            {
                return(null);
            }

            PacketBuffer buffer = new PacketBuffer();

            buffer.WriteBytes(PacketData);

            LocalFileStructure fStruct = new LocalFileStructure();

            buffer.ReadInteger(); // Ignore the local ID
            fStruct.FileSize      = buffer.ReadLong();
            fStruct.FileName      = buffer.ReadString();
            fStruct.FileExtension = buffer.ReadString();
            fStruct.FullName      = fStruct.FileName + fStruct.FileExtension;

            buffer.Dispose();

            return(fStruct);
        }