From 0f5f07f3ff30bb02713e2860e0f6925d02cfd5d8 Mon Sep 17 00:00:00 2001 From: Thurman Gillespy Date: Thu, 25 Apr 2019 11:59:28 -0700 Subject: [PATCH 1/4] added both echo_binary_text demo programs --- example/CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 9e6c92c1..52156f83 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -14,7 +14,9 @@ set ( example_source_dir "${CMAKE_SOURCE_DIR}/example" ) set ( example_sources "${example_source_dir}/local_echo_demo.cpp" "${example_source_dir}/chat_server_demo.cpp" - "${example_source_dir}/simple_chat_demo.cpp" ) + "${example_source_dir}/simple_chat_demo.cpp" + "${example_source_dir}/echo_binary_text_server_demo.cpp" + "${example_source_dir}/echo_binary_text_client_demo.cpp" ) set ( OPTIONS "" ) set ( DEFINITIONS "" ) From 27db3af2fe3460fb20cbb404086a2d7b654cd16c Mon Sep 17 00:00:00 2001 From: Thurman Gillespy Date: Thu, 25 Apr 2019 16:56:51 -0700 Subject: [PATCH 2/4] data marshalling for message header with extract_append.hpp --- example/echo_binary_text_client_demo.cpp | 26 ++++++++++++++++-------- example/echo_binary_text_server_demo.cpp | 19 ++++++++++++++--- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/example/echo_binary_text_client_demo.cpp b/example/echo_binary_text_client_demo.cpp index 3d121b37..b03e3792 100644 --- a/example/echo_binary_text_client_demo.cpp +++ b/example/echo_binary_text_client_demo.cpp @@ -35,6 +35,8 @@ echo_binary_text_client_demo.cpp -lpthread -o echo_client #include "net_ip/net_ip.hpp" #include "net_ip/basic_net_entity.hpp" #include "net_ip/component/worker.hpp" +#include "utility/cast_ptr_to.hpp" +#include "marshall/extract_append.hpp" using io_context = asio::io_context; using io_interface = chops::net::tcp_io_interface; @@ -51,9 +53,9 @@ bool process_args(int argc, char* argv[], bool& print_errors, std::string& ip_ad " -h Print useage\n" " -e Print error messages\n" " ip address Default: 127.0.0.1 (LOCAL LOOP)\n" - " port Default port: 5002\n" + " port Default: 5002\n" " change port and use local loop:\n" - " ./echo_client [e] \"\" port"; + " ./echo_client [-e] \"\" port"; int offset = 0; @@ -119,7 +121,10 @@ int main(int argc, char* argv[]) { } else { hdr_processed = true; // 1st 2 bytes is message size - uint16_t size = *(static_cast (buf.data())); + // uint16_t size = *(static_cast (buf.data())); // OLD + // endian correct data marshalling + uint16_t size = chops::extract_val + (static_cast (buf.data())); return size; } @@ -187,14 +192,19 @@ int main(int argc, char* argv[]) { } // buffer to send entered message from user - chops::mutable_shared_buffer buf; + chops::mutable_shared_buffer buf_out; - // 1st 2 bytes size of message are the string length + // 1st 2 bytes size of message (header) are the string length uint16_t size_val = s.size(); - buf.append(&size_val, sizeof(size_val)); // put 2 bytes into buffer - buf.append(s.data(), s.size()); // now add the string + // endian correct data marshalling + std::byte tbuf[HDR_SIZE]; + std::size_t result = chops::append_val(tbuf, size_val); + assert(result == HDR_SIZE); + // buf.append(&size_val, sizeof(size_val)); // put 2 bytes into buffer + buf_out.append(tbuf, sizeof(tbuf)); // write the header + buf_out.append(s.data(), s.size()); // now add the text data // send message to server (TCP_acceptor) - tcp_iof.send(buf.data(), buf.size()); + tcp_iof.send(buf_out.data(), buf_out.size()); } // cleanup diff --git a/example/echo_binary_text_server_demo.cpp b/example/echo_binary_text_server_demo.cpp index fb90a4da..9a539943 100644 --- a/example/echo_binary_text_server_demo.cpp +++ b/example/echo_binary_text_server_demo.cpp @@ -34,7 +34,8 @@ echo_binary_text_server_demo.cpp -lpthread -o echo_server #include "net_ip/net_ip.hpp" #include "net_ip/basic_net_entity.hpp" #include "net_ip/component/worker.hpp" -#include "asio.hpp" +#include "utility/cast_ptr_to.hpp" +#include "marshall/extract_append.hpp" using io_context = asio::io_context; using io_interface = chops::net::tcp_io_interface; @@ -87,6 +88,7 @@ int main(int argc, char* argv[]) { auto msg_hndlr = [] (const_buf buf, io_interface iof, endpoint ep) { // create string from buf, omit 1st 2 bytes (header) std::string s (static_cast (buf.data()) + 2, buf.size() - 2); + // std::string ss (chops::cast_ptr_to (buf.data()), buf.size()); // print info about client std::cout << "received request from " << ep.address() << ":" << ep.port() << std::endl; std::cout << " text: " << s << std::endl; @@ -98,7 +100,14 @@ int main(int argc, char* argv[]) { chops::mutable_shared_buffer buf_out; // 1st 2 bytes are the size of the message uint16_t size_val = s.size(); - buf_out.append(&size_val, sizeof(size_val)); // write 2 byte size + // endian correct data marshalling + std::byte tbuf[HDR_SIZE]; + std::size_t result = chops::append_val(tbuf, size_val); + assert(result == HDR_SIZE); + // chops::append_val(const_cast + // (static_cast (buf.data())), size_val); + // buf_out.append(&size_val, sizeof(size_val)); // write 2 byte size + buf_out.append(tbuf, sizeof(tbuf)); // write the header buf_out.append(s.data(), s.size()); // now add the text data iof.send(buf_out.data(), buf_out.size()); @@ -117,7 +126,11 @@ int main(int argc, char* argv[]) { } else { hdr_processed = true; // 1st 2 bytes is message size - uint16_t size = *(static_cast (buf.data())); + // uint16_t size = *(static_cast (buf.data())); + // endian correct data marshalling + uint16_t size = chops::extract_val + (static_cast (buf.data())); + // std::cerr << "msg_frame: size = " << size << std::endl; return size; } From e2cac1426ad05f22346b6ee7c7aa15dd8438acf1 Mon Sep 17 00:00:00 2001 From: Thurman Gillespy Date: Thu, 25 Apr 2019 17:22:58 -0700 Subject: [PATCH 3/4] removed include for cast_ptr_to.hpp --- example/echo_binary_text_client_demo.cpp | 2 +- example/echo_binary_text_server_demo.cpp | 2 +- example/simple_chat_demo.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/example/echo_binary_text_client_demo.cpp b/example/echo_binary_text_client_demo.cpp index b03e3792..be33de7b 100644 --- a/example/echo_binary_text_client_demo.cpp +++ b/example/echo_binary_text_client_demo.cpp @@ -35,7 +35,7 @@ echo_binary_text_client_demo.cpp -lpthread -o echo_client #include "net_ip/net_ip.hpp" #include "net_ip/basic_net_entity.hpp" #include "net_ip/component/worker.hpp" -#include "utility/cast_ptr_to.hpp" +// #include "utility/cast_ptr_to.hpp" #include "marshall/extract_append.hpp" using io_context = asio::io_context; diff --git a/example/echo_binary_text_server_demo.cpp b/example/echo_binary_text_server_demo.cpp index 9a539943..77557982 100644 --- a/example/echo_binary_text_server_demo.cpp +++ b/example/echo_binary_text_server_demo.cpp @@ -34,7 +34,7 @@ echo_binary_text_server_demo.cpp -lpthread -o echo_server #include "net_ip/net_ip.hpp" #include "net_ip/basic_net_entity.hpp" #include "net_ip/component/worker.hpp" -#include "utility/cast_ptr_to.hpp" +// #include "utility/cast_ptr_to.hpp" #include "marshall/extract_append.hpp" using io_context = asio::io_context; diff --git a/example/simple_chat_demo.cpp b/example/simple_chat_demo.cpp index 92507f06..5c8a735c 100644 --- a/example/simple_chat_demo.cpp +++ b/example/simple_chat_demo.cpp @@ -244,7 +244,7 @@ int main(int argc, char* argv[]) { screen.insert_scroll_line("2nd tcp_connector client rejected" + DELIM, SYSTEM); screen.draw_screen(); - iof.start_io(); + iof.start_io(DELIM, msg_hndlr); const std::string err = "only one tcp connection allowed"; iof.send(err.data(), err.size()); std::this_thread::sleep_for(std::chrono::milliseconds(500)); From dde2eb1bbd53e7b8333fe7034418e165a6e3cc57 Mon Sep 17 00:00:00 2001 From: Thurman Gillespy Date: Fri, 26 Apr 2019 13:03:51 -0700 Subject: [PATCH 4/4] header and comments cleanup --- example/echo_binary_text_client_demo.cpp | 26 +++++++++---------- example/echo_binary_text_server_demo.cpp | 33 ++++++++++++------------ 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/example/echo_binary_text_client_demo.cpp b/example/echo_binary_text_client_demo.cpp index be33de7b..59058600 100644 --- a/example/echo_binary_text_client_demo.cpp +++ b/example/echo_binary_text_client_demo.cpp @@ -3,13 +3,13 @@ * * @ingroup example_module * - * @brief TCP connector (client) that sends binary text message to + * @brief TCP connector (client) that sends binary text message to a * server, receives message back converted to upper case. * * @author Thurman Gillespy * * Copyright (c) Thurman Gillespy - * 4/25/19 + * 4/26/19 * * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -35,7 +35,6 @@ echo_binary_text_client_demo.cpp -lpthread -o echo_client #include "net_ip/net_ip.hpp" #include "net_ip/basic_net_entity.hpp" #include "net_ip/component/worker.hpp" -// #include "utility/cast_ptr_to.hpp" #include "marshall/extract_append.hpp" using io_context = asio::io_context; @@ -48,7 +47,7 @@ bool process_args(int argc, char* argv[], bool& print_errors, std::string& ip_ad std::string& port) { const std::string HELP = "-h"; const std::string PRINT_ERRS = "-e"; - const std::string usage = \ + const std::string USEAGE = \ "useage: ./echo_client [-h | -e] [ip address/hostname] [port]\n" " -h Print useage\n" " -e Print error messages\n" @@ -60,7 +59,7 @@ bool process_args(int argc, char* argv[], bool& print_errors, std::string& ip_ad int offset = 0; if (argc > 4 || (argc > 1 && argv[1] == HELP)) { - std::cout << usage << std::endl; + std::cout << USEAGE << std::endl; return EXIT_FAILURE; } @@ -75,7 +74,7 @@ bool process_args(int argc, char* argv[], bool& print_errors, std::string& ip_ad ip_address = argv[1 + offset]; port = argv[2 + offset]; } else if (argc > 3 + offset) { - std::cout << usage << std::endl; + std::cout << USEAGE << std::endl; return EXIT_FAILURE; } @@ -84,7 +83,7 @@ bool process_args(int argc, char* argv[], bool& print_errors, std::string& ip_ad int main(int argc, char* argv[]) { - const std::size_t HDR_SIZE = 2; // 1st 2 bytes of header is message size + const std::size_t HDR_SIZE = 2; // 1st 2 bytes of data is message size const std::string PORT = "5002"; const std::string LOCAL_LOOP = "127.0.0.1"; @@ -92,6 +91,7 @@ int main(int argc, char* argv[]) { std::string port = PORT; bool hdr_processed = false; bool print_errors = false; + io_interface tcp_iof; // use this to send text messages if (process_args(argc, argv, print_errors, ip_address, port) == EXIT_FAILURE) { @@ -121,7 +121,6 @@ int main(int argc, char* argv[]) { } else { hdr_processed = true; // 1st 2 bytes is message size - // uint16_t size = *(static_cast (buf.data())); // OLD // endian correct data marshalling uint16_t size = chops::extract_val (static_cast (buf.data())); @@ -160,12 +159,12 @@ int main(int argc, char* argv[]) { // create @c net_ip instance chops::net::net_ip echo_client(wk.get_io_context()); - // creae a network entity + // create a @c tcp_connector network entity chops::net::tcp_connector_net_entity net_entity_connect; net_entity_connect = echo_client.make_tcp_connector(port.c_str(), ip_address.c_str(), std::chrono::milliseconds(5000)); assert(net_entity_connect.is_valid()); - // start network entity, emplace handlers + // start @c network_entity, emplace handlers net_entity_connect.start(io_state_chng_hndlr, err_func); // begin @@ -185,7 +184,7 @@ int main(int argc, char* argv[]) { shutdown = true; continue; } - // tcp.iof is not valid when there is no network connection + // @c tcp.iof is not valid when there is no network connection if (!tcp_iof.is_valid()) { std::cout << "no connection..." << std::endl; continue; // back to top of loop @@ -197,10 +196,11 @@ int main(int argc, char* argv[]) { // 1st 2 bytes size of message (header) are the string length uint16_t size_val = s.size(); // endian correct data marshalling - std::byte tbuf[HDR_SIZE]; + std::byte tbuf[HDR_SIZE]; // temp buffer to hold the header + // write those 2 bytes to the temp buffer std::size_t result = chops::append_val(tbuf, size_val); assert(result == HDR_SIZE); - // buf.append(&size_val, sizeof(size_val)); // put 2 bytes into buffer + // now append our header and string data to the output buffer buf_out.append(tbuf, sizeof(tbuf)); // write the header buf_out.append(s.data(), s.size()); // now add the text data // send message to server (TCP_acceptor) diff --git a/example/echo_binary_text_server_demo.cpp b/example/echo_binary_text_server_demo.cpp index 77557982..195e8366 100644 --- a/example/echo_binary_text_server_demo.cpp +++ b/example/echo_binary_text_server_demo.cpp @@ -9,7 +9,7 @@ * @author Thurman Gillespy * * Copyright (c) Thurman Gillespy - * 4/25/19 + * 4/26/19 * * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -29,12 +29,12 @@ echo_binary_text_server_demo.cpp -lpthread -o echo_server #include // std::size_t #include #include +#include // std::for_each #include #include "net_ip/net_ip.hpp" #include "net_ip/basic_net_entity.hpp" #include "net_ip/component/worker.hpp" -// #include "utility/cast_ptr_to.hpp" #include "marshall/extract_append.hpp" using io_context = asio::io_context; @@ -42,18 +42,19 @@ using io_interface = chops::net::tcp_io_interface; using const_buf = asio::const_buffer; using endpoint = asio::ip::tcp::endpoint; +// process command line args (if any) bool processArgs(int argc, char* argv[], bool& print_errors, std::string& port) { const std::string HELP = "-h"; const std::string PRINT_ERRS = "-e"; - const std::string usage = \ + const std::string USEAGE = \ "useage: ./echo_server [-h | -e] [port]\n" " -h Print useage\n" " -e Print error messages\n" - " port Default port: 5002"; + " port Default: 5002"; int offset = 0; if (argc > 3 || (argc > 1 && argv[1] == HELP)) { - std::cout << usage << std::endl; + std::cout << USEAGE << std::endl; return EXIT_FAILURE; } @@ -70,7 +71,7 @@ bool processArgs(int argc, char* argv[], bool& print_errors, std::string& port) } int main(int argc, char* argv[]) { - const std::size_t HDR_SIZE = 2; // 1st 2 bytes of header is message size + const std::size_t HDR_SIZE = 2; // 1st 2 bytes of data is message size const std::string PORT = "5002"; std::string port = PORT; @@ -88,28 +89,27 @@ int main(int argc, char* argv[]) { auto msg_hndlr = [] (const_buf buf, io_interface iof, endpoint ep) { // create string from buf, omit 1st 2 bytes (header) std::string s (static_cast (buf.data()) + 2, buf.size() - 2); - // std::string ss (chops::cast_ptr_to (buf.data()), buf.size()); + // print info about client std::cout << "received request from " << ep.address() << ":" << ep.port() << std::endl; std::cout << " text: " << s << std::endl; - // convert to uppercase + // convert received text to uppercase auto to_upper = [] (char& c) { c = ::toupper(c); }; std::for_each(s.begin(), s.end(), to_upper); - // create buffer to send test data + // create buffer to send altered text back to client chops::mutable_shared_buffer buf_out; // 1st 2 bytes are the size of the message uint16_t size_val = s.size(); // endian correct data marshalling - std::byte tbuf[HDR_SIZE]; + std::byte tbuf[HDR_SIZE]; // temp buffer to hold the header + // write those 2 bytes to the temp buffer std::size_t result = chops::append_val(tbuf, size_val); assert(result == HDR_SIZE); - // chops::append_val(const_cast - // (static_cast (buf.data())), size_val); - // buf_out.append(&size_val, sizeof(size_val)); // write 2 byte size + // now append our header and string data to the output buffer buf_out.append(tbuf, sizeof(tbuf)); // write the header buf_out.append(s.data(), s.size()); // now add the text data - + // send message back to the client iof.send(buf_out.data(), buf_out.size()); return true; @@ -126,13 +126,11 @@ int main(int argc, char* argv[]) { } else { hdr_processed = true; // 1st 2 bytes is message size - // uint16_t size = *(static_cast (buf.data())); // endian correct data marshalling uint16_t size = chops::extract_val (static_cast (buf.data())); - // std::cerr << "msg_frame: size = " << size << std::endl; - return size; + return size; // return the size of the text data (obtained from header) } }; @@ -172,6 +170,7 @@ int main(int argc, char* argv[]) { // start network entity, emplace handlers net_entity_accept.start(io_state_chng_hndlr, err_func); + // begin std::cout << "chops-net-ip binary text echo demo - server" << std::endl; std::cout << " IP address:port = 127.0.0.1:" << port << std::endl; std::cout << " print error messages: " << (print_errors ? "ON" : "OFF") << std::endl;