Merge remote-tracking branch 'origin/master'

This commit is contained in:
Unknown 2017-05-29 18:57:47 +01:00
commit bf8a6345d7
14 changed files with 253 additions and 31 deletions

34
examples/CMakeLists.txt Normal file
View File

@ -0,0 +1,34 @@
cmake_minimum_required(VERSION 3.5)
project(frnetlib_test)
if( WIN32 )
set( ADDITIONAL_LIB ws2_32 ) # Ws2_32.lib
set( FRNETLIB_ROOT_PATH "C:/tools/cmake_install_libs/frnetlib/" ) # change it to your install directory
set( FRNETLIB_INCLUDE_PATH ${FRNETLIB_ROOT_PATH}/include )
set( FRNETLIB_LIB ${FRNETLIB_ROOT_PATH}/lib/frnetlib-s-d.lib )
include_directories( ${FRNETLIB_INCLUDE_PATH} )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -std=c++14")
elseif(APPLE)
set( ADDITIONAL_LIB "" )
set( FRNETLIB_LIB frnetlib)
else()
set( ADDITIONAL_LIB "" )
set( FRNETLIB_LIB frnetlib)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -m64 -fPIC -std=c++14 -pthread -lmbedtls -lmbedx509 -lmbedcrypto")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -m64 -fPIC -std=c++14 -pthread")
endif()
add_executable(${PROJECT_NAME} ../main.cpp)
add_executable(packet packet.cpp)
add_executable(tcpsocket_server tcpsocket_server.cpp)
add_executable(tcpsocket_client tcpsocket_client.cpp)
target_link_libraries(${PROJECT_NAME} ${FRNETLIB_LIB} ${ADDITIONAL_LIB})
target_link_libraries(packet ${FRNETLIB_LIB} ${ADDITIONAL_LIB})
target_link_libraries(tcpsocket_server ${FRNETLIB_LIB} ${ADDITIONAL_LIB})
target_link_libraries(tcpsocket_client ${FRNETLIB_LIB} ${ADDITIONAL_LIB})

14
examples/packet.cpp Normal file
View File

@ -0,0 +1,14 @@
#include <iostream>
#include "frnetlib/Packet.h"
int main()
{
fr::Packet packet;
std::vector<std::pair<int, int>> bob = {{1, 2}, {3, 4}};
packet << bob;
bob.clear();
packet >> bob;
std::cout << bob[0].first << ", " << bob[0].second << ", " << bob[1].first << ", " << bob[1].second << std::endl;
}

View File

@ -0,0 +1,90 @@
#include <frnetlib/TcpSocket.h>
#include <frnetlib/TcpListener.h>
using namespace std;
int client_round(fr::TcpSocket& socket)
{
cout << "CLIENT:Going to send something ..." << endl;
//Receive the request
fr::Packet packet;
packet << "Hello there, I am" << (float)1.2 << "years old";
if (socket.send(packet) != fr::Socket::Success)
{
//Failed to send packet
cout << "CLIENT:Seems got something wrong when sending" << endl;
return -1;
}
cout << "CLIENT:Going to receive ..." << endl;
if (socket.receive(packet) != fr::Socket::Success)
{
cout << "CLIENT:seems got something wrong when receiving" << endl;
return -2;
}
std::string str1, str2;
float age;
packet >> str1 >> age >> str2;
cout << "CLIENT:we got:" << str1 << age << str2 << endl;
cout << "CLIENT:round finished" << endl;
cout << endl << endl << endl << endl;
return 0;
}
int main()
{
fr::TcpSocket socket;
string server_ip = "127.0.0.1";
string server_port = "8081";
if (socket.connect(server_ip, server_port) != fr::Socket::Success)
{
//Failed to connect
cout << "CLIENT:it seem that the socket can be accessed or there is no such socket at all" << endl;
socket.close_socket();
return -1;
}
string op_str;
int rtn = 0;
while (true) {
cout << "CLIENT:choose what you want to do, `c` for `continue`, `q` for `quit`:" << endl;
cin >> op_str; // count for possible mutiple char input
if (op_str.length() > 1) {
cout << "CLIENT:Seems that you input more than one char, plese check your input" << endl;
continue;
}
char op = op_str[0];
switch (op) {
case 'c':
cout << "continue" << endl;
rtn = client_round(socket);
break;
case 'q':
break;
}
if (op == 'q')
break;
if (rtn != 0)
break;
}
socket.close_socket();
cout << "all done, bye" << endl;
}

View File

@ -0,0 +1,76 @@
#include <frnetlib/TcpSocket.h>
#include <frnetlib/TcpListener.h>
using namespace std;
int main(){
//fr::HttpSocket<fr::TcpSocket> client; //fr::TcpSocket for HTTP. fr::SSLSocket for HTTPS.
fr::TcpSocket client;
fr::TcpListener listener; //Use an fr::SSLListener if HTTPS.
string port = "8081";
//Bind to a port
if(listener.listen(port) != fr::Socket::Success)
{
cout << "LISTENER:Failed to bind to port, going to shutdown" << endl;
listener.shutdown();
return -1;
}
cout << "LISTENER:Listener is listening on port " << port << " ..." << endl;
while (true)
{
cout << "LISTENER:Waiting for a new connection ..." << endl;
//Accept a new connection
if (listener.accept(client) != fr::Socket::Success)
{
cout << "LISTENER:Failed to accept client, shutdown" << endl;
break;
}
while (true) // infinate loop for the communication
{
try
{
fr::Packet packet;
if (client.receive(packet) != fr::Socket::Success)
{
cout << "LISTENER:Failed to receive request" << endl;
}
std::string str1, str2;
float age;
packet >> str1 >> age >> str2;
cout << "LISTENER:We got from client:" << str1 << age << str2 << endl;
if (client.send(packet) != fr::Socket::Success)
{
cout << "LISTENER:Seems got something wrong when sending" << endl;
//return -2;
}
}
catch (const std::exception& e)
{
cout << "ERROR: " << e.what() << endl;
cout << "LISTENER:Seems that the client stop the connection, just destory current connection and wait for another" << endl;
//Close connection
client.close_socket();
break;
}
} // inner while
}// out while
cout << "Should not got there !!" << endl;
listener.shutdown();
return 0;
}

