Abandon the socket properly when freeing a DC hub.
[doldaconnect.git] / clients / test.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <stdlib.h>
3#include <sys/poll.h>
4#include <time.h>
5#include <sys/time.h>
6
7#include <doldaconnect/uilib.h>
8#include <doldaconnect/uimisc.h>
9#include <doldaconnect/utils.h>
10
11int done;
12double btime;
13
14void authcallback(int err, wchar_t *reason, void *data)
15{
16 printf("auth: %f\n", ntime() - btime);
17 printf("Logged in: %i\n", err);
18 exit(0);
19 dc_queuecmd(NULL, NULL, L"quit", NULL);
20}
21
22int main(int argc, char **argv)
23{
24 struct pollfd pfd;
25 int fd;
26 struct dc_response *resp;
27
28 btime = ntime();
29 dc_init();
30 printf("init: %f\n", ntime() - btime);
31 fd = dc_connect(NULL);
32 done = 0;
33 while(!done)
34 {
35 pfd.fd = fd;
36 pfd.events = POLLIN;
37 if(dc_wantwrite())
38 pfd.events = POLLOUT;
39 if(poll(&pfd, 1, -1) < 0)
40 {
41 perror("poll");
42 exit(1);
43 }
44 if((pfd.revents & POLLIN) && dc_handleread())
45 done = 1;
46 if((pfd.revents & POLLOUT) && dc_handlewrite())
47 done = 1;
48 while((resp = dc_getresp()) != NULL)
49 {
50 if(!wcscmp(resp->cmdname, L".connect"))
51 {
52 printf("conn: %f\n", ntime() - btime);
53 printf("Connected: %i\n", resp->code);
54 if(resp->code == 201)
55 dc_loginasync(NULL, 1, NULL, authcallback, NULL);
56 }
57 dc_freeresp(resp);
58 }
59 }
60 printf("fini: %f\n", ntime() - btime);
61 dc_cleanup();
62 printf("exit: %f\n", ntime() - btime);
63 return(0);
64}