To properly close a transfer to the FTP server, you need to send an Abort command (see http://www.w3.org/Protocols/rfc959/4_FileTransfer.html (ABORT) ). Flex FTP doesn't support this so I added some functionality to be able to send an abort.
First I added the Abort command to the Commands class:
-
public static const ABOR:String = "ABOR";
After that I added a new invoker to the invokers package, AbortInv.
The FTPInvoker class required a new public method to support the Abort command, which can be overridden by the specific implementations. In FTPInvoker I added an abort method like this:
-
/**
-
* Stops this Invoker from executing.
-
*/
-
public function abort():void {
-
// Some Invokers just don't need to do anything.
-
finalize();
-
}
There was only one invoker where I had to override the abort method to get things working correctly and that was the UploadInv class. Here I added the following code:
-
/**
-
* @inheritDoc
-
*/
-
override public function abort():void {
-
super.abort();
-
-
if(interval) clearInterval(interval);
-
if(sourceFile) sourceFile.close();
-
if(passiveSocket && passiveSocket.connected) passiveSocket.close();
-
}
After that it was a matter of updating the FTPClient class with an abort method to get the circle complete.
-
/**
-
* Aborts the previous FTP command.
-
*/
-
public function abort():void {
-
if(currentProcess) {
-
if(currentProcess is UploadInv) {
-
swallowNextResponse = true;
-
}
-
currentProcess.abort();
-
currentProcess = null;
-
}
-
-
invoke( new AbortInv(this) );
-
}
Unfortunately I also had to update handleData method in the FTPClient class a little bit, because aborting an upload would result into 2 (or more) resonses at once from the FTP server.
My handleData method now looks like this:
-
private function handleData (pEvt:ProgressEvent):void
-
{
-
processing = false;
-
var response:String = ctrlSocket.readUTFBytes(ctrlSocket.bytesAvailable);
-
var responseList:Array = response.split("\n");
-
// Apparently we always get a linebreak with a response...
-
responseList.pop();
-
-
var eventList:Array = new Array();
-
var evt:FTPEvent;
-
for(var i:int = 0; i <responseList.length; i++) {
-
evt = new FTPEvent(FTPEvent.RESPONSE);
-
evt.response = FTPResponse.parseResponse(responseList[i]);
-
eventList.push(evt);
-
if (FTPClient.DEBUG) trace("->" + evt.response.code+" "+evt.response.message);
-
}
-
-
if(swallowNextResponse) {
-
if (FTPClient.DEBUG) trace(" - ignoring this response...");
-
swallowNextResponse = false;
-
responseList.shift();
-
}
-
-
if(responseList.length> 0) {
-
for(var k:int = 0; k <responseList.length; k++) {
-
dispatchEvent(eventList[k]);
-
}
-
}
-
}
Links