Added windows support. Updated build system.

CMake build system builds as a library, not an executable now.

Windows is now supported and should work fine.
This commit is contained in:
Cloaked9000 2016-12-19 12:10:39 +00:00
parent 9479029c87
commit 2de9540819
27 changed files with 141 additions and 63 deletions

View File

@ -1,8 +1,12 @@
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.5)
project(frnetlib) project(frnetlib)
#Set module path
set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules) set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules)
#User options
option(USE_SSL "Use SSL" ON) option(USE_SSL "Use SSL" ON)
set(FRNETLIB_BUILD_SHARED_LIBS true CACHE BOOL "Build shared library.")
if(USE_SSL) if(USE_SSL)
FIND_PACKAGE(MBEDTLS) FIND_PACKAGE(MBEDTLS)
@ -10,12 +14,67 @@ if(USE_SSL)
add_definitions(-DSSL_ENABLED) add_definitions(-DSSL_ENABLED)
endif() endif()
include_directories(include) set( INCLUDE_PATH "${PROJECT_SOURCE_DIR}/include" )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -m64 -fPIC -pthread -lmbedtls -lmbedx509 -lmbedcrypto") set( SOURCE_PATH "${PROJECT_SOURCE_DIR}/src" )
set(SOURCE_FILES main.cpp src/TcpSocket.cpp include/TcpSocket.h src/TcpListener.cpp include/TcpListener.h src/Socket.cpp include/Socket.h src/Packet.cpp include/Packet.h include/NetworkEncoding.h src/SocketSelector.cpp include/SocketSelector.h src/HttpSocket.cpp include/HttpSocket.h src/HttpRequest.cpp include/HttpRequest.h src/HttpResponse.cpp include/HttpResponse.h src/Http.cpp include/Http.h src/SSLSocket.cpp include/SSLSocket.h src/SSLListener.cpp include/SSLListener.h include/SSLContext.h) set(SOURCE_FILES main.cpp src/TcpSocket.cpp include/frnetlib/TcpSocket.h src/TcpListener.cpp include/frnetlib/TcpListener.h src/Socket.cpp include/frnetlib/Socket.h src/Packet.cpp include/frnetlib/Packet.h include/frnetlib/NetworkEncoding.h src/SocketSelector.cpp include/frnetlib/SocketSelector.h src/HttpSocket.cpp include/frnetlib/HttpSocket.h src/HttpRequest.cpp include/frnetlib/HttpRequest.h src/HttpResponse.cpp include/frnetlib/HttpResponse.h src/Http.cpp include/frnetlib/Http.h src/SSLSocket.cpp include/frnetlib/SSLSocket.h src/SSLListener.cpp include/frnetlib/SSLListener.h include/frnetlib/SSLContext.h)
add_executable(frnetlib ${SOURCE_FILES})
include_directories(include)
# Set the library output directory
set( LIBRARY_OUTPUT_PATH "${PROJECT_BINARY_DIR}/lib" )
# Add the library.
if( FRNETLIB_BUILD_SHARED_LIBS )
add_library( frnetlib SHARED ${SOURCE_FILES} )
set_target_properties( frnetlib PROPERTIES DEBUG_POSTFIX -d )
else()
add_definitions( -DFRNETLIB_STATIC )
add_library( frnetlib ${SOURCE_FILES} )
set_target_properties( frnetlib PROPERTIES DEBUG_POSTFIX -s-d )
set_target_properties( frnetlib PROPERTIES RELEASE_POSTFIX -s )
set_target_properties( frnetlib PROPERTIES MINSIZEREL_POSTFIX -s )
endif()
# Tell the compiler to export when necessary.
set_target_properties( frnetlib PROPERTIES DEFINE_SYMBOL FRNETLIB_EXPORTS )
if( WIN32 )
set( SHARE_PATH "." )
set( LIB_PATH "lib" )
elseif(APPLE)
set( SHARE_PATH "${CMAKE_INSTALL_PREFIX}/share/FRNETLIB" )
set( LIB_PATH "lib" )
else()
set( SHARE_PATH "${CMAKE_INSTALL_PREFIX}/share/FRNETLIB" )
if( LIB_SUFFIX )
set( LIB_PATH "lib${LIB_SUFFIX}" )
else()
set( LIB_PATH "lib" )
endif()
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -m64 -fPIC")
if(USE_SSL) if(USE_SSL)
TARGET_LINK_LIBRARIES(frnetlib ${MBEDTLS_LIBRARIES} -lmbedtls -lmbedx509 -lmbedcrypto) if( WIN32 )
TARGET_LINK_LIBRARIES(frnetlib ${MBEDTLS_LIBRARIES} -lws2_32)
else()
TARGET_LINK_LIBRARIES(frnetlib ${MBEDTLS_LIBRARIES})
endif() endif()
endif()
install(
TARGETS frnetlib
RUNTIME DESTINATION bin COMPONENT bin
LIBRARY DESTINATION "${LIB_PATH}" COMPONENT bin
ARCHIVE DESTINATION "${LIB_PATH}" COMPONENT dev
)
install(
DIRECTORY include
DESTINATION .
)

