The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / liboi / test / ping_pong.c @ 90fc8d36

History | View | Annotate | Download (3.44 KB)

1
#include "test/common.c"
2

    
3
#define PING "PING"
4
#define PONG "PONG"
5
#define EXCHANGES 100
6

    
7
int successful_ping_count; 
8

    
9
static void 
10
on_peer_read(oi_socket *socket, const void *base, size_t len)
11
{
12
  if(len == 0) 
13
    return;
14

    
15
  char buf[2000];
16
  strncpy(buf, base, len);
17
  buf[len] = 0;
18
  //printf("server got message: %s\n", buf);
19

    
20
  oi_socket_write_simple(socket, PONG, sizeof PONG);
21
}
22

    
23
static void 
24
on_peer_error(oi_socket *socket, struct oi_error e)
25
{
26
  assert(0);
27
}
28

    
29
static void 
30
on_client_close(oi_socket *socket)
31
{
32
  //printf("client connection closed\n");
33
  ev_unloop(EV_DEFAULT_ EVUNLOOP_ALL);
34
}
35

    
36
static oi_socket* 
37
on_server_connection(oi_server *server, struct sockaddr *addr, socklen_t len)
38
{
39
  oi_socket *socket = malloc(sizeof(oi_socket));
40
  oi_socket_init(socket, 5.0);
41
  socket->on_read = on_peer_read;
42
  socket->on_error = on_peer_error;
43
  socket->on_close = on_peer_close;
44
  socket->on_timeout = on_peer_timeout;
45

    
46
  nconnections++;
47

    
48
#if HAVE_GNUTLS
49
# if SECURE
50
  anon_tls_server(socket);
51
# endif
52
#endif
53

    
54
  //printf("on server connection\n");
55

    
56
  return socket;
57
}
58

    
59
static void 
60
on_client_connect(oi_socket *socket)
61
{
62
  //printf("client connected. sending ping\n");
63
  oi_socket_write_simple(socket, PING, sizeof PING);
64
}
65

    
66
static void 
67
on_client_read(oi_socket *socket, const void *base, size_t len)
68
{
69
  char buf[200000];
70
  strncpy(buf, base, len);
71
  buf[len] = 0;
72
  //printf("client got message: %s\n", buf);
73
  
74
  if(strcmp(buf, PONG) == 0) {
75

    
76
    if(++successful_ping_count > EXCHANGES) {
77
      oi_socket_close(socket);
78
      return;
79
    } 
80
    oi_socket_write_simple(socket, PING, sizeof PING);
81
  } else {
82
    assert(0);
83
  }
84
}
85

    
86
int 
87
main(int argc, const char *argv[])
88
{
89
  int r;
90
  oi_server server;
91
  oi_socket client;
92

    
93
  //printf("sizeof(oi_server): %d\n", sizeof(oi_server));
94
  //printf("sizeof(oi_socket): %d\n", sizeof(oi_socket));
95

    
96
  oi_server_init(&server, 10);
97
  server.on_connection = on_server_connection;
98

    
99
#if HAVE_GNUTLS
100
# if SECURE
101
  anon_tls_init();
102
# endif
103
#endif
104

    
105
  struct addrinfo *servinfo;
106
  struct addrinfo hints;
107
  memset(&hints, 0, sizeof hints);
108
#if TCP
109
    hints.ai_family = AF_UNSPEC;
110
    hints.ai_socktype = SOCK_STREAM;
111
    hints.ai_flags = AI_PASSIVE;
112
    r = getaddrinfo(NULL, PORT, &hints, &servinfo);
113
    assert(r == 0);
114
#else
115
    struct stat tstat;
116
    if (lstat(SOCKFILE, &tstat) == 0) {
117
      if (S_ISSOCK(tstat.st_mode))
118
        unlink(SOCKFILE);
119
    }
120

    
121
    servinfo = malloc(sizeof(struct addrinfo));
122
    servinfo->ai_family = AF_UNIX;
123
    servinfo->ai_socktype = SOCK_STREAM;
124
    servinfo->ai_protocol = 0;
125

    
126
    struct sockaddr_un *sockaddr = calloc(sizeof(struct sockaddr_un), 1);
127
    sockaddr->sun_family = AF_UNIX;
128
    strcpy(sockaddr->sun_path, SOCKFILE);
129

    
130
    servinfo->ai_addr = (struct sockaddr*)sockaddr;
131
    servinfo->ai_addrlen = sizeof(struct sockaddr_un);
132
#endif
133
  r = oi_server_listen(&server, servinfo);
134
  assert(r == 0);
135
  oi_server_attach(EV_DEFAULT_ &server);
136

    
137
  oi_socket_init(&client, 5.0);
138
  client.on_read    = on_client_read;
139
  client.on_error   = on_client_error;
140
  client.on_connect = on_client_connect;
141
  client.on_close   = on_client_close;
142
  client.on_timeout = on_client_timeout;
143

    
144
#if HAVE_GNUTLS
145
# if SECURE
146
  anon_tls_client(&client);
147
# endif
148
#endif
149

    
150
  r = oi_socket_connect(&client, servinfo);
151
  assert(r == 0 && "problem connecting");
152
  oi_socket_attach(EV_DEFAULT_ &client);
153

    
154
  ev_loop(EV_DEFAULT_ 0);
155

    
156
  assert(successful_ping_count == EXCHANGES + 1);
157
  assert(nconnections == 1);
158

    
159
#if TCP
160
  freeaddrinfo(servinfo);
161
#endif
162

    
163
  return 0;
164
}