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);
79 ecore_con_event_client_del(cl);
81 return ECORE_CALLBACK_CANCEL;
84 if (cl->host_server->want_write)
85 ecore_con_local_win32_client_flush(cl);
87 ecore_main_win32_handler_del(wh);
89 return ECORE_CALLBACK_DONE;
93 _ecore_con_local_win32_server_peek_client_handler(void *data, Ecore_Win32_Handler *wh)
100 cl = (Ecore_Con_Client *)data;
102 if (!ResetEvent(cl->host_server->event_peek))
103 return ECORE_CALLBACK_RENEW;
106 msg = evil_last_error_get();
109 ecore_con_event_server_error(cl->host_server, msg);
113 if (!cl->host_server->delete_me)
114 ecore_con_event_server_del(cl->host_server);
115 cl->host_server->dead = EINA_TRUE;
116 return ECORE_CALLBACK_CANCEL;
118 ecore_main_win32_handler_del(wh);
120 return ECORE_CALLBACK_DONE;
124 _ecore_con_local_win32_client_peek_server_handler(void *data, Ecore_Win32_Handler *wh)
126 Ecore_Con_Server *svr;
131 svr = (Ecore_Con_Server *)data;
133 if (!ResetEvent(svr->event_peek))
134 return ECORE_CALLBACK_RENEW;
136 msg = evil_last_error_get();
139 ecore_con_event_server_error(svr, msg);
144 ecore_con_event_server_del(svr);
145 svr->dead = EINA_TRUE;
146 return ECORE_CALLBACK_CANCEL;
148 ecore_main_win32_handler_del(wh);
150 return ECORE_CALLBACK_DONE;
154 _ecore_con_local_win32_client_read_server_handler(void *data, Ecore_Win32_Handler *wh)
156 Ecore_Con_Server *svr;
159 Eina_Bool broken_pipe = EINA_FALSE;
161 svr = (Ecore_Con_Server *)data;
163 if (!ResetEvent(svr->event_read))
164 return ECORE_CALLBACK_RENEW;
166 buf = malloc(svr->nbr_bytes);
168 return ECORE_CALLBACK_RENEW;
170 if (ReadFile(svr->pipe, buf, svr->nbr_bytes, &n, NULL))
173 ecore_con_event_server_data(svr, buf, svr->nbr_bytes, EINA_FALSE);
178 if (GetLastError() == ERROR_BROKEN_PIPE)
179 broken_pipe = EINA_TRUE;
187 msg = evil_last_error_get();
190 ecore_con_event_server_error(svr, msg);
195 ecore_con_event_server_del(svr);
196 svr->dead = EINA_TRUE;
197 return ECORE_CALLBACK_CANCEL;
201 ecore_con_local_win32_server_flush(svr);
203 ecore_main_win32_handler_del(wh);
205 return ECORE_CALLBACK_DONE;
208 /* thread to read data sent by the server to the client */
209 static unsigned int __stdcall
210 _ecore_con_local_win32_client_read_server_thread(void *data)
212 Ecore_Con_Server *svr;
215 svr = (Ecore_Con_Server *)data;
217 svr->read_stopped = EINA_FALSE;
219 while (!svr->read_stop)
221 if (PeekNamedPipe(svr->pipe, NULL, 0, NULL, &nbr_bytes, NULL))
226 svr->nbr_bytes = nbr_bytes;
227 if (!SetEvent(svr->event_read))
232 if (GetLastError() == ERROR_BROKEN_PIPE)
234 if (!SetEvent(svr->event_peek))
241 printf(" ### %s\n", __FUNCTION__);
242 svr->read_stopped = EINA_TRUE;
247 /* thread to read data sent by the client to the server */
248 static unsigned int __stdcall
249 _ecore_con_local_win32_server_read_client_thread(void *data)
251 Ecore_Con_Client *cl;
254 cl = (Ecore_Con_Client *)data;
256 cl->host_server->read_stopped = EINA_FALSE;
258 while (!cl->host_server->read_stop)
260 if (PeekNamedPipe(cl->host_server->pipe, NULL, 0, NULL, &nbr_bytes, NULL))
265 cl->host_server->nbr_bytes = nbr_bytes;
266 if (!SetEvent(cl->host_server->event_read))
271 if (GetLastError() == ERROR_BROKEN_PIPE)
273 if (!SetEvent(cl->host_server->event_peek))
280 printf(" ### %s\n", __FUNCTION__);
281 cl->host_server->read_stopped = EINA_TRUE;
287 _ecore_con_local_win32_client_add(void *data, Ecore_Win32_Handler *wh)
289 Ecore_Con_Client *cl = NULL;
290 Ecore_Con_Server *svr;
291 Ecore_Win32_Handler *handler_read;
292 Ecore_Win32_Handler *handler_peek;
294 svr = (Ecore_Con_Server *)data;
297 return ECORE_CALLBACK_CANCEL;
300 return ECORE_CALLBACK_CANCEL;
303 return ECORE_CALLBACK_CANCEL;
305 if ((svr->client_limit >= 0) && (!svr->reject_excess_clients) &&
306 (svr->client_count >= (unsigned int)svr->client_limit))
307 return ECORE_CALLBACK_CANCEL;
309 cl = calloc(1, sizeof(Ecore_Con_Client));
312 ERR("allocation failed");
313 return ECORE_CALLBACK_CANCEL;
316 cl->host_server = svr;
317 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
319 cl->host_server->event_read = CreateEvent(NULL, TRUE, FALSE, NULL);
320 if (!cl->host_server->event_read)
322 ERR("Can not create event read");
326 handler_read = ecore_main_win32_handler_add(cl->host_server->event_read,
327 _ecore_con_local_win32_server_read_client_handler,
331 ERR("Can not create handler read");
332 goto close_event_read;
335 cl->host_server->event_peek = CreateEvent(NULL, TRUE, FALSE, NULL);
336 if (!cl->host_server->event_peek)
338 ERR("Can not create event peek");
339 goto del_handler_read;
342 handler_peek = ecore_main_win32_handler_add(cl->host_server->event_peek,
343 _ecore_con_local_win32_server_peek_client_handler,
347 ERR("Can not create handler peek");
348 goto close_event_peek;
351 cl->host_server->read_stopped = EINA_TRUE;
352 cl->host_server->thread_read = (HANDLE)_beginthreadex(NULL, 0, _ecore_con_local_win32_server_read_client_thread, cl, CREATE_SUSPENDED, NULL);
353 if (!cl->host_server->thread_read)
355 ERR("Can not launch thread");
356 goto del_handler_peek;
359 svr->clients = eina_list_append(svr->clients, cl);
363 ecore_con_event_client_add(cl);
365 ecore_main_win32_handler_del(wh);
367 ResumeThread(cl->host_server->thread_read);
368 return ECORE_CALLBACK_DONE;
371 ecore_main_win32_handler_del(handler_peek);
373 CloseHandle(cl->host_server->event_peek);
375 ecore_main_win32_handler_del(handler_read);
377 CloseHandle(cl->host_server->event_read);
381 return ECORE_CALLBACK_CANCEL;
384 static unsigned int __stdcall
385 _ecore_con_local_win32_listening(void *data)
387 Ecore_Con_Server *svr;
390 svr = (Ecore_Con_Server *)data;
394 res = ConnectNamedPipe(svr->pipe, NULL);
397 ERR("Opening the connection to the client failed");
398 CloseHandle(svr->pipe);
404 DBG("Client connected");
406 printf(" ### %s\n", __FUNCTION__);
412 ecore_con_local_listen(Ecore_Con_Server *svr)
415 HANDLE thread_listening;
416 Ecore_Win32_Handler *handler;
418 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
420 ERR("Your system does not support abstract sockets!");
424 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_USER)
425 snprintf(buf, sizeof(buf), "\\\\.\\pipe\\%s", svr->name);
426 else if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_SYSTEM)
428 const char *computername;
430 computername = getenv("CoMPUTERNAME");
431 snprintf(buf, sizeof(buf), "\\\\%s\\pipe\\%s", computername, svr->name);
434 svr->path = strdup(buf);
437 ERR("Allocation failed");
446 svr->pipe = CreateNamedPipe(svr->path,
448 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
449 PIPE_UNLIMITED_INSTANCES,
454 if (svr->pipe == INVALID_HANDLE_VALUE)
456 ERR("Creation of the named pipe failed");
461 * We use ConnectNamedPipe() to wait for a client to connect.
462 * As the function is blocking, to let the main loop continuing
463 * its iterations, we call ConnectNamedPipe() in a thread
465 thread_listening = (HANDLE)_beginthreadex(NULL, 0, _ecore_con_local_win32_listening, svr, CREATE_SUSPENDED, NULL);
466 if (!thread_listening)
468 ERR("Creation of the listening thread failed");
472 handler = ecore_main_win32_handler_add(thread_listening,
473 _ecore_con_local_win32_client_add,
477 ERR("Creation of the client add handler failed");
481 svr->read_stopped = EINA_TRUE;
482 ResumeThread(thread_listening);
487 ecore_main_win32_handler_del(handler);
489 CloseHandle(svr->pipe);
498 ecore_con_local_win32_server_del(Ecore_Con_Server *svr)
500 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
503 if (((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_USER) &&
504 ((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_SYSTEM))
508 while (!svr->read_stopped)
512 CloseHandle(svr->event_peek);
513 svr->event_peek = NULL;
515 CloseHandle(svr->event_read);
516 svr->event_read = NULL;
520 CloseHandle(svr->pipe);
525 ecore_con_local_win32_client_del(Ecore_Con_Client *cl)
527 if ((cl->host_server->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
530 if (((cl->host_server->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_USER) &&
531 ((cl->host_server->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_SYSTEM))
534 cl->host_server->read_stop = 1;
535 while (!cl->host_server->read_stopped)
538 if (cl->host_server->event_peek)
539 CloseHandle(cl->host_server->event_peek);
540 cl->host_server->event_peek = NULL;
541 if (cl->host_server->event_read)
542 CloseHandle(cl->host_server->event_read);
543 cl->host_server->event_read = NULL;
544 free(cl->host_server->path);
545 cl->host_server->path = NULL;
546 if (cl->host_server->pipe)
547 CloseHandle(cl->host_server->pipe);
548 cl->host_server->pipe = NULL;
552 ecore_con_local_connect(Ecore_Con_Server *svr,
553 Eina_Bool (*cb_done)(void *data,
554 Ecore_Fd_Handler *fd_handler),
555 void (*cb_free)(void *data, void *ev))
558 Ecore_Win32_Handler *handler_read;
559 Ecore_Win32_Handler *handler_peek;
561 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
563 ERR("Your system does not support abstract sockets!");
567 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_USER)
568 snprintf(buf, sizeof(buf), "\\\\.\\pipe\\%s", svr->name);
569 else if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_SYSTEM)
571 const char *computername;
573 computername = getenv("COMPUTERNAME");
574 snprintf(buf, sizeof(buf), "\\\\%s\\pipe\\%s", computername, svr->name);
579 svr->pipe = CreateFile(buf,
580 GENERIC_READ | GENERIC_WRITE,
586 if (svr->pipe != INVALID_HANDLE_VALUE)
589 /* if pipe not busy, we exit */
590 if (GetLastError() != ERROR_PIPE_BUSY)
592 ERR("Connection to a server failed");
596 /* pipe busy, so we wait for it */
597 if (!WaitNamedPipe(buf, NMPWAIT_WAIT_FOREVER))
599 ERR("Can not wait for a server");
604 svr->path = strdup(buf);
607 ERR("Allocation failed");
611 svr->event_read = CreateEvent(NULL, TRUE, FALSE, NULL);
612 if (!svr->event_read)
614 ERR("Can not create event read");
618 handler_read = ecore_main_win32_handler_add(svr->event_read,
619 _ecore_con_local_win32_client_read_server_handler,
623 ERR("Can not create handler read");
624 goto close_event_read;
627 svr->event_peek = CreateEvent(NULL, TRUE, FALSE, NULL);
628 if (!svr->event_peek)
630 ERR("Can not create event peek");
631 goto del_handler_read;
634 handler_peek = ecore_main_win32_handler_add(svr->event_peek,
635 _ecore_con_local_win32_client_peek_server_handler,
639 ERR("Can not create handler peek");
640 goto close_event_peek;
643 svr->thread_read = (HANDLE)_beginthreadex(NULL, 0, _ecore_con_local_win32_client_read_server_thread, svr, CREATE_SUSPENDED, NULL);
644 if (!svr->thread_read)
646 ERR("Can not launch thread");
647 goto del_handler_peek;
652 Ecore_Con_Event_Server_Add *e;
654 e = calloc(1, sizeof(Ecore_Con_Event_Server_Add));
659 ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e,
664 ResumeThread(svr->thread_read);
669 ecore_main_win32_handler_del(handler_peek);
671 CloseHandle(svr->event_peek);
673 ecore_main_win32_handler_del(handler_read);
675 CloseHandle(svr->event_read);
680 CloseHandle(svr->pipe);
686 ecore_con_local_win32_server_flush(Ecore_Con_Server *svr)
692 /* This check should never be true */
693 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
696 if (((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_USER) &&
697 ((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_SYSTEM))
700 num = svr->write_buf_size - svr->write_buf_offset;
701 if (num <= 0) return EINA_TRUE;
703 res = WriteFile(svr->pipe, svr->write_buf + svr->write_buf_offset, num, &written, NULL);
708 msg = evil_last_error_get();
711 ecore_con_event_server_error(svr, msg);
715 ecore_con_event_server_del(svr);
716 svr->dead = EINA_TRUE;
719 svr->write_buf_offset += written;
720 if (svr->write_buf_offset >= svr->write_buf_size)
722 svr->write_buf_size = 0;
723 svr->write_buf_offset = 0;
724 free(svr->write_buf);
725 svr->write_buf = NULL;
728 else if (written < (DWORD)num)
735 ecore_con_local_win32_client_flush(Ecore_Con_Client *cl)
742 type = cl->host_server->type & ECORE_CON_TYPE;
744 /* This check should never be true */
745 if (type == ECORE_CON_LOCAL_ABSTRACT)
748 if ((type != ECORE_CON_LOCAL_USER) &&
749 (type != ECORE_CON_LOCAL_SYSTEM))
752 num = cl->buf_size - cl->buf_offset;
753 if (num <= 0) return EINA_TRUE;
755 res = WriteFile(cl->host_server->pipe, cl->buf + cl->buf_offset, num, &written, NULL);
760 msg = evil_last_error_get();
763 ecore_con_event_client_error(cl, msg);
767 ecore_con_event_client_del(cl);
768 cl->dead = EINA_TRUE;
771 cl->buf_offset += written;
772 if (cl->buf_offset >= cl->buf_size)
778 cl->host_server->want_write = 0;
780 else if (written < (DWORD)num)
781 cl->host_server->want_write = 1;