View File

@ -235,7 +235,7 @@ You can both set and get GET/POST data through the fr::(HttpRequest/HttpResponse
//Remove them from the selector and close the connection //Remove them from the selector and close the connection
selector.remove(client); selector.remove(client);
client.close(); client.close_socket();
iter = connections.erase(iter); iter = connections.erase(iter);
} }
else else

View File

@ -5,9 +5,26 @@
#ifndef FRNETLIB_NETWORKENCODING_H #ifndef FRNETLIB_NETWORKENCODING_H
#define FRNETLIB_NETWORKENCODING_H #define FRNETLIB_NETWORKENCODING_H
#include <netinet/in.h>
#include <fcntl.h>
#include <cstring> #include <cstring>
#include <cstdint>
//Windows and UNIX require some different headers.
//We also need some compatibility defines for cross platform support.
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#else
#define closesocket(x) close(x)
#define INVALID_SOCKET 0
#define SOCKET_ERROR -1
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#endif
#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32)) #define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32)) #define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
@ -71,21 +88,4 @@ inline void set_unix_socket_blocking(int32_t socket_descriptor, bool is_blocking
} }
//Windows and UNIX require some different headers.
//We also need some compatibility defines for cross platform support.
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#else
#define closesocket(x) ::close(x)
#define INVALID_SOCKET 0
#define SOCKET_ERROR -1
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#endif
#endif //FRNETLIB_NETWORKENCODING_H #endif //FRNETLIB_NETWORKENCODING_H

View File

@ -5,7 +5,6 @@
#ifndef FRNETLIB_PACKET_H #ifndef FRNETLIB_PACKET_H
#define FRNETLIB_PACKET_H #define FRNETLIB_PACKET_H
#include <string> #include <string>
#include <netinet/in.h>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include "NetworkEncoding.h" #include "NetworkEncoding.h"

View File

@ -65,7 +65,7 @@ namespace fr
std::shared_ptr<SSLContext> ssl_context; std::shared_ptr<SSLContext> ssl_context;
//Stubs //Stubs
virtual void close(){} virtual void close_socket(){}
virtual Socket::Status connect(const std::string &address, const std::string &port){return Socket::Error;} virtual Socket::Status connect(const std::string &address, const std::string &port){return Socket::Error;}
virtual Status send_raw(const char *data, size_t size) {return Socket::Error;} virtual Status send_raw(const char *data, size_t size) {return Socket::Error;}
virtual Status receive_raw(void *data, size_t data_size, size_t &received) {return Socket::Error;} virtual Status receive_raw(void *data, size_t data_size, size_t &received) {return Socket::Error;}

View File

