aboutsummaryrefslogblamecommitdiffstats
path: root/libglouglou/tests/sendrecv.c
blob: 63ba6e36f887e0ddc903c20280d51d6cbd0426ab (plain) (tree)
1
2
3
4
5
6
7
8
9







                           
                                        
                                                             
 


                                        




                                                               


                                        




          



                                   
 














                                                                                  
 

                                  
 



                                        
 
                                                  
 

                                  
 



                                                    
 
                                              
 
#include <event.h>

#include "../libglouglou.h"

int srv_recv_ok = 0;
int cli_recv_ok = 0;

int
srv_handle_packet(struct gg_server *srv,
		  struct gg_user *usr, struct gg_packet *pkt)
{
	if (pkt->type == PACKET_NEWCONN)
		srv_recv_ok = 1;
	return 0;
}

int
cli_handle_packet(struct gg_client *cli, struct gg_packet *pkt)
{
	if (pkt->type == PACKET_NEWCONN)
		cli_recv_ok = 1;
	return 0;
}

int
main(void)
{
	struct event_base *ev_base;
	struct gg_server *srv;
	struct gg_client *cli;
	struct gg_packet pkt;

	ev_base = event_base_new();
	srv = gg_server_start(ev_base, "127.0.0.1", GLOUGLOU_PROBE_DEFAULT_PORT,
			NULL, srv_handle_packet, NULL);
	if (!srv) {
		printf("error: gg_server_start returned NULL\n");
		return 1;
	}
	cli = gg_client_connect(ev_base, "127.0.0.1", GLOUGLOU_PROBE_DEFAULT_PORT,
			NULL, cli_handle_packet, NULL);
	if (!cli) {
		printf("error: gg_client_connect returned NULL\n");
		return 1;
	}
	event_base_loop(ev_base, EVLOOP_NONBLOCK);
	event_base_loop(ev_base, EVLOOP_NONBLOCK);

	pkt.ver = PACKET_VERSION;
	pkt.type = PACKET_NEWCONN;

	gg_server_send(srv, &pkt, NULL);
	gg_server_send_flush(srv, NULL);
	gg_client_send(cli, &pkt);
	gg_client_send_flush(cli);

	event_base_loop(ev_base, EVLOOP_NONBLOCK);

	gg_client_disconnect(cli);
	gg_server_stop(srv);

	if (srv_recv_ok == 0)
		printf("error: srv_recv_ok == 0\n");
	if (cli_recv_ok == 0)
		printf("error: cli_recv_ok == 0\n");

	return (!srv_recv_ok || !cli_recv_ok);
}