Esempio n. 1
0
        public static bool ReceiveAllDirectoryPackets(byte[] first, DirMetadata meta, UdpClient target)
        {
            IPEndPoint remoteIPEndPoint = null;
            if (first == null) { return false; }
            Data data = new Data(first);

            if (meta.dirLength < Constants.PAYLOAD_SIZE)
                Console.WriteLine(Encoding.Default.GetString(data.payload,0,meta.dirLength));
            else
                Console.WriteLine(Encoding.Default.GetString(data.payload));

            SendAckTo(data.id, target);
            int currentPacket = 1;
            while (currentPacket < meta.totalPackets) {
                byte[] recBytes = target.Receive(ref remoteIPEndPoint);
                if (!Utils.VerifyChecksum(recBytes)) {
                    continue;
                }
                data = new Data(recBytes);
                if (data.id < currentPacket) {
                    SendAckTo(data.id, target);
                }
                else if (data.id == currentPacket && currentPacket != meta.totalPackets - 1) {
                    Console.WriteLine(Encoding.Default.GetString(data.payload));
                    SendAckTo(data.id, target);
                    currentPacket++;
                }
                else if (data.id == currentPacket && currentPacket == meta.totalPackets - 1) {

                    byte[] finalBytes = new byte[(meta.dirLength % Constants.PAYLOAD_SIZE)];
                    for (int i = 0; i < finalBytes.Length; i++) {
                        finalBytes[i] = data.payload[i + Constants.FIELD_PAYLOAD];
                    }
                    Console.WriteLine(Encoding.Default.GetString(finalBytes));
                    currentPacket++;

                    SendAckTo(data.id, target);
                }
            }
            return true;
        }
Esempio n. 2
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);
        }