make logic work with both SOCK_STREAM and SEQ_PACKET

This commit is contained in:
Denis 2023-12-14 13:41:33 +01:00
parent 4d2c9c2e36
commit c2e3a7704a
No known key found for this signature in database
GPG key ID: DD9B63F805CF5C03
2 changed files with 25 additions and 20 deletions

View file

@ -529,7 +529,8 @@ void* dev_in_thread_func(void *ptr) {
}
if (dev_in_data->communication.type == ipc_client_socket) {
const int write_res = write(dev_in_data->communication.endpoint.socket.fd, (void*)&controller_msg[0], sizeof(in_message_t) * controller_msg_count);
for (int msg_idx = 0; msg_idx < controller_msg_count; ++msg_idx) {
const int write_res = write(dev_in_data->communication.endpoint.socket.fd, (void*)&controller_msg[0], sizeof(in_message_t));
if (write_res < 0) {
fprintf(stderr, "Error in writing input event messages: %d -- connection will be drop and retried\n", write_res);
@ -537,14 +538,17 @@ void* dev_in_thread_func(void *ptr) {
close(dev_in_data->communication.endpoint.socket.fd);
dev_in_data->communication.endpoint.socket.fd = -1;
}
}
} else if (dev_in_data->communication.type == ipc_unix_pipe) {
const int write_res = write(dev_in_data->communication.endpoint.pipe.in_message_pipe_fd, (void*)&controller_msg[0], sizeof(in_message_t) * controller_msg_count);
for (int msg_idx = 0; msg_idx < controller_msg_count; ++msg_idx) {
const int write_res = write(dev_in_data->communication.endpoint.pipe.in_message_pipe_fd, (void*)&controller_msg[0], sizeof(in_message_t));
if (write_res < 0) {
fprintf(stderr, "Error in writing input event messages: %d\n", write_res);
}
}
}
}
}
// end communication
if (dev_in_data->communication.type == ipc_server_sockets) {

View file

@ -324,26 +324,27 @@ void *dev_out_thread_func(void *ptr) {
}
// send out game-generated events to sockets
int fd = -1;
const size_t bytes_to_send = sizeof(out_message_t) * out_msgs_count;
if (dev_out_data->communication.type == ipc_unix_pipe) {
const int write_res = write(dev_out_data->communication.endpoint.pipe.out_message_pipe_fd, (void*)&out_msgs, bytes_to_send);
if (write_res != bytes_to_send) {
for (int msg_idx = 0; msg_idx < out_msgs_count; ++msg_idx) {
const int write_res = write(dev_out_data->communication.endpoint.pipe.out_message_pipe_fd, (void*)&out_msgs, sizeof(out_message_t));
if (write_res != sizeof(out_message_t)) {
fprintf(stderr, "Error in writing out_message to out_message_pipe: %d\n", write_res);
}
}
} else if (dev_out_data->communication.type == ipc_server_sockets) {
if (pthread_mutex_lock(&dev_out_data->communication.endpoint.ssocket.mutex) == 0) {
for (int i = 0; i < MAX_CONNECTED_CLIENTS; ++i) {
if (dev_out_data->communication.endpoint.ssocket.clients[i] > 0) {
const int write_res = write(dev_out_data->communication.endpoint.ssocket.clients[i], (void*)&out_msgs, bytes_to_send);
if (write_res != bytes_to_send) {
for (int msg_idx = 0; msg_idx < out_msgs_count; ++msg_idx) {
const int write_res = write(dev_out_data->communication.endpoint.ssocket.clients[i], (void*)&out_msgs, sizeof(out_message_t));
if (write_res != sizeof(out_message_t)) {
fprintf(stderr, "Error in writing out_message to socket number %d: %d\n", i, write_res);
close(dev_out_data->communication.endpoint.ssocket.clients[i]);
dev_out_data->communication.endpoint.ssocket.clients[i] = -1;
}
}
}
}
pthread_mutex_unlock(&dev_out_data->communication.endpoint.ssocket.mutex);
}