diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/ecore/src/examples/ecore_con_client_simple_example.c | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/libraries/ecore/src/examples/ecore_con_client_simple_example.c b/libraries/ecore/src/examples/ecore_con_client_simple_example.c new file mode 100644 index 0000000..f6eb257 --- /dev/null +++ b/libraries/ecore/src/examples/ecore_con_client_simple_example.c | |||
@@ -0,0 +1,124 @@ | |||
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 _Server { | ||
12 | int sdata; | ||
13 | }; | ||
14 | |||
15 | Eina_Bool | ||
16 | _add(void *data __UNUSED__, int type __UNUSED__, Ecore_Con_Event_Server_Add *ev) | ||
17 | { | ||
18 | char welcome[] = "hello! - sent from the client"; | ||
19 | struct _Server *server = malloc(sizeof(*server)); | ||
20 | server->sdata = 0; | ||
21 | |||
22 | ecore_con_server_data_set(ev->server, server); | ||
23 | printf("Server with ip %s, name %s, port %d, connected = %d!\n", | ||
24 | ecore_con_server_ip_get(ev->server), | ||
25 | ecore_con_server_name_get(ev->server), | ||
26 | ecore_con_server_port_get(ev->server), | ||
27 | ecore_con_server_connected_get(ev->server)); | ||
28 | ecore_con_server_send(ev->server, welcome, sizeof(welcome)); | ||
29 | ecore_con_server_flush(ev->server); | ||
30 | |||
31 | return ECORE_CALLBACK_RENEW; | ||
32 | } | ||
33 | |||
34 | |||
35 | Eina_Bool | ||
36 | _del(void *data __UNUSED__, int type __UNUSED__, Ecore_Con_Event_Server_Del *ev) | ||
37 | { | ||
38 | if (!ev->server) | ||
39 | { | ||
40 | printf("Failed to establish connection to the server.\nExiting.\n"); | ||
41 | ecore_main_loop_quit(); | ||
42 | return ECORE_CALLBACK_RENEW; | ||
43 | } | ||
44 | |||
45 | struct _Server *server = ecore_con_server_data_get(ev->server); | ||
46 | |||
47 | printf("Lost server with ip %s!\n", ecore_con_server_ip_get(ev->server)); | ||
48 | |||
49 | if (server) | ||
50 | { | ||
51 | printf("Total data received from this server: %d\n", server->sdata); | ||
52 | free(server); | ||
53 | } | ||
54 | |||
55 | ecore_con_server_del(ev->server); | ||
56 | |||
57 | ecore_main_loop_quit(); | ||
58 | return ECORE_CALLBACK_RENEW; | ||
59 | } | ||
60 | |||
61 | Eina_Bool | ||
62 | _data(void *data __UNUSED__, int type __UNUSED__, Ecore_Con_Event_Server_Data *ev) | ||
63 | { | ||
64 | char fmt[128]; | ||
65 | struct _Server *server = ecore_con_server_data_get(ev->server); | ||
66 | |||
67 | snprintf(fmt, sizeof(fmt), | ||
68 | "Received %i bytes from server:\n" | ||
69 | ">>>>>\n" | ||
70 | "%%.%is\n" | ||
71 | ">>>>>\n", | ||
72 | ev->size, ev->size); | ||
73 | |||
74 | printf(fmt, ev->data); | ||
75 | |||
76 | server->sdata += ev->size; | ||
77 | return ECORE_CALLBACK_RENEW; | ||
78 | } | ||
79 | |||
80 | int main(int argc, const char *argv[]) | ||
81 | { | ||
82 | Ecore_Con_Server *svr; | ||
83 | const char *address; | ||
84 | int port = 8080; | ||
85 | |||
86 | if (argc < 2) | ||
87 | { | ||
88 | printf("wrong usage. Command syntax is:\n"); | ||
89 | printf("\tecore_con_client_simple_example <address> [port]\n"); | ||
90 | exit (1); | ||
91 | } | ||
92 | |||
93 | address = argv[1]; | ||
94 | |||
95 | if (argc > 2) | ||
96 | port = atoi(argv[2]); | ||
97 | |||
98 | eina_init(); | ||
99 | ecore_init(); | ||
100 | ecore_con_init(); | ||
101 | |||
102 | if (!(svr = ecore_con_server_connect(ECORE_CON_REMOTE_TCP, address, port, NULL))) | ||
103 | { | ||
104 | printf("could not connect to the server: %s, port %d.\n", | ||
105 | address, port); | ||
106 | exit(2); | ||
107 | } | ||
108 | |||
109 | /* set event handler for server connect */ | ||
110 | ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD, (Ecore_Event_Handler_Cb)_add, NULL); | ||
111 | /* set event handler for server disconnect */ | ||
112 | ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL, (Ecore_Event_Handler_Cb)_del, NULL); | ||
113 | /* set event handler for receiving server data */ | ||
114 | ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DATA, (Ecore_Event_Handler_Cb)_data, NULL); | ||
115 | |||
116 | /* start client */ | ||
117 | ecore_main_loop_begin(); | ||
118 | |||
119 | ecore_con_init(); | ||
120 | ecore_init(); | ||
121 | eina_init(); | ||
122 | |||
123 | return 0; | ||
124 | } | ||