private static void Main()
        {
            byte[] receivedBytes;
            UdpClient client = GetConnectedClientObject();

            while (true) {
                receivedBytes = client.Receive(ref remoteIPEndPoint);

                IPHostEntry host;
                host = Dns.GetHostEntry(remoteIPEndPoint.Address);

                if (host.HostName == Constants.IGNORE_HOSTNAME_STRING) {
                    continue;
                }

                sendAddress = remoteIPEndPoint.Address;
                remoteIPEndPoint = new IPEndPoint(sendAddress, remoteIPEndPoint.Port);
                client.Connect(remoteIPEndPoint);

                if (!Utils.VerifyChecksum(receivedBytes))
                    continue;

                switch (receivedBytes[Constants.FIELD_TYPE]) {
                    case Constants.TYPE_DIRECTORY_REQUEST:
                        bool success = false;
                        while (!success) {
                            AckPacket ack = new AckPacket(-1);
                            Utils.SendTo(client, ack.MyPacketAsBytes);
                            success = PingPong.SendDirectoryTo(client);
                        }
                        break;
                    case Constants.TYPE_FILE_DELIVERY:
                        if (Constants.DEBUG_PING_PONG_ACTIVE) {
                            bool receiveSuccess = false;
                            while (!receiveSuccess) {
                                AckPacket ack = new AckPacket(-1);
                                Utils.SendTo(client, ack.MyPacketAsBytes);
                                receiveSuccess = PingPong.ReceiveFileFrom(receivedBytes, client);
                            }
                        }
                        break;
                    case Constants.TYPE_FILE_REQUEST:
                        byte[] rawFilename = new byte[BitConverter.ToInt32(receivedBytes, Constants.FIELD_FILENAME_LENGTH)];
                        for (int i = 0; i < rawFilename.Length; i++) {
                            rawFilename[i] = receivedBytes[i + Constants.FIELD_FILENAME];
                        }

                        PingPong.SendFileTo(Encoding.Default.GetString(rawFilename), client);

                        break;
                    default:
                        break;
                }
            }
        }
Ejemplo n.º 2
0
        public static bool StartReceiveDirectory(byte[] metadataAsBytes, UdpClient target)
        {
            if (!Utils.VerifyChecksum(metadataAsBytes)) {
                return false;
            }
            AckPacket ack = new AckPacket(-1);
            Utils.SendTo(target, ack.MyPacketAsBytes);

            bool success = false;
            while (!success) {
                success = CatchMetadataResend(metadataAsBytes, target);
            }
            return true;
        }
Ejemplo n.º 3
0
 public static void SendAckTo(int id, UdpClient target)
 {
     AckPacket theAckToSend = new AckPacket(id);
     Utils.SendTo(target, theAckToSend.MyPacketAsBytes);
 }
