pollfd
Karchnu 2020-06-29 21:07:19 +02:00
parent 2a8e9a3707
commit a6ca358847
4 changed files with 63 additions and 72 deletions

View File

@ -4,8 +4,6 @@
#include <stdio.h>
#include <errno.h> // error numbers
#include <poll.h>
#include <stdlib.h>
#include <string.h>

View File

@ -9,6 +9,8 @@
#include <errno.h> // error numbers
#include <time.h>
#include <poll.h>
/***
* global defaults
**/

View File

@ -13,48 +13,47 @@
fprintf(stderr, "error while %s: %s\n", msg, err);\
}
void non_interactive (char *env[])
void non_interactive ()
{
struct ipc_message m;
memset (&m, 0, sizeof (struct ipc_message));
SECURE_DECLARATION(struct ipc_connection_info, srv);
SECURE_DECLARATION(struct ipc_message, m);
SECURE_DECLARATION(struct ipc_ctx, ctx);
// init service
TEST_IPC_Q(ipc_connection (env, &srv, SERVICE_NAME), EXIT_FAILURE);
TEST_IPC_Q(ipc_connection (&ctx, SERVICE_NAME), EXIT_FAILURE);
int server_fd = ctx.pollfd[0].fd;
printf ("msg to send (%ld): %.*s\n", (ssize_t) strlen(MSG) +1, (int) strlen(MSG), MSG);
TEST_IPC_Q(ipc_message_format_data (&m, /* type */ 'a', MSG, (ssize_t) strlen(MSG) +1), EXIT_FAILURE);
TEST_IPC_Q(ipc_write (&srv, &m), EXIT_FAILURE);
TEST_IPC_Q(ipc_read (&srv, &m), EXIT_FAILURE);
m.fd = server_fd;
TEST_IPC_Q(ipc_write (&ctx, &m), EXIT_FAILURE);
TEST_IPC_Q(ipc_read (&ctx, 0 /* only one option */, &m), EXIT_FAILURE);
printf ("msg recv: %s\n", m.payload);
ipc_message_empty (&m);
TEST_IPC_Q(ipc_close (&srv), EXIT_FAILURE);
TEST_IPC_Q(ipc_close_all (&ctx), EXIT_FAILURE);
}
void interactive (char *env[])
#if 0
void interactive ()
{
SECURE_DECLARATION(struct ipc_connection_info, srv);
// index and version should be filled
srv.index = 0;
srv.version = 0;
SECURE_DECLARATION(struct ipc_ctx, ctx);
// init service
TEST_IPC_Q(ipc_connection (env, &srv, SERVICE_NAME), EXIT_FAILURE);
TEST_IPC_Q(ipc_connection (&ctx, SERVICE_NAME), EXIT_FAILURE);
int server_fd = ctx.pollfd[0].fd;
SECURE_DECLARATION(struct ipc_event, event);
SECURE_DECLARATION(struct ipc_ctx, services);
TEST_IPC_Q(ipc_add (&services, &srv), EXIT_FAILURE);
long timer = 10;
while (1) {
printf ("msg to send: ");
fflush (stdout);
TEST_IPC_Q(ipc_wait_event (&services, NULL, &event, &timer), EXIT_FAILURE);
TEST_IPC_Q(ipc_events_loop (&ctx, &event, &timer), EXIT_FAILURE);
switch (event.type) {
case IPC_EVENT_TYPE_TIMER: {
@ -71,14 +70,16 @@ void interactive (char *env[])
ipc_message_empty (m);
free (m);
ipc_connections_free (&services);
TEST_IPC_Q(ipc_close_all (&ctx), EXIT_FAILURE);
TEST_IPC_Q(ipc_close (&srv), EXIT_FAILURE);
ipc_connections_free (&ctx);
exit (EXIT_SUCCESS);
}
TEST_IPC_Q(ipc_write (&srv, m), EXIT_FAILURE);
m->fd = server_fd;
TEST_IPC_Q(ipc_write (&ctx, m), EXIT_FAILURE);
}
break;
case IPC_EVENT_TYPE_MESSAGE:
@ -96,16 +97,19 @@ void interactive (char *env[])
}
}
}
#endif
int main (int argc, char *argv[], char *env[])
//int main (int argc, char *argv[])
int main (void)
{
argc = argc; // warnings
argv = argv; // warnings
non_interactive ();
#if 0
if (argc == 1)
non_interactive (env);
non_interactive ();
else
interactive (env);
interactive ();
#endif
return EXIT_SUCCESS;
}

View File

