onbsd/tests/01_server.c
2024-11-16 14:37:49 -06:00

65 lines
2.5 KiB
C

#define DEBUG
#include "../include/onbsd.h"
#include <stdlib.h>
void ev_handler(Conn *c, int events, void *ptr){
(void)ptr;
switch(events){
case EV_POLL:
if(c->is_listening){
fprintf(stdout,"EV_POLL - Listening for connections on %s:%hu", inet_ntoa(c->loc.sin_addr), ntohs(c->loc.sin_port));
}
if(c->is_accepted){
fprintf(stdout,"EV_POLL - Remote connection %s:%hu", inet_ntoa(c->rem.sin_addr), ntohs(c->rem.sin_port));
}
if(c->is_client){
fprintf(stdout,"EV_POLL - Local connection %s:%hu", inet_ntoa(c->loc.sin_addr), ntohs(c->loc.sin_port));
}
break;
case EV_OPEN:
if(c->is_listening){
fprintf(stdout,"EV_OPEN - Listening for connections on %s:%hu", inet_ntoa(c->loc.sin_addr), ntohs(c->loc.sin_port));
}
if(c->is_accepted){
fprintf(stdout,"EV_OPEN - Opening connection on %s:%hu from %s:%hu",
inet_ntoa(c->loc.sin_addr), ntohs(c->loc.sin_port),
inet_ntoa(c->rem.sin_addr), ntohs(c->rem.sin_port));
}
if(c->is_client){
fprintf(stdout,"EV_OPEN - Opening connection on %s:%hu from %s:%hu",
inet_ntoa(c->loc.sin_addr), ntohs(c->loc.sin_port),
inet_ntoa(c->rem.sin_addr), ntohs(c->rem.sin_port));
}
break;
case EV_READ:
fprintf(stdout,"EV_READ - Reading connection %s:%hu from %s:%hu",
inet_ntoa(c->loc.sin_addr), ntohs(c->loc.sin_port),
inet_ntoa(c->rem.sin_addr), ntohs(c->rem.sin_port));
break;
case EV_CLOSE:
fprintf(stdout,"EV_CLOSE - Closing connections on %s:%hu from %s:%hu",
inet_ntoa(c->loc.sin_addr), ntohs(c->loc.sin_port),
inet_ntoa(c->rem.sin_addr), ntohs(c->rem.sin_port));
break;
case EV_HTTP_MSG:
fprintf(stdout,"EV_HTTP_MSG - HTTP message on %s:%hu from %s:%hu",
inet_ntoa(c->loc.sin_addr), ntohs(c->loc.sin_port),
inet_ntoa(c->rem.sin_addr), ntohs(c->rem.sin_port));
break;
default:
break;
}
fprintf(stdout,"\n");
fflush(stdout);
}
int main(void){
Mgr m;
init_mgr(&m);
mgr_listen(&m, 3000, INADDR_LOOPBACK, ev_handler, NULL);
for(;;){
mgr_poll(&m,5000);
}
return EXIT_SUCCESS;
}