@ -53,7 +53,7 @@ namespace fr
/*! /*!
* Close the connection. * Close the connection.
*/ */
void close() override; void close_socket() override;
/*! /*!
* Connects the socket to an address. * Connects the socket to an address.

View File

@ -30,11 +30,12 @@ namespace fr
Socket() noexcept; Socket() noexcept;
virtual ~Socket() noexcept = default; virtual ~Socket() noexcept = default;
Socket(Socket &&) noexcept = default;
/*! /*!
* Close the connection. * Close the connection.
*/ */
virtual void close()=0; virtual void close_socket()=0;
/*! /*!
* Connects the socket to an address. * Connects the socket to an address.
@ -138,6 +139,11 @@ namespace fr
std::string remote_address; std::string remote_address;
bool is_blocking; bool is_blocking;
bool is_connected; bool is_connected;
#ifdef _WIN32
static WSADATA wsaData;
static uint32_t instance_count;
#endif // _WIN32
}; };
} }

View File

@ -5,7 +5,6 @@
#ifndef FRNETLIB_TCPLISTENER_H #ifndef FRNETLIB_TCPLISTENER_H
#define FRNETLIB_TCPLISTENER_H #define FRNETLIB_TCPLISTENER_H
#include <string> #include <string>
#include <netdb.h>
#include "TcpSocket.h" #include "TcpSocket.h"
#include "Socket.h" #include "Socket.h"
#include "NetworkEncoding.h" #include "NetworkEncoding.h"
@ -40,7 +39,7 @@ private:
int32_t socket_descriptor; int32_t socket_descriptor;
//Stubs //Stubs
virtual void close(){} virtual void close_socket(){}
virtual Socket::Status connect(const std::string &address, const std::string &port){return Socket::Error;} virtual Socket::Status connect(const std::string &address, const std::string &port){return Socket::Error;}
virtual void set_blocking(bool val){} virtual void set_blocking(bool val){}
virtual fr::Socket::Status send_raw(const char*, size_t){return Socket::Error;} virtual fr::Socket::Status send_raw(const char*, size_t){return Socket::Error;}

View File

@ -23,7 +23,7 @@ public:
/*! /*!
* Close the connection. * Close the connection.
*/ */
virtual void close(); virtual void close_socket();
/*! /*!
* Connects the socket to an address. * Connects the socket to an address.

View File

@ -1,15 +1,15 @@
#include <iostream> #include <iostream>
#include <SSLListener.h> #include <frnetlib/SSLListener.h>
#include "include/Packet.h" #include "frnetlib/Packet.h"
#include "include/TcpSocket.h" #include "frnetlib/TcpSocket.h"
#include "include/TcpListener.h" #include "frnetlib/TcpListener.h"
#include "include/SocketSelector.h" #include "frnetlib/SocketSelector.h"
#include "HttpSocket.h" #include "frnetlib/HttpSocket.h"
#include "HttpRequest.h" #include "frnetlib/HttpRequest.h"
#include "HttpResponse.h" #include "frnetlib/HttpResponse.h"
#include "SSLSocket.h" #include "frnetlib/SSLSocket.h"
#include "SSLContext.h" #include "frnetlib/SSLContext.h"
#include "SSLListener.h" #include "frnetlib/SSLListener.h"
int main() int main()
{ {

View File

@ -3,7 +3,7 @@
// //
#include <iostream> #include <iostream>
#include "Http.h" #include "frnetlib/Http.h"
namespace fr namespace fr
{ {

View File

@ -2,7 +2,7 @@
// Created by fred on 10/12/16. // Created by fred on 10/12/16.
// //
#include "HttpRequest.h" #include "frnetlib/HttpRequest.h"
namespace fr namespace fr
{ {

View File

@ -3,7 +3,7 @@
// //
#include <iostream> #include <iostream>
#include "HttpResponse.h" #include "frnetlib/HttpResponse.h"
namespace fr namespace fr
{ {
@ -70,7 +70,7 @@ namespace fr
//Add in required headers if they're missing //Add in required headers if they're missing
if(headers.find("Connection") == headers.end()) if(headers.find("Connection") == headers.end())
response += "Connection: close\r\n"; response += "Connection: close_socket\r\n";
if(headers.find("Content-type") == headers.end()) if(headers.find("Content-type") == headers.end())
response += "Content-type: text/html\r\n"; response += "Content-type: text/html\r\n";

View File

@ -2,8 +2,8 @@
// Created by fred on 10/12/16. // Created by fred on 10/12/16.
// //
#include <HttpResponse.h> #include <frnetlib/HttpResponse.h>
#include "HttpSocket.h" #include "frnetlib/HttpSocket.h"
namespace fr namespace fr
{ {

View File

@ -2,7 +2,7 @@
// Created by fred on 06/12/16. // Created by fred on 06/12/16.
// //
#include "Packet.h" #include "frnetlib/Packet.h"
namespace fr namespace fr
{ {

View File

@ -3,7 +3,7 @@
// //
#include <chrono> #include <chrono>
#include "SSLListener.h" #include "frnetlib/SSLListener.h"
#ifdef SSL_ENABLED #ifdef SSL_ENABLED
namespace fr namespace fr

View File

@ -2,7 +2,7 @@
// Created by fred on 12/12/16. // Created by fred on 12/12/16.
// //
#include "SSLSocket.h" #include "frnetlib/SSLSocket.h"
#include <memory> #include <memory>
#ifdef SSL_ENABLED #ifdef SSL_ENABLED
@ -20,13 +20,13 @@ namespace fr
SSLSocket::~SSLSocket() noexcept SSLSocket::~SSLSocket() noexcept
{ {
//Close connection if active //Close connection if active
close(); close_socket();
//Cleanup mbedsql stuff //Cleanup mbedsql stuff
mbedtls_ssl_config_free(&conf); mbedtls_ssl_config_free(&conf);
} }
void SSLSocket::close() void SSLSocket::close_socket()
{ {
if(is_connected) if(is_connected)
{ {

View File

@ -2,16 +2,31 @@
// Created by fred on 06/12/16. // Created by fred on 06/12/16.
// //
#include "Socket.h" #include "frnetlib/Socket.h"
namespace fr namespace fr
{ {
#ifdef _WIN32
WSADATA Socket::wsaData = WSADATA();
uint32_t Socket::instance_count = 0;
#endif // _WIN32
Socket::Socket() noexcept Socket::Socket() noexcept
: is_blocking(true), : is_blocking(true),
is_connected(false) is_connected(false)
{ {
#ifdef _WIN32
if(instance_count == 0)
{
int wsa_result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if(wsa_result != 0)
{
std::cout << "Failed to initialise WSA." << std::endl;
return;
}
}
instance_count++;
#endif // _WIN32
} }
Socket::Status Socket::send(const Packet &packet) Socket::Status Socket::send(const Packet &packet)

View File

@ -2,7 +2,7 @@
// Created by fred on 09/12/16. // Created by fred on 09/12/16.
// //
#include "SocketSelector.h" #include "frnetlib/SocketSelector.h"
namespace fr namespace fr
{ {

View File

@ -2,7 +2,7 @@
// Created by fred on 06/12/16. // Created by fred on 06/12/16.
// //
#include "TcpListener.h" #include "frnetlib/TcpListener.h"
namespace fr namespace fr
{ {

View File

@ -3,7 +3,7 @@
// //
#include <iostream> #include <iostream>
#include "TcpSocket.h" #include "frnetlib/TcpSocket.h"
namespace fr namespace fr
{ {
@ -16,7 +16,7 @@ namespace fr
TcpSocket::~TcpSocket() noexcept TcpSocket::~TcpSocket() noexcept
{ {
close(); close_socket();
} }
Socket::Status TcpSocket::send_raw(const char *data, size_t size) Socket::Status TcpSocket::send_raw(const char *data, size_t size)
@ -43,11 +43,11 @@ namespace fr
return Socket::Status::Success; return Socket::Status::Success;
} }
void TcpSocket::close() void TcpSocket::close_socket()
{ {
if(is_connected) if(is_connected)
{ {
::close(socket_descriptor); ::closesocket(socket_descriptor);
is_connected = false; is_connected = false;
} }
} }