@ -14,52 +14,49 @@
int cpt = 0;
struct ipc_connection_info *srv = 0;
struct ipc_ctx *clients;
struct ipc_ctx *ctx;
void main_loop ()
{
SECURE_DECLARATION(struct ipc_error, ret);
clients = malloc (sizeof (struct ipc_ctx));
memset(clients, 0, sizeof(struct ipc_ctx));
ctx = malloc (sizeof (struct ipc_ctx));
memset(ctx, 0, sizeof(struct ipc_ctx));
SECURE_DECLARATION(struct ipc_event,event);
SECURE_DECLARATION(struct ipc_event, event);
long timer = 10;
int timer = 10000;
while(1) {
// ipc_wait_event provides one event at a time
// warning: event->m is free'ed if not NULL
ret = ipc_wait_event (clients, srv, &event, &timer);
ret = ipc_events_loop (ctx, &event, &timer);
if (ret.error_code != IPC_ERROR_NONE && ret.error_code != IPC_ERROR_CLOSED_RECIPIENT) {
PRINTERR(ret,"service poll event");
// the application will shut down, and close the service
TEST_IPC_Q(ipc_server_close (srv), EXIT_FAILURE);
TEST_IPC_Q(ipc_close_all (ctx), EXIT_FAILURE);
exit (EXIT_FAILURE);
}
switch (event.type) {
case IPC_EVENT_TYPE_TIMER: {
fprintf(stderr, "time up!\n");
timer = 10;
timer = 10000;
};
break;
case IPC_EVENT_TYPE_CONNECTION: {
cpt++;
printf ("connection: %d clients connected\n", cpt);
printf ("new client has the fd %d\n", ((struct ipc_connection_info*) event.origin)->fd);
printf ("connection: %d ctx connected\n", cpt);
printf ("new client has the fd %d\n", event.origin);
};
break;
case IPC_EVENT_TYPE_DISCONNECTION:
{
cpt--;
printf ("disconnection: %d clients remaining\n", cpt);
// free the ipc_connection_info structure
free (event.origin);
// TODO: something?
};
break;
case IPC_EVENT_TYPE_MESSAGE:
@ -69,16 +66,20 @@ void main_loop ()
printf ("message received (type %d): %.*s\n", m->type, m->length, m->payload);
}
ret = ipc_write (event.origin, m);
ret = ipc_write (ctx, m);
if (ret.error_code != IPC_ERROR_NONE) {
PRINTERR(ret,"server write");
}
};
break;
case IPC_EVENT_TYPE_TX:
{
printf ("a message was sent\n");
}
break;
case IPC_EVENT_TYPE_ERROR:
{
fprintf (stderr, "a problem happened with client %d\n"
, ((struct ipc_connection_info*) event.origin)->fd);
fprintf (stderr, "a problem happened with client %d\n", event.origin);
};
break;
default :
@ -97,48 +98,34 @@ void exit_program(int signal)
{
printf("Quitting, signal: %d\n", signal);
// free remaining clients
for (size_t i = 0; i < clients->size ; i++) {
struct ipc_connection_info *cli = clients->cinfos[i];
if (cli != NULL) {
free (cli);
}
clients->cinfos[i] = NULL;
}
ipc_connections_free (clients);
free (clients);
// the application will shut down, and close the service
TEST_IPC_Q(ipc_server_close (srv), EXIT_FAILURE);
free (srv);
TEST_IPC_Q(ipc_close_all (ctx), EXIT_FAILURE);
// free remaining ctx
ipc_connections_free (ctx);
free (ctx);
exit(EXIT_SUCCESS);
}
/*
* service ping-pong: send back everything sent by the clients
* service ping-pong: send back everything sent by the ctx
* stop the program on SIG{TERM,INT,ALRM,USR{1,2},HUP} signals
*/
int main(int argc, char * argv[], char **env)
int main(void)
{
argc = argc; // warnings
argv = argv; // warnings
printf ("pid = %d\n", getpid ());
srv = malloc (sizeof (struct ipc_connection_info));
if (srv == NULL) {
ctx = malloc (sizeof (struct ipc_ctx));
if (ctx == NULL) {
exit (1);
}
memset (srv, 0, sizeof (struct ipc_connection_info));
srv->index = 0;
srv->version = 0;
memset (ctx, 0, sizeof (struct ipc_ctx));
TEST_IPC_Q(ipc_server_init (env, srv, PONGD_SERVICE_NAME), EXIT_FAILURE);
TEST_IPC_Q(ipc_server_init (ctx, PONGD_SERVICE_NAME), EXIT_FAILURE);
struct ipc_connection_info * srv = &ctx->cinfos[0];
printf ("Listening on %s.\n", srv->spath);
printf("MAIN: server created\n" );