jsfw/server.c

133 lines
3.8 KiB
C
Raw Normal View History

2022-08-29 18:06:25 -05:00
#include <arpa/inet.h>
2022-08-30 08:37:34 -05:00
#include <fcntl.h>
#include <linux/input.h>
2022-08-29 18:06:25 -05:00
#include <netinet/tcp.h>
#include <pthread.h>
2022-08-30 08:37:34 -05:00
#include <stdbool.h>
2022-08-29 18:06:25 -05:00
#include <stdint.h>
#include <stdio.h>
#include <string.h>
2022-08-30 08:37:34 -05:00
#include <sys/ioctl.h>
#include <sys/poll.h>
2022-08-29 18:06:25 -05:00
#include <sys/socket.h>
2022-08-29 17:27:03 -05:00
2022-08-29 18:06:25 -05:00
#include "hid.h"
2022-08-29 17:27:03 -05:00
#include "net.h"
2022-08-30 08:37:34 -05:00
#include "util.h"
2022-08-29 18:06:25 -05:00
#include "vec.h"
2022-08-29 17:27:03 -05:00
struct Connection {
2022-08-29 18:06:25 -05:00
int socket;
2022-08-29 17:27:03 -05:00
uint32_t id;
};
2022-08-29 18:06:25 -05:00
void *server_handle_conn(void *args_) {
struct Connection *args = args_;
2022-08-29 17:27:03 -05:00
2022-08-30 08:37:34 -05:00
printf("CONN(%u): start\n", args->id);
2022-08-29 18:06:25 -05:00
2022-08-30 08:37:34 -05:00
int enable = true;
2022-08-29 18:06:25 -05:00
int idle_time = 10;
int keep_count = 5;
2022-08-30 08:37:34 -05:00
int keep_interval = 2;
2022-08-29 18:06:25 -05:00
if (setsockopt(args->socket, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable)) != 0)
printf("ERR(server_handle_conn): Enabling socket keepalives on client\n");
if (setsockopt(args->socket, SOL_TCP, TCP_KEEPIDLE, &idle_time, sizeof(idle_time)) != 0)
printf("ERR(server_handle_conn): Setting initial ERR()-time value\n");
if (setsockopt(args->socket, SOL_TCP, TCP_KEEPCNT, &keep_count, sizeof(keep_count)) != 0)
printf("ERR(server_handle_conn): Setting idle retry count\n");
if (setsockopt(args->socket, SOL_TCP, TCP_KEEPINTVL, &keep_interval, sizeof(keep_interval)) != 0)
printf("ERR(server_handle_conn): Setting idle retry interval\n");
2022-08-30 08:37:34 -05:00
uint8_t buf[2048];
2022-08-29 18:06:25 -05:00
PhysicalDevice dev = get_device();
2022-08-30 08:37:34 -05:00
printf("CONN(%u): got device '%s'\n", args->id, dev.name);
2022-08-29 18:06:25 -05:00
2022-08-30 08:37:34 -05:00
int len = msg_serialize(buf, 2048, (Message *)&dev.device_info);
write(args->socket, buf, len);
2022-08-29 18:06:25 -05:00
2022-08-30 08:37:34 -05:00
struct pollfd pfd[2] = {};
pfd[0].fd = args->socket;
pfd[0].events = POLLIN;
pfd[1].fd = dev.event;
pfd[1].events = POLLIN;
while (1) {
int rc = poll(pfd, 1, -1);
if (rc < 0) // error (connection closed)
2022-08-29 18:06:25 -05:00
goto conn_end;
2022-08-30 08:37:34 -05:00
if (pfd[0].revents & POLLIN) {
int len = recv(args->socket, buf, 2048, 0);
if (len <= 0)
goto conn_end;
Message msg;
if (msg_deserialize(buf, len, &msg) == 0) {
if(msg.code == ControllerState) {
apply_controller_state(&dev, (MessageControllerState*)&msg);
} else {
printf("CONN(%d): Illegal message\n", args->id);
}
} else {
printf("CONN(%d): Couldn't parse message.\n", args->id);
}
}
if (pfd[1].revents & POLLIN) {
2022-08-29 18:06:25 -05:00
2022-08-29 17:27:03 -05:00
}
}
2022-08-29 18:06:25 -05:00
conn_end:
2022-08-30 08:37:34 -05:00
shutdown(args->socket, SHUT_RDWR);
printf("CONN(%u): connection closed\n", args->id);
2022-08-29 18:06:25 -05:00
return_device(&dev);
2022-08-29 17:27:03 -05:00
free(args);
return NULL;
}
void server_run(uint16_t port) {
printf("SERVER: start\n");
2022-08-29 18:06:25 -05:00
2022-08-29 17:27:03 -05:00
int sock = socket(AF_INET, SOCK_STREAM, 0);
2022-08-29 18:06:25 -05:00
if (sock < 0)
panicf("Couldn't open socket\n");
2022-08-29 17:27:03 -05:00
struct sockaddr_in addr = {};
2022-08-29 18:06:25 -05:00
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) != 0)
2022-08-29 17:27:03 -05:00
panicf("Couldn't bind to the socket\n");
2022-08-29 18:06:25 -05:00
if (listen(sock, 16) != 0)
2022-08-29 17:27:03 -05:00
panicf("Couldn't listen on socket\n");
uint32_t ids = 0;
2022-08-29 18:06:25 -05:00
while (1) {
struct sockaddr con_addr;
socklen_t con_len = sizeof(con_addr);
2022-08-29 17:27:03 -05:00
struct Connection conn;
conn.socket = accept(sock, &con_addr, &con_len);
2022-08-29 18:06:25 -05:00
if (conn.socket >= 0) {
2022-08-29 17:27:03 -05:00
printf("SERVER: got connection\n");
conn.id = ids++;
2022-08-29 18:06:25 -05:00
struct Connection *conn_ptr = malloc(sizeof(struct Connection));
2022-08-29 17:27:03 -05:00
memcpy(conn_ptr, &conn, sizeof(struct Connection));
pthread_t thread;
pthread_create(&thread, NULL, server_handle_conn, conn_ptr);
} else {
printf("Couldn't accept connection (%d)\n", conn.socket);
}
}
}