10 #include "Ecore_Con.h"
11 #include "ecore_con_private.h"
16 static int _ecore_con_local_init_count = 0;
19 ecore_con_local_init(void)
21 if (++_ecore_con_local_init_count != 1)
22 return _ecore_con_local_init_count;
24 return _ecore_con_local_init_count;
28 ecore_con_local_shutdown(void)
30 if (--_ecore_con_local_init_count != 0)
31 return _ecore_con_local_init_count;
33 return _ecore_con_local_init_count;
38 _ecore_con_local_win32_server_read_client_handler(void *data, Ecore_Win32_Handler *wh)
43 Eina_Bool broken_pipe = EINA_FALSE;
45 cl = (Ecore_Con_Client *)data;
47 if (!ResetEvent(cl->host_server->event_read))
48 return ECORE_CALLBACK_RENEW;
50 buf = malloc(cl->host_server->nbr_bytes);
52 return ECORE_CALLBACK_RENEW;
54 if (ReadFile(cl->host_server->pipe, buf, cl->host_server->nbr_bytes, &n, NULL))
57 ecore_con_event_client_data(cl, buf, cl->host_server->nbr_bytes, EINA_FALSE);
58 cl->host_server->want_write = 1;
62 if (GetLastError() == ERROR_BROKEN_PIPE)
63 broken_pipe = EINA_TRUE;
71 msg = evil_last_error_get();
74 ecore_con_event_client_error(cl, msg);
78 _ecore_con_client_kill(cl);
79 return ECORE_CALLBACK_CANCEL;
82 if (cl->host_server->want_write)
83 ecore_con_local_win32_client_flush(cl);
85 ecore_main_win32_handler_del(wh);
87 return ECORE_CALLBACK_DONE;
91 _ecore_con_local_win32_server_peek_client_handler(void *data, Ecore_Win32_Handler *wh)
98 cl = (Ecore_Con_Client *)data;
100 if (!ResetEvent(cl->host_server->event_peek))
101 return ECORE_CALLBACK_RENEW;
104 msg = evil_last_error_get();
107 ecore_con_event_server_error(cl->host_server, msg);
111 _ecore_con_server_kill(cl->host_server);
112 return ECORE_CALLBACK_CANCEL;
114 ecore_main_win32_handler_del(wh);
116 return ECORE_CALLBACK_DONE;
120 _ecore_con_local_win32_client_peek_server_handler(void *data, Ecore_Win32_Handler *wh)
122 Ecore_Con_Server *svr;
127 svr = (Ecore_Con_Server *)data;
129 if (!ResetEvent(svr->event_peek))
130 return ECORE_CALLBACK_RENEW;
132 msg = evil_last_error_get();
135 ecore_con_event_server_error(svr, msg);
139 _ecore_con_server_kill(svr);
140 return ECORE_CALLBACK_CANCEL;
142 ecore_main_win32_handler_del(wh);
144 return ECORE_CALLBACK_DONE;
148 _ecore_con_local_win32_client_read_server_handler(void *data, Ecore_Win32_Handler *wh)
150 Ecore_Con_Server *svr;
153 Eina_Bool broken_pipe = EINA_FALSE;
155 svr = (Ecore_Con_Server *)data;
157 if (!ResetEvent(svr->event_read))
158 return ECORE_CALLBACK_RENEW;
160 buf = malloc(svr->nbr_bytes);
162 return ECORE_CALLBACK_RENEW;
164 if (ReadFile(svr->pipe, buf, svr->nbr_bytes, &n, NULL))
167 ecore_con_event_server_data(svr, buf, svr->nbr_bytes, EINA_FALSE);
172 if (GetLastError() == ERROR_BROKEN_PIPE)
173 broken_pipe = EINA_TRUE;
181 msg = evil_last_error_get();
184 ecore_con_event_server_error(svr, msg);
188 _ecore_con_server_kill(svr);
189 return ECORE_CALLBACK_CANCEL;
193 ecore_con_local_win32_server_flush(svr);
195 ecore_main_win32_handler_del(wh);
197 return ECORE_CALLBACK_DONE;
200 /* thread to read data sent by the server to the client */
201 static unsigned int __stdcall
202 _ecore_con_local_win32_client_read_server_thread(void *data)
204 Ecore_Con_Server *svr;
207 svr = (Ecore_Con_Server *)data;
209 svr->read_stopped = EINA_FALSE;
211 while (!svr->read_stop)
213 if (PeekNamedPipe(svr->pipe, NULL, 0, NULL, &nbr_bytes, NULL))
218 svr->nbr_bytes = nbr_bytes;
219 if (!SetEvent(svr->event_read))
224 if (GetLastError() == ERROR_BROKEN_PIPE)
226 if (!SetEvent(svr->event_peek))
233 printf(" ### %s\n", __FUNCTION__);
234 svr->read_stopped = EINA_TRUE;
239 /* thread to read data sent by the client to the server */
240 static unsigned int __stdcall
241 _ecore_con_local_win32_server_read_client_thread(void *data)
243 Ecore_Con_Client *cl;
246 cl = (Ecore_Con_Client *)data;
248 cl->host_server->read_stopped = EINA_FALSE;
250 while (!cl->host_server->read_stop)
252 if (PeekNamedPipe(cl->host_server->pipe, NULL, 0, NULL, &nbr_bytes, NULL))
257 cl->host_server->nbr_bytes = nbr_bytes;
258 if (!SetEvent(cl->host_server->event_read))
263 if (GetLastError() == ERROR_BROKEN_PIPE)
265 if (!SetEvent(cl->host_server->event_peek))
272 printf(" ### %s\n", __FUNCTION__);
273 cl->host_server->read_stopped = EINA_TRUE;
279 _ecore_con_local_win32_client_add(void *data, Ecore_Win32_Handler *wh)
281 Ecore_Con_Client *cl = NULL;
282 Ecore_Con_Server *svr;
283 Ecore_Win32_Handler *handler_read;
284 Ecore_Win32_Handler *handler_peek;
286 svr = (Ecore_Con_Server *)data;
289 return ECORE_CALLBACK_CANCEL;
292 return ECORE_CALLBACK_CANCEL;
294 if ((svr->client_limit >= 0) && (!svr->reject_excess_clients) &&
295 (svr->client_count >= (unsigned int)svr->client_limit))
296 return ECORE_CALLBACK_CANCEL;
298 cl = calloc(1, sizeof(Ecore_Con_Client));
301 ERR("allocation failed");
302 return ECORE_CALLBACK_CANCEL;
305 cl->host_server = svr;
306 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
308 cl->host_server->event_read = CreateEvent(NULL, TRUE, FALSE, NULL);
309 if (!cl->host_server->event_read)
311 ERR("Can not create event read");
315 handler_read = ecore_main_win32_handler_add(cl->host_server->event_read,
316 _ecore_con_local_win32_server_read_client_handler,
320 ERR("Can not create handler read");
321 goto close_event_read;
324 cl->host_server->event_peek = CreateEvent(NULL, TRUE, FALSE, NULL);
325 if (!cl->host_server->event_peek)
327 ERR("Can not create event peek");
328 goto del_handler_read;
331 handler_peek = ecore_main_win32_handler_add(cl->host_server->event_peek,
332 _ecore_con_local_win32_server_peek_client_handler,
336 ERR("Can not create handler peek");
337 goto close_event_peek;
340 cl->host_server->read_stopped = EINA_TRUE;
341 cl->host_server->thread_read = (HANDLE)_beginthreadex(NULL, 0, _ecore_con_local_win32_server_read_client_thread, cl, CREATE_SUSPENDED, NULL);
342 if (!cl->host_server->thread_read)
344 ERR("Can not launch thread");
345 goto del_handler_peek;
348 svr->clients = eina_list_append(svr->clients, cl);
352 ecore_con_event_client_add(cl);
354 ecore_main_win32_handler_del(wh);
356 ResumeThread(cl->host_server->thread_read);
357 return ECORE_CALLBACK_DONE;
360 ecore_main_win32_handler_del(handler_peek);
362 CloseHandle(cl->host_server->event_peek);
364 ecore_main_win32_handler_del(handler_read);
366 CloseHandle(cl->host_server->event_read);
370 return ECORE_CALLBACK_CANCEL;
373 static unsigned int __stdcall
374 _ecore_con_local_win32_listening(void *data)
376 Ecore_Con_Server *svr;
379 svr = (Ecore_Con_Server *)data;
383 res = ConnectNamedPipe(svr->pipe, NULL);
386 ERR("Opening the connection to the client failed");
387 CloseHandle(svr->pipe);
393 DBG("Client connected");
395 printf(" ### %s\n", __FUNCTION__);
401 ecore_con_local_listen(Ecore_Con_Server *svr)
404 HANDLE thread_listening;
405 Ecore_Win32_Handler *handler;
407 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
409 ERR("Your system does not support abstract sockets!");
413 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_USER)
414 snprintf(buf, sizeof(buf), "\\\\.\\pipe\\%s", svr->name);
415 else if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_SYSTEM)
417 const char *computername;
419 computername = getenv("CoMPUTERNAME");
420 snprintf(buf, sizeof(buf), "\\\\%s\\pipe\\%s", computername, svr->name);
423 svr->path = strdup(buf);
426 ERR("Allocation failed");
435 svr->pipe = CreateNamedPipe(svr->path,
437 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
438 PIPE_UNLIMITED_INSTANCES,
443 if (svr->pipe == INVALID_HANDLE_VALUE)
445 ERR("Creation of the named pipe failed");
450 * We use ConnectNamedPipe() to wait for a client to connect.
451 * As the function is blocking, to let the main loop continuing
452 * its iterations, we call ConnectNamedPipe() in a thread
454 thread_listening = (HANDLE)_beginthreadex(NULL, 0, _ecore_con_local_win32_listening, svr, CREATE_SUSPENDED, NULL);
455 if (!thread_listening)
457 ERR("Creation of the listening thread failed");
461 handler = ecore_main_win32_handler_add(thread_listening,
462 _ecore_con_local_win32_client_add,
466 ERR("Creation of the client add handler failed");
470 svr->read_stopped = EINA_TRUE;
471 ResumeThread(thread_listening);
476 ecore_main_win32_handler_del(handler);
478 CloseHandle(svr->pipe);
487 ecore_con_local_win32_server_del(Ecore_Con_Server *svr)
489 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
492 if (((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_USER) &&
493 ((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_SYSTEM))
497 while (!svr->read_stopped)
501 CloseHandle(svr->event_peek);
502 svr->event_peek = NULL;
504 CloseHandle(svr->event_read);
505 svr->event_read = NULL;
509 CloseHandle(svr->pipe);
514 ecore_con_local_win32_client_del(Ecore_Con_Client *cl)
516 if ((cl->host_server->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
519 if (((cl->host_server->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_USER) &&
520 ((cl->host_server->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_SYSTEM))
523 cl->host_server->read_stop = 1;
524 while (!cl->host_server->read_stopped)
527 if (cl->host_server->event_peek)
528 CloseHandle(cl->host_server->event_peek);
529 cl->host_server->event_peek = NULL;
530 if (cl->host_server->event_read)
531 CloseHandle(cl->host_server->event_read);
532 cl->host_server->event_read = NULL;
533 free(cl->host_server->path);
534 cl->host_server->path = NULL;
535 if (cl->host_server->pipe)
536 CloseHandle(cl->host_server->pipe);
537 cl->host_server->pipe = NULL;
541 ecore_con_local_connect(Ecore_Con_Server *svr,
542 Eina_Bool (*cb_done)(void *data,
543 Ecore_Fd_Handler *fd_handler))
546 Ecore_Win32_Handler *handler_read;
547 Ecore_Win32_Handler *handler_peek;
549 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
551 ERR("Your system does not support abstract sockets!");
555 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_USER)
556 snprintf(buf, sizeof(buf), "\\\\.\\pipe\\%s", svr->name);
557 else if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_SYSTEM)
559 const char *computername;
561 computername = getenv("COMPUTERNAME");
562 snprintf(buf, sizeof(buf), "\\\\%s\\pipe\\%s", computername, svr->name);
567 svr->pipe = CreateFile(buf,
568 GENERIC_READ | GENERIC_WRITE,
574 if (svr->pipe != INVALID_HANDLE_VALUE)
577 /* if pipe not busy, we exit */
578 if (GetLastError() != ERROR_PIPE_BUSY)
580 ERR("Connection to a server failed");
584 /* pipe busy, so we wait for it */
585 if (!WaitNamedPipe(buf, NMPWAIT_WAIT_FOREVER))
587 ERR("Can not wait for a server");
592 svr->path = strdup(buf);
595 ERR("Allocation failed");
599 svr->event_read = CreateEvent(NULL, TRUE, FALSE, NULL);
600 if (!svr->event_read)
602 ERR("Can not create event read");
606 handler_read = ecore_main_win32_handler_add(svr->event_read,
607 _ecore_con_local_win32_client_read_server_handler,
611 ERR("Can not create handler read");
612 goto close_event_read;
615 svr->event_peek = CreateEvent(NULL, TRUE, FALSE, NULL);
616 if (!svr->event_peek)
618 ERR("Can not create event peek");
619 goto del_handler_read;
622 handler_peek = ecore_main_win32_handler_add(svr->event_peek,
623 _ecore_con_local_win32_client_peek_server_handler,
627 ERR("Can not create handler peek");
628 goto close_event_peek;
631 svr->thread_read = (HANDLE)_beginthreadex(NULL, 0, _ecore_con_local_win32_client_read_server_thread, svr, CREATE_SUSPENDED, NULL);
632 if (!svr->thread_read)
634 ERR("Can not launch thread");
635 goto del_handler_peek;
638 if (!svr->delete_me) ecore_con_event_server_add(svr);
640 ResumeThread(svr->thread_read);
645 ecore_main_win32_handler_del(handler_peek);
647 CloseHandle(svr->event_peek);
649 ecore_main_win32_handler_del(handler_read);
651 CloseHandle(svr->event_read);
656 CloseHandle(svr->pipe);
662 ecore_con_local_win32_server_flush(Ecore_Con_Server *svr)
668 /* This check should never be true */
669 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
672 if (((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_USER) &&
673 ((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_SYSTEM))
676 num = eina_binbuf_length_get(svr->buf) - svr->write_buf_offset;
677 if (num <= 0) return EINA_TRUE;
679 res = WriteFile(svr->pipe, eina_binbuf_string_get(svr->buf) + svr->write_buf_offset, num, &written, NULL);
684 msg = evil_last_error_get();
687 ecore_con_event_server_error(svr, msg);
690 _ecore_con_server_kill(svr);
693 svr->write_buf_offset += written;
694 if (svr->write_buf_offset >= eina_binbuf_length_get(svr->buf))
696 svr->write_buf_offset = 0;
697 eina_binbuf_free(svr->buf);
701 else if (written < (DWORD)num)
708 ecore_con_local_win32_client_flush(Ecore_Con_Client *cl)
715 type = cl->host_server->type & ECORE_CON_TYPE;
717 /* This check should never be true */
718 if (type == ECORE_CON_LOCAL_ABSTRACT)
721 if ((type != ECORE_CON_LOCAL_USER) &&
722 (type != ECORE_CON_LOCAL_SYSTEM))
725 num = eina_binbuf_length_get(cl->buf) - cl->buf_offset;
726 if (num <= 0) return EINA_TRUE;
728 res = WriteFile(cl->host_server->pipe, eina_binbuf_string_get(cl->buf) + cl->buf_offset, num, &written, NULL);
733 msg = evil_last_error_get();
736 ecore_con_event_client_error(cl, msg);
739 _ecore_con_client_kill(cl);
742 cl->buf_offset += written;
743 if (cl->buf_offset >= eina_binbuf_length_get(cl->buf))
746 eina_binbuf_free(cl->buf);
748 cl->host_server->want_write = 0;
750 else if (written < (DWORD)num)
751 cl->host_server->want_write = 1;