From 23e3e5271663e4f7b42233131c0baa7fd8f00a6a Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Thu, 16 May 2024 21:50:49 +0200 Subject: [PATCH] comm for tcp sockets --- ESP32/comm_tcp_socket.cpp | 1 + ESP32/comm_tcp_socket.h | 1 + comm_tcp_socket.cpp | 114 ++++++++++++++++++++++++++++++++++++++ comm_tcp_socket.h | 31 +++++++++++ 4 files changed, 147 insertions(+) create mode 120000 ESP32/comm_tcp_socket.cpp create mode 120000 ESP32/comm_tcp_socket.h create mode 100644 comm_tcp_socket.cpp create mode 100644 comm_tcp_socket.h diff --git a/ESP32/comm_tcp_socket.cpp b/ESP32/comm_tcp_socket.cpp new file mode 120000 index 0000000..fabc9cf --- /dev/null +++ b/ESP32/comm_tcp_socket.cpp @@ -0,0 +1 @@ +../comm_tcp_socket.cpp \ No newline at end of file diff --git a/ESP32/comm_tcp_socket.h b/ESP32/comm_tcp_socket.h new file mode 120000 index 0000000..26c9bed --- /dev/null +++ b/ESP32/comm_tcp_socket.h @@ -0,0 +1 @@ +../comm_tcp_socket.h \ No newline at end of file diff --git a/comm_tcp_socket.cpp b/comm_tcp_socket.cpp new file mode 100644 index 0000000..4848079 --- /dev/null +++ b/comm_tcp_socket.cpp @@ -0,0 +1,114 @@ +// (C) 2024 by Folkert van Heusden +// Released under MIT license + +#include "gen.h" + +#if defined(ESP32) +#include +#endif +#if defined(ESP32) +#include +#include +#include +#include +#elif defined(_WIN32) +#include +#include +#else +#include +#include +#include +#endif +#if IS_POSIX +#include +#include +#include +#include +#endif + +#include "comm_tcp_socket.h" +#include "log.h" +#include "utils.h" + + +comm_tcp_socket::comm_tcp_socket(const int port) +{ + fd = socket(AF_INET, SOCK_STREAM, 0); + + int reuse_addr = 1; + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse_addr, sizeof(reuse_addr)) == -1) { + close(fd); + fd = INVALID_SOCKET; + + DOLOG(warning, true, "Cannot set reuseaddress for port %d (comm_tcp_socket)", port); + return; + } + + set_nodelay(fd); + + sockaddr_in listen_addr; + memset(&listen_addr, 0, sizeof(listen_addr)); + listen_addr.sin_family = AF_INET; + listen_addr.sin_addr.s_addr = htonl(INADDR_ANY); + listen_addr.sin_port = htons(port); + + if (bind(fd, reinterpret_cast(&listen_addr), sizeof(listen_addr)) == -1) { + close(fd); + fd = INVALID_SOCKET; + + DOLOG(warning, true, "Cannot bind to port %d (comm_tcp_socket)", port); + return; + } + + if (listen(fd, SOMAXCONN) == -1) { + close(fd); + fd = INVALID_SOCKET; + + DOLOG(warning, true, "Cannot listen on port %d (comm_tcp_socket)", port); + return; + } +} + +comm_tcp_socket::~comm_tcp_socket() +{ +} + +bool comm_tcp_socket::is_connected() +{ +} + +bool comm_tcp_socket::has_data() +{ +#if defined(_WIN32) + WSAPOLLFD fds[] { { fd, POLLIN, 0 } }; + int rc = WSAPoll(fds, 1, 0); +#else + pollfd fds[] { { fd, POLLIN, 0 } }; + int rc = poll(fds, 1, 0); +#endif + + return rc == 1; +} + +uint8_t comm_tcp_socket::get_byte() +{ + uint8_t c = 0; + read(fd, &c, 1); // TODO error checking + + return c; +} + +void comm_tcp_socket::send_data(const uint8_t *const in, const size_t n) +{ + const uint8_t *p = in; + size_t len = n; + + while(len > 0) { + int rc = write(fd, p, len); + if (rc <= 0) // TODO error checking + break; + + p += rc; + len -= rc; + } +} diff --git a/comm_tcp_socket.h b/comm_tcp_socket.h new file mode 100644 index 0000000..ae2b406 --- /dev/null +++ b/comm_tcp_socket.h @@ -0,0 +1,31 @@ +// (C) 2024 by Folkert van Heusden +// Released under MIT license + +#include "gen.h" +#include "comm.h" + +#if defined(_WIN32) +#include +#include +#else +#define SOCKET int +#define INVALID_SOCKET -1 +#endif + + +class comm_tcp_socket: public comm +{ +private: + SOCKET fd { INVALID_SOCKET }; + +public: + comm_tcp_socket(const int port); + virtual ~comm_tcp_socket(); + + virtual bool is_connected() = 0; + + virtual bool has_data() = 0; + virtual uint8_t get_byte() = 0; + + virtual void send_data(const uint8_t *const in, const size_t n) = 0; +};