From 12f1d7b0cec1bcd4b84bee39ad87858338622e55 Mon Sep 17 00:00:00 2001 From: fred Date: Sat, 4 May 2019 00:07:43 +0100 Subject: [PATCH] Fix broken examples --- README.md | 24 +++++++++---------- .../SimpleHttpServer.cpp | 16 ++++++------- .../SimpleWebsocketClient.cpp | 18 +++++++------- .../SimpleWebsocketServer.cpp | 22 ++++++++--------- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index fc6fea3..71e9301 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Frnetlib is tested on both Linux and Windows and currently supports: #include 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 } @@ -33,14 +33,14 @@ Here, we create a new fr::TcpSocket and connect it to an address. Simple. fr::Tc fr::TcpListener listener; //Bind to a port -if(listener.listen("8081") != fr::Socket::Success) +if(listener.listen("8081") != fr::Socket::Status::Success) { //Failed to bind to port } //Accept a connection fr::TcpSocket client; -if(listener.accept(client) != fr::Socket::Success) +if(listener.accept(client) != fr::Socket::Status::Success) { //Failed to accept a new connection } @@ -76,7 +76,7 @@ SSLListener accepts a lot more arguments than its unencrypted counterpart, TcpLi fr::Packet packet; 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 } @@ -87,7 +87,7 @@ To send messages using frnetlib's framing, use fr::Packet. Data added to the pac ```c++ fr::Packet packet; -if(client.receive(packet) != fr::Socket::Success) +if(client.receive(packet) != fr::Socket::Status::Success) { //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. //Bind to a port -if(listener.listen("8081") != fr::Socket::Success) +if(listener.listen("8081") != fr::Socket::Status::Success) { //Failed to bind to port } @@ -116,14 +116,14 @@ if(listener.listen("8081") != fr::Socket::Success) while(true) { //Accept a new connection - if(listener.accept(client) != fr::Socket::Success) + if(listener.accept(client) != fr::Socket::Status::Success) { //Failed to accept client } //Receive client HTTP request fr::HttpRequest request; - if(client.receive(request) != fr::Socket::Success) + if(client.receive(request) != fr::Socket::Status::Success) { //Failed to receive request } @@ -133,7 +133,7 @@ while(true) response.set_body("

Hello, World!

"); //Send it - if(client.send(response) != fr::Socket::Success) + if(client.send(response) != fr::Socket::Status::Success) { //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 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 } @@ -167,14 +167,14 @@ request.post("isalive") = "true"; request["my-header"] = "value"; //Send the request -if(socket.send(request) != fr::Socket::Success) +if(socket.send(request) != fr::Socket::Status::Success) { //Failed to send request } //Wait for a response fr::HttpResponse response; -if(socket.receive(response) != fr::Socket::Success) +if(socket.receive(response) != fr::Socket::Status::Success) { //Failed to receive response } diff --git a/examples/simple_http_server_and_client/SimpleHttpServer.cpp b/examples/simple_http_server_and_client/SimpleHttpServer.cpp index ab66c35..36c78e5 100644 --- a/examples/simple_http_server_and_client/SimpleHttpServer.cpp +++ b/examples/simple_http_server_and_client/SimpleHttpServer.cpp @@ -14,26 +14,26 @@ int main() fr::TcpListener listener; //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; } while(true) { //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; } //Receive client HTTP 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 @@ -41,9 +41,9 @@ int main() response.set_body("

Hello, World!

