2011-05-14 10:52:30 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <process.h>
|
|
|
|
|
|
|
|
#include <Evil.h>
|
|
|
|
#include <Ecore.h>
|
|
|
|
|
|
|
|
#include "Ecore_Con.h"
|
|
|
|
#include "ecore_con_private.h"
|
|
|
|
|
|
|
|
#define BUFSIZE 512
|
|
|
|
|
|
|
|
static int _ecore_con_local_init_count = 0;
|
|
|
|
|
|
|
|
int
|
|
|
|
ecore_con_local_init(void)
|
|
|
|
{
|
|
|
|
if (++_ecore_con_local_init_count != 1)
|
|
|
|
return _ecore_con_local_init_count;
|
|
|
|
|
|
|
|
return _ecore_con_local_init_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ecore_con_local_shutdown(void)
|
|
|
|
{
|
|
|
|
if (--_ecore_con_local_init_count != 0)
|
|
|
|
return _ecore_con_local_init_count;
|
|
|
|
|
|
|
|
return _ecore_con_local_init_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Bool
|
|
|
|
_ecore_con_local_win32_server_read_client_handler(void *data, Ecore_Win32_Handler *wh)
|
|
|
|
{
|
2014-08-26 06:53:23 -07:00
|
|
|
Ecore_Con_Client *obj = data;
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Client_Data *cl = efl_data_scope_get(obj, EFL_NETWORK_CLIENT_CLASS);
|
2013-12-19 21:07:50 -08:00
|
|
|
void *buf;
|
|
|
|
DWORD n;
|
|
|
|
Eina_Bool broken_pipe = EINA_FALSE;
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Server_Data *host_svr = efl_data_scope_get(cl->host_server, EFL_NETWORK_SERVER_CLASS);
|
2013-12-19 21:07:50 -08:00
|
|
|
|
2014-08-27 07:46:31 -07:00
|
|
|
if (!ResetEvent(host_svr->event_read))
|
2013-12-19 21:07:50 -08:00
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
|
2014-08-27 07:46:31 -07:00
|
|
|
buf = malloc(host_svr->nbr_bytes);
|
2013-12-19 21:07:50 -08:00
|
|
|
if (!buf)
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
|
2014-08-27 07:46:31 -07:00
|
|
|
if (ReadFile(host_svr->pipe, buf, host_svr->nbr_bytes, &n, NULL))
|
2013-12-19 21:07:50 -08:00
|
|
|
{
|
|
|
|
if (!cl->delete_me)
|
2014-08-27 07:46:31 -07:00
|
|
|
ecore_con_event_client_data(obj, buf, host_svr->nbr_bytes, EINA_FALSE);
|
|
|
|
host_svr->want_write = 1;
|
2013-12-19 21:07:50 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (GetLastError() == ERROR_BROKEN_PIPE)
|
|
|
|
broken_pipe = EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (broken_pipe)
|
|
|
|
{
|
2011-05-14 10:52:30 -07:00
|
|
|
#if 0
|
2017-12-10 22:06:02 -08:00
|
|
|
ecore_con_event_client_error(cl, evil_last_error_get());
|
2011-05-14 10:52:30 -07:00
|
|
|
#endif
|
2014-08-27 07:46:31 -07:00
|
|
|
_ecore_con_client_kill(obj);
|
2013-12-19 21:07:50 -08:00
|
|
|
return ECORE_CALLBACK_CANCEL;
|
|
|
|
}
|
2011-05-14 10:52:30 -07:00
|
|
|
|
2014-08-27 07:46:31 -07:00
|
|
|
if (host_svr->want_write)
|
|
|
|
ecore_con_local_win32_client_flush(obj);
|
2011-05-14 10:52:30 -07:00
|
|
|
|
|
|
|
ecore_main_win32_handler_del(wh);
|
|
|
|
|
2013-12-19 21:07:50 -08:00
|
|
|
return ECORE_CALLBACK_DONE;
|
2011-05-14 10:52:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Bool
|
|
|
|
_ecore_con_local_win32_server_peek_client_handler(void *data, Ecore_Win32_Handler *wh)
|
|
|
|
{
|
2014-08-26 06:53:23 -07:00
|
|
|
Ecore_Con_Client *obj = data;
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Client_Data *cl = efl_data_scope_get(obj, EFL_NETWORK_CLIENT_CLASS);
|
|
|
|
Efl_Network_Server_Data *host_svr = efl_data_scope_get(cl->host_server, EFL_NETWORK_SERVER_CLASS);
|
2011-05-14 10:52:30 -07:00
|
|
|
#if 0
|
|
|
|
char *msg;
|
|
|
|
#endif
|
|
|
|
|
2014-08-27 07:46:31 -07:00
|
|
|
if (!ResetEvent(host_svr->event_peek))
|
2011-05-14 10:52:30 -07:00
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
|
|
|
|
#if 0
|
2017-12-10 22:06:02 -08:00
|
|
|
ecore_con_event_server_error(host_svr, evil_last_error_get());
|
2011-05-14 10:52:30 -07:00
|
|
|
#endif
|
2012-01-19 00:02:41 -08:00
|
|
|
_ecore_con_server_kill(cl->host_server);
|
2011-05-14 10:52:30 -07:00
|
|
|
return ECORE_CALLBACK_CANCEL;
|
|
|
|
|
|
|
|
ecore_main_win32_handler_del(wh);
|
|
|
|
|
|
|
|
return ECORE_CALLBACK_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Bool
|
|
|
|
_ecore_con_local_win32_client_peek_server_handler(void *data, Ecore_Win32_Handler *wh)
|
|
|
|
{
|
2014-08-27 07:46:31 -07:00
|
|
|
Ecore_Con_Server *obj = data;
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Server_Data *svr = efl_data_scope_get(obj, EFL_NETWORK_SERVER_CLASS);
|
2011-05-14 10:52:30 -07:00
|
|
|
#if 0
|
|
|
|
char *msg;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!ResetEvent(svr->event_peek))
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
#if 0
|
2017-12-10 22:06:02 -08:00
|
|
|
ecore_con_event_server_error(svr, evil_last_error_get());
|
2011-05-14 10:52:30 -07:00
|
|
|
#endif
|
2014-08-26 06:53:23 -07:00
|
|
|
_ecore_con_server_kill(obj);
|
2011-05-14 10:52:30 -07:00
|
|
|
return ECORE_CALLBACK_CANCEL;
|
|
|
|
|
|
|
|
ecore_main_win32_handler_del(wh);
|
|
|
|
|
|
|
|
return ECORE_CALLBACK_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Bool
|
|
|
|
_ecore_con_local_win32_client_read_server_handler(void *data, Ecore_Win32_Handler *wh)
|
|
|
|
{
|
2014-08-27 07:46:31 -07:00
|
|
|
Ecore_Con_Server *obj = data;
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Server_Data *svr = efl_data_scope_get(obj, EFL_NETWORK_SERVER_CLASS);
|
2013-12-19 21:07:50 -08:00
|
|
|
void *buf;
|
|
|
|
DWORD n;
|
|
|
|
Eina_Bool broken_pipe = EINA_FALSE;
|
|
|
|
|
|
|
|
if (!ResetEvent(svr->event_read))
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
|
|
|
|
buf = malloc(svr->nbr_bytes);
|
|
|
|
if (!buf)
|
|
|
|
return ECORE_CALLBACK_RENEW;
|
|
|
|
|
|
|
|
if (ReadFile(svr->pipe, buf, svr->nbr_bytes, &n, NULL))
|
|
|
|
{
|
|
|
|
if (!svr->delete_me)
|
2014-08-26 06:53:23 -07:00
|
|
|
ecore_con_event_server_data(obj, buf, svr->nbr_bytes, EINA_FALSE);
|
2013-12-19 21:07:50 -08:00
|
|
|
svr->want_write = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (GetLastError() == ERROR_BROKEN_PIPE)
|
|
|
|
broken_pipe = EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (broken_pipe)
|
|
|
|
{
|
2011-05-14 10:52:30 -07:00
|
|
|
#if 0
|
2017-12-10 22:06:02 -08:00
|
|
|
ecore_con_event_server_error(svr, evil_last_error_get());
|
2011-05-14 10:52:30 -07:00
|
|
|
#endif
|
2014-08-26 06:53:23 -07:00
|
|
|
_ecore_con_server_kill(obj);
|
2013-12-19 21:07:50 -08:00
|
|
|
return ECORE_CALLBACK_CANCEL;
|
|
|
|
}
|
2011-05-14 10:52:30 -07:00
|
|
|
|
2013-12-19 21:07:50 -08:00
|
|
|
if (svr->want_write)
|
2014-08-26 06:53:23 -07:00
|
|
|
ecore_con_local_win32_server_flush(obj);
|
2011-05-14 10:52:30 -07:00
|
|
|
|
|
|
|
ecore_main_win32_handler_del(wh);
|
|
|
|
|
2013-12-19 21:07:50 -08:00
|
|
|
return ECORE_CALLBACK_DONE;
|
2011-05-14 10:52:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* thread to read data sent by the server to the client */
|
|
|
|
static unsigned int __stdcall
|
|
|
|
_ecore_con_local_win32_client_read_server_thread(void *data)
|
|
|
|
{
|
2014-08-27 07:46:31 -07:00
|
|
|
Ecore_Con_Server *obj = data;
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Server_Data *svr = efl_data_scope_get(obj, EFL_NETWORK_SERVER_CLASS);
|
2011-05-14 10:52:30 -07:00
|
|
|
DWORD nbr_bytes = 0;
|
|
|
|
|
|
|
|
svr->read_stopped = EINA_FALSE;
|
|
|
|
|
|
|
|
while (!svr->read_stop)
|
|
|
|
{
|
|
|
|
if (PeekNamedPipe(svr->pipe, NULL, 0, NULL, &nbr_bytes, NULL))
|
|
|
|
{
|
|
|
|
if (nbr_bytes <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
svr->nbr_bytes = nbr_bytes;
|
|
|
|
if (!SetEvent(svr->event_read))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (GetLastError() == ERROR_BROKEN_PIPE)
|
|
|
|
{
|
|
|
|
if (!SetEvent(svr->event_peek))
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
svr->read_stopped = EINA_TRUE;
|
|
|
|
_endthreadex(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* thread to read data sent by the client to the server */
|
|
|
|
static unsigned int __stdcall
|
|
|
|
_ecore_con_local_win32_server_read_client_thread(void *data)
|
|
|
|
{
|
2014-08-26 06:53:23 -07:00
|
|
|
Ecore_Con_Client *obj = data;
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Client_Data *cl = efl_data_scope_get(obj, EFL_NETWORK_CLIENT_CLASS);
|
|
|
|
Efl_Network_Server_Data *host_svr = efl_data_scope_get(cl->host_server, EFL_NETWORK_SERVER_CLASS);
|
2011-05-14 10:52:30 -07:00
|
|
|
DWORD nbr_bytes = 0;
|
|
|
|
|
2014-08-27 07:46:31 -07:00
|
|
|
host_svr->read_stopped = EINA_FALSE;
|
2011-05-14 10:52:30 -07:00
|
|
|
|
2014-08-27 07:46:31 -07:00
|
|
|
while (!host_svr->read_stop)
|
2011-05-14 10:52:30 -07:00
|
|
|
{
|
2014-08-27 07:46:31 -07:00
|
|
|
if (PeekNamedPipe(host_svr->pipe, NULL, 0, NULL, &nbr_bytes, NULL))
|
2011-05-14 10:52:30 -07:00
|
|
|
{
|
|
|
|
if (nbr_bytes <= 0)
|
|
|
|
continue;
|
|
|
|
|
2014-08-27 07:46:31 -07:00
|
|
|
host_svr->nbr_bytes = nbr_bytes;
|
|
|
|
if (!SetEvent(host_svr->event_read))
|
2011-05-14 10:52:30 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (GetLastError() == ERROR_BROKEN_PIPE)
|
|
|
|
{
|
2014-08-27 07:46:31 -07:00
|
|
|
if (!SetEvent(host_svr->event_peek))
|
2011-05-14 10:52:30 -07:00
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-27 07:46:31 -07:00
|
|
|
host_svr->read_stopped = EINA_TRUE;
|
2011-05-14 10:52:30 -07:00
|
|
|
_endthreadex(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Bool
|
|
|
|
_ecore_con_local_win32_client_add(void *data, Ecore_Win32_Handler *wh)
|
|
|
|
{
|
2014-08-26 06:53:23 -07:00
|
|
|
Ecore_Con_Server *obj = data;
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Server_Data *svr = efl_data_scope_get(obj, EFL_NETWORK_SERVER_CLASS);
|
2011-05-14 10:52:30 -07:00
|
|
|
Ecore_Win32_Handler *handler_read;
|
|
|
|
Ecore_Win32_Handler *handler_peek;
|
|
|
|
|
|
|
|
if (!svr->pipe)
|
|
|
|
return ECORE_CALLBACK_CANCEL;
|
|
|
|
|
|
|
|
if (svr->delete_me)
|
|
|
|
return ECORE_CALLBACK_CANCEL;
|
|
|
|
|
|
|
|
if ((svr->client_limit >= 0) && (!svr->reject_excess_clients) &&
|
|
|
|
(svr->client_count >= (unsigned int)svr->client_limit))
|
|
|
|
return ECORE_CALLBACK_CANCEL;
|
|
|
|
|
2018-03-28 08:42:10 -07:00
|
|
|
Ecore_Con_Client *cl_obj = efl_add(EFL_NETWORK_CLIENT_CLASS, efl_main_loop_get());
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Client_Data *cl = efl_data_scope_get(obj, EFL_NETWORK_CLIENT_CLASS);
|
2011-05-14 10:52:30 -07:00
|
|
|
if (!cl)
|
|
|
|
{
|
|
|
|
ERR("allocation failed");
|
|
|
|
return ECORE_CALLBACK_CANCEL;
|
|
|
|
}
|
|
|
|
|
2014-08-26 06:53:23 -07:00
|
|
|
cl->host_server = obj;
|
2011-05-14 10:52:30 -07:00
|
|
|
|
2014-08-26 06:53:23 -07:00
|
|
|
svr->event_read = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!svr->event_read)
|
2011-05-14 10:52:30 -07:00
|
|
|
{
|
|
|
|
ERR("Can not create event read");
|
|
|
|
goto free_cl;
|
|
|
|
}
|
|
|
|
|
2014-08-26 06:53:23 -07:00
|
|
|
handler_read = ecore_main_win32_handler_add(svr->event_read,
|
2011-05-14 10:52:30 -07:00
|
|
|
_ecore_con_local_win32_server_read_client_handler,
|
2014-08-26 06:53:23 -07:00
|
|
|
obj);
|
2011-05-14 10:52:30 -07:00
|
|
|
if (!handler_read)
|
|
|
|
{
|
|
|
|
ERR("Can not create handler read");
|
|
|
|
goto close_event_read;
|
|
|
|
}
|
|
|
|
|
2014-08-26 06:53:23 -07:00
|
|
|
svr->event_peek = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!svr->event_peek)
|
2011-05-14 10:52:30 -07:00
|
|
|
{
|
|
|
|
ERR("Can not create event peek");
|
|
|
|
goto del_handler_read;
|
|
|
|
}
|
|
|
|
|
2014-08-26 06:53:23 -07:00
|
|
|
handler_peek = ecore_main_win32_handler_add(svr->event_peek,
|
2011-05-14 10:52:30 -07:00
|
|
|
_ecore_con_local_win32_server_peek_client_handler,
|
2014-08-26 06:53:23 -07:00
|
|
|
obj);
|
2011-05-14 10:52:30 -07:00
|
|
|
if (!handler_peek)
|
|
|
|
{
|
|
|
|
ERR("Can not create handler peek");
|
|
|
|
goto close_event_peek;
|
|
|
|
}
|
|
|
|
|
2014-08-26 06:53:23 -07:00
|
|
|
svr->read_stopped = EINA_TRUE;
|
|
|
|
svr->thread_read = (HANDLE)_beginthreadex(NULL, 0, _ecore_con_local_win32_server_read_client_thread, cl, CREATE_SUSPENDED, NULL);
|
|
|
|
if (!svr->thread_read)
|
2011-05-14 10:52:30 -07:00
|
|
|
{
|
|
|
|
ERR("Can not launch thread");
|
|
|
|
goto del_handler_peek;
|
|
|
|
}
|
|
|
|
|
2014-08-26 06:53:23 -07:00
|
|
|
svr->clients = eina_list_append(svr->clients, obj);
|
2011-05-14 10:52:30 -07:00
|
|
|
svr->client_count++;
|
|
|
|
|
|
|
|
if (!cl->delete_me)
|
2014-08-26 06:53:23 -07:00
|
|
|
ecore_con_event_client_add(obj);
|
2011-05-14 10:52:30 -07:00
|
|
|
|
|
|
|
ecore_main_win32_handler_del(wh);
|
|
|
|
|
2014-08-26 06:53:23 -07:00
|
|
|
ResumeThread(svr->thread_read);
|
2011-05-14 10:52:30 -07:00
|
|
|
return ECORE_CALLBACK_DONE;
|
|
|
|
|
2013-12-19 21:07:50 -08:00
|
|
|
del_handler_peek:
|
2011-05-14 10:52:30 -07:00
|
|
|
ecore_main_win32_handler_del(handler_peek);
|
2013-12-19 21:07:50 -08:00
|
|
|
close_event_peek:
|
2014-08-26 06:53:23 -07:00
|
|
|
CloseHandle(svr->event_peek);
|
2013-12-19 21:07:50 -08:00
|
|
|
del_handler_read:
|
2011-05-14 10:52:30 -07:00
|
|
|
ecore_main_win32_handler_del(handler_read);
|
2013-12-19 21:07:50 -08:00
|
|
|
close_event_read:
|
2014-08-27 07:46:31 -07:00
|
|
|
CloseHandle(svr->event_read);
|
2013-12-19 21:07:50 -08:00
|
|
|
free_cl:
|
2018-03-28 08:42:10 -07:00
|
|
|
efl_del(cl_obj);
|
2011-05-14 10:52:30 -07:00
|
|
|
|
|
|
|
return ECORE_CALLBACK_CANCEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int __stdcall
|
|
|
|
_ecore_con_local_win32_listening(void *data)
|
|
|
|
{
|
2014-08-26 06:53:23 -07:00
|
|
|
Ecore_Con_Server *obj = data;
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Server_Data *svr = efl_data_scope_get(obj, EFL_NETWORK_SERVER_CLASS);
|
2011-05-14 10:52:30 -07:00
|
|
|
BOOL res;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
res = ConnectNamedPipe(svr->pipe, NULL);
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
ERR("Opening the connection to the client failed");
|
|
|
|
CloseHandle(svr->pipe);
|
|
|
|
svr->pipe = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG("Client connected");
|
|
|
|
|
|
|
|
_endthreadex(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:10:31 -08:00
|
|
|
EAPI char *
|
|
|
|
ecore_con_local_path_new(Eina_Bool is_system, const char *name, int port)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
if (!is_system)
|
|
|
|
snprintf(buf, sizeof(buf), "\\\\.\\pipe\\%s%ld", name, GetProcessId(GetCurrentProcess()));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *computername;
|
|
|
|
|
|
|
|
computername = getenv("COMPUTERNAME");
|
|
|
|
snprintf(buf, sizeof(buf), "\\\\%s\\pipe\\%s%ld", computername, name, GetProcessId(GetCurrentProcess()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return strdup(buf);
|
|
|
|
(void)port; // CHECK-ME: shouldn't we use port to be similar to UNIX?
|
|
|
|
}
|
|
|
|
|
2011-05-14 10:52:30 -07:00
|
|
|
Eina_Bool
|
2014-08-26 06:53:23 -07:00
|
|
|
ecore_con_local_listen(Ecore_Con_Server *obj)
|
2011-05-14 10:52:30 -07:00
|
|
|
{
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Server_Data *svr = efl_data_scope_get(obj, EFL_NETWORK_SERVER_CLASS);
|
2011-05-14 10:52:30 -07:00
|
|
|
HANDLE thread_listening;
|
|
|
|
Ecore_Win32_Handler *handler;
|
|
|
|
|
|
|
|
if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
|
|
|
|
{
|
|
|
|
ERR("Your system does not support abstract sockets!");
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_USER)
|
2016-11-23 06:10:31 -08:00
|
|
|
svr->path = ecore_con_local_path_new(EINA_FALSE, svr->name, svr->port);
|
2011-05-14 10:52:30 -07:00
|
|
|
else if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_SYSTEM)
|
2016-11-23 06:10:31 -08:00
|
|
|
svr->path = ecore_con_local_path_new(EINA_TRUE, svr->name, svr->port);
|
2011-05-14 10:52:30 -07:00
|
|
|
|
|
|
|
if (!svr->path)
|
|
|
|
{
|
|
|
|
ERR("Allocation failed");
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* synchronuous
|
|
|
|
* block mode
|
|
|
|
* wait mode
|
|
|
|
*/
|
|
|
|
svr->pipe = CreateNamedPipe(svr->path,
|
|
|
|
PIPE_ACCESS_DUPLEX,
|
|
|
|
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
|
|
|
|
PIPE_UNLIMITED_INSTANCES,
|
|
|
|
BUFSIZE,
|
|
|
|
BUFSIZE,
|
|
|
|
5000,
|
|
|
|
NULL);
|
|
|
|
if (svr->pipe == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2016-06-13 01:31:51 -07:00
|
|
|
DBG("Creation of the named pipe '%s' failed", svr->path);
|
2011-05-14 10:52:30 -07:00
|
|
|
goto free_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We use ConnectNamedPipe() to wait for a client to connect.
|
|
|
|
* As the function is blocking, to let the main loop continuing
|
|
|
|
* its iterations, we call ConnectNamedPipe() in a thread
|
|
|
|
*/
|
2014-08-26 06:53:23 -07:00
|
|
|
thread_listening = (HANDLE)_beginthreadex(NULL, 0, _ecore_con_local_win32_listening, obj, CREATE_SUSPENDED, NULL);
|
2011-05-14 10:52:30 -07:00
|
|
|
if (!thread_listening)
|
|
|
|
{
|
|
|
|
ERR("Creation of the listening thread failed");
|
|
|
|
goto close_pipe;
|
|
|
|
}
|
|
|
|
|
|
|
|
handler = ecore_main_win32_handler_add(thread_listening,
|
|
|
|
_ecore_con_local_win32_client_add,
|
2014-08-26 06:53:23 -07:00
|
|
|
obj);
|
2011-05-14 10:52:30 -07:00
|
|
|
if (!handler)
|
|
|
|
{
|
|
|
|
ERR("Creation of the client add handler failed");
|
|
|
|
goto del_handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
svr->read_stopped = EINA_TRUE;
|
|
|
|
ResumeThread(thread_listening);
|
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
|
2013-12-19 21:07:50 -08:00
|
|
|
del_handler:
|
2011-05-14 10:52:30 -07:00
|
|
|
ecore_main_win32_handler_del(handler);
|
2013-12-19 21:07:50 -08:00
|
|
|
close_pipe:
|
2011-05-14 10:52:30 -07:00
|
|
|
CloseHandle(svr->pipe);
|
2013-12-19 21:07:50 -08:00
|
|
|
free_path:
|
2011-05-14 10:52:30 -07:00
|
|
|
free(svr->path);
|
|
|
|
svr->path = NULL;
|
|
|
|
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-08-26 06:53:23 -07:00
|
|
|
ecore_con_local_win32_server_del(Ecore_Con_Server *obj)
|
2011-05-14 10:52:30 -07:00
|
|
|
{
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Server_Data *svr = efl_data_scope_get(obj, EFL_NETWORK_SERVER_CLASS);
|
2011-05-14 10:52:30 -07:00
|
|
|
if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_USER) &&
|
|
|
|
((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_SYSTEM))
|
|
|
|
return;
|
|
|
|
|
|
|
|
svr->read_stop = 1;
|
2015-07-07 03:34:40 -07:00
|
|
|
/* FIXME: we should try to stop these thread in one way or another */
|
|
|
|
/* should we use ecore_thread ? */
|
|
|
|
/* while (!svr->read_stopped) */
|
|
|
|
/* Sleep(100); */
|
2011-05-14 10:52:30 -07:00
|
|
|
|
|
|
|
if (svr->event_peek)
|
|
|
|
CloseHandle(svr->event_peek);
|
|
|
|
svr->event_peek = NULL;
|
|
|
|
if (svr->event_read)
|
|
|
|
CloseHandle(svr->event_read);
|
|
|
|
svr->event_read = NULL;
|
|
|
|
free(svr->path);
|
|
|
|
svr->path = NULL;
|
|
|
|
if (svr->pipe)
|
|
|
|
CloseHandle(svr->pipe);
|
|
|
|
svr->pipe = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-08-26 06:53:23 -07:00
|
|
|
ecore_con_local_win32_client_del(Ecore_Con_Client *obj)
|
2011-05-14 10:52:30 -07:00
|
|
|
{
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Client_Data *cl = efl_data_scope_get(obj, EFL_NETWORK_CLIENT_CLASS);
|
|
|
|
Efl_Network_Server_Data *svr = efl_data_scope_get(cl->host_server, EFL_NETWORK_SERVER_CLASS);
|
2014-08-26 06:53:23 -07:00
|
|
|
|
|
|
|
if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
|
2011-05-14 10:52:30 -07:00
|
|
|
return;
|
|
|
|
|
2014-08-26 06:53:23 -07:00
|
|
|
if (((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_USER) &&
|
|
|
|
((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_SYSTEM))
|
2011-05-14 10:52:30 -07:00
|
|
|
return;
|
|
|
|
|
2014-08-26 06:53:23 -07:00
|
|
|
svr->read_stop = 1;
|
|
|
|
while (!svr->read_stopped)
|
2011-05-14 10:52:30 -07:00
|
|
|
Sleep(100);
|
|
|
|
|
2014-08-26 06:53:23 -07:00
|
|
|
if (svr->event_peek)
|
|
|
|
CloseHandle(svr->event_peek);
|
|
|
|
svr->event_peek = NULL;
|
|
|
|
if (svr->event_read)
|
|
|
|
CloseHandle(svr->event_read);
|
|
|
|
svr->event_read = NULL;
|
|
|
|
free(svr->path);
|
|
|
|
svr->path = NULL;
|
|
|
|
if (svr->pipe)
|
|
|
|
CloseHandle(svr->pipe);
|
|
|
|
svr->pipe = NULL;
|
2011-05-14 10:52:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Eina_Bool
|
2014-08-26 06:53:23 -07:00
|
|
|
ecore_con_local_connect(Ecore_Con_Server *obj,
|
2011-05-14 10:52:30 -07:00
|
|
|
Eina_Bool (*cb_done)(void *data,
|
2011-07-04 17:06:27 -07:00
|
|
|
Ecore_Fd_Handler *fd_handler))
|
2011-05-14 10:52:30 -07:00
|
|
|
{
|
2014-06-27 04:41:20 -07:00
|
|
|
#warning "I am pretty sure cb_done should be used."
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Server_Data *svr = efl_data_scope_get(obj, EFL_NETWORK_SERVER_CLASS);
|
2016-11-29 05:11:55 -08:00
|
|
|
char *buf = NULL;
|
2011-05-14 10:52:30 -07:00
|
|
|
Ecore_Win32_Handler *handler_read;
|
|
|
|
Ecore_Win32_Handler *handler_peek;
|
|
|
|
|
|
|
|
if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
|
|
|
|
{
|
2015-07-07 03:33:39 -07:00
|
|
|
WRN("Your system does not support abstract sockets!");
|
2011-05-14 10:52:30 -07:00
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_USER)
|
2016-11-23 06:10:31 -08:00
|
|
|
buf = ecore_con_local_path_new(EINA_FALSE, svr->name, svr->port);
|
2011-05-14 10:52:30 -07:00
|
|
|
else if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_SYSTEM)
|
2016-11-23 06:10:31 -08:00
|
|
|
buf = ecore_con_local_path_new(EINA_TRUE, svr->name, svr->port);
|
2011-05-14 10:52:30 -07:00
|
|
|
|
2016-11-23 06:10:31 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(buf, EINA_FALSE);
|
2011-05-14 10:52:30 -07:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
svr->pipe = CreateFile(buf,
|
|
|
|
GENERIC_READ | GENERIC_WRITE,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
if (svr->pipe != INVALID_HANDLE_VALUE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* if pipe not busy, we exit */
|
|
|
|
if (GetLastError() != ERROR_PIPE_BUSY)
|
|
|
|
{
|
2015-07-07 03:33:39 -07:00
|
|
|
DBG("Connection to a server failed");
|
2016-11-23 06:10:31 -08:00
|
|
|
free(buf);
|
2011-05-14 10:52:30 -07:00
|
|
|
return EINA_FALSE;
|
2013-12-19 21:07:50 -08:00
|
|
|
}
|
2011-05-14 10:52:30 -07:00
|
|
|
|
|
|
|
/* pipe busy, so we wait for it */
|
|
|
|
if (!WaitNamedPipe(buf, NMPWAIT_WAIT_FOREVER))
|
|
|
|
{
|
2015-07-07 03:33:39 -07:00
|
|
|
DBG("Can not wait for a server");
|
2011-05-14 10:52:30 -07:00
|
|
|
goto close_pipe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 06:10:31 -08:00
|
|
|
svr->path = buf;
|
|
|
|
buf = NULL;
|
2011-05-14 10:52:30 -07:00
|
|
|
|
|
|
|
svr->event_read = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!svr->event_read)
|
|
|
|
{
|
|
|
|
ERR("Can not create event read");
|
|
|
|
goto free_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
handler_read = ecore_main_win32_handler_add(svr->event_read,
|
|
|
|
_ecore_con_local_win32_client_read_server_handler,
|
2014-08-26 06:53:23 -07:00
|
|
|
obj);
|
2011-05-14 10:52:30 -07:00
|
|
|
if (!handler_read)
|
|
|
|
{
|
|
|
|
ERR("Can not create handler read");
|
|
|
|
goto close_event_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
svr->event_peek = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!svr->event_peek)
|
|
|
|
{
|
|
|
|
ERR("Can not create event peek");
|
|
|
|
goto del_handler_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
handler_peek = ecore_main_win32_handler_add(svr->event_peek,
|
|
|
|
_ecore_con_local_win32_client_peek_server_handler,
|
2014-08-26 06:53:23 -07:00
|
|
|
obj);
|
2011-05-14 10:52:30 -07:00
|
|
|
if (!handler_peek)
|
|
|
|
{
|
|
|
|
ERR("Can not create handler peek");
|
|
|
|
goto close_event_peek;
|
|
|
|
}
|
|
|
|
|
2014-08-26 06:53:23 -07:00
|
|
|
svr->thread_read = (HANDLE)_beginthreadex(NULL, 0, _ecore_con_local_win32_client_read_server_thread, obj, CREATE_SUSPENDED, NULL);
|
2011-05-14 10:52:30 -07:00
|
|
|
if (!svr->thread_read)
|
|
|
|
{
|
|
|
|
ERR("Can not launch thread");
|
|
|
|
goto del_handler_peek;
|
|
|
|
}
|
|
|
|
|
2014-08-26 06:53:23 -07:00
|
|
|
if (!svr->delete_me) ecore_con_event_server_add(obj);
|
2011-05-14 10:52:30 -07:00
|
|
|
|
|
|
|
ResumeThread(svr->thread_read);
|
2016-11-23 06:10:31 -08:00
|
|
|
free(buf);
|
2011-05-14 10:52:30 -07:00
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
|
2013-12-19 21:07:50 -08:00
|
|
|
del_handler_peek:
|
2011-05-14 10:52:30 -07:00
|
|
|
ecore_main_win32_handler_del(handler_peek);
|
2013-12-19 21:07:50 -08:00
|
|
|
close_event_peek:
|
2011-05-14 10:52:30 -07:00
|
|
|
CloseHandle(svr->event_peek);
|
2013-12-19 21:07:50 -08:00
|
|
|
del_handler_read:
|
2011-05-14 10:52:30 -07:00
|
|
|
ecore_main_win32_handler_del(handler_read);
|
2013-12-19 21:07:50 -08:00
|
|
|
close_event_read:
|
2011-05-14 10:52:30 -07:00
|
|
|
CloseHandle(svr->event_read);
|
2013-12-19 21:07:50 -08:00
|
|
|
free_path:
|
2011-05-14 10:52:30 -07:00
|
|
|
free(svr->path);
|
|
|
|
svr->path = NULL;
|
2013-12-19 21:07:50 -08:00
|
|
|
close_pipe:
|
2011-05-14 10:52:30 -07:00
|
|
|
CloseHandle(svr->pipe);
|
2016-11-23 06:10:31 -08:00
|
|
|
free(buf);
|
2011-05-14 10:52:30 -07:00
|
|
|
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Eina_Bool
|
2014-08-26 06:53:23 -07:00
|
|
|
ecore_con_local_win32_server_flush(Ecore_Con_Server *obj)
|
2011-05-14 10:52:30 -07:00
|
|
|
{
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Server_Data *svr = efl_data_scope_get(obj, EFL_NETWORK_SERVER_CLASS);
|
2014-04-04 03:35:16 -07:00
|
|
|
size_t num;
|
2011-05-14 10:52:30 -07:00
|
|
|
BOOL res;
|
|
|
|
DWORD written;
|
|
|
|
|
|
|
|
/* This check should never be true */
|
|
|
|
if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT)
|
|
|
|
return EINA_TRUE;
|
|
|
|
|
|
|
|
if (((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_USER) &&
|
|
|
|
((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_SYSTEM))
|
|
|
|
return EINA_FALSE;
|
|
|
|
|
2011-07-17 04:11:25 -07:00
|
|
|
num = eina_binbuf_length_get(svr->buf) - svr->write_buf_offset;
|
2017-10-13 12:07:25 -07:00
|
|
|
if (num == 0) return EINA_TRUE;
|
2011-05-14 10:52:30 -07:00
|
|
|
|
2011-07-17 04:11:25 -07:00
|
|
|
res = WriteFile(svr->pipe, eina_binbuf_string_get(svr->buf) + svr->write_buf_offset, num, &written, NULL);
|
2011-05-14 10:52:30 -07:00
|
|
|
if (!res)
|
|
|
|
{
|
2017-12-10 22:06:02 -08:00
|
|
|
ecore_con_event_server_error(obj, evil_last_error_get());
|
2014-08-26 06:53:23 -07:00
|
|
|
_ecore_con_server_kill(obj);
|
2011-05-14 10:52:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
svr->write_buf_offset += written;
|
2011-07-17 04:11:25 -07:00
|
|
|
if (svr->write_buf_offset >= eina_binbuf_length_get(svr->buf))
|
2011-05-14 10:52:30 -07:00
|
|
|
{
|
|
|
|
svr->write_buf_offset = 0;
|
2012-01-19 00:02:41 -08:00
|
|
|
eina_binbuf_free(svr->buf);
|
2011-07-17 04:11:25 -07:00
|
|
|
svr->buf = NULL;
|
2011-05-14 10:52:30 -07:00
|
|
|
svr->want_write = 0;
|
|
|
|
}
|
|
|
|
else if (written < (DWORD)num)
|
|
|
|
svr->want_write = 1;
|
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Eina_Bool
|
2014-08-26 06:53:23 -07:00
|
|
|
ecore_con_local_win32_client_flush(Ecore_Con_Client *obj)
|
2011-05-14 10:52:30 -07:00
|
|
|
{
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Client_Data *cl = efl_data_scope_get(obj, EFL_NETWORK_CLIENT_CLASS);
|
2011-05-14 10:52:30 -07:00
|
|
|
Ecore_Con_Type type;
|
2014-04-04 03:35:16 -07:00
|
|
|
size_t num;
|
2011-05-14 10:52:30 -07:00
|
|
|
BOOL res;
|
|
|
|
DWORD written;
|
2016-08-15 06:44:41 -07:00
|
|
|
Efl_Network_Server_Data *svr = efl_data_scope_get(cl->host_server, EFL_NETWORK_SERVER_CLASS);
|
2011-05-14 10:52:30 -07:00
|
|
|
|
2014-08-26 06:53:23 -07:00
|
|
|
type = svr->type & ECORE_CON_TYPE;
|
2011-05-14 10:52:30 -07:00
|
|
|
|
|
|
|
/* This check should never be true */
|
|
|
|
if (type == ECORE_CON_LOCAL_ABSTRACT)
|
|
|
|
return EINA_TRUE;
|
|
|
|
|
|
|
|
if ((type != ECORE_CON_LOCAL_USER) &&
|
|
|
|
(type != ECORE_CON_LOCAL_SYSTEM))
|
|
|
|
return EINA_FALSE;
|
|
|
|
|
2011-07-17 04:11:25 -07:00
|
|
|
num = eina_binbuf_length_get(cl->buf) - cl->buf_offset;
|
2017-10-13 12:07:25 -07:00
|
|
|
if (num == 0) return EINA_TRUE;
|
2011-05-14 10:52:30 -07:00
|
|
|
|
2014-08-26 06:53:23 -07:00
|
|
|
res = WriteFile(svr->pipe, eina_binbuf_string_get(cl->buf) + cl->buf_offset, num, &written, NULL);
|
2011-05-14 10:52:30 -07:00
|
|
|
if (!res)
|
|
|
|
{
|
2017-12-10 22:06:02 -08:00
|
|
|
ecore_con_event_client_error(obj, evil_last_error_get());
|
2014-08-26 06:53:23 -07:00
|
|
|
_ecore_con_client_kill(obj);
|
2011-05-14 10:52:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
cl->buf_offset += written;
|
2011-07-17 04:11:25 -07:00
|
|
|
if (cl->buf_offset >= eina_binbuf_length_get(cl->buf))
|
2011-05-14 10:52:30 -07:00
|
|
|
{
|
|
|
|
cl->buf_offset = 0;
|
2011-07-17 04:11:25 -07:00
|
|
|
eina_binbuf_free(cl->buf);
|
2011-05-14 10:52:30 -07:00
|
|
|
cl->buf = NULL;
|
2014-08-26 06:53:23 -07:00
|
|
|
svr->want_write = 0;
|
2011-05-14 10:52:30 -07:00
|
|
|
}
|
|
|
|
else if (written < (DWORD)num)
|
2014-08-26 06:53:23 -07:00
|
|
|
svr->want_write = 1;
|
2011-05-14 10:52:30 -07:00
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
2013-12-19 21:07:50 -08:00
|
|
|
|