I am trying to implement an async FTP client and a tried the following. It seems to work well with the exception of FTPS connections. As I was revieing my issue with FTPS i found the Rebex.Net.FtpOptions.TimeoutAsynchronousMethods enum. How does this work? What happens when the async method times out? How can i handle the timeout? Also If i want multiple options can i just bitwise & them together like the following:

// Set client options

client.Options = Rebex.Net.FtpOptions.TimeoutAsynchronousMethods & Rebex.Net.FtpOptions.KeepAliveDuringTransfer & Rebex.Net.FtpOptions.UseLargeBuffers & Rebex.Net.FtpOptions.DisableProgressPercentage;

// Old code mentioned above

public void Connect()
{
    IAsyncResult asyncResult = null;

    try
    {
        if(log != null)
            log.DebugFormat("TaskId: {0} | Openning a FTP connection to server '{1}:{2}'", taskId, this.ServerIP, this.Port);

        asyncResult = client.BeginConnect(this.ServerIP, this.Port, new AsyncCallback(Connected), null);

        if (log != null)
            log.DebugFormat("TaskId: {0} | Waiting for the FTP connection to open", taskId);

        asyncResult.AsyncWaitHandle.WaitOne(timeout, true);

        if (!asyncResult.IsCompleted)
            throw new Rebex.Net.FtpException("A timeout occured while attempting to open a FTP connection to the server", Rebex.Net.FtpExceptionStatus.Timeout);
    }
    finally
    {
        client.EndConnect(asyncResult);

        if (asyncResult != null)
             asyncResult.AsyncWaitHandle.Close();

        asyncResult = null;
    }
}

private void Connected(IAsyncResult asyncResult)
{
    if (log != null)
        log.DebugFormat("TaskId: {0} | Successfully opened the FTP connection", taskId);
}

asked 08 Sep '11, 23:47

mbell8903's gravatar image

mbell8903
15
accept rate: 0%

edited 09 Sep '11, 15:21

Lukas%20Pokorny's gravatar image

Lukas Pokorny ♦♦
2.4k28


By default, asynchronous operations started by methods (such as BeginConnect or BeginPutFile) never time out and the value of Ftp object's Timeout property doesn't affect them. But when the TimeoutAsynchronousMethods option is enabled, they time out just like synchronous methods (such as Connect or PutFile).

When an asynchronous method times out, an FtpException with Status of FtpExceptionStatus.Timeout is thrown. This is similar to other kind of errors. The asynchronous operations ends immediately, which means the callback gets called. The exception is thrown as soon as you can the End* method (such as EndConnect or EndPutFile).

Please note that the Timeout value doesn't specify the maximum time allowed for the whole operation. Instead, the operation times out when no response has been received for the specified amount of time.


You can specify multiple options, but you have to use bitwise | instead of &:

// Set client options
client.Options = Rebex.Net.FtpOptions.TimeoutAsynchronousMethods |
    Rebex.Net.FtpOptions.KeepAliveDuringTransfer |
    Rebex.Net.FtpOptions.UseLargeBuffers |
    Rebex.Net.FtpOptions.DisableProgressPercentage;

Using the & operator would be equivalent to client.Options = 0; and no flags would be set. For detailed explanation, check out this link.

link

answered 09 Sep '11, 15:32

Lukas%20Pokorny's gravatar image

Lukas Pokorny ♦♦
2.4k28
accept rate: 31%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×152
×4
×2

Asked: 08 Sep '11, 23:47

Seen: 376 times

Last updated: 09 Sep '11, 15:32