aboutsummaryrefslogtreecommitdiffstats
path: root/libglouglou/tests/sendrecv.c
blob: fbf4aedca0f8266f595c9a33a36922b50506cc31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#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", 12345, 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", 12345, NULL, cli_handle_packet, NULL);
  if (!cli) {
    printf("error: gg_client_connect returned NULL\n");
    return 1;
  }
  event_base_loop(ev_base, EVLOOP_ONCE);
  event_base_loop(ev_base, EVLOOP_ONCE);

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

  gg_server_send(srv, &pkt, NULL);
  event_base_loop(ev_base, EVLOOP_ONCE);

  gg_client_send(cli, &pkt);
  event_base_loop(ev_base, EVLOOP_ONCE);

  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);
}