diff --git a/CMakeLists.txt b/CMakeLists.txt index d9af14a..339ed07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,15 +31,11 @@ if(MSVC) else() add_definitions(-fpermissive) endif() -set( FTS_DIR "" CACHE PATH "Path to the FTS root src directory" ) set( src ./src/connection.cpp ./src/TraditionalConnection.cpp ./src/packet.cpp ./src/Logger.cpp ./src/socket_connection_waiter.cpp ./src/connection_waiter.cpp) set( src_h ./src/TraditionalConnection.h ./src/socket_connection_waiter.h ) set( hdr ./include/connection.h ./include/packet.h ./include/packet_header.h ./include/Logger.h ./include/TextFormatting.h ./include/dsrv_constants.h ./include/connection_waiter.h) -set( FTS_UTIL_SRC ${FTS_DIR}/utilities/DataContainer.cpp ${FTS_DIR}/utilities/threading.cpp) include_directories(${PROJECT_SOURCE_DIR}/include) -include_directories(${FTS_DIR}) -add_definitions(-DD_NOCEGUI) if(MSVC) add_definitions(-D_CRT_SECURE_NO_WARNINGS) @@ -47,10 +43,9 @@ add_definitions(-D_WINSOCK_DEPRECATED_NO_WARNINGS) source_group( Source FILES ${src}) source_group( Header FILES ${hdr}) source_group( InternalHeader FILES ${src_h}) -source_group( Utility FILES ${FTS_UTIL_SRC}) endif() -add_library(fts-net STATIC ${hdr} ${src_h} ${src} ${FTS_LOGGER_SRC} ${FTS_UTIL_SRC}) +add_library(fts-net STATIC ${hdr} ${src_h} ${src} ) set_property(TARGET fts-net PROPERTY CXX_STANDARD 14) set_property(TARGET fts-net PROPERTY CXX_STANDARD_REQUIRED ON) diff --git a/include/connection.h b/include/connection.h index 69cdf52..2c320e1 100644 --- a/include/connection.h +++ b/include/connection.h @@ -36,9 +36,8 @@ enum class FTSC_ERR { using PacketStats = std::unordered_map>; namespace FTS { -class RawDataContainer; -RawDataContainer *getHTTPFile( const std::string &in_sServer, const std::string &in_sPath, std::uint64_t in_ulMaxWaitMillisec ); +FTSC_ERR getHTTPFile(std::vector& out_data, const std::string &in_sServer, const std::string &in_sPath, std::uint64_t in_ulMaxWaitMillisec ); int downloadHTTPFile( const std::string &in_sServer, const std::string &in_sPath, const std::string &in_sLocal, std::uint64_t in_ulMaxWaitMillisec ); /// The FTS connection class diff --git a/src/TraditionalConnection.cpp b/src/TraditionalConnection.cpp index 48a224e..d3c5d7f 100644 --- a/src/TraditionalConnection.cpp +++ b/src/TraditionalConnection.cpp @@ -19,8 +19,6 @@ #include "packet.h" #include "Logger.h" -#include "utilities/DataContainer.h" - #if !defined( _WIN32 ) # include # include @@ -914,12 +912,12 @@ int FTS::TraditionalConnection::setSocketBlocking( SOCKET in_socket, bool in_bBl * * \author Pompei2 */ -int FTS::getHTTPFile(FTS::RawDataContainer &out_data, const std::string &in_sServer, const std::string &in_sPath, std::uint64_t in_ulMaxWaitMillisec) +FTSC_ERR FTS::getHTTPFile( std::vector& out_data, const std::string &in_sServer, const std::string &in_sPath, std::uint64_t in_ulMaxWaitMillisec) { // We connect using the traditional connection. TraditionalConnection tradConn(in_sServer, 80, in_ulMaxWaitMillisec); if(!tradConn.isConnected()) - return -1; + return FTSC_ERR::NOT_CONNECTED; // Send the request to get that file. std::string sToSend = "GET http://" + in_sServer + in_sPath + " HTTP/1.0\r\n\r\n"; @@ -936,11 +934,11 @@ int FTS::getHTTPFile(FTS::RawDataContainer &out_data, const std::string &in_sSer std::string sCode = sLine.substr( pos + 9, 3 ); if( sCode != "200" ) { // 200 means OK. - return -2; + return FTSC_ERR::INVALID_INPUT; } } else { // The code can't be found. - return -2; + return FTSC_ERR::INVALID_INPUT; } // The following loop reads out the HTTP header and gets the data size. uint64_t uiFileSize = 0; @@ -963,48 +961,18 @@ int FTS::getHTTPFile(FTS::RawDataContainer &out_data, const std::string &in_sSer // Did not get any header about the data length. if(uiFileSize == 0) - return -3; + return FTSC_ERR::INVALID_INPUT; // Get the memory to write the data in. - out_data.resize(uiFileSize); + out_data.resize( uiFileSize, 0 ); // Get the data. - if( FTSC_ERR::OK != tradConn.get_lowlevel(out_data.getData(), static_cast(uiFileSize)) ) { - return -4; + auto err = tradConn.get_lowlevel( out_data.data(), static_cast(uiFileSize)); + if( FTSC_ERR::OK != err ) { + return err; } - return 0; -} - -/// Gets a file via HTTP. -/** This sends an HTTP server the request to get a file and then gets that file - * from the server. - * - * \param in_sServer The server address to connect to, ex: arkana-fts.org - * \param in_sPath The path to the file on the server, ex: /path/to/file.ex - * \param out_uiFileSize Will be set to the size of the data that will be returned. - * - * \return If successful: A new DataContainer containing the file. - * \return If failed: NULL - * - * \note The DataContainer object you get should be deleted by you. - * \note On linux, only an exactitude of 1-10 millisecond may be achieved. Anyway, on most PC's - * an exactitude of more the 10ms is nearly never possible. - * \note Although the \a out_uiFileSize is a 64-bit unsigned integer, the - * support for large files (>4GB) is not implemented yet. - * - * \author Pompei2 - */ -FTS::RawDataContainer *FTS::getHTTPFile(const std::string &in_sServer, const std::string &in_sPath, std::uint64_t in_ulMaxWaitMillisec) -{ - // Get the memory to write the data in. - FTS::RawDataContainer *pdc = new FTS::RawDataContainer(0); - - if( 0 != FTS::getHTTPFile( *pdc, in_sServer, in_sPath, in_ulMaxWaitMillisec ) ) { - delete pdc; - pdc = nullptr; - } - return pdc; + return FTSC_ERR::OK; } /// Downloads a file via HTTP and saves it locally. @@ -1029,22 +997,21 @@ FTS::RawDataContainer *FTS::getHTTPFile(const std::string &in_sServer, const std */ int FTS::downloadHTTPFile(const std::string &in_sServer, const std::string &in_sPath, const std::string &in_sLocal, std::uint64_t in_ulMaxWaitMillisec) { - FTS::DataContainer *pData = FTS::getHTTPFile(in_sServer, in_sPath, in_ulMaxWaitMillisec); - if(pData == nullptr) + std::vector Data; + auto err = FTS::getHTTPFile(Data, in_sServer, in_sPath, in_ulMaxWaitMillisec); + if(err != FTSC_ERR::OK) return -1; FILE *pFile = fopen(in_sLocal.c_str(), "w+b"); if(!pFile) { FTS18N("File_FopenW", MsgType::Error, in_sLocal, strerror(errno)); - delete pData; return -2; } - bool bSuccess = fwrite(pData->getData(), static_cast(pData->getSize()), 1, pFile) == 1; + bool bSuccess = fwrite(Data.data(), Data.size(), 1, pFile) == 1; if(!bSuccess) FTS18N("File_Write", MsgType::Error, in_sLocal, strerror(errno)); - delete pData; fclose(pFile); return bSuccess ? 0 : -3; diff --git a/src/TraditionalConnection.h b/src/TraditionalConnection.h index 014033c..447d817 100644 --- a/src/TraditionalConnection.h +++ b/src/TraditionalConnection.h @@ -40,9 +40,6 @@ using SOCKADDR_IN = struct sockaddr_in; namespace FTS { - class RawDataContainer; - -int getHTTPFile(FTS::RawDataContainer &out_data, const std::string &in_sServer, const std::string &in_sPath, std::uint64_t in_ulMaxWaitMillisec); /// A Traditional TCP/IP implementation of the connection class. /** @@ -58,7 +55,7 @@ int getHTTPFile(FTS::RawDataContainer &out_data, const std::string &in_sServer, **/ class TraditionalConnection : public Connection { friend class OnDemandHTTPConnection; - friend int FTS::getHTTPFile(FTS::RawDataContainer &out_data, const std::string &in_sServer, const std::string &in_sPath, std::uint64_t in_ulMaxWaitMillisec); + friend FTSC_ERR getHTTPFile( std::vector& out_data, const std::string &in_sServer, const std::string &in_sPath, std::uint64_t in_ulMaxWaitMillisec ); public: TraditionalConnection(const std::string &in_sName, std::uint16_t in_usPort, std::uint64_t in_ulTimeoutInMillisec); diff --git a/src/connection.cpp b/src/connection.cpp index 7469038..696ce50 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -19,7 +19,6 @@ #include "packet.h" #include "Logger.h" #include "TraditionalConnection.h" -#include "utilities/DataContainer.h" #if defined( _WIN32 ) # if defined(_DEBUG) diff --git a/src/socket_connection_waiter.cpp b/src/socket_connection_waiter.cpp index 512d7fd..43942a2 100644 --- a/src/socket_connection_waiter.cpp +++ b/src/socket_connection_waiter.cpp @@ -74,6 +74,24 @@ int FTS::SocketConnectionWaiter::init(std::uint16_t in_usPort, std::function #if defined(_WIN32) #include @@ -31,7 +30,6 @@ class SocketConnectionWaiter : public ConnectionWaiter { protected: SOCKET m_listenSocket; ///< The socket that has been prepared for listening. - FTS::Mutex m_mutex; ///< Mutex for the connections list. unsigned short m_port; ///< For debugging hold the port no we listening. std::function m_cb; };