aboutsummaryrefslogtreecommitdiff
path: root/client/sock.cpp
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2024-05-20 13:24:13 +0200
committerlonkaars <loek@pipeframe.xyz>2024-05-20 13:24:13 +0200
commit41ed6fa61a65432843feb596726026bc5772ae19 (patch)
tree9f141d6c4fbaee4e4bed380d9b80f4a1c65d9ed8 /client/sock.cpp
parent5876e74fa32881b41478cd67c5b0895161fbdc9c (diff)
socket connect working (sorta)
Diffstat (limited to 'client/sock.cpp')
-rw-r--r--client/sock.cpp75
1 files changed, 66 insertions, 9 deletions
diff --git a/client/sock.cpp b/client/sock.cpp
index 703ee24..17d9e35 100644
--- a/client/sock.cpp
+++ b/client/sock.cpp
@@ -1,29 +1,86 @@
+#include <arpa/inet.h>
+#include <cstring>
+#include <stdexcept>
#include <unistd.h>
#include <cstdio>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <errno.h>
#include <thread>
#include "sock.h"
#include "rl.h"
-PBSocket::PBSocket() {
- printf("Init PBSocket!\n");
-}
+using std::logic_error;
+using std::thread;
+PBSocket::PBSocket() { }
PBSocket::PBSocket(char* addr, uint16_t port) : PBSocket() {
- connect(addr, port);
+ set_server(addr, port);
+ sock_connect();
+}
+
+PBSocket::~PBSocket() {
+ // stop TCP listen thread
+ if (_thread != nullptr) {
+ _thread->detach();
+ delete _thread;
+ }
+
+ sock_close();
+}
+
+void PBSocket::set_server(char* addr, uint16_t port) {
+ _addr = addr;
+ _port = port;
}
-void PBSocket::connect(char* addr, uint16_t port) {
- printf("Connect to %s on port %d\n", addr, port);
+void PBSocket::sock_connect() {
+ if (_addr == NULL) throw logic_error("no server address defined");
+ if (_port == 0) throw logic_error("no server port defined");
- this->_thread = std::thread(&PBSocket::sock_task, this);
+ if (_thread != nullptr) throw logic_error("already connected");
+
+ rl_printf("connecting to %s on port %d...\n", _addr, _port);
+
+ _fd = socket(AF_INET, SOCK_STREAM, 0);
+ if (_fd < 0) throw logic_error("socket create failed");
+
+ struct sockaddr_in server = {
+ .sin_family = AF_INET,
+ .sin_port = htons(_port),
+ .sin_addr = {
+ .s_addr = inet_addr(_addr),
+ },
+ };
+ int ret = connect(_fd, (struct sockaddr*) &server, sizeof(server));
+ if (ret != 0) throw logic_error(strerror(errno));
+
+ this->_thread = new thread(&PBSocket::sock_task, this);
+}
+
+void PBSocket::sock_close() {
+ if (_fd < 0) return; // already closed
+ close(_fd);
+ _fd = -1;
}
void PBSocket::sock_task() {
while(1) {
- sleep(3);
- rl_printf("Testing asynchronous messages in prompt...\n");
+ char buf[80];
+ ssize_t bytes = read(_fd, buf, sizeof(buf));
+
+ if (bytes == -1) {
+ rl_printf("error: %s (%d)\n", strerror(errno), errno);
+ sock_close();
+ break;
+ }
+
+ if (bytes > 0) {
+ rl_printf("received %d bytes\n", bytes);
+ }
}
}