Archived
3
0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues or pull requests.
libipc-old/core/client.c

109 lines
2.4 KiB
C
Raw Normal View History

2018-10-04 01:22:50 +02:00
#include "client.h"
2016-12-20 23:36:00 +01:00
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
2017-01-19 22:07:52 +01:00
#include <string.h>
2016-12-19 01:08:55 +01:00
2018-10-04 01:22:50 +02:00
struct ipc_client * ipc_server_client_copy (const struct ipc_client *p)
{
if (p == NULL)
return NULL;
2018-10-04 00:30:47 +02:00
struct ipc_client * copy = malloc (sizeof(struct ipc_client));
memset (copy, 0, sizeof (struct ipc_client));
2018-10-04 00:30:47 +02:00
memcpy (copy, p, sizeof (struct ipc_client));
return copy;
}
2018-11-02 21:03:54 +01:00
int32_t ipc_server_client_eq (const struct ipc_client *p1, const struct ipc_client *p2)
{
2017-01-19 22:07:52 +01:00
return (p1->version == p2->version && p1->index == p2->index
&& p1->proc_fd == p2->proc_fd);
}
2018-10-04 01:22:50 +02:00
void ipc_server_client_gen (struct ipc_client *p
2018-11-02 21:03:54 +01:00
, uint32_t index, uint32_t version)
{
p->version = version;
p->index = index;
}
int32_t ipc_clients_add (struct ipc_clients *clients, struct ipc_client *p)
2016-12-22 21:48:35 +01:00
{
2018-10-04 01:54:12 +02:00
assert(clients != NULL);
2016-12-20 23:36:00 +01:00
assert(p != NULL);
2018-10-08 16:15:35 +02:00
2018-10-04 01:54:12 +02:00
clients->size++;
clients->clients = realloc(clients->clients
, sizeof(struct ipc_client) * clients->size);
2016-12-22 21:48:35 +01:00
2018-10-04 01:54:12 +02:00
if (clients->clients == NULL) {
2016-12-20 23:36:00 +01:00
return -1;
}
2018-10-04 01:54:12 +02:00
clients->clients[clients->size - 1] = p;
2016-12-20 23:36:00 +01:00
return 0;
}
int32_t ipc_clients_del (struct ipc_clients *clients, struct ipc_client *p)
2016-12-22 21:48:35 +01:00
{
2018-10-04 01:54:12 +02:00
assert(clients != NULL);
2016-12-20 23:36:00 +01:00
assert(p != NULL);
2018-10-04 01:54:12 +02:00
if (clients->clients == NULL) {
2016-12-23 01:33:52 +01:00
return -1;
}
2018-11-02 21:03:54 +01:00
int32_t i;
2018-10-04 01:54:12 +02:00
for (i = 0; i < clients->size; i++) {
if (clients->clients[i] == p) {
clients->clients[i] = clients->clients[clients->size-1];
clients->size--;
if (clients->size == 0) {
2018-10-08 16:15:35 +02:00
ipc_clients_free (clients);
2016-12-23 01:33:52 +01:00
}
else {
2018-10-04 01:54:12 +02:00
clients->clients = realloc(clients->clients
, sizeof(struct ipc_client) * clients->size);
2016-12-22 21:48:35 +01:00
2018-10-04 01:54:12 +02:00
if (clients->clients == NULL) {
2016-12-23 01:33:52 +01:00
return -2;
}
2016-12-20 23:36:00 +01:00
}
2016-12-23 01:33:52 +01:00
2016-12-20 23:36:00 +01:00
return 0;
}
}
2016-12-23 01:33:52 +01:00
return -3;
2016-12-20 23:36:00 +01:00
}
2018-10-04 01:22:50 +02:00
void client_print (struct ipc_client *p)
{
if (p != NULL)
2018-10-04 01:22:50 +02:00
printf ("client %d : index %d, version %d\n"
2016-12-22 21:48:35 +01:00
, p->proc_fd, p->index, p->version);
}
2016-12-20 23:36:00 +01:00
2018-10-08 16:15:35 +02:00
void ipc_clients_print (struct ipc_clients *ap)
2016-12-22 21:48:35 +01:00
{
2018-11-02 21:03:54 +01:00
int32_t i;
2016-12-20 23:36:00 +01:00
for (i = 0; i < ap->size; i++) {
printf("%d : ", i);
2018-10-04 01:54:12 +02:00
client_print(ap->clients[i]);
2016-12-20 23:36:00 +01:00
}
}
2018-10-08 16:15:35 +02:00
void ipc_clients_free (struct ipc_clients *ap)
2016-12-22 21:48:35 +01:00
{
2018-10-04 01:54:12 +02:00
if (ap->clients != NULL) {
free (ap->clients);
ap->clients = NULL;
2016-12-23 01:33:52 +01:00
}
ap->size = 0;
2016-12-22 21:48:35 +01:00
}
2018-10-10 23:08:58 +02:00