Updated send/receive documentation. Updated readme.

This commit is contained in:
Fred Nicolson 2016-12-11 20:54:31 +00:00
parent 27a88febfa
commit 91921867fa
3 changed files with 14 additions and 12 deletions

View File

@ -13,7 +13,7 @@ if(socket.connect("127.0.0.1", "8081") != fr::Socket::Success)
//Failed to connect //Failed to connect
} }
``` ```
Here, we create a new fr::TcpSocket and connect it to an address. Simple. fr::TcpSocket is the core class, used to send and receive data over TCP, either with frnetlib's own message framing, or raw data for communicating with other protocols. Unfortunately, UDP is not supported at this point. Sockets are blocking by default, and there is currently no way of disabling blocking. Here, we create a new fr::TcpSocket and connect it to an address. Simple. fr::TcpSocket is the core class, used to send and receive data over TCP, either with frnetlib's own message framing, or raw data for communicating with other protocols. Unfortunately, UDP is not supported at this point. Sockets are blocking by default.
# Listening and accepting connections: # Listening and accepting connections:
@ -52,7 +52,7 @@ if(socket.send(packet) != fr::Socket::Success)
//Failed to send packet //Failed to send packet
} }
``` ```
To send messages using frnetlib's framing, use fr::Packet. Data added to the packet, using the '<<' operator will automatically be packed and converted to network byte order if applicable. The data should be unpacked using the '>>' operator in the same order as it was packed. It is *important* to explicitly typecast non-strings as shown above, otherwise the compiler might interpret '10' as a uint16_t instead of a uint32_t like you might have wanted, scrambling the data. To send the packet, just call fr::TcpSocket::send. To send messages using frnetlib's framing, use fr::Packet. Data added to the packet, using the '<<' operator will automatically be packed and converted to network byte order if applicable. The data should be unpacked using the '>>' operator in the same order as it was packed. It is *important* to explicitly typecast non-strings as shown above, otherwise the compiler might interpret '10' as a uint16_t instead of a uint32_t like you might have wanted, scrambling the data. To send the packet, just call fr::TcpSocket::send.
# Receiving packets: # Receiving packets:
@ -67,7 +67,7 @@ std::string str1, str2;
float age; float age;
packet >> str1 >> age >> str2; packet >> str1 >> age >> str2;
``` ```
Effectively the reverse of sending packets. We call fr::TcpSocket::receive, passing it a fr::Packet object, to receive a packet, and then extract the data in the same order that we packed it. Effectively the reverse of sending packets. We call fr::TcpSocket::receive, passing it a fr::Packet object, to receive a packet, and then extract the data in the same order that we packed it. fr::Socket::receive is blocking by default, but you can toggle this using fr::Socket::set_blocking(). If the socket is blocking when you call receive, it will wait until data has been received before returning. If the socket is non-blocking, then it will return immediately, even if the socket is not ready to receive data. If the socket is non-blocking and is not ready to receive data when you call receive, then it will return a fr::Socket::Status::WouldBlock value.
# A simple HTTP server: # A simple HTTP server:

View File

@ -35,7 +35,7 @@ namespace fr
* Send a packet through the socket * Send a packet through the socket
* *
* @param packet The packet to send * @param packet The packet to send
* @return True on success, false on failure. * @return A status enum value indicating if the operation succeeded or not. Success on success, Error on failure, Disconnected on disconnection etc.
*/ */
virtual Status send(const Packet &packet)=0; virtual Status send(const Packet &packet)=0;
@ -43,7 +43,7 @@ namespace fr
* Receive a packet through the socket * Receive a packet through the socket
* *
* @param packet The packet to receive * @param packet The packet to receive
* @return True on success, false on failure. * @return A status enum value indicating if the operation succeeded or not. Success on success, Error on failure, Disconnected on disconnection, WouldBlock if the socket is non-blocking and the socket was not ready to receive, etc.
*/ */
virtual Status receive(Packet &packet)=0; virtual Status receive(Packet &packet)=0;

View File

@ -51,6 +51,7 @@ void server()
for(auto iter = clients.begin(); iter != clients.end();) for(auto iter = clients.begin(); iter != clients.end();)
{ {
if(selector.is_ready(**iter)) if(selector.is_ready(**iter))
{ {
//This client has sent a HTTP request, so receive_request it //This client has sent a HTTP request, so receive_request it
fr::HttpRequest request; fr::HttpRequest request;
@ -92,15 +93,16 @@ void client()
return; return;
} }
fr::HttpRequest request; socket.set_blocking(false);
request.get("name") = "fred"; socket.set_blocking(true);
if(socket.send(request) != fr::Socket::Success)
std::cout << "Failed to send HTTP request to server!" << std::endl;
fr::HttpResponse response; fr::HttpResponse response;
if(socket.receive(response) != fr::Socket::Success) fr::Socket::Status status = socket.receive(response);
if(status != fr::Socket::Success)
{
if(status == fr::Socket::WouldBlock)
std::cout << "WouldBlock" << std::endl;
std::cout << "Failed to receive HTTP response from the server!" << std::endl; std::cout << "Failed to receive HTTP response from the server!" << std::endl;
}
std::cout << "Got page body: " << response.get_body() << std::endl; std::cout << "Got page body: " << response.get_body() << std::endl;
return; return;