Logo Search packages:      
Sourcecode: camstream version File versions  Download package

CFTPClient Class Reference

List of all members.

Detailed Description

A low-level, asynchronous FTP client based on RFC 959.

This class implements an FTP client. It allows the usual things done through FTP (uploading, downloading, dir-listing, etc), but at a very basic level.

It may be a bit difficult to use, because it is so low level. It isn't as simple as "open ftp server, do dir-listing, upload file, close". All these functions are there, but they are asynchronuous. That is, they return immediately and don't wait for the end result. Nor can you queue commands: only one command can be executed at a time. This means you will have to do the high-level protocol stuff in your own class. On the other hand, it allows you to accurately follow the progress of your commands. Think of it as a wrapper around the FTP protocol itself, with some helper functions for the data transfers.

With this class you have to make a distinction between 'commands' and 'state'. A command is just that: the user demands a certain action, and the class executes it. A string is sent to the FTP server to perform the command; if a data stream is needed that is opened as well.

A command will cause various changes in the class that can be tracked the ref StateChange signal. For example, uploading a file will cause the following states: SendingPort, WaitData, Transfer, ClosingData, Idle (or Failed). It reflects the various steps necessary by the FTP protocol to perform an actual filetransfer. It is up to the calling class to interpret these responses (most aren't really interesting to the user anyway).

All commands will end in either "Idle" (== success) or "Failed" state. "Failed" indicates something went wrong; the result code from StateChange contains the numeric FTP response code that explains the error. "Idle", which also doubles as "Success", indicates completion of the command without error.

Note that it is possible for a state to be set multiple times in a row. If, for example, Login was succesful and was followed by a "Change Dir" that also succeeds, StateChange will be emitted twice with state "Idle". This is okay; 2 commands were issued, so you get two StateChange(Idle) (possibly intermingled with other StateChange()s).

Most commands listed below show a short state transition diagram, so you know what StateChange()s to expect.

Definition at line 17 of file FTPClient.h.

Public Types

enum  Commands {
  cmdNop, cmdLogin, cmdLogout, cmdSetType,
  cmdChangeDir, cmdListDir, cmdUpload, cmdDownload,
  cmdRename, cmdPassive, cmdDestroy
enum  States {
  stNOC, stConnecting, stConnected, stLogin,
  stAuthenticate, stIdle, stSendingPort, stWaitData,
  stTransfer, stClosingData, stFailed, stUnknown


void ControlPortClosed ()
 Emitted when the control connection was closed (either by us or the remote party).
void ListDirEntry (const QString &filename)
void LoggedIn ()
 Emitted when login was succesfull.
void LoginFailed ()
 Emitted when login failed.
void Progress (int offset)
 Emitted every now and then to report on progress. Reports total amount of bytes transfered so far.
void StateChange (int command, int new_state, int result, const QString &server_msg)
void TimeOut ()

Public Member Functions

void ChangeDir (const QString &new_dir)
 Change to remote directory on server.
void Connect (const QString &user, const QString &pass, const QString &server, int port=21)
 Initiate connection to FTP server.
int GetCommand () const
QString GetErrorString (int result) const
int GetState () const
void ListDir ()
 List contents of remote directory.
void Logout ()
void Rename (const QString &from, const QString &to)
 Rename a file on the server.
void SetActive ()
 Turns off passive mode; see SetPassive.
void SetPassive ()
 Set transfer mode to passive.
void SetTypeAscii ()
void SetTypeBinary ()
void Upload (const QString &local_file, const QString &remote_file=QString::null)
 Upload file to remote site.

Static Public Attributes

static const char * CommandStr []
static const char * StateStr []

Private Slots

void ControlRead (int fd)
void DataConnect (int fd)
 Asynchronuous data connect.
void DataRead (int fd)
void DataWrite (int fd)
 Activated when the output socket has space.
void ListenRead (int fd)
 Activated when our data port is contacted.

Private Member Functions

void CloseAllFD ()
 Closes all TCP connections immediately.
void CloseControl ()
 Shut down connection to FTP server control port.
void CloseData ()
 Close data socket.
void CloseFile ()
 Close fd of file on disk.
void CloseListen ()
 Close our listen port.
void HookData ()
void InitBuffers ()
void InterpretLine ()
 Takes a line from the control port input buffer and unfolds it.
void InterpretResponse ()
 Takes a complete, unfolded line from the control port and interprets the response.
void Send ()
 Send content of outputbuffer.
void SendList ()
void SendPass ()
void SendPort (struct sockaddr_in &addr, int port)
void SendStore (const QString &filename)
void SendUser ()
void SetState (States new_op, int response=-1)
 Set new state en emit signal.
int SetupControl ()
 Try to connect to the server's FTP control port.
int SetupDataActive ()
 Make a data pipe in active mode (that is, we listen).
int SetupDataPassive ()
int SetupListen ()
 Sets up our incoming port where we will listen.
int SetupLocal (const QString &filename, bool write)
 Set up the local file for transfer.
void StartReceiving ()
void StartSending ()

Private Attributes

int CtrlFD
Commands CurrentCommand
States CurrentState
int DataFD
bool Direction
char * inputbuffer
char LastChar
char * linebuffer
int LineLen
int ListenFD
int LocalFileFD
bool m_Passive
unsigned int m_PassiveAddress
unsigned short m_PassivePort
bool MultiLine
QString outputbuffer
QString Password
QSocketNotifier * pControlPipe
QSocketNotifier * pDataPipe
QSocketNotifier * pListenPipe
QString RemoteFileName
int RespLen
int Response
char * responsebuffer
struct sockaddr_in MyAddr ServerAddress
int TBufHead
int TBufLen
int TBufTail
int TBufUsed
int TotalTransfered
uchar * transferbuffer
int TransferMethod
QString UserName

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index