diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/ecore/src/examples/ecore_con_server_example.c | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/libraries/ecore/src/examples/ecore_con_server_example.c b/libraries/ecore/src/examples/ecore_con_server_example.c new file mode 100644 index 0000000..5423eb1 --- /dev/null +++ b/libraries/ecore/src/examples/ecore_con_server_example.c | |||
@@ -0,0 +1,81 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <Ecore.h> | ||
3 | #include <Ecore_Con.h> | ||
4 | |||
5 | /* Ecore_Con server 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_Client_Add *ev) | ||
18 | { | ||
19 | printf("Client with ip %s connected!\n", ecore_con_client_ip_get(ev->client)); | ||
20 | ecore_con_client_send(ev->client, "hello!", 6); | ||
21 | // ecore_con_client_flush(ev->client); | ||
22 | ecore_con_client_timeout_set(ev->client, 5); | ||
23 | |||
24 | return ECORE_CALLBACK_RENEW; | ||
25 | } | ||
26 | |||
27 | |||
28 | Eina_Bool | ||
29 | _del(void *data, int type, Ecore_Con_Event_Client_Del *ev) | ||
30 | { | ||
31 | printf("Lost client with ip %s!\n", ecore_con_client_ip_get(ev->client)); | ||
32 | ecore_main_loop_quit(); | ||
33 | return ECORE_CALLBACK_RENEW; | ||
34 | } | ||
35 | |||
36 | Eina_Bool | ||
37 | _data(void *data, int type, Ecore_Con_Event_Client_Data *ev) | ||
38 | { | ||
39 | char fmt[128]; | ||
40 | |||
41 | snprintf(fmt, sizeof(fmt), | ||
42 | "Received %i bytes from client:\n" | ||
43 | ">>>>>\n" | ||
44 | "%%.%is\n" | ||
45 | ">>>>>\n", | ||
46 | ev->size, ev->size); | ||
47 | |||
48 | printf(fmt, ev->data); | ||
49 | return ECORE_CALLBACK_RENEW; | ||
50 | } | ||
51 | int main() | ||
52 | { | ||
53 | Ecore_Con_Server *svr; | ||
54 | eina_init(); | ||
55 | ecore_init(); | ||
56 | ecore_con_init(); | ||
57 | |||
58 | /* comment if not using gnutls */ | ||
59 | gnutls_global_set_log_level(9); | ||
60 | gnutls_global_set_log_function(tls_log_func); | ||
61 | |||
62 | |||
63 | /* to use a PEM certificate with TLS and SSL3, uncomment the lines below */ | ||
64 | if (!(svr = ecore_con_server_add(ECORE_CON_REMOTE_TCP | ECORE_CON_USE_TLS | ECORE_CON_USE_SSL3 | ECORE_CON_LOAD_CERT, "127.0.0.1", 8080, NULL))) | ||
65 | |||
66 | /* to use simple tcp with ssl/tls, use this line */ | ||
67 | // if (!ecore_con_server_add(ECORE_CON_REMOTE_TCP | ECORE_CON_USE_SSL3, "127.0.0.1", 8080, NULL)) | ||
68 | exit(1); | ||
69 | |||
70 | ecore_con_ssl_server_cert_add(svr, "server.pem"); | ||
71 | ecore_con_ssl_server_privkey_add(svr, "server.pem"); | ||
72 | /* set event handler for client connect */ | ||
73 | ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD, (Ecore_Event_Handler_Cb)_add, NULL); | ||
74 | /* set event handler for client disconnect */ | ||
75 | ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DEL, (Ecore_Event_Handler_Cb)_del, NULL); | ||
76 | /* set event handler for receiving client data */ | ||
77 | ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DATA, (Ecore_Event_Handler_Cb)_data, NULL); | ||
78 | |||
79 | /* start server */ | ||
80 | ecore_main_loop_begin(); | ||
81 | } | ||