minor fix

This commit is contained in:
lxsang
2018-10-09 17:24:00 +02:00
parent a3509ca6aa
commit d60109fcf9
6 changed files with 126 additions and 19 deletions

View File

@ -71,18 +71,96 @@ int response(void* client, const char* data)
int antd_send(void *src, const void* data, int len)
{
if(!src || !data) return -1;
int ret;
int written;
antd_client_t * source = (antd_client_t *) src;
#ifdef USE_OPENSSL
if(usessl())
{
//LOG("SSL WRITE\n");
ret = SSL_write((SSL*) source->ssl, data, len);
//ret = SSL_write((SSL*) source->ssl, data, len);
int count;
char* ptr = (char* )data;
int writelen = len > BUFFLEN?BUFFLEN:len;
written = 0;
fd_set fds;
struct timeval timeout;
while (writelen > 0)
{
count = SSL_write (source->ssl, ptr+written, writelen);
if (count > 0)
{
written += count;
writelen = (len - written) > BUFFLEN?BUFFLEN:(len-written);
}
else
{
//printf(" received equal to or less than 0\n")
int err = SSL_get_error(source->ssl, count);
switch (err)
{
case SSL_ERROR_NONE:
{
// no real error, just try again...
//LOG("SSL_ERROR_NONE \n");
continue;
}
case SSL_ERROR_ZERO_RETURN:
{
// peer disconnected...
//printf("SSL_ERROR_ZERO_RETURN \n");
break;
}
case SSL_ERROR_WANT_READ:
{
// no data available right now, wait a few seconds in case new data arrives...
//printf("SSL_ERROR_WANT_READ\n");
int sock = SSL_get_rfd(source->ssl);
FD_ZERO(&fds);
FD_SET(sock, &fds);
timeout.tv_sec = 0;
timeout.tv_usec = 500;
err = select(sock+1, &fds, NULL, NULL, &timeout);
if (err == 0 || (err > 0 && FD_ISSET(sock, &fds)))
continue; // more data to read...
break;
}
case SSL_ERROR_WANT_WRITE:
{
// socket not writable right now, wait a few seconds and try again...
//printf("SSL_ERROR_WANT_WRITE \n");
int sock = SSL_get_wfd(source->ssl);
FD_ZERO(&fds);
FD_SET(sock, &fds);
timeout.tv_sec = 0;
timeout.tv_usec = 500;
err = select(sock+1, NULL, &fds, NULL, &timeout);
if (err == 0 || (err > 0 && FD_ISSET(sock, &fds)))
continue; // can write more data now...
break;
}
default:
{
// other error
break;
}
}
break;
}
}
}
else
{
#endif
ret = send(source->sock, data, len, 0);
written = send(source->sock, data, len, 0);
#ifdef USE_OPENSSL
}
#endif
@ -90,7 +168,7 @@ int antd_send(void *src, const void* data, int len)
{
antd_close(src);
}*/
return ret;
return written;
}
int antd_recv(void *src, void* data, int len)
{

View File

@ -118,6 +118,15 @@ static void work(antd_scheduler_t* scheduler)
pthread_mutex_unlock(&scheduler->worker_lock);
// execute the task
//LOG("task executed by worker %d\n", worker->pid);
// no task to execute, just sleep for 500usec
if(!it)
{
struct timespec ts_sleep;
ts_sleep.tv_sec = 0;
ts_sleep.tv_nsec = 500000;
nanosleep(&ts_sleep, NULL);
continue;
}
antd_execute_task(scheduler, it);
}
}
@ -256,7 +265,7 @@ int antd_scheduler_busy(antd_scheduler_t* scheduler)
return scheduler->pending_task != 0;
}
void antd_task_schedule(antd_scheduler_t* scheduler)
int antd_task_schedule(antd_scheduler_t* scheduler)
{
// fetch next task from the task_queue
antd_task_item_t it = NULL;
@ -271,7 +280,7 @@ void antd_task_schedule(antd_scheduler_t* scheduler)
pthread_mutex_unlock(&scheduler->scheduler_lock);
if(!it)
{
return;
return 0;
}
// has the task now
// check the type of tas
@ -289,4 +298,5 @@ void antd_task_schedule(antd_scheduler_t* scheduler)
pthread_mutex_unlock(&scheduler->worker_lock);
free(it);
}
return 1;
}

View File

@ -92,5 +92,5 @@ int antd_scheduler_busy(antd_scheduler_t*);
/*
schedule a task
*/
void antd_task_schedule(antd_scheduler_t*);
int antd_task_schedule(antd_scheduler_t*);
#endif