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.
This commit is contained in:
parent
fa843b57c8
commit
13f709aebb
10
README.md
10
README.md
@ -1,7 +1,15 @@
|
||||
# frnetlib
|
||||

|
||||
|
||||
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:
|
||||
|
||||
|
||||
@ -1 +1,2 @@
|
||||
add_subdirectory(simple_server_and_client)
|
||||
add_subdirectory(simple_http_server_and_client)
|
||||
add_subdirectory(simple_websocket_server_and_client)
|
||||
|
||||
@ -47,6 +47,6 @@ int main()
|
||||
}
|
||||
|
||||
//Close connection
|
||||
client.close_socket();
|
||||
client.disconnect();
|
||||
}
|
||||
}
|
||||
@ -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<SSLContext> ssl_context;
|
||||
std::unique_ptr<mbedtls_net_context> ssl_socket_descriptor;
|
||||
std::unique_ptr<mbedtls_ssl_context> ssl;
|
||||
|
||||
@ -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;
|
||||
};
|
||||
|
||||
|
||||
@ -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<WebFrame&>(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.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user