View File

@ -41,7 +41,7 @@ namespace fr
*
* @param header_end_pos The position in 'body' of the end of the header
*/
void parse_header(ssize_t header_end_pos);
void parse_header(int32_t header_end_pos);
/*!
* Parses the POST data from the body
@ -64,7 +64,7 @@ namespace fr
//State
bool header_ended;
ssize_t last_parsed_character;
int32_t last_parsed_character;
size_t content_length;
};

View File

@ -41,7 +41,7 @@ namespace fr
*
* @param header_end_pos The position in 'body' of the end of the header
*/
void parse_header(ssize_t header_end_pos);
void parse_header(int32_t header_end_pos);
//State
bool header_ended;

View File

@ -28,44 +28,49 @@
#endif
#ifdef __GNUC__
#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))
inline float htonf(float val)
{
uint32_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = htonl(ret);
memcpy(&val, &ret, sizeof(val));
return val;
uint32_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = htonl(ret);
memcpy(&val, &ret, sizeof(val));
return val;
}
inline float ntohf(float val)
{
uint32_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = ntohl(ret);
memcpy(&val, &ret, sizeof(val));
return val;
uint32_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = ntohl(ret);
memcpy(&val, &ret, sizeof(val));
return val;
}
inline double htond(double val)
{
uint64_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = htonll(ret);
memcpy(&val, &ret, sizeof(val));
return val;
uint64_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = htonll(ret);
memcpy(&val, &ret, sizeof(val));
return val;
}
inline double ntohd(double val)
{
uint64_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = ntohll(ret);
memcpy(&val, &ret, sizeof(val));
return val;
uint64_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = ntohll(ret);
memcpy(&val, &ret, sizeof(val));
return val;
}
#endif
inline void *get_sin_addr(struct sockaddr *sa)
{
if(sa->sa_family == AF_INET)

View File

@ -53,6 +53,7 @@ namespace fr
*/
bool load_ca_certs_from_memory(const std::string &ca_certs)
{
std::cerr << "Note: load_ca_certs_from_memory() seems to be broken. Please use load_ca_certs_from_file() until this is resolved." << std::endl;
int error = mbedtls_x509_crt_parse(&cacert, (const unsigned char *)ca_certs.c_str(), ca_certs.size());
if(error < 0)
{

View File

@ -52,7 +52,7 @@ namespace fr
return true;
}
void HttpRequest::parse_header(ssize_t header_end_pos)
void HttpRequest::parse_header(int32_t header_end_pos)
{
//Split the header into lines
size_t line = 0;
@ -74,7 +74,7 @@ namespace fr
//Store content length value if it exists
auto length_header_iter = header_data.find("content-length");
if(length_header_iter != header_data.end())
content_length = std::stoull(length_header_iter->second);
content_length = (size_t)std::stoull(length_header_iter->second);
}
std::string HttpRequest::construct(const std::string &host) const

View File

@ -62,7 +62,7 @@ namespace fr
return response;
}
void HttpResponse::parse_header(ssize_t header_end_pos)
void HttpResponse::parse_header(int32_t header_end_pos)
{
//Split the header into lines
size_t line = 0;

View File

@ -3,11 +3,12 @@
//
#include <chrono>
#include <mbedtls/net_sockets.h>
#include <frnetlib/TcpListener.h>
#include "frnetlib/SSLListener.h"
#ifdef SSL_ENABLED
#include <mbedtls/net_sockets.h>
namespace fr
{
SSLListener::SSLListener(std::shared_ptr<SSLContext> ssl_context_, const std::string &crt_path, const std::string &pem_path, const std::string &private_key_path) noexcept

View File

@ -4,10 +4,11 @@
#include "frnetlib/SSLSocket.h"
#include <memory>
#include <mbedtls/net_sockets.h>
#ifdef SSL_ENABLED
#include <mbedtls/net_sockets.h>
namespace fr
{
SSLSocket::SSLSocket(std::shared_ptr<SSLContext> ssl_context_) noexcept

View File

@ -83,7 +83,7 @@ namespace fr
if(!connected())
return Socket::Disconnected;
ssize_t bytes_remaining = buffer_size;
int32_t bytes_remaining = buffer_size;
size_t bytes_read = 0;
while(bytes_remaining > 0)
{

View File

@ -25,7 +25,7 @@ namespace fr
size_t sent = 0;
while(sent < size)
{
ssize_t status = ::send(socket_descriptor, data + sent, size - sent, 0);
int32_t status = ::send(socket_descriptor, data + sent, size - sent, 0);
if(status > 0)
{
sent += status;
@ -53,7 +53,7 @@ namespace fr
received = 0;
//Read RECV_CHUNK_SIZE bytes into the recv buffer
ssize_t status = ::recv(socket_descriptor, (char*)data, buffer_size, 0);
int32_t status = ::recv(socket_descriptor, (char*)data, buffer_size, 0);
if(status > 0)
{