using thread for wvnc

This commit is contained in:
lxsang 2018-10-09 17:24:44 +02:00
parent 143e13083d
commit 52081f16fb

110
wvnc.c
View File

@ -2,6 +2,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <rfb/rfbclient.h> #include <rfb/rfbclient.h>
#include <pthread.h>
#ifdef USE_ZLIB #ifdef USE_ZLIB
#include <zlib.h> #include <zlib.h>
#endif #endif
@ -37,6 +38,7 @@ typedef struct
//int rate; //int rate;
} wvnc_user_data_t; } wvnc_user_data_t;
typedef struct typedef struct
{ {
uint8_t r_shift; uint8_t r_shift;
@ -477,48 +479,29 @@ void open_session(void *data, const char *addr)
user_data->status = CONNECTED; user_data->status = CONNECTED;
} }
void* waitfor(void* data) void waitfor(void* data)
{ {
wvnc_user_data_t *user_data = get_user_data(data); wvnc_user_data_t *user_data = get_user_data(data);
antd_task_t* task = NULL; while (user_data->status != DISCONNECTED)
process(user_data, 0);
if (user_data->status == DISCONNECTED)
{ {
// quit process(user_data, 0);
goto quit;
}
if (user_data->status == CONNECTED) if (user_data->status == CONNECTED)
{ {
// process other message // process other message
int status = WaitForMessage(user_data->vncl, 200); //500 int status = WaitForMessage(user_data->vncl, 500); //500
if (status < 0) if (status < 0)
{ {
goto quit; break;
} }
if (status) if (status)
{ {
if (!HandleRFBServerMessage(user_data->vncl)) if (!HandleRFBServerMessage(user_data->vncl))
{ {
goto quit; break;
} }
} }
} }
task = antd_create_task(waitfor, user_data, NULL);
task->priority++;
task->type = HEAVY;
return task;
quit:
if(user_data->vncl->frameBuffer)
{
free(user_data->vncl->frameBuffer);
user_data->vncl->frameBuffer = NULL;
} }
if (user_data->vncl)
rfbClientCleanup(user_data->vncl);
task = antd_create_task(NULL, user_data->wscl, NULL);
task->priority++;
free(user_data);
return task;
} }
void *vnc_fatal(void *data, const char *msg) void *vnc_fatal(void *data, const char *msg)
@ -612,26 +595,23 @@ static void got_clipboard(rfbClient *cl, const char *text, int len)
} }
free(ack); free(ack);
} }
void* waitchild(void* data)
void* handle(void *data)
{ {
antd_request_t *rq = (antd_request_t *)data; wvnc_user_data_t *user_data = (wvnc_user_data_t*)(data);
void *cl = (void *)rq->client; antd_task_t* task = NULL;
if (ws_enable(rq->request)) if(user_data->status == DISCONNECTED)
{ {
//set time out for the tcp socket task = antd_create_task(NULL, user_data->wscl, NULL);
// set timeout to socket free(user_data);
//struct timeval timeout; }
//timeout.tv_sec = 0; else
// timeout.tv_usec = 200; task = antd_create_task(waitchild, user_data, NULL);
task->priority++;
//if (setsockopt(((antd_client_t *)cl)->sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) return task;
// perror("setsockopt failed\n"); }
void event_loop(void* data)
//if (setsockopt (((antd_client_t*)cl)->sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,sizeof(timeout)) < 0) {
// perror("setsockopt failed\n"); wvnc_user_data_t *user_data = get_user_data(data);
//rfbClient* vncl;
rfbClient *vncl = NULL; rfbClient *vncl = NULL;
vncl = rfbGetClient(8, 3, 4); vncl = rfbGetClient(8, 3, 4);
vncl->MallocFrameBuffer = resize; vncl->MallocFrameBuffer = resize;
@ -644,27 +624,51 @@ void* handle(void *data)
vncl->GetCredential = get_credential; vncl->GetCredential = get_credential;
vncl->listenPort = LISTEN_PORT_OFFSET; vncl->listenPort = LISTEN_PORT_OFFSET;
vncl->listen6Port = LISTEN_PORT_OFFSET; vncl->listen6Port = LISTEN_PORT_OFFSET;
wvnc_user_data_t *user_data = (wvnc_user_data_t *)malloc(sizeof(wvnc_user_data_t));
user_data->wscl = rq;
user_data->status = READY; // 1 for ready for connect user_data->status = READY; // 1 for ready for connect
user_data->vncl = vncl; user_data->vncl = vncl;
rfbClientSetClientData(vncl, vncl, user_data); rfbClientSetClientData(vncl, vncl, user_data);
//while(1) waitfor((void*) user_data);
//{ // child
antd_task_t *task = antd_create_task(waitfor, (void *)user_data, NULL); if(user_data->vncl->frameBuffer)
{
free(user_data->vncl->frameBuffer);
user_data->vncl->frameBuffer = NULL;
}
if (user_data->vncl)
rfbClientCleanup(user_data->vncl);
}
void* handle(void *data)
{
antd_request_t *rq = (antd_request_t *)data;
pthread_t th;
antd_task_t *task = NULL;
void *cl = (void *)rq->client;
wvnc_user_data_t *user_data = (wvnc_user_data_t *)malloc(sizeof(wvnc_user_data_t));
user_data->wscl = rq;
if (ws_enable(rq->request))
{
if (pthread_create(&th, NULL,(void* (*)(void *))event_loop, (void*)user_data) != 0)
{
free(user_data);
perror("pthread_create: cannot create thread for wvnc\n");
}
else
{
pthread_detach(th);
task = antd_create_task(waitchild, (void *)user_data, NULL);
task->priority++; task->priority++;
task->type = HEAVY;
return task; return task;
//} }
} }
else else
{ {
html(cl); html(cl);
__t(cl, "Welcome to WVNC, plese use a websocket connection"); __t(cl, "Welcome to WVNC, please use a websocket connection");
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL);
}
task = antd_create_task(NULL, (void *)rq, NULL);
task->priority++; task->priority++;
return task; return task;
}
//LOG("%s\n", "EXIT Streaming.."); //LOG("%s\n", "EXIT Streaming..");
} }
void init() void init()