Fix broken examples

This commit is contained in:
fred 2019-05-04 00:07:43 +01:00
parent 52ff3c6e27
commit 12f1d7b0ce
4 changed files with 40 additions and 40 deletions

View File

@ -17,7 +17,7 @@ Frnetlib is tested on both Linux and Windows and currently supports:
#include <TcpSocket.h> #include <TcpSocket.h>
fr::TcpSocket socket; fr::TcpSocket socket;
if(socket.connect("127.0.0.1", "8081", std::chrono::seconds(10)) != fr::Socket::Success) if(socket.connect("127.0.0.1", "8081", std::chrono::seconds(10)) != fr::Socket::Status::Success)
{ {
//Failed to connect //Failed to connect
} }
@ -33,14 +33,14 @@ Here, we create a new fr::TcpSocket and connect it to an address. Simple. fr::Tc
fr::TcpListener listener; fr::TcpListener listener;
//Bind to a port //Bind to a port
if(listener.listen("8081") != fr::Socket::Success) if(listener.listen("8081") != fr::Socket::Status::Success)
{ {
//Failed to bind to port //Failed to bind to port
} }
//Accept a connection //Accept a connection
fr::TcpSocket client; fr::TcpSocket client;
if(listener.accept(client) != fr::Socket::Success) if(listener.accept(client) != fr::Socket::Status::Success)
{ {
//Failed to accept a new connection //Failed to accept a new connection
} }
@ -76,7 +76,7 @@ SSLListener accepts a lot more arguments than its unencrypted counterpart, TcpLi
fr::Packet packet; fr::Packet packet;
packet << "Hello there, I am" << (float)1.2 << "years old"; packet << "Hello there, I am" << (float)1.2 << "years old";
if(socket.send(packet) != fr::Socket::Success) if(socket.send(packet) != fr::Socket::Status::Success)
{ {
//Failed to send packet //Failed to send packet
} }
@ -87,7 +87,7 @@ To send messages using frnetlib's framing, use fr::Packet. Data added to the pac
```c++ ```c++
fr::Packet packet; fr::Packet packet;
if(client.receive(packet) != fr::Socket::Success) if(client.receive(packet) != fr::Socket::Status::Success)
{ {
//Failed to receive packet //Failed to receive packet
} }
@ -108,7 +108,7 @@ fr::TcpSocket client; //fr::TcpSocket for HTTP. fr::SSLSocket fo
fr::TcpListener listener; //Use an fr::SSLListener if HTTPS. fr::TcpListener listener; //Use an fr::SSLListener if HTTPS.
//Bind to a port //Bind to a port
if(listener.listen("8081") != fr::Socket::Success) if(listener.listen("8081") != fr::Socket::Status::Success)
{ {
//Failed to bind to port //Failed to bind to port
} }
@ -116,14 +116,14 @@ if(listener.listen("8081") != fr::Socket::Success)
while(true) while(true)
{ {
//Accept a new connection //Accept a new connection
if(listener.accept(client) != fr::Socket::Success) if(listener.accept(client) != fr::Socket::Status::Success)
{ {
//Failed to accept client //Failed to accept client
} }
//Receive client HTTP request //Receive client HTTP request
fr::HttpRequest request; fr::HttpRequest request;
if(client.receive(request) != fr::Socket::Success) if(client.receive(request) != fr::Socket::Status::Success)
{ {
//Failed to receive request //Failed to receive request
} }
@ -133,7 +133,7 @@ while(true)
response.set_body("<h1>Hello, World!</h1>"); response.set_body("<h1>Hello, World!</h1>");
//Send it //Send it
if(client.send(response) != fr::Socket::Success) if(client.send(response) != fr::Socket::Status::Success)
{ {
//Failed to send response; //Failed to send response;
} }
@ -154,7 +154,7 @@ fr::HttpRequest objects are used for dealing with data being sent *to* the serve
//Connect to the website example.com on port 80, with a 10 second connection timeout //Connect to the website example.com on port 80, with a 10 second connection timeout
fr::TcpSocket socket; fr::TcpSocket socket;
if(socket.connect("example.com", "80", std::chrono::seconds(10)) != fr::Socket::Success) if(socket.connect("example.com", "80", std::chrono::seconds(10)) != fr::Socket::Status::Success)
{ {
//Failed to connect to site //Failed to connect to site
} }
@ -167,14 +167,14 @@ request.post("isalive") = "true";
request["my-header"] = "value"; request["my-header"] = "value";
//Send the request //Send the request
if(socket.send(request) != fr::Socket::Success) if(socket.send(request) != fr::Socket::Status::Success)
{ {
//Failed to send request //Failed to send request
} }
//Wait for a response //Wait for a response
fr::HttpResponse response; fr::HttpResponse response;
if(socket.receive(response) != fr::Socket::Success) if(socket.receive(response) != fr::Socket::Status::Success)
{ {
//Failed to receive response //Failed to receive response
} }

View File

@ -14,26 +14,26 @@ int main()
fr::TcpListener listener; fr::TcpListener listener;
//Bind to a port //Bind to a port
if((err = listener.listen("8081")) != fr::Socket::Success) if((err = listener.listen("8081")) != fr::Socket::Status::Success)
{ {
std::cerr << "Failed to bind to port: " << err << std::endl; std::cerr << "Failed to bind to port: " << fr::Socket::status_to_string(err) << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
while(true) while(true)
{ {
//Accept a new connection //Accept a new connection
if((err = listener.accept(client)) != fr::Socket::Success) if((err = listener.accept(client)) != fr::Socket::Status::Success)
{ {
std::cerr << "Failed to accept new connection: " << err << std::endl; std::cerr << "Failed to accept new connection: " << fr::Socket::status_to_string(err) << std::endl;
continue; continue;
} }
//Receive client HTTP request //Receive client HTTP request
fr::HttpRequest request; fr::HttpRequest request;
if((err = client.receive(request)) != fr::Socket::Success) if((err = client.receive(request)) != fr::Socket::Status::Success)
{ {
std::cerr << "Failed to receive request from client: " << err << std::endl; std::cerr << "Failed to receive request from client: " << fr::Socket::status_to_string(err) << std::endl;
} }
//Construct a response //Construct a response
@ -41,9 +41,9 @@ int main()
response.set_body("<h1>Hello, World!</h1>"); response.set_body("<h1>Hello, World!</h1>");
//Send it //Send it
if((err = client.send(response)) != fr::Socket::Success) if((err = client.send(response)) != fr::Socket::Status::Success)
{ {
std::cerr << "Failed to send response to client: " << err << std::endl; std::cerr << "Failed to send response to client: " << fr::Socket::status_to_string(err) << std::endl;
} }
//Close connection //Close connection

View File

@ -12,7 +12,7 @@ int main()
{ {
//Connect to the WebSocket server //Connect to the WebSocket server
fr::WebSocket<fr::TcpSocket> socket; //Use an fr::SSLSocket for secure connections fr::WebSocket<fr::TcpSocket> socket; //Use an fr::SSLSocket for secure connections
if(socket.connect("127.0.0.1", "9091", {}) != fr::Socket::Success) if(socket.connect("127.0.0.1", "9091", {}) != fr::Socket::Status::Success)
{ {
std::cerr << "Failed to connect to server!" << std::endl; std::cerr << "Failed to connect to server!" << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
@ -33,7 +33,7 @@ int main()
} }
else if(message == "ping") else if(message == "ping")
{ {
frame.set_opcode(fr::WebFrame::Ping); frame.set_opcode(fr::WebFrame::Opcode::Ping);
} }
frame.set_payload(std::move(message)); frame.set_payload(std::move(message));
socket.send(frame); socket.send(frame);
@ -47,20 +47,20 @@ int main()
{ {
//Receive the next frame //Receive the next frame
fr::WebFrame frame; fr::WebFrame frame;
if(socket.receive(frame) != fr::Socket::Success) if(socket.receive(frame) != fr::Socket::Status::Success)
continue; continue;
//If it's a Ping then we need to send back a frame containing the same payload, but of type Pong. //If it's a Ping then we need to send back a frame containing the same payload, but of type Pong.
if(frame.get_opcode() == fr::WebFrame::Ping) if(frame.get_opcode() == fr::WebFrame::Opcode::Ping)
{ {
std::cout << "Server sent a ping!" << std::endl; std::cout << "Server sent a ping!" << std::endl;
frame.set_opcode(fr::WebFrame::Pong); frame.set_opcode(fr::WebFrame::Opcode::Pong);
socket.send(frame); socket.send(frame);
continue; continue;
} }
//If it's a disconnect message, then we should finish sending across any messages, and then call disconnect //If it's a disconnect message, then we should finish sending across any messages, and then call disconnect
if(frame.get_opcode() == fr::WebFrame::Disconnect) if(frame.get_opcode() == fr::WebFrame::Opcode::Disconnect)
{ {
socket.disconnect(); socket.disconnect();
continue; continue;
@ -71,9 +71,9 @@ int main()
//a continuation from a previous message. You can check if it's the final part of the //a continuation from a previous message. You can check if it's the final part of the
//message using fr::WebFrame::is_final(). //message using fr::WebFrame::is_final().
std::cout << "Got a new message from the server. It's a "; std::cout << "Got a new message from the server. It's a ";
if(frame.get_opcode() == fr::WebFrame::Text) std::cout << "text"; if(frame.get_opcode() == fr::WebFrame::Opcode::Text) std::cout << "text";
else if(frame.get_opcode() == fr::WebFrame::Binary) std::cout << "binary"; else if(frame.get_opcode() == fr::WebFrame::Opcode::Binary) std::cout << "binary";
else if(frame.get_opcode() == fr::WebFrame::Pong) std::cout << "pong"; else if(frame.get_opcode() == fr::WebFrame::Opcode::Pong) std::cout << "pong";
else std::cout << "continuation of a previous"; else std::cout << "continuation of a previous";
std::cout << " message. It is "; std::cout << " message. It is ";
if(!frame.is_final()) std::cout << "not "; if(!frame.is_final()) std::cout << "not ";

View File

@ -14,7 +14,7 @@ int main()
{ {
//Bind to port, fr::SSLListener should be used for TLS connections //Bind to port, fr::SSLListener should be used for TLS connections
fr::TcpListener listener; fr::TcpListener listener;
if(listener.listen("9091") != fr::Socket::Success) if(listener.listen("9091") != fr::Socket::Status::Success)
{ {
std::cerr << "Failed to bind to port!" << std::endl; std::cerr << "Failed to bind to port!" << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
@ -29,7 +29,7 @@ int main()
fr::WebFrame frame; fr::WebFrame frame;
while(true) while(true)
{ {
frame.set_opcode(fr::WebFrame::Text); frame.set_opcode(fr::WebFrame::Opcode::Text);
std::cout << "Message: " << std::endl; std::cout << "Message: " << std::endl;
std::getline(std::cin, message); std::getline(std::cin, message);
if(message == "exit") if(message == "exit")
@ -39,7 +39,7 @@ int main()
} }
else if(message == "ping") else if(message == "ping")
{ {
frame.set_opcode(fr::WebFrame::Ping); frame.set_opcode(fr::WebFrame::Opcode::Ping);
} }
frame.set_payload(std::move(message)); frame.set_payload(std::move(message));
socket.send(frame); socket.send(frame);
@ -51,7 +51,7 @@ int main()
while(true) while(true)
{ {
//Accept a new WebSocket connection. //Accept a new WebSocket connection.
if(listener.accept(socket) != fr::Socket::Success) if(listener.accept(socket) != fr::Socket::Status::Success)
continue; continue;
std::cout << "Accepted new connection: " << socket.get_remote_address() << std::endl; std::cout << "Accepted new connection: " << socket.get_remote_address() << std::endl;
@ -60,20 +60,20 @@ int main()
{ {
//Receive the next frame //Receive the next frame
fr::WebFrame frame; fr::WebFrame frame;
if(socket.receive(frame) != fr::Socket::Success) if(socket.receive(frame) != fr::Socket::Status::Success)
continue; continue;
//If it's a Ping then we need to send back a frame containing the same payload, but of type Pong. //If it's a Ping then we need to send back a frame containing the same payload, but of type Pong.
if(frame.get_opcode() == fr::WebFrame::Ping) if(frame.get_opcode() == fr::WebFrame::Opcode::Ping)
{ {
std::cout << "Client sent a ping!" << std::endl; std::cout << "Client sent a ping!" << std::endl;
frame.set_opcode(fr::WebFrame::Pong); frame.set_opcode(fr::WebFrame::Opcode::Pong);
socket.send(frame); socket.send(frame);
continue; continue;
} }
//If it's a disconnect message, then we should finish sending across any messages, and then call disconnect //If it's a disconnect message, then we should finish sending across any messages, and then call disconnect
if(frame.get_opcode() == fr::WebFrame::Disconnect) if(frame.get_opcode() == fr::WebFrame::Opcode::Disconnect)
{ {
socket.disconnect(); socket.disconnect();
continue; continue;
@ -84,9 +84,9 @@ int main()
//a continuation from a previous message. You can check if it's the final part of the //a continuation from a previous message. You can check if it's the final part of the
//message using fr::WebFrame::is_final(). //message using fr::WebFrame::is_final().
std::cout << "Got a new message from the client. It's a "; std::cout << "Got a new message from the client. It's a ";
if(frame.get_opcode() == fr::WebFrame::Text) std::cout << "text"; if(frame.get_opcode() == fr::WebFrame::Opcode::Text) std::cout << "text";
else if(frame.get_opcode() == fr::WebFrame::Binary) std::cout << "binary"; else if(frame.get_opcode() == fr::WebFrame::Opcode::Binary) std::cout << "binary";
else if(frame.get_opcode() == fr::WebFrame::Pong) std::cout << "pong"; else if(frame.get_opcode() == fr::WebFrame::Opcode::Pong) std::cout << "pong";
else std::cout << "continuation of a previous"; else std::cout << "continuation of a previous";
std::cout << " message. It is "; std::cout << " message. It is ";
if(!frame.is_final()) std::cout << "not "; if(!frame.is_final()) std::cout << "not ";