ecore_con_server_simple_example.c

Shows how to setup a simple server that accepts client connections and sends a "hello" string to them. See the complete example description at Ecore_Con - Creating a server

//Compile with:
// gcc -o ecore_con_server_simple_example ecore_con_server_simple_example.c `pkg-config --libs --cflags ecore ecore-con eina`
#include <stdio.h>
#include <Ecore.h>
#include <Ecore_Con.h>
struct _Client
{
int sdata;
};
_add(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Client_Add *ev)
{
char welcome[] = "hello! - sent from the server";
const Eina_List *clients, *l;
struct _Client *client = malloc(sizeof(*client));
client->sdata = 0;
printf("Client with ip %s, port %d, connected = %d!\n",
ecore_con_client_send(ev->client, welcome, sizeof(welcome));
printf("Clients connected to this server:\n");
EINA_LIST_FOREACH(clients, l, cl)
printf("%s\n", ecore_con_client_ip_get(cl));
}
_del(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Client_Del *ev)
{
struct _Client *client;
if (!ev->client)
printf("Lost client with ip %s!\n", ecore_con_client_ip_get(ev->client));
if (client)
{
printf("Total data received from this client: %d\n", client->sdata);
free(client);
}
printf("Client was connected for %0.3f seconds.\n",
}
_data(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Client_Data *ev)
{
char fmt[128];
struct _Client *client = ecore_con_client_data_get(ev->client);
snprintf(fmt, sizeof(fmt),
"Received %i bytes from client %s port %d:\n"
">>>>>\n"
"%%.%is\n"
">>>>>\n",
printf(fmt, ev->data);
client->sdata += ev->size;
}
int
main(void)
{
const Eina_List *clients, *l;
if (!(svr = ecore_con_server_add(ECORE_CON_REMOTE_TCP, "127.0.0.1", 8080, NULL)))
exit(1);
printf("Clients connected to this server when exiting: %d\n",
eina_list_count(clients));
EINA_LIST_FOREACH(clients, l, cl)
{
printf("%s\n", ecore_con_client_ip_get(cl));
}
printf("Server was up for %0.3f seconds\n",
return 0;
}
ecore_con_client_ip_get
const EAPI char * ecore_con_client_ip_get(const Ecore_Con_Client *cl)
Gets the IP address of a client that has connected.
Definition: ecore_con_legacy.c:739
Ecore_Con_Client
struct _Ecore_Con_Client Ecore_Con_Client
Definition: Ecore_Con.h:332
ecore_con_server_timeout_set
EAPI void ecore_con_server_timeout_set(Ecore_Con_Server *svr, double timeout)
Sets the default time after which an inactive client will be disconnected.
Definition: ecore_con_legacy.c:2284
ecore_con_shutdown
EAPI int ecore_con_shutdown(void)
Shuts down the Ecore_Con library.
Definition: ecore_con.c:133
EINA_LIST_FOREACH
#define EINA_LIST_FOREACH(list, l, _data)
Definition for the macro to iterate over a list.
Definition: eina_list.h:1415
EINA_UNUSED
#define EINA_UNUSED
Definition: eina_types.h:339
_Ecore_Con_Event_Client_Del::client
Ecore_Con_Client * client
the client that was lost
Definition: Ecore_Con.h:486
ecore_con_client_data_set
EAPI void ecore_con_client_data_set(Ecore_Con_Client *cl, const void *data)
Sets the data associated with the given client to data.
Definition: ecore_con_legacy.c:724
_Ecore_Con_Event_Client_Data::size
int size
the length of the data sent
Definition: Ecore_Con.h:545
_Ecore_Con_Event_Client_Data::data
void * data
the data that the client sent
Definition: Ecore_Con.h:544
ECORE_CON_EVENT_CLIENT_ADD
EAPI int ECORE_CON_EVENT_CLIENT_ADD
A client has connected to the server.
Definition: ecore_con_legacy.c:157
_Ecore_Con_Event_Client_Data
Definition: Ecore_Con.h:541
eina_init
int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:279
ecore_con_client_flush
EAPI void ecore_con_client_flush(Ecore_Con_Client *cl)
Flushes all pending data to the given client.
Definition: ecore_con_legacy.c:767
ecore_con_server_clients_get
const EAPI Eina_List * ecore_con_server_clients_get(const Ecore_Con_Server *svr)
Retrieves the current list of clients.
Definition: ecore_con_legacy.c:2277
ecore_event_handler_add
Ecore_Event_Handler * ecore_event_handler_add(int type, Ecore_Event_Handler_Cb func, const void *data)
Adds an event handler.
Definition: ecore_events.c:13
ECORE_CALLBACK_RENEW
#define ECORE_CALLBACK_RENEW
Return value to keep a callback.
Definition: Ecore_Common.h:153
Ecore_Con_Server
struct _Ecore_Con_Server Ecore_Con_Server
Definition: Ecore_Con.h:318
Ecore_Event_Handler_Cb
Eina_Bool(* Ecore_Event_Handler_Cb)(void *data, int type, void *event)
Definition: Ecore_Common.h:603
ecore_con_server_add
EAPI Ecore_Con_Server * ecore_con_server_add(Ecore_Con_Type type, const char *name, int port, const void *data)
Creates a server to listen for connections.
Definition: ecore_con_legacy.c:1696
ECORE_CON_EVENT_CLIENT_DATA
EAPI int ECORE_CON_EVENT_CLIENT_DATA
A client connected to the server has sent data.
Definition: ecore_con_legacy.c:166
ecore_main_loop_begin
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1298
eina_shutdown
int eina_shutdown(void)
Shuts down the Eina library.
Definition: eina_main.c:350
ecore_con_client_send
EAPI int ecore_con_client_send(Ecore_Con_Client *cl, const void *data, int size)
Sends the given data to the given client.
Definition: ecore_con_legacy.c:668
_Ecore_Con_Event_Client_Add
Definition: Ecore_Con.h:465
ECORE_CON_EVENT_CLIENT_DEL
EAPI int ECORE_CON_EVENT_CLIENT_DEL
A client has disconnected from the server.
Definition: ecore_con_legacy.c:158
_Ecore_Con_Event_Client_Add::client
Ecore_Con_Client * client
the client that connected
Definition: Ecore_Con.h:467
ecore_con_client_server_get
EAPI Ecore_Con_Server * ecore_con_client_server_get(const Ecore_Con_Client *cl)
The server the client is connected to.
Definition: ecore_con_legacy.c:753
Eina_Bool
unsigned char Eina_Bool
Definition: eina_types.h:527
ecore_con_client_connected_get
EAPI Eina_Bool ecore_con_client_connected_get(const Ecore_Con_Client *cl)
Returns whether the client is still connected.
Definition: ecore_con_legacy.c:690
ecore_con_init
EAPI int ecore_con_init(void)
Initializes the Ecore_Con library.
Definition: ecore_con.c:68
_Eina_List
Definition: eina_list.h:317
ecore_con_client_uptime_get
EAPI double ecore_con_client_uptime_get(const Ecore_Con_Client *cl)
Checks how long a client has been connected.
Definition: ecore_con_legacy.c:760
ecore_shutdown
EAPI int ecore_shutdown(void)
Shuts down connections, signal handlers sockets etc.
Definition: ecore.c:366
ecore_con_client_data_get
EAPI void * ecore_con_client_data_get(Ecore_Con_Client *cl)
Retrieves the data associated with the given client.
Definition: ecore_con_legacy.c:732
ecore_con_server_client_limit_set
EAPI void ecore_con_server_client_limit_set(Ecore_Con_Server *svr, int client_limit, char reject_excess_clients)
Sets a limit on the number of clients that can be handled concurrently by the given server,...
Definition: ecore_con_legacy.c:2267
ecore_con_client_port_get
EAPI int ecore_con_client_port_get(const Ecore_Con_Client *cl)
Returns the port that the client has connected to.
Definition: ecore_con_legacy.c:746
_Ecore_Con_Event_Client_Data::client
Ecore_Con_Client * client
the client that connected
Definition: Ecore_Con.h:543
ecore_con_client_del
EAPI void * ecore_con_client_del(Ecore_Con_Client *cl)
Closes the connection and free memory allocated to the given client.
Definition: ecore_con_legacy.c:711
_Ecore_Con_Event_Client_Del
Definition: Ecore_Con.h:484
ecore_con_server_uptime_get
EAPI double ecore_con_server_uptime_get(const Ecore_Con_Server *svr)
Checks how long a server has been connected.
Definition: ecore_con_legacy.c:2405
ecore_init
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:225
eina_list_count
static unsigned int eina_list_count(const Eina_List *list)
Gets the count of the number of items in a list.
ecore_con_client_timeout_set
EAPI void ecore_con_client_timeout_set(Ecore_Con_Client *cl, double timeout)
Sets the time after which the client will be disconnected when inactive.
Definition: ecore_con_legacy.c:697