Skip to content

Commit

Permalink
Removed dependencies to the FTS utilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
xkbeyer committed Dec 28, 2015
1 parent 8ed3d92 commit 46c4f62
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 89 deletions.
7 changes: 1 addition & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,21 @@ 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)
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)

Expand Down
3 changes: 1 addition & 2 deletions include/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ enum class FTSC_ERR {
using PacketStats = std::unordered_map<master_request_t, std::pair<std::uint64_t, std::uint64_t>>;

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<std::uint8_t>& 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
Expand Down
61 changes: 14 additions & 47 deletions src/TraditionalConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#include "packet.h"
#include "Logger.h"

#include "utilities/DataContainer.h"

#if !defined( _WIN32 )
# include <unistd.h>
# include <poll.h>
Expand Down Expand Up @@ -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<uint8_t>& 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";
Expand All @@ -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;
Expand All @@ -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<uint32_t>(uiFileSize)) ) {
return -4;
auto err = tradConn.get_lowlevel( out_data.data(), static_cast<uint32_t>(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.
Expand All @@ -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<uint8_t> 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<size_t>(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;
Expand Down
5 changes: 1 addition & 4 deletions src/TraditionalConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/**
Expand All @@ -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<uint8_t>& 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);
Expand Down
1 change: 0 additions & 1 deletion src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "packet.h"
#include "Logger.h"
#include "TraditionalConnection.h"
#include "utilities/DataContainer.h"

#if defined( _WIN32 )
# if defined(_DEBUG)
Expand Down
51 changes: 24 additions & 27 deletions src/socket_connection_waiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ int FTS::SocketConnectionWaiter::init(std::uint16_t in_usPort, std::function<voi
return ERR_OK;
}

void printSocketError()
{
if( errno == EAGAIN || errno == EWOULDBLOCK ) {
return;
} else {
#if defined(_WIN32)
auto err = WSAGetLastError();
if( err == WSAEWOULDBLOCK ) {
return ;
}
FTSMSG( "[ERROR] socket accept: " + toString( err ), MsgType::Error );
#else
// Some error ... but continue waiting for a connection.
FTSMSG( "[ERROR] socket accept: " + string( strerror( errno ) ), MsgType::Error );
#endif
}
}

bool FTS::SocketConnectionWaiter::waitForThenDoConnection(std::int64_t in_ulMaxWaitMillisec)
{
auto startTime = std::chrono::steady_clock::now();
Expand All @@ -87,38 +105,17 @@ bool FTS::SocketConnectionWaiter::waitForThenDoConnection(std::int64_t in_ulMaxW

SOCKADDR_IN clientAddress;
socklen_t iClientAddressSize = sizeof( clientAddress );
SOCKET connectSocket;
if( (connectSocket = accept( m_listenSocket, ( sockaddr * ) & clientAddress, &iClientAddressSize )) != -1 ) {
SOCKET connectSocket = accept( m_listenSocket, (sockaddr *) & clientAddress, &iClientAddressSize );
if( connectSocket != -1 ) {
// Yeah, we got someone !

// Build up a class that will work this connection.
Connection *pCon = new TraditionalConnection(connectSocket, clientAddress);
Connection *pCon = new TraditionalConnection( connectSocket, clientAddress );
m_cb( pCon );
return true;

} else if(errno == EAGAIN || errno == EWOULDBLOCK) {
// yoyo, wait a bit to avoid megaload of cpu. 1000 microsec = 1 millisec.
std::this_thread::sleep_for( std::chrono::milliseconds(1) );
continue;
} else {
#if WINDOOF
if ( connectSocket == INVALID_SOCKET)
{
auto err = WSAGetLastError();
if ( err == WSAEWOULDBLOCK )
{
// yoyo, wait a bit to avoid megaload of cpu. 1000 microsec = 1 millisec.
std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
continue;
}

FTSMSG( "[ERROR] socket accept: " + toString( err ), MsgType::Error );
}

#endif
// Some error ... but continue waiting for a connection.
FTSMSG("[ERROR] socket accept: "+string(strerror(errno)), MsgType::Error);
// yoyo, wait a bit to avoid megaload of cpu. 1000 microsec = 1 millisec.
printSocketError();
// Even if an error occured we don't leave.
std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
continue;
}
Expand Down
2 changes: 0 additions & 2 deletions src/socket_connection_waiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#define FTS_SOCKETCONNECTIONWAITER_H

#include "connection_waiter.h"
#include <utilities/threading.h>

#if defined(_WIN32)
#include <WinSock2.h>
Expand All @@ -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<void( FTS::Connection* )> m_cb;
};
Expand Down

0 comments on commit 46c4f62

Please sign in to comment.