comm for tcp sockets

This commit is contained in:
folkert van heusden 2024-05-16 21:50:49 +02:00
parent e45b978134
commit 23e3e52716
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1
4 changed files with 147 additions and 0 deletions

1
ESP32/comm_tcp_socket.cpp Symbolic link
View file

@ -0,0 +1 @@
../comm_tcp_socket.cpp

1
ESP32/comm_tcp_socket.h Symbolic link
View file

@ -0,0 +1 @@
../comm_tcp_socket.h

114
comm_tcp_socket.cpp Normal file
View file

@ -0,0 +1,114 @@
// (C) 2024 by Folkert van Heusden
// Released under MIT license
#include "gen.h"
#if defined(ESP32)
#include <Arduino.h>
#endif
#if defined(ESP32)
#include <lwip/sockets.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <driver/uart.h>
#elif defined(_WIN32)
#include <ws2tcpip.h>
#include <winsock2.h>
#else
#include <poll.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#endif
#if IS_POSIX
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include <thread>
#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<struct sockaddr *>(&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;
}
}

31
comm_tcp_socket.h Normal file
View file

@ -0,0 +1,31 @@
// (C) 2024 by Folkert van Heusden
// Released under MIT license
#include "gen.h"
#include "comm.h"
#if defined(_WIN32)
#include <ws2tcpip.h>
#include <winsock2.h>
#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;
};