\hypertarget{ecore_pipe_simple_example_8c-example}{}\section{ecore\+\_\+pipe\+\_\+simple\+\_\+example.\+c}

\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{//gcc -g -Wall -o ecore\_pipe\_simple\_example ecore\_pipe\_simple\_example.c `pkg-config --cflags --libs ecore`}

\textcolor{preprocessor}{#include <unistd.h>}
\textcolor{preprocessor}{#include <Ecore.h>}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
do\_lengthy\_task(\mbox{\hyperlink{group__Ecore__Pipe__Group_ga0092a6b764881362d1a051d7af215b3f}{Ecore\_Pipe}} *\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}})
\{
   \textcolor{keywordtype}{int} i, j;
   \textcolor{keywordtype}{char} *buffer;
   \textcolor{keywordflow}{for} (i = 0; i < 20; i++)
     \{
        sleep(1);
        buffer = malloc(\textcolor{keyword}{sizeof}(\textcolor{keywordtype}{char}) * i);
        \textcolor{keywordflow}{for} (j = 0; j < i; j++)
          buffer[j] = \textcolor{charliteral}{'a'} + j;
        \mbox{\hyperlink{group__Ecore__Pipe__Group_ga5336bc71e829c391926c26750ae0f407}{ecore\_pipe\_write}}(\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}}, buffer, i);
        free(buffer);
     \}
   \mbox{\hyperlink{group__Ecore__Pipe__Group_ga5336bc71e829c391926c26750ae0f407}{ecore\_pipe\_write}}(\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}}, \textcolor{stringliteral}{"close"}, 5);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
handler(\textcolor{keywordtype}{void} *data \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}}, \textcolor{keywordtype}{void} *buf, \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} len)
\{
   \textcolor{keywordtype}{char} *str = malloc(\textcolor{keyword}{sizeof}(\textcolor{keywordtype}{char}) * len + 1);
   memcpy(str, buf, len);
   str[len] = \textcolor{charliteral}{'\(\backslash\)0'};
   printf(\textcolor{stringliteral}{"received %u bytes\(\backslash\)n"}, len);
   printf(\textcolor{stringliteral}{"content: %s\(\backslash\)n"}, (\textcolor{keyword}{const} \textcolor{keywordtype}{char} *)str);
   free(str);
   \textcolor{keywordflow}{if} (len && !strncmp(buf, \textcolor{stringliteral}{"close"}, len < 5 ? len : 5))
     \{
        printf(\textcolor{stringliteral}{"close requested\(\backslash\)n"});
        \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();
     \}
\}

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{void})
\{
   \mbox{\hyperlink{group__Ecore__Pipe__Group_ga0092a6b764881362d1a051d7af215b3f}{Ecore\_Pipe}} *\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}};
   pid\_t child\_pid;

   \mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}();

   \mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}} = \mbox{\hyperlink{group__Ecore__Pipe__Group_ga3d584a8f962644f748dfd84d39d3421a}{ecore\_pipe\_add}}(handler, NULL);

   child\_pid = fork();
   \textcolor{keywordflow}{if} (!child\_pid)
     \{
        \mbox{\hyperlink{group__Ecore__Pipe__Group_gab9df469d955d67dbcd95fd6f2dabdc79}{ecore\_pipe\_read\_close}}(\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}});
        do\_lengthy\_task(\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}});
     \}
   \textcolor{keywordflow}{else}
     \{
        \mbox{\hyperlink{group__Ecore__Pipe__Group_ga60099202e0880dd3de617ca14fa0c58b}{ecore\_pipe\_write\_close}}(\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}});
        \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();
     \}

   \mbox{\hyperlink{group__Ecore__Pipe__Group_ga7ed5669e83a3119d2023c64288f9af60}{ecore\_pipe\_del}}(\mbox{\hyperlink{group__Escape__Unistd__Group_ga5ecdf33dcbe17677f251c99743b545ce}{pipe}});
   \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}}();

   \textcolor{keywordflow}{return} 0;
\}

\end{DoxyCodeInclude}
 