private static void HandleDirectoryRequest(UdpClient server) { DirectoryMetadataPacket dir = new DirectoryMetadataPacket(Constants.TYPE_DIRECTORY_REQUEST); PingPong.sendUntilAck(dir, server); IPEndPoint remoteIPEndPoint = null; bool success = false; while (!success) { success = PingPong.StartReceiveDirectory(server.Receive(ref remoteIPEndPoint), server); } }
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; }