Ejemplo n.º 4
0
        public static bool CatchMetadataResend(byte[] metadataAsBytes, UdpClient target)
        {
            byte[] receivedBytes = null;
            IPEndPoint remoteIPEndPoint = null;
            DirMetadata directoryMetadata = new DirMetadata(metadataAsBytes);
            bool received = false;
            while (!received) {
                receivedBytes = target.Receive(ref remoteIPEndPoint);
                if (Utils.VerifyChecksum(receivedBytes)) {
                    if (receivedBytes[Constants.FIELD_TYPE] == Constants.TYPE_DIRECTORY_DELIVERY) {
                        AckPacket ack = new AckPacket(-1);
                        Utils.SendTo(target, ack.MyPacketAsBytes);
                        return false;
                    }
                    received = true;
                }

            }
            return ReceiveAllDirectoryPackets(receivedBytes, directoryMetadata, target);
        }
        private static void HandleFileGetRequest(UdpClient target)
        {
            IPEndPoint remoteIPEndPoint = null;
            Console.Write("Please enter a filepath\\filename: ");
            string name = Console.ReadLine();
            FileMetadataPacket pack = new FileMetadataPacket(Constants.TYPE_FILE_REQUEST, 0, name.Length, Encoding.Default.GetBytes(name), 0);
            byte[] receivedBytes = null;

            bool sent = false;

            Stopwatch timeout = new Stopwatch();
            byte[] backup = new byte[Constants.PACKET_SIZE];
            while (!sent) {
                for (int i = 0; i < Constants.PACKET_SIZE; i++) {
                    backup[i] = pack.MyPacketAsBytes[i];
                }
                Utils.SendTo(target, backup);
                timeout.Restart();
                while (timeout.ElapsedMilliseconds < Constants.PACKET_TIMEOUT_MILLISECONDS) {
                    if (target.Available != 0) {
                        receivedBytes = target.Receive(ref remoteIPEndPoint);

                        if (Utils.VerifyChecksum(receivedBytes)) {
                            if (receivedBytes[Constants.FIELD_TYPE] == Constants.TYPE_FILE_DELIVERY) {
                                sent = true;
                            }
                        }
                    }
                }
            }
            PingPong.SendAckTo(-1, target);

            bool success = false;
            while (!success) {
                AckPacket ack = new AckPacket(-1);
                Utils.SendTo(target, ack.MyPacketAsBytes);
                success = PingPong.ReceiveFileFrom(receivedBytes, target);
            }
            Console.WriteLine("File received successfully.");
        }
Ejemplo n.º 6
0
        public static void ReceiveFile(UdpClient udpSource, byte[] metadata)
        {
            int currentWorkingPacket = 0;
            int filenameSize = BitConverter.ToInt32(metadata, Constants.FIELD_FILENAME_LENGTH);
            int fileLength = BitConverter.ToInt32(metadata, Constants.FIELD_FILE_LENGTH);
            int packetTotal = BitConverter.ToInt32(metadata, Constants.FIELD_TOTAL_PACKETS);
            byte[] filenameBytes = new byte[filenameSize];
            for (int i = 0; i < filenameSize; i++) {
                filenameBytes[i] = metadata[i + Constants.FIELD_FILENAME];
            }
            string filename = Encoding.Unicode.GetString(filenameBytes);

            List<dataPacketBuffer> buffer = new List<dataPacketBuffer>();

            IPEndPoint remoteIPEndPoint = null;
            while (currentWorkingPacket < packetTotal) {
                byte[] receivedBytes = udpSource.Receive(ref remoteIPEndPoint);

                if (VerifyChecksum(receivedBytes)) {
                    byte[] payloadUnpacker = new byte[Constants.PAYLOAD_SIZE];
                    for (int i = 0; i < Constants.PAYLOAD_SIZE; i++) {
                        payloadUnpacker[i] = receivedBytes[i + Constants.FIELD_PAYLOAD];
                    }
                    dataPacketBuffer receivedPacket = new dataPacketBuffer(BitConverter.ToInt32(receivedBytes, Constants.FIELD_PACKET_ID), payloadUnpacker);

                    if (receivedPacket.getID() < currentWorkingPacket) {
                        AckPacket respond = new AckPacket(receivedPacket.getID());
                        SendTo(udpSource, respond.MakePacket());
                    }
                    else if (receivedPacket.getID() == currentWorkingPacket) {
                        File.AppendAllText(filename, Encoding.Default.GetString(payloadUnpacker));
                    }
                    else if (receivedPacket.getID() > currentWorkingPacket) {
                        buffer.Add(receivedPacket);
                    }
                }

                buffer.Sort(
                    delegate(dataPacketBuffer s1, dataPacketBuffer s2) {
                        return s1.getID().CompareTo(s2.getID());
                    }
                );
                foreach (dataPacketBuffer item in buffer) {
                    if (item.getID() == currentWorkingPacket) {
                        File.AppendAllText(filename, Encoding.Unicode.GetString(item.getPayload()));
                        buffer.Remove(item);
                        currentWorkingPacket++;
                    }
                }
            }
        }