diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/ecore/src/examples/ecore_con_server_simple_example.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/libraries/ecore/src/examples/ecore_con_server_simple_example.c b/libraries/ecore/src/examples/ecore_con_server_simple_example.c new file mode 100644 index 0000000..e466ed4 --- /dev/null +++ b/libraries/ecore/src/examples/ecore_con_server_simple_example.c | |||
@@ -0,0 +1,131 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <Ecore.h> | ||
3 | #include <Ecore_Con.h> | ||
4 | |||
5 | #ifdef HAVE_CONFIG_H | ||
6 | # include "config.h" | ||
7 | #else | ||
8 | # define __UNUSED__ | ||
9 | #endif | ||
10 | |||
11 | struct _Client { | ||
12 | int sdata; | ||
13 | }; | ||
14 | |||
15 | Eina_Bool | ||
16 | _add(void *data __UNUSED__, int type __UNUSED__, Ecore_Con_Event_Client_Add *ev) | ||
17 | { | ||
18 | char welcome[] = "hello! - sent from the server"; | ||
19 | Ecore_Con_Server *srv; | ||
20 | Ecore_Con_Client *cl; | ||
21 | const Eina_List *clients, *l; | ||
22 | |||
23 | struct _Client *client = malloc(sizeof(*client)); | ||
24 | client->sdata = 0; | ||
25 | |||
26 | printf("Client with ip %s, port %d, connected = %d!\n", | ||
27 | ecore_con_client_ip_get(ev->client), | ||
28 | ecore_con_client_port_get(ev->client), | ||
29 | ecore_con_client_connected_get(ev->client)); | ||
30 | |||
31 | ecore_con_client_send(ev->client, welcome, sizeof(welcome)); | ||
32 | ecore_con_client_flush(ev->client); | ||
33 | |||
34 | ecore_con_client_timeout_set(ev->client, 6); | ||
35 | |||
36 | ecore_con_client_data_set(ev->client, client); | ||
37 | |||
38 | srv = ecore_con_client_server_get(ev->client); | ||
39 | printf("Clients connected to this server:\n"); | ||
40 | clients = ecore_con_server_clients_get(srv); | ||
41 | EINA_LIST_FOREACH(clients, l, cl) | ||
42 | printf("%s\n", ecore_con_client_ip_get(cl)); | ||
43 | |||
44 | return ECORE_CALLBACK_RENEW; | ||
45 | } | ||
46 | |||
47 | |||
48 | Eina_Bool | ||
49 | _del(void *data __UNUSED__, int type __UNUSED__, Ecore_Con_Event_Client_Del *ev) | ||
50 | { | ||
51 | struct _Client *client; | ||
52 | |||
53 | if (!ev->client) | ||
54 | return ECORE_CALLBACK_RENEW; | ||
55 | |||
56 | client = ecore_con_client_data_get(ev->client); | ||
57 | |||
58 | printf("Lost client with ip %s!\n", ecore_con_client_ip_get(ev->client)); | ||
59 | printf("Total data received from this client: %d\n", client->sdata); | ||
60 | printf("Client was connected for %0.3f seconds.\n", | ||
61 | ecore_con_client_uptime_get(ev->client)); | ||
62 | |||
63 | if (client) | ||
64 | free(client); | ||
65 | |||
66 | ecore_con_client_del(ev->client); | ||
67 | |||
68 | return ECORE_CALLBACK_RENEW; | ||
69 | } | ||
70 | |||
71 | Eina_Bool | ||
72 | _data(void *data __UNUSED__, int type __UNUSED__, Ecore_Con_Event_Client_Data *ev) | ||
73 | { | ||
74 | char fmt[128]; | ||
75 | struct _Client *client = ecore_con_client_data_get(ev->client); | ||
76 | |||
77 | snprintf(fmt, sizeof(fmt), | ||
78 | "Received %i bytes from client %s port %d:\n" | ||
79 | ">>>>>\n" | ||
80 | "%%.%is\n" | ||
81 | ">>>>>\n", | ||
82 | ev->size, ecore_con_client_ip_get(ev->client), | ||
83 | ecore_con_client_port_get(ev->client), ev->size); | ||
84 | |||
85 | printf(fmt, ev->data); | ||
86 | |||
87 | client->sdata += ev->size; | ||
88 | |||
89 | return ECORE_CALLBACK_RENEW; | ||
90 | } | ||
91 | |||
92 | int main(void) | ||
93 | { | ||
94 | Ecore_Con_Server *svr; | ||
95 | Ecore_Con_Client *cl; | ||
96 | const Eina_List *clients, *l; | ||
97 | |||
98 | eina_init(); | ||
99 | ecore_init(); | ||
100 | ecore_con_init(); | ||
101 | |||
102 | if (!(svr = ecore_con_server_add(ECORE_CON_REMOTE_TCP, "127.0.0.1", 8080, NULL))) | ||
103 | exit(1); | ||
104 | |||
105 | ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD, (Ecore_Event_Handler_Cb)_add, NULL); | ||
106 | ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DEL, (Ecore_Event_Handler_Cb)_del, NULL); | ||
107 | ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DATA, (Ecore_Event_Handler_Cb)_data, NULL); | ||
108 | |||
109 | ecore_con_server_timeout_set(svr, 10); | ||
110 | ecore_con_server_client_limit_set(svr, 3, 0); | ||
111 | |||
112 | ecore_main_loop_begin(); | ||
113 | |||
114 | clients = ecore_con_server_clients_get(svr); | ||
115 | printf("Clients connected to this server when exiting: %d\n", | ||
116 | eina_list_count(clients)); | ||
117 | EINA_LIST_FOREACH(clients, l, cl) | ||
118 | { | ||
119 | printf("%s\n", ecore_con_client_ip_get(cl)); | ||
120 | free(ecore_con_client_data_get(cl)); | ||
121 | } | ||
122 | |||
123 | printf("Server was up for %0.3f seconds\n", | ||
124 | ecore_con_server_uptime_get(svr)); | ||
125 | |||
126 | ecore_con_shutdown(); | ||
127 | ecore_shutdown(); | ||
128 | eina_shutdown(); | ||
129 | |||
130 | return 0; | ||
131 | } | ||