Updated send/receive documentation. Updated readme.
This commit is contained in:
parent
27a88febfa
commit
91921867fa
@ -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:
|
||||
|
||||
@ -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:
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
16
main.cpp
16
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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user