diff --git a/CMakeLists.txt b/CMakeLists.txt index 2665868..e63b904 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ endif() set( INCLUDE_PATH "${PROJECT_SOURCE_DIR}/include" ) set( SOURCE_PATH "${PROJECT_SOURCE_DIR}/src" ) -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 src/SocketReactor.cpp include/frnetlib/SocketReactor.h include/frnetlib/Packetable.h include/frnetlib/Listener.h src/URL.cpp include/frnetlib/URL.h include/frnetlib/WebSocket.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/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 src/SocketReactor.cpp include/frnetlib/SocketReactor.h include/frnetlib/Packetable.h include/frnetlib/Listener.h src/URL.cpp include/frnetlib/URL.h include/frnetlib/Sendable.h) include_directories(include) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") diff --git a/README.md b/README.md index 768c512..6b6b207 100644 --- a/README.md +++ b/README.md @@ -54,8 +54,6 @@ fr::SSLListener listener(ssl_context, "crt_path", "pem_path", "private_key_path" fr::SSLSocket socket(ssl_context); //This is the SSL equivilent to fr::TcpSocket -fr::HttpSocket socket(ssl_context); //This is the SSL equivilent for a HTTP socket. - ``` As you've probably noticed, everything unencrypted has it's equivalent encrypted counterpart, usually just by replacing 'TCP' with 'SSL' and providing an SSLContext object. fr::SSLContext stores SSL information which need not be duplicated across each socket and listener, such as the random number generator, and public key list. It is *important* to build mbedtls with thread protection enabled, if your program is multithreaded. This SSLContext object can then be passed to any SSL sockets or listeners which you may create. @@ -98,7 +96,7 @@ Effectively the reverse of sending packets. We call fr::TcpSocket::receive, pass #include #include -fr::HttpSocket client; //fr::TcpSocket for HTTP. fr::SSLSocket for HTTPS. +fr::TcpSocket client; //fr::TcpSocket for HTTP. fr::SSLSocket for HTTPS. fr::TcpListener listener; //Use an fr::SSLListener if HTTPS. //Bind to a port @@ -136,19 +134,18 @@ while(true) client.close(); } ``` -To deal with HTTP requests, we need to use fr::HttpSockets, to automatically construct and extract fr::HttpResponse/fr::HttpRequest objects. fr::HttpSocket is internally just a wrapper around fr::TcpSocket for HTTP requests, allowing you to add it to a fr::TcpListener the same you would a fr::TcpSocket. After binding to the port, we infinitely try and receive a new request, construct a response with the body of 'Hello, World!' and send it back to the client before closing the socket. +After binding to the port, we infinitely try and receive a new request, construct a response with the body of 'Hello, World!' and send it back to the client before closing the socket. fr::HttpRequest, and fr::HttpResponse both inherit fr::Sendable, which allows them to be sent and received through sockets just like fr::Packets. fr::HttpRequest objects are used for dealing with data being sent *to* the server, whereas fr::HttpResponse objects are used for dealing with data being sent *from* the server. GET/POST/Header information can be manipulated the same as in the example below. # A simple HTTP client: ```c++ -#include #include #include //Connect to the website -fr::HttpSocket socket; +fr::TcpSocket socket; if(socket.connect("example.com", "80") != fr::Socket::Success) { //Failed to connect to site @@ -177,7 +174,7 @@ if(socket.receive(response) != fr::Socket::Success) //Print out the response std::cout << request.get_body() << std::endl; ``` -Here we create a fr::HttpSocket object, connect to a domain (don't include the 'http://' bit). The socket is non-SSL and so the underlying socket type is fr::TcpSocket. If this were an SSL socket, then it'd be fr::HttpSocket. After connecting, we construct a fr::HttpRequest object to send to the server, adding in some GET arguments, POST arguments and a request header. +Here we create an fr::TcpSocket object, connect to a domain (don't include the 'http://' bit). The socket is non-SSL and so the underlying socket type is fr::TcpSocket. If this were an SSL socket, then it'd be fr::SSLSocket. After connecting, we construct a fr::HttpRequest object to send to the server, adding in some GET arguments, POST arguments and a request header. You can both set and get GET/POST data through the fr::(HttpRequest/HttpResponse)::(get/post) functions. And access/set headers though the [] operator. Once we've sent a request, we wait for a response. Once received, we print out the body of the response and exit. @@ -206,7 +203,7 @@ You can both set and get GET/POST data through the fr::(HttpRequest/HttpResponse //Check if it was the selector who sent data if(selector.is_ready(listener)) { - std::unique_ptr> socket(new fr::HttpSocket); + std::unique_ptr socket(new fr::TcpSocket); if(listener.accept(*socket) == fr::Socket::Success) { selector.add(*socket); @@ -221,7 +218,7 @@ You can both set and get GET/POST data through the fr::(HttpRequest/HttpResponse for(auto iter = connections.begin(); iter != connections.end();) { //Eww - fr::HttpSocket &client = (fr::HttpSocket&)**iter; + fr::TcpSocket &client = (fr::TcpSocket &)**iter; //Check if it's this client if(selector.is_ready(client)) @@ -253,7 +250,7 @@ You can both set and get GET/POST data through the fr::(HttpRequest/HttpResponse } } ``` -fr::SocketSelector can be used to monitor lots of blocking sockets at once (both fr::TcpSocket's and fr::HttpSocket's), without polling, to see when data is being received or a connection has closed. To add a socket, just call fr::SocketSelector::add, and to remove a socket, which must be done before the socket object is destroyed, call fr::SocketSelector::remove. You can add as many fr::Socket's as you want.It is also important to add your fr::TcpListener to the selector, otherwise you wont be able to accept new connections whilst blocking. +fr::SocketSelector can be used to monitor lots of blocking sockets at once, without polling, to see when data is being received or a connection has closed. To add a socket, just call fr::SocketSelector::add, and to remove a socket, which must be done before the socket object is destroyed, call fr::SocketSelector::remove. You can add as many fr::Socket's as you want.It is also important to add your fr::TcpListener to the selector, otherwise you wont be able to accept new connections whilst blocking. Once added, you can call fr::SocketSelector::wait() to wait for socket events. You can also specify a timeout, forcing it to return even if there was no activity. If any of the sockets added to the selector send data or disconnect, then it will return true. If the specified timeout has expired, then it will return false. diff --git a/examples/simple_server_and_client/tcpsocket_client.cpp b/examples/simple_server_and_client/tcpsocket_client.cpp index 01dc2fd..39de75a 100644 --- a/examples/simple_server_and_client/tcpsocket_client.cpp +++ b/examples/simple_server_and_client/tcpsocket_client.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include diff --git a/examples/simple_server_and_client/tcpsocket_server.cpp b/examples/simple_server_and_client/tcpsocket_server.cpp index bf1686f..d3889ee 100644 --- a/examples/simple_server_and_client/tcpsocket_server.cpp +++ b/examples/simple_server_and_client/tcpsocket_server.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include #define PORT "8081" diff --git a/include/frnetlib/Http.h b/include/frnetlib/Http.h index 18dd37a..0754abf 100644 --- a/include/frnetlib/Http.h +++ b/include/frnetlib/Http.h @@ -7,11 +7,12 @@ #include #include #include -#define HTTP_RECV_BUFFER_SIZE 8192 +#include "Socket.h" +#include "Sendable.h" namespace fr { - class Http + class Http : public Sendable { public: enum RequestType @@ -91,9 +92,10 @@ namespace fr * into the object. * * @param data The request/response to parse + * @param datasz The length of data in bytes * @return True if more data is needed, false if finished. */ - virtual bool parse(const std::string &data)=0; + virtual bool parse(const char *data, size_t datasz)=0; /*! * Constructs a HTTP request/response to send. @@ -292,6 +294,26 @@ namespace fr */ void parse_header_line(const std::string &str); + /*! + * Overridable send, to allow + * custom types to be directly sent through + * sockets. + * + * @param socket The socket to send through + * @return Status indicating if the send succeeded or not. + */ + virtual Socket::Status send(Socket *socket) override; + + /*! + * Overrideable receive, to allow + * custom types to be directly received through + * sockets. + * + * @param socket The socket to send through + * @return Status indicating if the send succeeded or not. + */ + virtual Socket::Status receive(Socket *socket) override; + //Other request info std::unordered_map header_data; std::unordered_map post_data; diff --git a/include/frnetlib/HttpRequest.h b/include/frnetlib/HttpRequest.h index d2d1463..d4c6aad 100644 --- a/include/frnetlib/HttpRequest.h +++ b/include/frnetlib/HttpRequest.h @@ -24,9 +24,10 @@ namespace fr * Parse a HTTP response. * * @param data The HTTP response to parse + * @param datasz The length of data in bytes * @return True if more data is needed, false if finished. */ - bool parse(const std::string &data) override; + bool parse(const char *data, size_t datasz) override; /*! * Constructs a Http Request, ready to send. diff --git a/include/frnetlib/HttpResponse.h b/include/frnetlib/HttpResponse.h index 9b1fd65..b64c1bc 100644 --- a/include/frnetlib/HttpResponse.h +++ b/include/frnetlib/HttpResponse.h @@ -23,9 +23,10 @@ namespace fr * Parse a HTTP response. * * @param data The HTTP response to parse + * @param datasz The length of data in bytes * @return True if more data is needed, false if finished. */ - bool parse(const std::string &data) override; + bool parse(const char *data, size_t datasz) override; /*! * Constructs a HttpResponse, ready to send. diff --git a/include/frnetlib/HttpSocket.h b/include/frnetlib/HttpSocket.h deleted file mode 100644 index 5a6a051..0000000 --- a/include/frnetlib/HttpSocket.h +++ /dev/null @@ -1,64 +0,0 @@ -// -// Created by fred on 10/12/16. -// - -#ifndef FRNETLIB_HTTPSOCKET_H -#define FRNETLIB_HTTPSOCKET_H - -#include "Http.h" -#include "Socket.h" -#include "WebSocket.h" - -namespace fr -{ - template - class HttpSocket : public SocketType, public WebSocket - { - public: - HttpSocket() - : recv_buffer(HTTP_RECV_BUFFER_SIZE, '\0') - { - - } - - template - HttpSocket(T &&var) - : recv_buffer(HTTP_RECV_BUFFER_SIZE, '\0'), - SocketType(var) - { - - } - - virtual ~HttpSocket() = default; - - Socket::Status receive(Http &request) - { - size_t received = 0; - do - { - //Receive the request - Socket::Status status = SocketType::receive_raw(&recv_buffer[0], recv_buffer.size(), received); - if(status != Socket::Success) - return status; - recv_buffer.resize(received); - - //Parse it - } while(request.parse(recv_buffer.substr(0, received))); - - return Socket::Success; - } - - Socket::Status send(const Http &request) - { - std::string data = request.construct(SocketType::get_remote_address()); - return SocketType::send_raw(&data[0], data.size()); - } - - private: - //Create buffer to receive_request the request - std::string recv_buffer; - }; - -} - -#endif //FRNETLIB_HTTPSOCKET_H diff --git a/include/frnetlib/Listener.h b/include/frnetlib/Listener.h index a47a020..b9beb62 100644 --- a/include/frnetlib/Listener.h +++ b/include/frnetlib/Listener.h @@ -57,7 +57,7 @@ namespace fr * * @return The listen socket descriptor */ - virtual int32_t get_socket_descriptor()=0; + virtual int32_t get_socket_descriptor() const=0; /*! * Sets the socket descriptor. diff --git a/include/frnetlib/Packet.h b/include/frnetlib/Packet.h index fb63f7e..614f13f 100644 --- a/include/frnetlib/Packet.h +++ b/include/frnetlib/Packet.h @@ -11,11 +11,12 @@ #include #include "NetworkEncoding.h" #include "Packetable.h" +#include "Sendable.h" namespace fr { #define PACKET_HEADER_LENGTH sizeof(uint32_t) - class Packet + class Packet : public Sendable { public: Packet() noexcept @@ -510,6 +511,52 @@ namespace fr private: friend class Socket; + /*! + * Overridable send, to allow + * custom types to be directly sent through + * sockets. + * + * @param socket The socket to send through + * @return Status indicating if the send succeeded or not. + */ + virtual Socket::Status send(Socket *socket) override + { + const std::string &data = get_buffer(); + return socket->send_raw(data.c_str(), data.size()); + } + + /*! + * Overrideable receive, to allow + * custom types to be directly received through + * sockets. + * + * @param socket The socket to send through + * @return Status indicating if the send succeeded or not. + */ + virtual Socket::Status receive(Socket *socket) override + { + Socket::Status status; + + //Try to read packet length + uint32_t packet_length = 0; + status = socket->receive_all(&packet_length, sizeof(packet_length)); + if(status != Socket::Status::Success) + return status; + packet_length = ntohl(packet_length); + + //Check that packet_length doesn't exceed the limit, if any + if(socket->get_max_receive_size() && packet_length > socket->get_max_receive_size()) + return fr::Socket::MaxPacketSizeExceeded; + + //Now we've got the length, read the rest of the data in + buffer.resize(packet_length + PACKET_HEADER_LENGTH); + status = socket->receive_all(&buffer[PACKET_HEADER_LENGTH], packet_length); + if(status != Socket::Status::Success) + return status; + + return Socket::Status::Success; + } + /*! * Gets the data added to the packet * diff --git a/include/frnetlib/SSLListener.h b/include/frnetlib/SSLListener.h index ffef04f..7c5c47e 100644 --- a/include/frnetlib/SSLListener.h +++ b/include/frnetlib/SSLListener.h @@ -64,7 +64,7 @@ namespace fr * * @return The listen socket descriptor */ - virtual int32_t get_socket_descriptor() override; + virtual int32_t get_socket_descriptor() const override; /*! * Sets the socket descriptor. diff --git a/include/frnetlib/Sendable.h b/include/frnetlib/Sendable.h new file mode 100644 index 0000000..e0e4c62 --- /dev/null +++ b/include/frnetlib/Sendable.h @@ -0,0 +1,37 @@ +// +// Created by fred.nicolson on 18/07/17. +// + +#ifndef FRNETLIB_SENDABLE_H +#define FRNETLIB_SENDABLE_H +#include "Socket.h" + +namespace fr +{ + class Sendable + { + public: + /*! + * Overridable send, to allow + * custom types to be directly sent through + * sockets. + * + * @param socket The socket to send through + * @return Status indicating if the send succeeded or not. + */ + virtual Socket::Status send(Socket *socket) = 0; + + /*! + * Overrideable receive, to allow + * custom types to be directly received through + * sockets. + * + * @param socket The socket to send through + * @return Status indicating if the send succeeded or not. + */ + virtual Socket::Status receive(Socket *socket) = 0; + }; +} + + +#endif //FRNETLIB_SENDABLE_H diff --git a/include/frnetlib/Socket.h b/include/frnetlib/Socket.h index 2af7c17..1369e3a 100644 --- a/include/frnetlib/Socket.h +++ b/include/frnetlib/Socket.h @@ -7,10 +7,12 @@ #include #include "NetworkEncoding.h" -#include "Packet.h" +#define RECV_CHUNK_SIZE 4096 //How much data to try and recv at once namespace fr { + class Packet; + class Sendable; class Socket { public: @@ -113,21 +115,21 @@ namespace fr virtual bool connected() const =0; /*! - * Send a packet through the socket + * Send a Sendable object through the socket * - * @param packet The packet to send - * @return True on success, false on failure. + * @param obj The object to send + * @return The status of the send */ - Status send(Packet &packet); - Status send(Packet &&packet); + Status send(Sendable &obj); + Status send(Sendable &&obj); /*! - * Receive a packet through the socket + * Receive a Sendable object through the socket * - * @param packet The packet to receive - * @return True on success, false on failure. + * @param obj The object to receive + * @return The status of the receive */ - Status receive(Packet &packet); + Status receive(Sendable &obj); /*! * Reads size bytes into dest from the socket. @@ -161,7 +163,8 @@ namespace fr void set_inet_version(IP version); /*! - * Sets the maximum fr::Packet size that may be received by the socket. + * Sets the maximum receivable size that may be received by the socket. This does + * not apply to receive_raw(), but only things like fr::Packet, or HTTP responses. * * If a client attempts to send a packet larger than sz bytes, then * the client will be disconnected and an fr::Socket::MaxPacketSizeExceeded @@ -174,7 +177,18 @@ namespace fr * * @param sz The maximum number of bytes that may be received in an fr::Packet */ - void set_max_packet_size(uint32_t sz); + void set_max_receive_size(uint32_t sz); + + /*! + * Gets the max packet size. See set_max_packet_size + * for more information. + * + * @return The max packet size + */ + inline uint32_t get_max_receive_size() + { + return max_receive_size; + } protected: /*! @@ -188,7 +202,7 @@ namespace fr std::mutex outbound_mutex; std::mutex inbound_mutex; int ai_family; - uint32_t max_packet_size; + uint32_t max_receive_size; #ifdef _WIN32 static WSADATA wsaData; #endif // _WIN32 diff --git a/include/frnetlib/SocketReactor.h b/include/frnetlib/SocketReactor.h index eda4d7d..4a9a65f 100644 --- a/include/frnetlib/SocketReactor.h +++ b/include/frnetlib/SocketReactor.h @@ -7,6 +7,7 @@ #include #include +#include #include #include "Socket.h" #include "SocketSelector.h" @@ -27,14 +28,26 @@ namespace fr * @param socket The socket to add. * @param callback A function to call when the socket shows activity. Remember to remove it from the reactor if the client has disconnected. */ - void add(const Socket &socket, std::function callback); + template + inline void add(const T &socket, std::function callback) + { + socket_selector.add(socket); + socket_callbacks.emplace_back(std::make_pair(socket.get_remote_address(), callback)); + } /*! * Removes a socket from the socket selector. * * @param socket The socket to remove. */ - void remove(const Socket &socket); + template + inline void remove(const T &socket) + { + socket_selector.remove(socket); + socket_callbacks.erase(std::find_if(socket_callbacks.begin(), socket_callbacks.end(), [&](const std::pair> &check) { + return check.first == socket.get_socket_descriptor(); + })); + } /*! * Waits for a socket to become ready. @@ -45,7 +58,7 @@ namespace fr bool wait(std::chrono::milliseconds timeout = std::chrono::milliseconds(0)); private: - std::vector>> callbacks; + std::vector>> socket_callbacks; // fr::SocketSelector socket_selector; }; diff --git a/include/frnetlib/SocketSelector.h b/include/frnetlib/SocketSelector.h index 4541b3a..c8c9e75 100644 --- a/include/frnetlib/SocketSelector.h +++ b/include/frnetlib/SocketSelector.h @@ -32,7 +32,15 @@ namespace fr * * @param socket The socket to add. */ - void add(const Socket &socket); + template + void add(const T &socket) + { + //Add it to the set + FD_SET(socket.get_socket_descriptor(), &listen_set); + + if(socket.get_socket_descriptor() > max_descriptor) + max_descriptor = socket.get_socket_descriptor(); + } /*! * Checks to see if a socket inside of the selector is ready. @@ -42,14 +50,27 @@ namespace fr * @param socket The socket to check if it's ready * @return True if this socket is ready, false otherwise. */ - bool is_ready(const Socket &socket); + template + inline bool is_ready(const T &socket) + { + return (FD_ISSET(socket.get_socket_descriptor(), &listen_read)); + } + + inline bool is_ready(int32_t socket) + { + return (FD_ISSET(socket, &listen_read)); + } /*! * Removes a socket from the socket selector. * * @param socket The socket to remove. */ - void remove(const Socket &socket); + template + inline void remove(const T &socket) + { + FD_CLR(socket.get_socket_descriptor(), &listen_set); + } private: fd_set listen_set; diff --git a/include/frnetlib/TcpListener.h b/include/frnetlib/TcpListener.h index dc48393..15b1fdd 100644 --- a/include/frnetlib/TcpListener.h +++ b/include/frnetlib/TcpListener.h @@ -51,7 +51,7 @@ public: * * @return The listen socket descriptor */ - virtual int32_t get_socket_descriptor() override; + virtual int32_t get_socket_descriptor() const override; /*! * Sets the socket descriptor. diff --git a/include/frnetlib/TcpSocket.h b/include/frnetlib/TcpSocket.h index b997587..f7b6bc2 100644 --- a/include/frnetlib/TcpSocket.h +++ b/include/frnetlib/TcpSocket.h @@ -11,8 +11,6 @@ namespace fr { -#define RECV_CHUNK_SIZE 4096 //How much data to try and recv at once - class TcpSocket : public Socket { public: diff --git a/include/frnetlib/WebSocket.h b/include/frnetlib/WebSocket.h deleted file mode 100644 index 677c9b3..0000000 --- a/include/frnetlib/WebSocket.h +++ /dev/null @@ -1,28 +0,0 @@ -// -// Created by fred.nicolson on 02/06/17. -// - -#ifndef FRNETLIB_WEBSOCKET_H -#define FRNETLIB_WEBSOCKET_H - -#include "Socket.h" -#include "Http.h" - -namespace fr -{ - /*! - * fr::HttpSocket inherits this class. It's sole purpose is - * to allow you to store fr::HttpSocket types in a generic - * container. - */ - class WebSocket - { - public: - virtual ~WebSocket()=default; - virtual Socket::Status receive(Http &request) = 0; - virtual Socket::Status send(const Http &request) = 0; - }; -} - - -#endif //FRNETLIB_WEBSOCKET_H diff --git a/main.cpp b/main.cpp index 15a6755..fa476b1 100644 --- a/main.cpp +++ b/main.cpp @@ -8,7 +8,6 @@ #include "frnetlib/TcpSocket.h" #include "frnetlib/TcpListener.h" #include "frnetlib/SocketSelector.h" -#include "frnetlib/HttpSocket.h" #include "frnetlib/HttpRequest.h" #include "frnetlib/HttpResponse.h" #include "frnetlib/SSLSocket.h" diff --git a/src/Http.cpp b/src/Http.cpp index 68612c3..a7ee938 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -6,6 +6,7 @@ #include #include #include "frnetlib/Http.h" +#include "frnetlib/Socket.h" namespace fr { @@ -963,4 +964,27 @@ namespace fr return known_mimetypes[".bin"]; return iter->second; } + + Socket::Status Http::send(Socket *socket) + { + std::string data = construct(socket->get_remote_address()); + return socket->send_raw(&data[0], data.size()); + } + + Socket::Status Http::receive(Socket *socket) + { + char recv_buffer[RECV_CHUNK_SIZE]; + size_t received = 0; + do + { + //Receive the request + Socket::Status status = socket->receive_raw(recv_buffer, RECV_CHUNK_SIZE, received); + if(status != Socket::Success) + return status; + + //Parse it + } while(parse(recv_buffer, received)); + + return Socket::Success; + } } \ No newline at end of file diff --git a/src/HttpRequest.cpp b/src/HttpRequest.cpp index a5fa55b..a8fef54 100644 --- a/src/HttpRequest.cpp +++ b/src/HttpRequest.cpp @@ -15,9 +15,9 @@ namespace fr } - bool HttpRequest::parse(const std::string &request) + bool HttpRequest::parse(const char *request, size_t requestsz) { - body += request; + body += std::string(request, requestsz); //Ensure that the whole header has been parsed first if(!header_ended) @@ -44,7 +44,7 @@ namespace fr body.clear(); } - body += request.substr(header_end + header_end_size, request.size() - header_end - header_end_size); + body += std::string(request + header_end + header_end_size, requestsz - header_end - header_end_size); } //If we've got the whole request, parse the POST if it exists diff --git a/src/HttpResponse.cpp b/src/HttpResponse.cpp index b07be6f..29451fc 100644 --- a/src/HttpResponse.cpp +++ b/src/HttpResponse.cpp @@ -7,9 +7,9 @@ namespace fr { - bool HttpResponse::parse(const std::string &response_data) + bool HttpResponse::parse(const char *response_data, size_t datasz) { - body += response_data; + body += std::string(response_data, datasz); //Ensure that the whole header has been parsed first if(!header_ended) @@ -35,7 +35,7 @@ namespace fr body.clear(); } - body += response_data.substr(header_end + header_end_size, response_data.size() - header_end - header_end_size); + body += std::string(response_data + header_end + header_end_size, datasz - header_end - header_end_size); } if(body.size() > content_length) diff --git a/src/HttpSocket.cpp b/src/HttpSocket.cpp deleted file mode 100644 index 4a32dd2..0000000 --- a/src/HttpSocket.cpp +++ /dev/null @@ -1,10 +0,0 @@ -// -// Created by fred on 10/12/16. -// - -#include "frnetlib/HttpSocket.h" - -namespace fr -{ - -} \ No newline at end of file diff --git a/src/SSLListener.cpp b/src/SSLListener.cpp index 2a66f94..cd71174 100644 --- a/src/SSLListener.cpp +++ b/src/SSLListener.cpp @@ -134,7 +134,7 @@ namespace fr ::shutdown(listen_fd.fd, 0); } - int32_t SSLListener::get_socket_descriptor() + int32_t SSLListener::get_socket_descriptor() const { return listen_fd.fd; } diff --git a/src/Socket.cpp b/src/Socket.cpp index 15ed31c..5b63bb0 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -6,6 +6,7 @@ #include #include "frnetlib/NetworkEncoding.h" #include "frnetlib/Socket.h" +#include "frnetlib/Sendable.h" namespace fr { @@ -17,7 +18,7 @@ namespace fr Socket::Socket() noexcept : is_blocking(true), ai_family(AF_UNSPEC), - max_packet_size(0) + max_receive_size(0) { if(instance_count == 0) { @@ -36,54 +37,29 @@ namespace fr instance_count++; } - Socket::Status Socket::send(Packet &packet) + Socket::Status Socket::send(Sendable &obj) { if(!connected()) return Socket::Disconnected; - std::string &data = packet.get_buffer(); - return send_raw(data.c_str(), data.size()); + return obj.send(this); } - Socket::Status Socket::send(Packet &&packet) + Socket::Status Socket::send(Sendable &&obj) { if(!connected()) return Socket::Disconnected; - std::string &data = packet.get_buffer(); - return send_raw(data.c_str(), data.size()); + return obj.send(this); } - Socket::Status Socket::receive(Packet &packet) + Socket::Status Socket::receive(Sendable &obj) { if(!connected()) return Socket::Disconnected; - Socket::Status status; std::lock_guard guard(inbound_mutex); - - //Try to read packet length - uint32_t packet_length = 0; - status = receive_all(&packet_length, sizeof(packet_length)); - if(status != Socket::Status::Success) - return status; - packet_length = ntohl(packet_length); - - //Check that packet_length doesn't exceed the limit, if any - if(max_packet_size && max_packet_size > max_packet_size) - { - close_socket(); - return fr::Socket::MaxPacketSizeExceeded; - } - - //Now we've got the length, read the rest of the data in - packet.buffer.resize(packet_length + PACKET_HEADER_LENGTH); - status = receive_all(&packet.buffer[PACKET_HEADER_LENGTH], packet_length); - if(status != Socket::Status::Success) - return status; - - - return Socket::Status::Success; + return obj.receive(this); } Socket::Status Socket::receive_all(void *dest, size_t buffer_size) @@ -140,8 +116,8 @@ namespace fr } } - void Socket::set_max_packet_size(uint32_t sz) + void Socket::set_max_receive_size(uint32_t sz) { - max_packet_size = sz; + max_receive_size = sz; } } \ No newline at end of file diff --git a/src/SocketReactor.cpp b/src/SocketReactor.cpp index 2a3e36a..2a1e841 100644 --- a/src/SocketReactor.cpp +++ b/src/SocketReactor.cpp @@ -7,30 +7,15 @@ namespace fr { - - void SocketReactor::add(const Socket &socket, std::function callback) - { - socket_selector.add(socket); - callbacks.emplace_back(std::make_pair(&socket, callback)); - } - - void SocketReactor::remove(const Socket &socket) - { - socket_selector.remove(socket); - callbacks.erase(std::find_if(callbacks.begin(), callbacks.end(), [&](const std::pair> &check) { - return check.first->get_socket_descriptor() == socket.get_socket_descriptor(); - })); - } - bool SocketReactor::wait(std::chrono::milliseconds timeout) { bool found = false; if(socket_selector.wait(timeout)) { //Find which socket sent the activity - for(auto &callback : callbacks) + for(auto &callback : socket_callbacks) { - if(socket_selector.is_ready(*callback.first)) + if(socket_selector.is_ready(callback.first)) { //Call the socket's callback callback.second(); diff --git a/src/SocketSelector.cpp b/src/SocketSelector.cpp index 24f4846..aea5b24 100644 --- a/src/SocketSelector.cpp +++ b/src/SocketSelector.cpp @@ -18,15 +18,6 @@ namespace fr max_descriptor = 0; } - void SocketSelector::add(const Socket &socket) - { - //Add it to the set - FD_SET(socket.get_socket_descriptor(), &listen_set); - - if(socket.get_socket_descriptor() > max_descriptor) - max_descriptor = socket.get_socket_descriptor(); - } - bool SocketSelector::wait(std::chrono::milliseconds timeout) { //Windows will crash if we pass an empty set. Do a check. @@ -53,14 +44,4 @@ namespace fr return true; } - - void SocketSelector::remove(const Socket &socket) - { - FD_CLR(socket.get_socket_descriptor(), &listen_set); - } - - bool SocketSelector::is_ready(const Socket &socket) - { - return (FD_ISSET(socket.get_socket_descriptor(), &listen_read)); - } } \ No newline at end of file diff --git a/src/TcpListener.cpp b/src/TcpListener.cpp index 1aafdc4..8ce4b62 100644 --- a/src/TcpListener.cpp +++ b/src/TcpListener.cpp @@ -116,7 +116,7 @@ namespace fr ::shutdown(socket_descriptor, 0); } - int32_t TcpListener::get_socket_descriptor() + int32_t TcpListener::get_socket_descriptor() const { return socket_descriptor; }