simple-tcp: corrections and debug print
parent
9a83993795
commit
bef3b28656
|
@ -64,7 +64,9 @@ void send_receive (int sockfd)
|
||||||
// 2 | 6 | 0 | "coucou"
|
// 2 | 6 | 0 | "coucou"
|
||||||
// 1 B | 4 B | 1 | 6 B
|
// 1 B | 4 B | 1 | 6 B
|
||||||
ipc_message_raw_serialize ((char *)buf, MSG_TYPE_DATA, 42, "coucou", 6);
|
ipc_message_raw_serialize ((char *)buf, MSG_TYPE_DATA, 42, "coucou", 6);
|
||||||
print_hexa ("WAITING 10 seconds then message to send", buf, 12);
|
printf("\n");
|
||||||
|
print_hexa ("message to send", buf, 12);
|
||||||
|
printf("\n");
|
||||||
// sleep (1);
|
// sleep (1);
|
||||||
T_PERROR_Q ((send (sockfd, buf, 12, 0) == -1), "sending a message", EXIT_FAILURE);
|
T_PERROR_Q ((send (sockfd, buf, 12, 0) == -1), "sending a message", EXIT_FAILURE);
|
||||||
printf ("message 'coucou' sent\n");
|
printf ("message 'coucou' sent\n");
|
||||||
|
@ -72,6 +74,7 @@ void send_receive (int sockfd)
|
||||||
|
|
||||||
// receiving a message
|
// receiving a message
|
||||||
T_PERROR_Q (((paylen = recv (sockfd, buf, BUFSIZ, 0)) < 0), "receiving a message", EXIT_FAILURE);
|
T_PERROR_Q (((paylen = recv (sockfd, buf, BUFSIZ, 0)) < 0), "receiving a message", EXIT_FAILURE);
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
if (paylen == 0) {
|
if (paylen == 0) {
|
||||||
fprintf (stderr, "error: disconnection from the server\n");
|
fprintf (stderr, "error: disconnection from the server\n");
|
||||||
|
@ -96,7 +99,7 @@ int main (int argc, char *argv[])
|
||||||
|
|
||||||
send_receive (sockfd);
|
send_receive (sockfd);
|
||||||
|
|
||||||
printf ("Disconnection\n");
|
printf ("\nDisconnection\n");
|
||||||
|
|
||||||
// close the socket
|
// close the socket
|
||||||
close (sockfd);
|
close (sockfd);
|
||||||
|
|
|
@ -52,23 +52,23 @@ void chomp (char *str, size_t len)
|
||||||
|
|
||||||
#define SERVICE_NAME "simpletcp"
|
#define SERVICE_NAME "simpletcp"
|
||||||
|
|
||||||
struct ipcd *ipcd = NULL;
|
struct ipc_ctx *ctx = NULL;
|
||||||
|
|
||||||
void handle_disconnection (int fd)
|
void handle_disconnection (int fd)
|
||||||
{
|
{
|
||||||
int delfd;
|
int delfd;
|
||||||
|
|
||||||
delfd = ipc_switching_del (&ipcd->TCP_TO_IPCFD, fd);
|
delfd = ipc_switching_del (&ctx->switchdb, fd);
|
||||||
if (delfd >= 0) {
|
if (delfd >= 0) {
|
||||||
close (delfd);
|
close (delfd);
|
||||||
ipc_del_fd (&ipcd->ctx, delfd);
|
ipc_del_fd (ctx, delfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
close (fd);
|
close (fd);
|
||||||
ipc_del_fd (&ipcd->ctx, fd);
|
ipc_del_fd (ctx, fd);
|
||||||
|
|
||||||
// printf ("TCP_TO_IPCFD\n");
|
// printf ("ctx.switchdb\n");
|
||||||
ipc_switching_print (&ipcd->TCP_TO_IPCFD);
|
ipc_switching_print (&ctx->switchdb);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tcp_connection (int fd)
|
void tcp_connection (int fd)
|
||||||
|
@ -89,14 +89,14 @@ void tcp_connection (int fd)
|
||||||
// TODO: tests
|
// TODO: tests
|
||||||
T_PERROR_Q ((send (fd, "OK", 2, 0) <= 0), "sending a message", EXIT_FAILURE);
|
T_PERROR_Q ((send (fd, "OK", 2, 0) <= 0), "sending a message", EXIT_FAILURE);
|
||||||
|
|
||||||
struct ipc_error ret = ipc_connection (&ipcd->ctx, buf);
|
printf ("connection to %s\n", buf);
|
||||||
|
struct ipc_error ret = ipc_connection (ctx, buf);
|
||||||
if (ret.error_code != IPC_ERROR_NONE) {
|
if (ret.error_code != IPC_ERROR_NONE) {
|
||||||
fprintf (stderr, "%s\n", ret.error_message);
|
fprintf (stderr, "%s\n", ret.error_message);
|
||||||
exit (EXIT_FAILURE);
|
exit (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
ipc_switching_add (&ipcd->TCP_TO_IPCFD, fd, tcp_to_ipc_ci.fd);
|
ipc_switching_add (&ctx->switchdb, fd, ctx->pollfd[ctx->size-1].fd);
|
||||||
ipc_add_fd (&ipcd->ctx, tcp_to_ipc_ci.fd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int accept_new_client (int serverfd)
|
int accept_new_client (int serverfd)
|
||||||
|
@ -110,7 +110,7 @@ int accept_new_client (int serverfd)
|
||||||
EXIT_FAILURE);
|
EXIT_FAILURE);
|
||||||
|
|
||||||
// adding a client
|
// adding a client
|
||||||
ipc_add_fd (&ipcd->ctx, sock_fd_client);
|
ipc_add_fd (ctx, sock_fd_client);
|
||||||
|
|
||||||
return sock_fd_client;
|
return sock_fd_client;
|
||||||
}
|
}
|
||||||
|
@ -155,24 +155,27 @@ void main_loop (int argc, char **argv)
|
||||||
|
|
||||||
SECURE_DECLARATION (struct ipc_event, event);
|
SECURE_DECLARATION (struct ipc_event, event);
|
||||||
|
|
||||||
ipc_add_fd (&ipcd->ctx, serverfd);
|
ipc_add_fd (ctx, serverfd);
|
||||||
|
|
||||||
|
int cpt = 0;
|
||||||
|
|
||||||
|
int timer = 10000;
|
||||||
while (1) {
|
while (1) {
|
||||||
// ipc_wait_event provides one event at a time
|
// ipc_wait_event provides one event at a time
|
||||||
// warning: event->m is free'ed if not NULL
|
// warning: event->m is free'ed if not NULL
|
||||||
int timer = 10000;
|
|
||||||
|
|
||||||
TEST_IPC_WAIT_EVENT_Q (ipc_events_loop (&ipcd->ctx, &event, &timer), EXIT_FAILURE);
|
ipc_ctx_print (ctx);
|
||||||
|
TEST_IPC_WAIT_EVENT_Q (ipc_events_loop (ctx, &event, &timer), EXIT_FAILURE);
|
||||||
|
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case IPC_EVENT_TYPE_TIMER:{
|
case IPC_EVENT_TYPE_TIMER:{
|
||||||
printf ("timed out!\n");
|
printf ("timed out!\n");
|
||||||
|
|
||||||
timer = 10000;
|
timer = 10000;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IPC_EVENT_TYPE_SWITCH:{
|
case IPC_EVENT_TYPE_SWITCH:{
|
||||||
printf ("switch happened\n");
|
printf ("switch happened, from %d\n", event.origin);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -181,12 +184,14 @@ void main_loop (int argc, char **argv)
|
||||||
// NEW CLIENT
|
// NEW CLIENT
|
||||||
if (event.origin == serverfd) {
|
if (event.origin == serverfd) {
|
||||||
int sock_fd_client = accept_new_client (serverfd);
|
int sock_fd_client = accept_new_client (serverfd);
|
||||||
ipcd->cpt++;
|
cpt++;
|
||||||
printf ("TCP connection: %d ctx connected\n", ipcd->cpt);
|
printf ("TCP connection: %d ctx connected\n", cpt);
|
||||||
printf ("new TCP client has the fd %d\n", sock_fd_client);
|
printf ("new TCP client has the fd %d\n", sock_fd_client);
|
||||||
}
|
}
|
||||||
// CLIENT IS TALKING
|
// CLIENT IS TALKING
|
||||||
else {
|
else {
|
||||||
|
// Test: if the socket already is in the switch, this means we can just switch the packet.
|
||||||
|
// Is the socket in the switch db?
|
||||||
tcp_connection (event.origin);
|
tcp_connection (event.origin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -194,39 +199,43 @@ void main_loop (int argc, char **argv)
|
||||||
|
|
||||||
case IPC_EVENT_TYPE_CONNECTION:
|
case IPC_EVENT_TYPE_CONNECTION:
|
||||||
{
|
{
|
||||||
ipcd->cpt++;
|
cpt++;
|
||||||
printf ("connection: %d ctx connected\n", ipcd->cpt);
|
printf ("connection: %d ctx connected\n", cpt);
|
||||||
printf ("new client has the fd %d\n", event.origin);
|
printf ("new client has the fd %d\n", event.origin);
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IPC_EVENT_TYPE_DISCONNECTION:
|
case IPC_EVENT_TYPE_DISCONNECTION:
|
||||||
{
|
{
|
||||||
ipcd->cpt--;
|
cpt--;
|
||||||
printf ("disconnection: %d ctx remaining\n", ipcd->cpt);
|
printf ("disconnection: %d ctx remaining\n", cpt);
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IPC_EVENT_TYPE_MESSAGE:
|
case IPC_EVENT_TYPE_MESSAGE:
|
||||||
{
|
{
|
||||||
struct ipc_message *m = event.m;
|
struct ipc_message *m = event.m;
|
||||||
if (m->length > 0) {
|
if (m->length > 0) {
|
||||||
printf ("message received (type %d): %.*s\n", m->type, m->length, m->payload);
|
printf ("message received (type %d): %.*s\n", m->type, m->length, m->payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
// m->fd = 3;
|
// m->fd = 3;
|
||||||
TEST_IPC_P (ipc_write (event.ctx, m), "server write");
|
TEST_IPC_P (ipc_write (ctx, m), "server write");
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IPC_EVENT_TYPE_TX:
|
case IPC_EVENT_TYPE_TX:
|
||||||
{
|
{
|
||||||
printf ("a message was sent\n");
|
printf ("a message was sent\n");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IPC_EVENT_TYPE_ERROR:
|
case IPC_EVENT_TYPE_ERROR:
|
||||||
fprintf (stderr, "a problem happened with client %d\n", event.origin);
|
fprintf (stderr, "a problem happened with client %d\n", event.origin);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fprintf (stderr, "there must be a problem, event not set\n");
|
fprintf (stderr, "there must be a problem, event not set: %d\n", event.type);
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,11 +248,11 @@ void exit_program (int signal)
|
||||||
printf ("Quitting, signal: %d\n", signal);
|
printf ("Quitting, signal: %d\n", signal);
|
||||||
|
|
||||||
// Close then free remaining ctx.
|
// Close then free remaining ctx.
|
||||||
ipc_close_all (&ipcd->ctx);
|
ipc_close_all (ctx);
|
||||||
// ipc_ctx_free (&ipcd->ctx);
|
// ipc_ctx_free (ctx);
|
||||||
|
|
||||||
// free, free everything!
|
// free, free everything!
|
||||||
free (ipcd);
|
free (ctx);
|
||||||
|
|
||||||
exit (EXIT_SUCCESS);
|
exit (EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
@ -263,14 +272,15 @@ int main (int argc, char *argv[])
|
||||||
|
|
||||||
printf ("pid = %d\n", getpid ());
|
printf ("pid = %d\n", getpid ());
|
||||||
|
|
||||||
SECURE_BUFFER_HEAP_ALLOCATION_Q (ipcd, sizeof (struct ipcd),, EXIT_FAILURE);
|
ctx = malloc (sizeof (struct ipc_ctx));
|
||||||
|
memset(ctx, 0, sizeof (struct ipc_ctx));
|
||||||
|
|
||||||
struct ipc_error ret = ipc_server_init (&ipcd->ctx, SERVICE_NAME);
|
struct ipc_error ret = ipc_server_init (ctx, SERVICE_NAME);
|
||||||
if (ret.error_code != IPC_ERROR_NONE) {
|
if (ret.error_code != IPC_ERROR_NONE) {
|
||||||
fprintf (stderr, "%s\n", ret.error_message);
|
fprintf (stderr, "%s\n", ret.error_message);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
printf ("Listening on [%s].\n", ipcd->ctx->spath);
|
printf ("Listening on [%s].\n", ctx->cinfos[0].spath);
|
||||||
|
|
||||||
printf ("MAIN: server created\n");
|
printf ("MAIN: server created\n");
|
||||||
|
|
||||||
|
|
Reference in New Issue