onbsd/tests/02_client.c
2024-11-16 13:48:36 -06:00

63 lines
2.2 KiB
C

#include "../include/onbsd.h"
#include <stdbool.h>
#define PORT 3000
#define DEBUG
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;
default:
break;
}
fprintf(stdout,"\n");
fflush(stdout);
}
int main(void){
Mgr c;
init_mgr(&c);
mgr_connect(&c,ev_handler,NULL);
for(;;){
mgr_poll(&c,3000);
}
return 0;
}