11 # define ECORE_FINITE(t) isfinite(t)
14 # define ECORE_FINITE(t) _finite(t)
16 # define ECORE_FINITE(t) finite(t)
24 # include <sys/param.h>
44 #include "ecore_private.h"
46 /* How of then we should retry to write to the pipe */
47 #define ECORE_PIPE_WRITE_RETRY 6
50 * On Windows, pipe() is implemented with sockets.
51 * Contrary to Linux, Windows uses different functions
52 * for sockets and fd's: write() is for fd's and send
53 * is for sockets. So I need to put some win32 code
54 * here. I can't think of a solution where the win32
55 * code is in Evil and not here.
60 # include <winsock2.h>
62 # define pipe_write(fd, buffer, size) send((fd), (char *)(buffer), size, 0)
63 # define pipe_read(fd, buffer, size) recv((fd), (char *)(buffer), size, 0)
64 # define pipe_close(fd) closesocket(fd)
65 # define PIPE_FD_INVALID INVALID_SOCKET
66 # define PIPE_FD_ERROR SOCKET_ERROR
73 # define pipe_write(fd, buffer, size) write((fd), buffer, size)
74 # define pipe_read(fd, buffer, size) read((fd), buffer, size)
75 # define pipe_close(fd) close(fd)
76 # define PIPE_FD_INVALID -1
77 # define PIPE_FD_ERROR -1
82 #include "ecore_private.h"
89 Ecore_Fd_Handler *fd_handler;
91 Ecore_Pipe_Cb handler;
97 Eina_Bool delete_me : 1;
99 GENERIC_ALLOC_SIZE_DECLARE(Ecore_Pipe);
101 static Eina_Bool _ecore_pipe_read(void *data,
102 Ecore_Fd_Handler *fd_handler);
105 * @addtogroup Ecore_Pipe_Group
111 * Create two file descriptors (sockets on Windows). Add
112 * a callback that will be called when the file descriptor that
113 * is listened receives data. An event is also put in the event
114 * queue when data is received.
116 * @param handler The handler called when data is received.
117 * @param data Data to pass to @p handler when it is called.
118 * @return A newly created Ecore_Pipe object if successful.
122 ecore_pipe_add(Ecore_Pipe_Cb handler,
128 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
129 if (!handler) return NULL;
131 p = ecore_pipe_calloc(1);
136 ecore_pipe_mp_free(p);
140 ECORE_MAGIC_SET(p, ECORE_MAGIC_PIPE);
142 p->fd_write = fds[1];
143 p->handler = handler;
146 _ecore_fd_close_on_exec(fds[0]);
147 _ecore_fd_close_on_exec(fds[1]);
149 fcntl(p->fd_read, F_SETFL, O_NONBLOCK);
150 p->fd_handler = ecore_main_fd_handler_add(p->fd_read,
159 * Free an Ecore_Pipe object created with ecore_pipe_add().
161 * @param p The Ecore_Pipe object to be freed.
162 * @return The pointer to the private data
165 ecore_pipe_del(Ecore_Pipe *p)
169 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
170 if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE))
172 ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_del");
175 p->delete_me = EINA_TRUE;
176 if (p->handling > 0) return (void *)p->data;
177 if (p->fd_handler) _ecore_main_fd_handler_del(p->fd_handler);
178 if (p->fd_read != PIPE_FD_INVALID) pipe_close(p->fd_read);
179 if (p->fd_write != PIPE_FD_INVALID) pipe_close(p->fd_write);
180 data = (void *)p->data;
181 ecore_pipe_mp_free(p);
186 * Close the read end of an Ecore_Pipe object created with ecore_pipe_add().
188 * @param p The Ecore_Pipe object.
191 ecore_pipe_read_close(Ecore_Pipe *p)
193 EINA_MAIN_LOOP_CHECK_RETURN;
194 if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE))
196 ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_read_close");
201 _ecore_main_fd_handler_del(p->fd_handler);
202 p->fd_handler = NULL;
204 if (p->fd_read != PIPE_FD_INVALID)
206 pipe_close(p->fd_read);
207 p->fd_read = PIPE_FD_INVALID;
212 * Stop monitoring if necessary the pipe for reading. See ecore_pipe_thaw()
213 * for monitoring it again.
215 * @param p The Ecore_Pipe object.
219 ecore_pipe_freeze(Ecore_Pipe *p)
221 EINA_MAIN_LOOP_CHECK_RETURN;
222 if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE))
224 ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_read_freeze");
229 _ecore_main_fd_handler_del(p->fd_handler);
230 p->fd_handler = NULL;
235 * Start monitoring again the pipe for reading. See ecore_pipe_freeze() for
236 * stopping the monitoring activity. This will not work if
237 * ecore_pipe_read_close() was previously called on the same pipe.
239 * @param p The Ecore_Pipe object.
243 ecore_pipe_thaw(Ecore_Pipe *p)
245 EINA_MAIN_LOOP_CHECK_RETURN;
246 if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE))
248 ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_read_thaw");
251 if (!p->fd_handler && p->fd_read != PIPE_FD_INVALID)
253 p->fd_handler = ecore_main_fd_handler_add(p->fd_read,
262 * @brief Wait from another thread on the read side of a pipe.
264 * @param p The pipe to watch on.
265 * @param message_count The minimal number of message to wait before exiting.
266 * @param wait The amount of time in second to wait before exiting.
267 * @return the number of message catched during that wait call.
270 * Negative value for @p wait means infite wait.
273 ecore_pipe_wait(Ecore_Pipe *p,
277 struct timeval tv, *t;
284 EINA_MAIN_LOOP_CHECK_RETURN_VAL(-1);
285 if (p->fd_read == PIPE_FD_INVALID)
289 FD_SET(p->fd_read, &rset);
292 end = ecore_loop_time_get() + wait;
295 while (message_count > 0 && (timeout > 0.0 || wait <= 0.0))
299 /* finite() tests for NaN, too big, too small, and infinity. */
300 if ((!ECORE_FINITE(timeout)) || (timeout == 0.0))
305 else if (timeout > 0.0)
309 timeout += (0.5 / HZ);
311 usec = (int)((timeout - (double)sec) * 1000000);
314 usec = (int)((timeout - (double)sec) * 1000000);
326 ret = main_loop_select(p->fd_read + 1, &rset, NULL, NULL, t);
330 _ecore_pipe_read(p, NULL);
331 message_count -= p->message;
339 else if (errno != EINTR)
342 p->fd_read = PIPE_FD_INVALID;
347 timeout = end - ecore_loop_time_get();
354 * Close the write end of an Ecore_Pipe object created with ecore_pipe_add().
356 * @param p The Ecore_Pipe object.
359 ecore_pipe_write_close(Ecore_Pipe *p)
361 if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE))
363 ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_write_close");
366 if (p->fd_write != PIPE_FD_INVALID)
368 pipe_close(p->fd_write);
369 p->fd_write = PIPE_FD_INVALID;
374 * Write on the file descriptor the data passed as parameter.
376 * @param p The Ecore_Pipe object.
377 * @param buffer The data to write into the pipe.
378 * @param nbytes The size of the @p buffer in bytes
379 * @return @c EINA_TRUE on a successful write, @c EINA_FALSE on error.
382 ecore_pipe_write(Ecore_Pipe *p,
387 size_t already_written = 0;
388 int retry = ECORE_PIPE_WRITE_RETRY;
390 if (!ECORE_MAGIC_CHECK(p, ECORE_MAGIC_PIPE))
392 ECORE_MAGIC_FAIL(p, ECORE_MAGIC_PIPE, "ecore_pipe_write");
396 if (p->delete_me) return EINA_FALSE;
398 if (p->fd_write == PIPE_FD_INVALID) return EINA_FALSE;
400 /* First write the len into the pipe */
403 ret = pipe_write(p->fd_write, &nbytes, sizeof(nbytes));
404 if (ret == sizeof(nbytes))
406 retry = ECORE_PIPE_WRITE_RETRY;
411 /* XXX What should we do here? */
412 ERR("The length of the data was not written complete"
416 else if (ret == PIPE_FD_ERROR && errno == EPIPE)
418 pipe_close(p->fd_write);
419 p->fd_write = PIPE_FD_INVALID;
422 else if (ret == PIPE_FD_ERROR && errno == EINTR)
427 ERR("An unhandled error (ret: %zd errno: %d)"
428 "occurred while writing to the pipe the length",
434 if (retry != ECORE_PIPE_WRITE_RETRY) return EINA_FALSE;
436 /* and now pass the data to the pipe */
439 ret = pipe_write(p->fd_write,
440 ((unsigned char *)buffer) + already_written,
441 nbytes - already_written);
443 if (ret == (ssize_t)(nbytes - already_written))
447 already_written -= ret;
450 else if (ret == PIPE_FD_ERROR && errno == EPIPE)
452 pipe_close(p->fd_write);
453 p->fd_write = PIPE_FD_INVALID;
456 else if (ret == PIPE_FD_ERROR && errno == EINTR)
461 ERR("An unhandled error (ret: %zd errno: %d)"
462 "occurred while writing to the pipe the length",
475 /* Private function */
477 _ecore_pipe_unhandle(Ecore_Pipe *p)
487 _ecore_pipe_read(void *data,
488 Ecore_Fd_Handler *fd_handler __UNUSED__)
490 Ecore_Pipe *p = (Ecore_Pipe *)data;
494 for (i = 0; i < 16; i++)
498 /* if we already have read some data we don't need to read the len
499 * but to finish the already started job
503 /* read the len of the passed data */
504 ret = pipe_read(p->fd_read, &p->len, sizeof(p->len));
506 /* catch the non error case first */
507 /* read amount ok - nothing more to do */
508 if (ret == sizeof(p->len))
512 /* we got more data than we asked for - definite error */
513 ERR("Only read %i bytes from the pipe, although"
514 " we need to read %i bytes.",
515 (int)ret, (int)sizeof(p->len));
516 _ecore_pipe_unhandle(p);
517 return ECORE_CALLBACK_CANCEL;
524 /* no data on first try through means an error */
526 p->handler((void *)p->data, NULL, 0);
527 if (p->passed_data) free(p->passed_data);
528 p->passed_data = NULL;
532 pipe_close(p->fd_read);
533 p->fd_read = PIPE_FD_INVALID;
534 p->fd_handler = NULL;
535 _ecore_pipe_unhandle(p);
536 return ECORE_CALLBACK_CANCEL;
540 /* no data after first loop try is ok */
541 _ecore_pipe_unhandle(p);
542 return ECORE_CALLBACK_RENEW;
546 else if ((ret == PIPE_FD_ERROR) &&
547 ((errno == EINTR) || (errno == EAGAIN)))
549 return ECORE_CALLBACK_RENEW;
553 ERR("An unhandled error (ret: %i errno: %i [%s])"
554 "occurred while reading from the pipe the length",
555 (int)ret, errno, strerror(errno));
556 return ECORE_CALLBACK_RENEW;
559 else /* ret == PIPE_FD_ERROR is the only other case on Windows */
561 if (WSAGetLastError() != WSAEWOULDBLOCK)
564 p->handler((void *)p->data, NULL, 0);
565 if (p->passed_data) free(p->passed_data);
566 p->passed_data = NULL;
570 pipe_close(p->fd_read);
571 p->fd_read = PIPE_FD_INVALID;
572 p->fd_handler = NULL;
573 _ecore_pipe_unhandle(p);
574 return ECORE_CALLBACK_CANCEL;
580 /* if somehow we got less than or equal to 0 we got an errnoneous
581 * messages so call callback with null and len we got. this case should
586 p->handler((void *)p->data, NULL, 0);
587 /* reset all values to 0 */
588 if (p->passed_data) free(p->passed_data);
589 p->passed_data = NULL;
593 _ecore_pipe_unhandle(p);
594 return ECORE_CALLBACK_RENEW;
597 /* we dont have a buffer to hold the data, so alloc it */
600 p->passed_data = malloc(p->len);
601 /* alloc failed - error case */
605 p->handler((void *)p->data, NULL, 0);
610 pipe_close(p->fd_read);
611 p->fd_read = PIPE_FD_INVALID;
612 p->fd_handler = NULL;
613 _ecore_pipe_unhandle(p);
614 return ECORE_CALLBACK_CANCEL;
618 /* and read the passed data */
619 ret = pipe_read(p->fd_read,
620 ((unsigned char *)p->passed_data) + p->already_read,
621 p->len - p->already_read);
623 /* catch the non error case first */
624 /* if we read enough data to finish the message/buffer */
625 if (ret == (ssize_t)(p->len - p->already_read))
628 p->handler((void *)p->data, p->passed_data, p->len);
629 free(p->passed_data);
630 /* reset all values to 0 */
631 p->passed_data = NULL;
638 /* more data left to read */
639 p->already_read += ret;
640 _ecore_pipe_unhandle(p);
641 return ECORE_CALLBACK_RENEW;
645 /* 0 bytes to read - could be more to read next select wake up */
646 _ecore_pipe_unhandle(p);
647 return ECORE_CALLBACK_RENEW;
650 else if ((ret == PIPE_FD_ERROR) &&
651 ((errno == EINTR) || (errno == EAGAIN)))
653 _ecore_pipe_unhandle(p);
654 return ECORE_CALLBACK_RENEW;
658 ERR("An unhandled error (ret: %zd errno: %d)"
659 "occurred while reading from the pipe the data",
661 _ecore_pipe_unhandle(p);
662 return ECORE_CALLBACK_RENEW;
665 else /* ret == PIPE_FD_ERROR is the only other case on Windows */
667 if (WSAGetLastError() != WSAEWOULDBLOCK)
670 p->handler((void *)p->data, NULL, 0);
671 if (p->passed_data) free(p->passed_data);
672 p->passed_data = NULL;
676 pipe_close(p->fd_read);
677 p->fd_read = PIPE_FD_INVALID;
678 p->fd_handler = NULL;
679 _ecore_pipe_unhandle(p);
680 return ECORE_CALLBACK_CANCEL;
688 _ecore_pipe_unhandle(p);
689 return ECORE_CALLBACK_RENEW;