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

106 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));
memcpy (copy, p, sizeof (struct ipc_client));
return copy;
}
2018-10-04 01:22:50 +02:00
int 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
, unsigned int index, unsigned int version)
{
p->version = version;
p->index = index;
}
2018-10-04 01:54:12 +02:00
int ipc_client_add (struct ipc_client_array *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
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;
}
2018-10-04 01:54:12 +02:00
int ipc_client_del (struct ipc_client_array *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;
}
2016-12-20 23:36:00 +01:00
int i;
2018-10-04 01:54:12 +02:00
for (i = 0; i < clients->size; i++) {
if (clients->clients[i] == p) {
2016-12-22 21:48:35 +01:00
2018-10-04 01:54:12 +02:00
clients->clients[i] = clients->clients[clients->size-1];
clients->size--;
if (clients->size == 0) {
ipc_client_array_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-04 01:22:50 +02:00
void ipc_client_array_print (struct ipc_client_array *ap)
2016-12-22 21:48:35 +01:00
{
2016-12-20 23:36:00 +01:00
int i;
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-04 01:22:50 +02:00
void ipc_client_array_free (struct ipc_client_array *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
}