From 91921867fa2bc360c76dcab2f0cb1fee9e9e1b02 Mon Sep 17 00:00:00 2001 From: Fred Nicolson Date: Sun, 11 Dec 2016 20:54:31 +0000 Subject: [PATCH] Updated send/receive documentation. Updated readme. --- README.md | 6 +++--- include/Socket.h | 4 ++-- main.cpp | 16 +++++++++------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index aa93b57..94895a5 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ if(socket.connect("127.0.0.1", "8081") != fr::Socket::Success) //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: @@ -52,7 +52,7 @@ if(socket.send(packet) != fr::Socket::Success) //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: @@ -67,7 +67,7 @@ std::string str1, str2; float age; 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: diff --git a/include/Socket.h b/include/Socket.h index 7531941..eb7fa60 100644 --- a/include/Socket.h +++ b/include/Socket.h @@ -35,7 +35,7 @@ namespace fr * Send a packet through the socket * * @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; @@ -43,7 +43,7 @@ namespace fr * Receive a packet through the socket * * @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; diff --git a/main.cpp b/main.cpp index 6bfaab6..19eff82 100644 --- a/main.cpp +++ b/main.cpp @@ -51,6 +51,7 @@ void server() for(auto iter = clients.begin(); iter != clients.end();) { if(selector.is_ready(**iter)) + { //This client has sent a HTTP request, so receive_request it fr::HttpRequest request; @@ -92,15 +93,16 @@ void client() return; } - fr::HttpRequest request; - request.get("name") = "fred"; - - if(socket.send(request) != fr::Socket::Success) - std::cout << "Failed to send HTTP request to server!" << std::endl; - + socket.set_blocking(false); + socket.set_blocking(true); 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 << "Got page body: " << response.get_body() << std::endl; return;