Back to index

libsfml  1.6+dfsg2
Public Member Functions | Private Attributes
sf::Ftp::DataChannel Class Reference

List of all members.

Public Member Functions

 DataChannel (Ftp &Owner)
 Constructor /////////////////////////////////////////////////////////.
 ~DataChannel ()
 Destructor /////////////////////////////////////////////////////////.
Ftp::Response Open (Ftp::TransferMode Mode)
 Open the data channel using the specified mode and port /////////////////////////////////////////////////////////.
void Send (const std::vector< char > &Data)
 Send data on the data channel /////////////////////////////////////////////////////////.
void Receive (std::vector< char > &Data)
 Receive data on the data channel until it is closed /////////////////////////////////////////////////////////.

Private Attributes

Ftp & myFtp
 Reference to the owner Ftp instance.
SocketTCP myDataSocket
 Socket used for data transfers.

Detailed Description

Definition at line 42 of file Ftp.cpp.


Constructor & Destructor Documentation

Constructor /////////////////////////////////////////////////////////.

Definition at line 600 of file Ftp.cpp.

                                      :
myFtp(Owner)
{

}

Destructor /////////////////////////////////////////////////////////.

Definition at line 610 of file Ftp.cpp.

{
    // Close the data socket
    myDataSocket.Close();
}

Member Function Documentation

Ftp::Response sf::Ftp::DataChannel::Open ( Ftp::TransferMode  Mode)

Open the data channel using the specified mode and port /////////////////////////////////////////////////////////.

Definition at line 620 of file Ftp.cpp.

{
    // Open a data connection in active mode (we connect to the server)
    Ftp::Response Resp = myFtp.SendCommand("PASV");
    if (Resp.IsOk())
    {
        // Extract the connection address and port from the response
        std::string::size_type begin = Resp.GetMessage().find_first_of("0123456789");
        if (begin != std::string::npos)
        {
            sf::Uint8 Data[6] = {0, 0, 0, 0, 0, 0};
            std::string Str = Resp.GetMessage().substr(begin);
            std::size_t Index = 0;
            for (int i = 0; i < 6; ++i)
            {
                // Extract the current number
                while (isdigit(Str[Index]))
                {
                    Data[i] = Data[i] * 10 + (Str[Index] - '0');
                    Index++;
                }

                // Skip separator
                Index++;
            }

            // Reconstruct connection port and address
            unsigned short Port = Data[4] * 256 + Data[5];
            sf::IPAddress Address(static_cast<sf::Uint8>(Data[0]),
                                  static_cast<sf::Uint8>(Data[1]),
                                  static_cast<sf::Uint8>(Data[2]),
                                  static_cast<sf::Uint8>(Data[3]));

            // Connect the data channel to the server
            if (myDataSocket.Connect(Port, Address) == Socket::Done)
            {
                // Translate the transfer mode to the corresponding FTP parameter
                std::string ModeStr;
                switch (Mode)
                {
                    case Ftp::Binary : ModeStr = "I"; break;
                    case Ftp::Ascii :  ModeStr = "A"; break;
                    case Ftp::Ebcdic : ModeStr = "E"; break;
                }

                // Set the transfer mode
                Resp = myFtp.SendCommand("TYPE", ModeStr);
            }
            else
            {
                // Failed to connect to the server
                Resp = Ftp::Response(Ftp::Response::ConnectionFailed);
            }
        }
    }

    return Resp;
}
void sf::Ftp::DataChannel::Receive ( std::vector< char > &  Data)

Receive data on the data channel until it is closed /////////////////////////////////////////////////////////.

Definition at line 683 of file Ftp.cpp.

{
    // Receive data
    Data.clear();
    char Buffer[1024];
    std::size_t Received;
    while (myDataSocket.Receive(Buffer, sizeof(Buffer), Received) == sf::Socket::Done)
    {
        std::copy(Buffer, Buffer + Received, std::back_inserter(Data));
    }

    // Close the data socket
    myDataSocket.Close();
}
void sf::Ftp::DataChannel::Send ( const std::vector< char > &  Data)

Send data on the data channel /////////////////////////////////////////////////////////.

Definition at line 702 of file Ftp.cpp.

{
    // Send data
    if (!Data.empty())
        myDataSocket.Send(&Data[0], Data.size());

    // Close the data socket
    myDataSocket.Close();
}

Member Data Documentation

Socket used for data transfers.

Definition at line 77 of file Ftp.cpp.

Reference to the owner Ftp instance.

Definition at line 76 of file Ftp.cpp.


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