Ecore Connection Buffering

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.
return EINA_TRUE;
}
int
main(int argc, char **argv)
{
const char *url = "http://www.enlightenment.org/p/index/d/logo.png";
// This is single additional line to manage dynamic network data.
completion_callback,
data);
data_callback,
data);
return 0;
}
Eina Utility library.
EAPI int ECORE_CON_EVENT_URL_DATA
A URL object has data.
Definition: ecore_con_url.c:29
EAPI 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
EAPI int ecore_con_init(void)
Initializes the Ecore_Con library.
Definition: ecore_con.c:72
EAPI Eina_Bool ecore_con_url_get(Ecore_Con_Url *url_con)
Sends a get request.
Definition: ecore_con_url.c:864
EAPI 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:784
EAPI int ecore_con_url_init(void)
Initializes the Ecore_Con_Url library.
Definition: ecore_con_url.c:45
struct _Ecore_Con_Url Ecore_Con_Url
Used to provide legacy API/ABI compatibility with non-Eo applications.
Definition: Ecore_Con.h:347
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
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:229
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:1300
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1290
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.
const unsigned char * eina_binbuf_string_get(const Eina_Binbuf *buf)
Retrieves a pointer to the contents of a string buffer.
size_t eina_binbuf_length_get(const Eina_Binbuf *buf)
Retrieves the length of the string buffer's content.
void eina_binbuf_free(Eina_Binbuf *buf)
Frees a string buffer.
Eina_Binbuf * eina_binbuf_new(void)
Creates a new binary string buffer.
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
Used as the data param for the ECORE_CON_EVENT_URL_COMPLETE event.
Definition: Ecore_Con.h:610
int status
HTTP status code of the operation (200, 404, 401, etc.)
Definition: Ecore_Con.h:612
Used as the data param for the ECORE_CON_EVENT_URL_DATA event.
Definition: Ecore_Con.h:598
int size
the size of the current received data (in bytes)
Definition: Ecore_Con.h:600
unsigned char data[1]
the data received on this event
Definition: Ecore_Con.h:601
String buffer to facilitate string operations.
Definition: eina_strbuf_common.h:15