2013-02-08 09:35:17 -08:00
|
|
|
//Compile with:
|
|
|
|
// gcc -o ecore_con_server_example ecore_con_server_example.c `pkg-config --libs --cflags ecore ecore-con eina`
|
|
|
|
|
2013-01-04 15:51:53 -08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-12-03 11:36:16 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <Ecore.h>
|
|
|
|
#include <Ecore_Con.h>
|
|
|
|
|
|
|
|
/* Ecore_Con server example
|
|
|
|
* 2010 Mike Blumenkrantz
|
|
|
|
*/
|
|
|
|
|
2013-01-04 15:51:53 -08:00
|
|
|
#ifdef HAVE_GNUTLS
|
|
|
|
#include <gnutls/gnutls.h>
|
|
|
|
|
2012-12-03 11:36:16 -08:00
|
|
|
static void
|
|
|
|
tls_log_func(int level, const char *str)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "|<%d>| %s", level, str);
|
|
|
|
}
|
2013-01-04 15:51:53 -08:00
|
|
|
#endif
|
2012-12-03 11:36:16 -08:00
|
|
|
|
|
|
|
Eina_Bool
|
2014-01-08 23:33:20 -08:00
|
|
|
_add(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Client_Add *ev)
|
2012-12-03 11:36:16 -08:00
|
|
|
{
|
|
|
|
printf("Client with ip %s connected!\n", ecore_con_client_ip_get(ev->client));
|
|
|
|
ecore_con_client_send(ev->client, "hello!", 6);
|
|
|
|
// ecore_con_client_flush(ev->client);
|
|
|
|
ecore_con_client_timeout_set(ev->client, 5);
|
|
|
|
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
}
|
|
|
|
|
|
|
|
Eina_Bool
|
2014-01-08 23:33:20 -08:00
|
|
|
_del(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Client_Del *ev)
|
2012-12-03 11:36:16 -08:00
|
|
|
{
|
|
|
|
printf("Lost client with ip %s!\n", ecore_con_client_ip_get(ev->client));
|
|
|
|
ecore_main_loop_quit();
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
}
|
|
|
|
|
|
|
|
Eina_Bool
|
2014-01-08 23:33:20 -08:00
|
|
|
_data(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Client_Data *ev)
|
2012-12-03 11:36:16 -08:00
|
|
|
{
|
|
|
|
char fmt[128];
|
|
|
|
|
|
|
|
snprintf(fmt, sizeof(fmt),
|
|
|
|
"Received %i bytes from client:\n"
|
|
|
|
">>>>>\n"
|
|
|
|
"%%.%is\n"
|
|
|
|
">>>>>\n",
|
|
|
|
ev->size, ev->size);
|
|
|
|
|
|
|
|
printf(fmt, ev->data);
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
|
|
|
Ecore_Con_Server *svr;
|
|
|
|
eina_init();
|
|
|
|
ecore_init();
|
|
|
|
ecore_con_init();
|
|
|
|
|
2013-01-04 15:51:53 -08:00
|
|
|
#ifdef HAVE_GNUTLS
|
2012-12-03 11:36:16 -08:00
|
|
|
gnutls_global_set_log_level(9);
|
|
|
|
gnutls_global_set_log_function(tls_log_func);
|
2013-01-04 15:51:53 -08:00
|
|
|
#endif
|
2012-12-03 11:36:16 -08:00
|
|
|
|
|
|
|
/* to use a PEM certificate with TLS and SSL3, uncomment the lines below */
|
|
|
|
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)))
|
|
|
|
/* to use simple tcp with ssl/tls, use this line */
|
|
|
|
// if (!ecore_con_server_add(ECORE_CON_REMOTE_TCP | ECORE_CON_USE_SSL3, "127.0.0.1", 8080, NULL))
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
ecore_con_ssl_server_cert_add(svr, "server.pem");
|
|
|
|
ecore_con_ssl_server_privkey_add(svr, "server.pem");
|
|
|
|
/* set event handler for client connect */
|
|
|
|
ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD, (Ecore_Event_Handler_Cb)_add, NULL);
|
|
|
|
/* set event handler for client disconnect */
|
|
|
|
ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DEL, (Ecore_Event_Handler_Cb)_del, NULL);
|
|
|
|
/* set event handler for receiving client data */
|
|
|
|
ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DATA, (Ecore_Event_Handler_Cb)_data, NULL);
|
|
|
|
|
|
|
|
/* start server */
|
|
|
|
ecore_main_loop_begin();
|
|
|
|
}
|
|
|
|
|