diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/ecore/src/examples/ecore_con_client_example.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/libraries/ecore/src/examples/ecore_con_client_example.c b/libraries/ecore/src/examples/ecore_con_client_example.c new file mode 100644 index 0000000..2f656c7 --- /dev/null +++ b/libraries/ecore/src/examples/ecore_con_client_example.c | |||
@@ -0,0 +1,90 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <Ecore.h> | ||
3 | #include <Ecore_Con.h> | ||
4 | |||
5 | /* Ecore_Con client example | ||
6 | * 2010 Mike Blumenkrantz | ||
7 | */ | ||
8 | |||
9 | /* comment if not using gnutls */ | ||
10 | static void | ||
11 | tls_log_func(int level, const char *str) | ||
12 | { | ||
13 | fprintf(stderr, "|<%d>| %s", level, str); | ||
14 | } | ||
15 | |||
16 | Eina_Bool | ||
17 | _add(void *data, int type, Ecore_Con_Event_Server_Add *ev) | ||
18 | { | ||
19 | printf("Server with ip %s connected!\n", ecore_con_server_ip_get(ev->server)); | ||
20 | ecore_con_server_send(ev->server, "hello!", 6); | ||
21 | ecore_con_server_flush(ev->server); | ||
22 | |||
23 | return ECORE_CALLBACK_RENEW; | ||
24 | } | ||
25 | |||
26 | |||
27 | Eina_Bool | ||
28 | _del(void *data, int type, Ecore_Con_Event_Server_Del *ev) | ||
29 | { | ||
30 | printf("Lost server with ip %s!\n", ecore_con_server_ip_get(ev->server)); | ||
31 | ecore_main_loop_quit(); | ||
32 | return ECORE_CALLBACK_RENEW; | ||
33 | } | ||
34 | |||
35 | Eina_Bool | ||
36 | _data(void *data, int type, Ecore_Con_Event_Server_Data *ev) | ||
37 | { | ||
38 | char fmt[128]; | ||
39 | |||
40 | snprintf(fmt, sizeof(fmt), | ||
41 | "Received %i bytes from server:\n" | ||
42 | ">>>>>\n" | ||
43 | "%%.%is\n" | ||
44 | ">>>>>\n", | ||
45 | ev->size, ev->size); | ||
46 | |||
47 | printf(fmt, ev->data); | ||
48 | return ECORE_CALLBACK_RENEW; | ||
49 | } | ||
50 | int main() | ||
51 | { | ||
52 | Ecore_Con_Server *svr; | ||
53 | Eina_Iterator *it; | ||
54 | const char *ca; | ||
55 | |||
56 | eina_init(); | ||
57 | ecore_init(); | ||
58 | ecore_con_init(); | ||
59 | |||
60 | /* comment if not using gnutls */ | ||
61 | gnutls_global_set_log_level(9); | ||
62 | gnutls_global_set_log_function(tls_log_func); | ||
63 | |||
64 | if (!(it = eina_file_ls("/etc/ssl/certs"))) | ||
65 | exit(1); | ||
66 | |||
67 | if (!(svr = ecore_con_server_connect(ECORE_CON_REMOTE_TCP | ECORE_CON_USE_MIXED, "www.verisign.com", 443, NULL))) | ||
68 | exit(1); | ||
69 | |||
70 | /* add all the CAs */ | ||
71 | EINA_ITERATOR_FOREACH(it, ca) | ||
72 | { | ||
73 | if (!ecore_con_ssl_server_cafile_add(svr, ca)) | ||
74 | printf("Could not load CA: %s!\n", ca); | ||
75 | eina_stringshare_del(ca); | ||
76 | } | ||
77 | |||
78 | eina_iterator_free(it); | ||
79 | ecore_con_ssl_server_verify(svr); | ||
80 | |||
81 | /* set event handler for server connect */ | ||
82 | ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD, (Ecore_Event_Handler_Cb)_add, NULL); | ||
83 | /* set event handler for server disconnect */ | ||
84 | ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL, (Ecore_Event_Handler_Cb)_del, NULL); | ||
85 | /* set event handler for receiving server data */ | ||
86 | ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DATA, (Ecore_Event_Handler_Cb)_data, NULL); | ||
87 | |||
88 | /* start client */ | ||
89 | ecore_main_loop_begin(); | ||
90 | } | ||