\hypertarget{group__Ecore__Con__Buffer}{}\section{Ecore Connection Buffering}
\label{group__Ecore__Con__Buffer}\index{Ecore Connection Buffering@{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 A\+PI\textquotesingle{}s. The Eina\+\_\+\+Binbuf and Eina\+\_\+\+Strbuf A\+P\+Is, 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 A\+PI. 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 \hyperlink{group__Eina__Binary__Buffer__Group_ga225d04b203e52cac2825cfe8478fa01f}{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.


\begin{DoxyCode}
\textcolor{preprocessor}{#include <\hyperlink{Eina_8h}{Eina.h}>}
\textcolor{preprocessor}{#include <Ecore.h>}
\textcolor{preprocessor}{#include <Ecore\_Con.h>}

\textcolor{keyword}{static} \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
data\_callback(\textcolor{keywordtype}{void} *data, \textcolor{keywordtype}{int} type, \textcolor{keywordtype}{void} *event)
\{
   \hyperlink{struct__Ecore__Con__Event__Url__Data}{Ecore\_Con\_Event\_Url\_Data} *url\_data = event;
   \textcolor{keywordflow}{if} ( url\_data->\hyperlink{struct__Ecore__Con__Event__Url__Data_a1d4725aa77f3875cc3405597a5fbff91}{size} > 0)
     \{
        \textcolor{comment}{// append data as it arrives - don't worry where or how it gets stored.}
        \textcolor{comment}{// Also don't worry about size, expanding, reallocing etc.}
        \textcolor{comment}{// just keep appending - size is automatically handled.}

        \hyperlink{group__Eina__Binary__Buffer__Group_gab4b8d7a1e45eaf5ec6250a126c6ef967}{eina\_binbuf\_append\_length}(data, url\_data->\hyperlink{struct__Ecore__Con__Event__Url__Data_aaa4c4dbc6575b071164f8152a7808268}{data}, url\_data->
      \hyperlink{struct__Ecore__Con__Event__Url__Data_a1d4725aa77f3875cc3405597a5fbff91}{size});

        fprintf(stderr, \textcolor{stringliteral}{"Appended %d \(\backslash\)n"}, url\_data->\hyperlink{struct__Ecore__Con__Event__Url__Data_a1d4725aa77f3875cc3405597a5fbff91}{size});
     \}
   \textcolor{keywordflow}{return} \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
\}



\textcolor{keyword}{static} \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
completion\_callback(\textcolor{keywordtype}{void} *data, \textcolor{keywordtype}{int} type, \textcolor{keywordtype}{void} *event)
\{
   \hyperlink{struct__Ecore__Con__Event__Url__Complete}{Ecore\_Con\_Event\_Url\_Complete} *url\_complete = event;
   printf(\textcolor{stringliteral}{"download completed with status code: %d\(\backslash\)n"}, url\_complete->\hyperlink{struct__Ecore__Con__Event__Url__Complete_ab842caa1615a911c3578e0dfeae5f773}{status});

   \textcolor{comment}{// get the data back from Eina\_Binbuf}
   \textcolor{keywordtype}{char} *ptr = \hyperlink{group__Eina__Binary__Buffer__Group_gace23717e3c4371cc0b3f299147cec187}{eina\_binbuf\_string\_get}(data);
   \textcolor{keywordtype}{size\_t} size = \hyperlink{group__Eina__Binary__Buffer__Group_gade3052a6c39b4be19c25be92687cded3}{eina\_binbuf\_length\_get}(data);

   \textcolor{comment}{// process data as required (write to file)}
   fprintf(stderr, \textcolor{stringliteral}{"Size of data = %d bytes\(\backslash\)n"}, size);
   \textcolor{keywordtype}{int} fd = open(\textcolor{stringliteral}{"./elm.png"}, O\_CREAT);
   write(fd, ptr, size);
   close(fd);
  
   \textcolor{comment}{// free it when done.}
   \hyperlink{group__Eina__Binary__Buffer__Group_ga225d04b203e52cac2825cfe8478fa01f}{eina\_binbuf\_free}(data);

   \hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}();

   \textcolor{keywordflow}{return} \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
\}


\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{int} argc, \textcolor{keywordtype}{char} **argv)
\{

   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *url = \textcolor{stringliteral}{"http://www.enlightenment.org/p/index/d/logo.png"};

   \hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}();
   \hyperlink{group__Ecore__Con__Events__Group_gacf5ecf42c516bb9c8b6759721e9559bd}{ecore\_con\_init}();
   \hyperlink{group__Ecore__Con__Url__Group_gac3fbeaf9cde529362e4567fc37e45c9f}{ecore\_con\_url\_init}();
  

   \textcolor{comment}{// This is single additional line to manage dynamic network data.}
   \hyperlink{struct__Eina__Strbuf}{Eina\_Binbuf} *data = \hyperlink{group__Eina__Binary__Buffer__Group_ga3fd61ff27de5e842411aea6c0eb78014}{eina\_binbuf\_new}();
   Ecore\_Con\_Url *url\_con = \hyperlink{group__Ecore__Con__Url__Group_gae40b7c1a94413166c61524b707118f19}{ecore\_con\_url\_new}(url);

   \hyperlink{group__Ecore__Event__Group_gae75b0b1b2c50e73b8e42afc628b00a62}{ecore\_event\_handler\_add}(\hyperlink{group__Ecore__Con__Events__Group_gae90b9b857e1e0adc0c7f24e7ccecb44b}{ECORE\_CON\_EVENT\_URL\_COMPLETE},
                                                      completion\_callback,
                                                      data);
   \hyperlink{group__Ecore__Event__Group_gae75b0b1b2c50e73b8e42afc628b00a62}{ecore\_event\_handler\_add}(\hyperlink{group__Ecore__Con__Events__Group_ga6ebe51236dac1bc1255b5b390cc9e6e3}{ECORE\_CON\_EVENT\_URL\_DATA},
                                                      data\_callback,
                                                      data);
   \hyperlink{group__Ecore__Con__Url__Group_ga46f75358256eec26bd99eef96b25ffa8}{ecore\_con\_url\_get}(url\_con);

   \hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}();
   \textcolor{keywordflow}{return} 0;
\}
\end{DoxyCode}
 