Improved source. Can open socket

This commit is contained in:
Denis 2023-12-13 01:31:31 +01:00
parent 00fb3a02e3
commit 12fd7c768b
No known key found for this signature in database
GPG key ID: DD9B63F805CF5C03
7 changed files with 39 additions and 33 deletions

View file

@ -1,19 +0,0 @@
#CFLAGS= -g -O0 -D _DEFAULT_SOURCE -D_POSIX_C_SOURCE=200112L -std=c11 -fPIE -pedantic -Wall # -Werror
CFLAGS= -std=c17 -O3 -march=znver4 -D _DEFAULT_SOURCE -D_POSIX_C_SOURCE=200112L -std=c11 -fPIE -pedantic -Wall -flto=full # -Werror
LDFLAGS=-lpthread -levdev -ludev -lconfig -lrt -lm -flto=full
CC=clang
OBJECTS=main.o dev_in.o dev_out.o dev_iio.o dev_evdev.o dev_hidraw.o settings.o virt_ds4.o virt_ds5.o devices_status.o xbox360.o rog_ally.o legion_go.o rogue_enemy.o
TARGET=rogue-enemy
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
include depends
depends:
$(CC) -MM $(OBJECTS:.o=.c) > depends
clean:
rm -f ./$(TARGET) *.o depends

View file

@ -334,10 +334,22 @@ static void handle_rumble(dev_in_t *const in_devs, size_t in_devs_count, const o
}
}
static int open_socket(void) {
static int open_socket(struct sockaddr_un *serveraddr) {
int res = -ENODEV;
int sd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (sd < 0)
{
res = sd;
goto open_socket_err;
}
res = connect(sd, (struct sockaddr *)serveraddr, SUN_LEN(serveraddr));
if (res < 0) {
goto open_socket_err;
}
res = sd;
open_socket_err:
return res;
@ -378,11 +390,11 @@ void* dev_in_thread_func(void *ptr) {
if (dev_in_data->communication.type == ipc_unix_pipe) {
FD_SET(dev_in_data->communication.endpoint.pipe.out_message_pipe_fd, &read_fds);
} else if (dev_in_data->communication.type == ipc_client_socket) {
dev_in_data->communication.endpoint.socket = open_socket();
dev_in_data->communication.endpoint.socket.fd = open_socket(&dev_in_data->communication.endpoint.socket.serveraddr);
// do not do a thing! that will consume messages and they won't be available anymore!
if (dev_in_data->communication.endpoint.socket < 0) {
fprintf(stderr, "Unable to connect to server: %d -- will retry connection\n", dev_in_data->communication.endpoint.socket);
if (dev_in_data->communication.endpoint.socket.fd < 0) {
fprintf(stderr, "Unable to connect to server: %d -- will retry connection\n", dev_in_data->communication.endpoint.socket.fd);
usleep(500000);
continue;
}
@ -466,7 +478,7 @@ void* dev_in_thread_func(void *ptr) {
// in case of an error reschedule to socket for reconnection
if (dev_in_data->communication.type == ipc_client_socket) {
close(out_message_fd);
dev_in_data->communication.endpoint.socket = -1;
dev_in_data->communication.endpoint.socket.fd = -1;
}
}
}
@ -529,7 +541,7 @@ void* dev_in_thread_func(void *ptr) {
if (controller_msg_count > 0) {
int in_message_fd = -1;
if (dev_in_data->communication.type == ipc_client_socket) {
in_message_fd = dev_in_data->communication.endpoint.socket;
in_message_fd = dev_in_data->communication.endpoint.socket.fd;
} else if (dev_in_data->communication.type == ipc_unix_pipe) {
in_message_fd = dev_in_data->communication.endpoint.pipe.in_message_pipe_fd;
}
@ -541,7 +553,7 @@ void* dev_in_thread_func(void *ptr) {
// in case of an error reschedule to socket for reconnection
if (dev_in_data->communication.type == ipc_client_socket) {
close(in_message_fd);
dev_in_data->communication.endpoint.socket = -1;
dev_in_data->communication.endpoint.socket.fd = -1;
}
}
}

13
ipc.h
View file

@ -5,9 +5,14 @@
#define MAX_CONNECTED_CLIENTS 8
typedef struct ipc_strategy_socket {
struct sockaddr_un serveraddr;
int fd;
} ipc_strategy_socket_t;
typedef struct ipc_strategy_ssocket {
pthread_mutex_t mutex;
int clients[MAX_CONNECTED_CLIENTS];
} ipc_strategy_socket_t;
} ipc_strategy_ssocket_t;
typedef struct ipc_strategy_pipe {
@ -29,10 +34,10 @@ typedef struct ipc {
ipc_strategy_t type;
union {
ipc_strategy_pipe_t pipe;
ipc_strategy_socket_t ssocket;
int socket;
ipc_strategy_ssocket_t ssocket;
ipc_strategy_socket_t socket;
} endpoint;
} ipc_t;
#define SERVER_PATH "/tmp/server.sock"
#define SERVER_PATH "/tmp/rogue-enemy.sock"

11
main.c
View file

@ -4,6 +4,7 @@
#include "input_dev.h"
#include "dev_in.h"
#include "dev_out.h"
#include "ipc.h"
#include "settings.h"
#include "rog_ally.h"
@ -62,11 +63,19 @@ int main(int argc, char ** argv) {
.communication = {
.type = ipc_client_socket,
.endpoint = {
.socket = -1,
.socket = {
.fd = -1,
.serveraddr = {
.sun_path = SERVER_PATH,
.sun_family = AF_UNIX,
}
},
}
}
};
//memset(&dev_in_thread_data.communication.endpoint.socket.serveraddr, 0, sizeof(dev_in_thread_data.communication.endpoint.socket.serveraddr));
pthread_t dev_in_thread;
const int dev_in_thread_creation = pthread_create(&dev_in_thread, NULL, dev_in_thread_func, (void*)(&dev_in_thread_data));
if (dev_in_thread_creation != 0) {

0
rogue_iio.c Normal file
View file

View file

@ -60,10 +60,9 @@ int main(int argc, char ** argv) {
}
int sd=-1;
int rc, length;
struct sockaddr_un serveraddr;
do {
sd = socket(AF_UNIX, SOCK_STREAM, 0);
sd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (sd < 0)
{
fprintf(stderr, "socket() failed");

0
stray_output.c Normal file
View file