From 13f709aebbdcd4abcccdf37b25cf8a581d1d3130 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 3 Mar 2018 16:02:17 +0000 Subject: [PATCH] Added examples for a simple WebSocket client and Server. Removed automatic ping pong response, and disconnection from fr::WebSocket, as it should be handled by the library user, really. Updated ReadMe. --- README.md | 10 ++++- examples/CMakeLists.txt | 3 +- .../CMakeLists.txt | 0 .../SimpleHttpClient.cpp | 0 .../SimpleHttpServer.cpp | 2 +- include/frnetlib/SSLSocket.h | 11 +++--- include/frnetlib/TcpSocket.h | 11 +++--- include/frnetlib/WebSocket.h | 39 ------------------- 8 files changed, 24 insertions(+), 52 deletions(-) rename examples/{simple_server_and_client => simple_http_server_and_client}/CMakeLists.txt (100%) rename examples/{simple_server_and_client => simple_http_server_and_client}/SimpleHttpClient.cpp (100%) rename examples/{simple_server_and_client => simple_http_server_and_client}/SimpleHttpServer.cpp (97%) diff --git a/README.md b/README.md index 3b53e79..71b77f5 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,15 @@ # frnetlib ![Build Status](https://travis-ci.org/Cloaked9000/frnetlib.svg?branch=master) -Frnetlib, is a small and fast networking library written in C++. It can be used for both messaging and for sending/receiving HTTP requests. There are no library dependencies (unless you want to use SSL, in which case MbedTLS is required), and it should compile fine with any C++11 compliant compiler. The API should be considered relatively stable, but things could change as new features are added, given that the library is still in the early stages of development. +Frnetlib, is a cross-platform, small and fast networking library written in C++. There are no library dependencies (unless you want to use SSL, in which case MbedTLS is required), and it should compile fine with any C++11 compliant compiler. The API should be considered relatively stable, but things could change as new features are added, or existing ones changed. + +Frnetlib is tested on both Linux and Windows and currently supports: +* HTTP/HTTPS clients +* HTTP/HTTPS servers +* WebSocket clients +* WebSocket servers +* Raw socket communication +* Framed messaging of inbuilt and custom types # Connecting to a Socket: diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 7032a25..148bde0 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1 +1,2 @@ -add_subdirectory(simple_server_and_client) +add_subdirectory(simple_http_server_and_client) +add_subdirectory(simple_websocket_server_and_client) diff --git a/examples/simple_server_and_client/CMakeLists.txt b/examples/simple_http_server_and_client/CMakeLists.txt similarity index 100% rename from examples/simple_server_and_client/CMakeLists.txt rename to examples/simple_http_server_and_client/CMakeLists.txt diff --git a/examples/simple_server_and_client/SimpleHttpClient.cpp b/examples/simple_http_server_and_client/SimpleHttpClient.cpp similarity index 100% rename from examples/simple_server_and_client/SimpleHttpClient.cpp rename to examples/simple_http_server_and_client/SimpleHttpClient.cpp diff --git a/examples/simple_server_and_client/SimpleHttpServer.cpp b/examples/simple_http_server_and_client/SimpleHttpServer.cpp similarity index 97% rename from examples/simple_server_and_client/SimpleHttpServer.cpp rename to examples/simple_http_server_and_client/SimpleHttpServer.cpp index ec4f755..ab66c35 100644 --- a/examples/simple_server_and_client/SimpleHttpServer.cpp +++ b/examples/simple_http_server_and_client/SimpleHttpServer.cpp @@ -47,6 +47,6 @@ int main() } //Close connection - client.close_socket(); + client.disconnect(); } } \ No newline at end of file diff --git a/include/frnetlib/SSLSocket.h b/include/frnetlib/SSLSocket.h index 3ac02a4..1145250 100644 --- a/include/frnetlib/SSLSocket.h +++ b/include/frnetlib/SSLSocket.h @@ -51,11 +51,6 @@ namespace fr */ Socket::Status receive_raw(void *data, size_t data_size, size_t &received) override; - /*! - * Close the connection. - */ - void close_socket() override; - /*! * Connects the socket to an address. * @@ -126,6 +121,12 @@ namespace fr private: + + /*! + * Close the connection. + */ + void close_socket() override; + std::shared_ptr ssl_context; std::unique_ptr ssl_socket_descriptor; std::unique_ptr ssl; diff --git a/include/frnetlib/TcpSocket.h b/include/frnetlib/TcpSocket.h index d9f42ef..6a735ce 100644 --- a/include/frnetlib/TcpSocket.h +++ b/include/frnetlib/TcpSocket.h @@ -21,11 +21,6 @@ public: void operator=(TcpSocket &&)=delete; void operator=(const TcpSocket &)=delete; - /*! - * Close the connection. - */ - virtual void close_socket(); - /*! * Connects the socket to an address. * @@ -99,6 +94,12 @@ public: } protected: + + /*! + * Close the connection. + */ + virtual void close_socket(); + int32_t socket_descriptor; }; diff --git a/include/frnetlib/WebSocket.h b/include/frnetlib/WebSocket.h index 6ea8cef..cf53c91 100644 --- a/include/frnetlib/WebSocket.h +++ b/include/frnetlib/WebSocket.h @@ -128,45 +128,6 @@ namespace fr SocketType::send(response); } - /*! - * Receive a Sendable object through the socket. - * Internally sends back a pong if a ping is received. - * - * @param obj The object to receive - * @return The status of the receive - */ - Socket::Status receive(Sendable &obj) override - { - WebFrame &frame = dynamic_cast(obj); - - //Try and receive a message. If it's a ping, then silently send back a pong. - Socket::Status status; - while(true) - { - status = SocketType::receive(obj); - if(status != Socket::Success) - return status; - - if(frame.get_opcode() == WebFrame::Ping) - { - frame.set_opcode(WebFrame::Pong); - status = SocketType::send(frame); - if(status != fr::Socket::Success) - return status; - continue; - } - break; - } - - //If it's a disconnect - if(frame.get_opcode() == WebFrame::Disconnect) - { - disconnect(); - return Socket::Disconnected; - } - return status; - } - /*! * Checks to see if the socket initialised the connection, or * if it was accepted by a listener.