From f681c54c822d300b2c831e75c1eafbe976df32a9 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Fri, 21 Oct 2022 22:54:19 +0200 Subject: [PATCH] tools/hexnet: Reimplement TCP server, implement TCP client MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both can only receive at the moment as we still don’t handle input. --- src/tools/hexnet.cpp | 135 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 129 insertions(+), 6 deletions(-) diff --git a/src/tools/hexnet.cpp b/src/tools/hexnet.cpp index 6e3bf13..e73fb4b 100644 --- a/src/tools/hexnet.cpp +++ b/src/tools/hexnet.cpp @@ -17,8 +17,12 @@ //If not, see https://www.gnu.org/licenses/agpl-3.0.en.html #include +#include #include #include +#include +#include +#include #include #include @@ -30,22 +34,57 @@ #define EXIT_USAGE 2 #define EXIT_UNIMPLEMENTED 3 -std::mutex mutexStdin; -std::mutex mutexStdout; -std::mutex mutexNetIncoming; -std::mutex mutexNetOutgoing; +// TCP v4 server +sockpp::tcp_socket tcpSocket; +sockpp::tcp_acceptor tcpAcceptor; +// TCP v4 client +sockpp::tcp_connector tcpConnector; + +bool exitProgram = false; bool ipv4 = false; bool ipv6 = false; bool tcp = false; bool udp = false; +bool outgoing = false; +std::string host; +in_port_t port; void signalHandler(int signal) { // shut down gracefully - std::cerr << "Received signal " << signal << ", shutting down." << std::endl; + exitProgram = true; + + // tell sockpp to close TCP socket if open because it blocks when trying + // to read and there is no data + tcpAcceptor.shutdown(); + if (tcpSocket) { + tcpSocket.shutdown(SHUT_RD); + } + + std::cerr << "Received signal " << signal << ", bye!" << std::endl; std::exit(signal); } +void readFromTCPSocket() { + ssize_t byteCount; + uint8_t buffer[1536]; + if (outgoing) { + while (!exitProgram && (byteCount = tcpConnector.read(buffer, sizeof(buffer))) > 0) { + for (ssize_t i=0; i 0) { + for (ssize_t i=0; i