Exemplo n.º 1
0
        public static bool SendFileTo(string filename, UdpClient target)
        {
            FileInfo localMeta;
            byte[] fileNameAsBytes;
            int totalPackets;

            try {
                localMeta = new FileInfo(filename);

                fileNameAsBytes = Encoding.Default.GetBytes(filename);

                totalPackets = (int)localMeta.Length / Constants.PAYLOAD_SIZE;

                if ((int)localMeta.Length % Constants.PAYLOAD_SIZE != 0 || totalPackets < 1) {
                    totalPackets++;
                }
            }
            catch (ArgumentException ex) {
                return false;
            }
            catch (FileNotFoundException ex) {

                throw new FileNotFoundException();
            }

            FileMetadataPacket meta = new FileMetadataPacket(Constants.TYPE_FILE_DELIVERY, (int)localMeta.Length, filename.Length, fileNameAsBytes, totalPackets);
            sendUntilAck(meta, target);

            try {
                StreamReader localFile = new StreamReader(filename, Encoding.Default);
                for (int i = 0; i < totalPackets; i++) {

                    char[] stagedPayload = new char[Constants.PAYLOAD_SIZE];
                    localFile.Read(stagedPayload, 0, Constants.PAYLOAD_SIZE);
                    byte[] encoded = Encoding.Default.GetBytes(stagedPayload);
                    DataPacket stagedPacket = new DataPacket(Encoding.Default.GetBytes(stagedPayload), i);

                    sendUntilAck(stagedPacket, target);

                }
                localFile.Close();
            }
            catch (UnauthorizedAccessException ex) {
                Console.WriteLine("Insufficient permissions to access specified file: " + ex.Message);
            }
            catch (IOException ex) {

            }

            return true;
        }
Exemplo n.º 2
0
        public static bool SendDirectoryTo(UdpClient target)
        {
            IPEndPoint remoteIPEndPoint = null;
            byte[] directoryListing = Utils.GetDirectoryListing();
            int totalPackets = Utils.GetDirectoryPacketsTotal();
            DirectoryMetadataPacket directoryMetadataPacket = new DirectoryMetadataPacket(Constants.TYPE_DIRECTORY_DELIVERY, totalPackets, directoryListing.Length);
            bool sent = false;
            Stopwatch timeout = new Stopwatch();
            byte[] receivedBytes = null;

            while (!sent) {
                Utils.SendTo(target, directoryMetadataPacket.MyPacketAsBytes);
                timeout.Restart();
                while (timeout.ElapsedMilliseconds < Constants.PACKET_TIMEOUT_MILLISECONDS) {
                    if (target.Available != 0) {
                        receivedBytes = target.Receive(ref remoteIPEndPoint);
                        if (!Utils.VerifyChecksum(receivedBytes)) {
                            continue;
                        }
                        if (receivedBytes[Constants.FIELD_TYPE] == Constants.TYPE_DIRECTORY_REQUEST) {
                            return false;
                        }

                        if (BitConverter.ToInt32(receivedBytes, Constants.FIELD_ACK_ID) == -1) {
                            sent = true;
                            break;
                        }
                    }
                }
            }

            int byteIndex = 0;
            for (int i = 0; i < totalPackets; i++) {
                byte[] stagedPayload = new byte[Constants.PAYLOAD_SIZE];
                stagedPayload = Utils.InitializeArray(stagedPayload);
                for (int j = 0; j < stagedPayload.Length; j++) {
                    if (j < directoryListing.Length && byteIndex < directoryListing.Length) {
                        stagedPayload[j] = directoryListing[byteIndex];
                        byteIndex++;
                    }
                }
                DataPacket stagedPacket = new DataPacket(stagedPayload, i);
                sendUntilAck(stagedPacket, target);
            }
            return true;
        }