"); //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 diff --git a/examples/simple_websocket_server_and_client/SimpleWebsocketClient.cpp b/examples/simple_websocket_server_and_client/SimpleWebsocketClient.cpp index 3c42950..637414a 100644 --- a/examples/simple_websocket_server_and_client/SimpleWebsocketClient.cpp +++ b/examples/simple_websocket_server_and_client/SimpleWebsocketClient.cpp @@ -12,7 +12,7 @@ int main() { //Connect to the WebSocket server fr::WebSocket 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; return EXIT_FAILURE; @@ -33,7 +33,7 @@ int main() } else if(message == "ping") { - frame.set_opcode(fr::WebFrame::Ping); + frame.set_opcode(fr::WebFrame::Opcode::Ping); } frame.set_payload(std::move(message)); socket.send(frame); @@ -47,20 +47,20 @@ int main() { //Receive the next frame fr::WebFrame frame; - if(socket.receive(frame) != fr::Socket::Success) + if(socket.receive(frame) != fr::Socket::Status::Success) continue; //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; - frame.set_opcode(fr::WebFrame::Pong); + frame.set_opcode(fr::WebFrame::Opcode::Pong); socket.send(frame); continue; } //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(); continue; @@ -71,9 +71,9 @@ int main() //a continuation from a previous message. You can check if it's the final part of the //message using fr::WebFrame::is_final(). std::cout << "Got a new message from the server. It's a "; - if(frame.get_opcode() == fr::WebFrame::Text) std::cout << "text"; - else if(frame.get_opcode() == fr::WebFrame::Binary) std::cout << "binary"; - else if(frame.get_opcode() == fr::WebFrame::Pong) std::cout << "pong"; + if(frame.get_opcode() == fr::WebFrame::Opcode::Text) std::cout << "text"; + else if(frame.get_opcode() == fr::WebFrame::Opcode::Binary) std::cout << "binary"; + else if(frame.get_opcode() == fr::WebFrame::Opcode::Pong) std::cout << "pong"; else std::cout << "continuation of a previous"; std::cout << " message. It is "; if(!frame.is_final()) std::cout << "not "; diff --git a/examples/simple_websocket_server_and_client/SimpleWebsocketServer.cpp b/examples/simple_websocket_server_and_client/SimpleWebsocketServer.cpp index a1da89f..56ec877 100644 --- a/examples/simple_websocket_server_and_client/SimpleWebsocketServer.cpp +++ b/examples/simple_websocket_server_and_client/SimpleWebsocketServer.cpp @@ -14,7 +14,7 @@ int main() { //Bind to port, fr::SSLListener should be used for TLS connections 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; return EXIT_FAILURE; @@ -29,7 +29,7 @@ int main() fr::WebFrame frame; while(true) { - frame.set_opcode(fr::WebFrame::Text); + frame.set_opcode(fr::WebFrame::Opcode::Text); std::cout << "Message: " << std::endl; std::getline(std::cin, message); if(message == "exit") @@ -39,7 +39,7 @@ int main() } else if(message == "ping") { - frame.set_opcode(fr::WebFrame::Ping); + frame.set_opcode(fr::WebFrame::Opcode::Ping); } frame.set_payload(std::move(message)); socket.send(frame); @@ -51,7 +51,7 @@ int main() while(true) { //Accept a new WebSocket connection. - if(listener.accept(socket) != fr::Socket::Success) + if(listener.accept(socket) != fr::Socket::Status::Success) continue; std::cout << "Accepted new connection: " << socket.get_remote_address() << std::endl; @@ -60,20 +60,20 @@ int main() { //Receive the next frame fr::WebFrame frame; - if(socket.receive(frame) != fr::Socket::Success) + if(socket.receive(frame) != fr::Socket::Status::Success) continue; //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; - frame.set_opcode(fr::WebFrame::Pong); + frame.set_opcode(fr::WebFrame::Opcode::Pong); socket.send(frame); continue; } //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(); continue; @@ -84,9 +84,9 @@ int main() //a continuation from a previous message. You can check if it's the final part of the //message using fr::WebFrame::is_final(). std::cout << "Got a new message from the client. It's a "; - if(frame.get_opcode() == fr::WebFrame::Text) std::cout << "text"; - else if(frame.get_opcode() == fr::WebFrame::Binary) std::cout << "binary"; - else if(frame.get_opcode() == fr::WebFrame::Pong) std::cout << "pong"; + if(frame.get_opcode() == fr::WebFrame::Opcode::Text) std::cout << "text"; + else if(frame.get_opcode() == fr::WebFrame::Opcode::Binary) std::cout << "binary"; + else if(frame.get_opcode() == fr::WebFrame::Opcode::Pong) std::cout << "pong"; else std::cout << "continuation of a previous"; std::cout << " message. It is "; if(!frame.is_final()) std::cout << "not ";