Ecore Connection Buffering
Ecore » Ecore_Con - Connection functions

As Ecore_Con works on an event driven design, as data arrives, events will be produced containing the data that arrived.

As Ecore_Con works on an event driven design, as data arrives, events will be produced containing the data that arrived.

It is up to the user of Ecore_Con to either parse as they go, append to a file to later parse the whole file in one go, or append to memory to parse or handle later.

To help with this Eina has some handy API's. The Eina_Binbuf and Eina_Strbuf APIs, abstract dynamic buffer management and make it trivial to handle buffers at runtime, without having to manage them. Eina_Binbuf makes it possible to create, expand, reset and slice a blob of memory - all via API. No system calls, no pointer manipulations and no size calculation.

Additional functions include adding content at specified byte positions in the buffer, escaping the inputs, find and replace strings. This provides extreme flexibility to play around, with a dynamic blob of memory.

It is good to free it (using eina_binbuf_free()) after using it.

Eina_Binbuf compliments Ecore_Con use cases, where dynamic sizes of data arrive from the network (think http download in chunks). Using Eina_Binbuf provides enough flexibility to handle data as it arrives and to defer its processing until desired, without having to think about where to store the temporary data and how to manage its size.

An example of how to use these with Ecore_Con follows.

#include <Eina.h>
#include <Ecore.h>
#include <Ecore_Con.h>
static Eina_Bool
data_callback(void *data, int type, void *event)
{
Ecore_Con_Event_Url_Data *url_data = event;
if ( url_data->size > 0)
{
// append data as it arrives - don't worry where or how it gets stored.
// Also don't worry about size, expanding, reallocing etc.
// just keep appending - size is automatically handled.
eina_binbuf_append_length(data, url_data->data, url_data->size);
fprintf(stderr, "Appended %d \n", url_data->size);
}
return EINA_TRUE;
}
static Eina_Bool
completion_callback(void *data, int type, void *event)
{
Ecore_Con_Event_Url_Complete *url_complete = event;
printf("download completed with status code: %d\n", url_complete->status);
// get the data back from Eina_Binbuf
char *ptr = eina_binbuf_string_get(data);
size_t size = eina_binbuf_length_get(data);
// process data as required (write to file)
fprintf(stderr, "Size of data = %d bytes\n", size);
int fd = open("./elm.png", O_CREAT);
write(fd, ptr, size);
close(fd);
// free it when done.
eina_binbuf_free(data);
ecore_main_loop_quit();
return EINA_TRUE;
}
int
main(int argc, char **argv)
{
const char *url = "http://www.enlightenment.org/p/index/d/logo.png";
ecore_init();
ecore_con_init();
ecore_con_url_init();
// This is single additional line to manage dynamic network data.
Eina_Binbuf *data = eina_binbuf_new();
Ecore_Con_Url *url_con = ecore_con_url_new(url);
ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE,
completion_callback,
data);
ecore_event_handler_add(ECORE_CON_EVENT_URL_DATA,
data_callback,
data);
ecore_con_url_get(url_con);
ecore_main_loop_begin();
return 0;
}
Eina.h
Eina Utility library.
ECORE_CON_EVENT_URL_DATA
ECORE_CON_API int ECORE_CON_EVENT_URL_DATA
A URL object has data.
Definition: ecore_con_url.c:29
ECORE_CON_EVENT_URL_COMPLETE
ECORE_CON_API int ECORE_CON_EVENT_URL_COMPLETE
A URL object has completed its transfer to and from the server and can be reused.
Definition: ecore_con_url.c:30
ecore_con_init
ECORE_CON_API int ecore_con_init(void)
Initializes the Ecore_Con library.
Definition: ecore_con.c:68
ecore_con_url_new
ECORE_CON_API Ecore_Con_Url * ecore_con_url_new(const char *url)
Creates and initializes a new Ecore_Con_Url connection object.
Definition: ecore_con_url.c:782
ecore_con_url_init
ECORE_CON_API int ecore_con_url_init(void)
Initializes the Ecore_Con_Url library.
Definition: ecore_con_url.c:45
Ecore_Con_Url
struct _Ecore_Con_Url Ecore_Con_Url
Used to provide legacy API/ABI compatibility with non-Eo applications.
Definition: Ecore_Con.h:323
ecore_con_url_get
ECORE_CON_API Eina_Bool ecore_con_url_get(Ecore_Con_Url *url_con)
Sends a get request.
Definition: ecore_con_url.c:862
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_init
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:230
ecore_main_loop_quit
void ecore_main_loop_quit(void)
Quits the main loop once all the events currently on the queue have been processed.
Definition: ecore_main.c:1321
ecore_main_loop_begin
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
eina_binbuf_string_get
EINA_API const unsigned char * eina_binbuf_string_get(const Eina_Binbuf *buf)
Retrieves a pointer to the contents of a string buffer.
eina_binbuf_length_get
EINA_API size_t eina_binbuf_length_get(const Eina_Binbuf *buf)
Retrieves the length of the string buffer's content.
eina_binbuf_free
EINA_API void eina_binbuf_free(Eina_Binbuf *buf)
Frees a string buffer.
eina_binbuf_new
EINA_API Eina_Binbuf * eina_binbuf_new(void)
Creates a new binary string buffer.
eina_binbuf_append_length
EINA_API Eina_Bool eina_binbuf_append_length(Eina_Binbuf *buf, const unsigned char *str, size_t length)
Appends a string of exact length to a buffer, reallocating as necessary.
EINA_TRUE
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
Eina_Bool
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
_Ecore_Con_Event_Url_Complete
Used as the data param for the ECORE_CON_EVENT_URL_COMPLETE event.
Definition: Ecore_Con.h:586
_Ecore_Con_Event_Url_Complete::status
int status
HTTP status code of the operation (200, 404, 401, etc.)
Definition: Ecore_Con.h:588
_Ecore_Con_Event_Url_Data
Used as the data param for the ECORE_CON_EVENT_URL_DATA event.
Definition: Ecore_Con.h:574
_Ecore_Con_Event_Url_Data::size
int size
the size of the current received data (in bytes)
Definition: Ecore_Con.h:576
_Ecore_Con_Event_Url_Data::data
unsigned char data[1]
the data received on this event
Definition: Ecore_Con.h:577
_Eina_Strbuf
String buffer to facilitate string operations.
Definition: eina_strbuf_common.h:15