support alloc_recv_buffer callback
parent
3257a5f842
commit
2ebb51dcfd
|
|
@ -493,7 +493,7 @@ static int do_init()
|
|||
|
||||
return sf_service_init_ex2(&g_sf_context, "idemp-receipt",
|
||||
receipt_alloc_thread_extra_data, receipt_thread_loop_callback,
|
||||
NULL, sf_proto_set_body_length, receipt_deal_task,
|
||||
NULL, sf_proto_set_body_length, NULL, receipt_deal_task,
|
||||
receipt_task_finish_cleanup, receipt_recv_timeout_callback,
|
||||
1000, sizeof(SFCommonProtoHeader), 0, receipt_init_task, NULL);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ SFGlobalVariables g_sf_global_vars = {
|
|||
|
||||
SFContext g_sf_context = {
|
||||
{'\0'}, NULL, 0, -1, -1, 0, 0, 1, DEFAULT_WORK_THREADS,
|
||||
{'\0'}, {'\0'}, 0, true, true, NULL, NULL, NULL,
|
||||
{'\0'}, {'\0'}, 0, true, true, NULL, NULL, NULL, NULL,
|
||||
sf_task_finish_clean_up, NULL
|
||||
};
|
||||
|
||||
|
|
|
|||
19
src/sf_nio.c
19
src/sf_nio.c
|
|
@ -45,12 +45,14 @@
|
|||
|
||||
void sf_set_parameters_ex(SFContext *sf_context, const int header_size,
|
||||
sf_set_body_length_callback set_body_length_func,
|
||||
sf_alloc_recv_buffer_callback alloc_recv_buffer_func,
|
||||
sf_deal_task_func deal_func, TaskCleanUpCallback cleanup_func,
|
||||
sf_recv_timeout_callback timeout_callback, sf_release_buffer_callback
|
||||
release_buffer_callback)
|
||||
{
|
||||
sf_context->header_size = header_size;
|
||||
sf_context->set_body_length = set_body_length_func;
|
||||
sf_context->alloc_recv_buffer = alloc_recv_buffer_func;
|
||||
sf_context->deal_task = deal_func;
|
||||
sf_context->task_cleanup_func = cleanup_func;
|
||||
sf_context->timeout_callback = timeout_callback;
|
||||
|
|
@ -525,11 +527,13 @@ int sf_client_sock_read(int sock, short event, void *arg)
|
|||
task->network_timeout);
|
||||
if (task->length == 0) { //recv header
|
||||
recv_bytes = SF_CTX->header_size - task->offset;
|
||||
bytes = read(sock, task->data + task->offset, recv_bytes);
|
||||
} else {
|
||||
recv_bytes = task->length - task->offset;
|
||||
bytes = read(sock, task->recv_body + (task->offset -
|
||||
SF_CTX->header_size), recv_bytes);
|
||||
}
|
||||
|
||||
bytes = read(sock, task->data + task->offset, recv_bytes);
|
||||
if (bytes < 0) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
break;
|
||||
|
|
@ -609,6 +613,12 @@ int sf_client_sock_read(int sock, short event, void *arg)
|
|||
return -1;
|
||||
}
|
||||
|
||||
if (SF_CTX->alloc_recv_buffer != NULL) {
|
||||
if ((task->recv_body=SF_CTX->alloc_recv_buffer(task)) == NULL) {
|
||||
ioevent_add_to_deleted_list(task);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
if (task->length > task->size) {
|
||||
int old_size;
|
||||
|
||||
|
|
@ -635,10 +645,13 @@ int sf_client_sock_read(int sock, short event, void *arg)
|
|||
}
|
||||
|
||||
logDebug("file: "__FILE__", line: %d, "
|
||||
"client ip: %s, task length: %d, realloc buffer size "
|
||||
"from %d to %d", __LINE__, task->client_ip,
|
||||
"client ip: %s, task length: %d, realloc buffer "
|
||||
"size from %d to %d", __LINE__, task->client_ip,
|
||||
task->length, old_size, task->size);
|
||||
}
|
||||
|
||||
task->recv_body = task->data + SF_CTX->header_size;
|
||||
}
|
||||
}
|
||||
|
||||
if (task->offset >= task->length) { //recv done
|
||||
|
|
|
|||
|
|
@ -32,15 +32,16 @@ extern "C" {
|
|||
|
||||
void sf_set_parameters_ex(SFContext *sf_context, const int header_size,
|
||||
sf_set_body_length_callback set_body_length_func,
|
||||
sf_alloc_recv_buffer_callback alloc_recv_buffer_func,
|
||||
sf_deal_task_func deal_func, TaskCleanUpCallback cleanup_func,
|
||||
sf_recv_timeout_callback timeout_callback, sf_release_buffer_callback
|
||||
release_buffer_callback);
|
||||
|
||||
#define sf_set_parameters(header_size, set_body_length_func, \
|
||||
deal_func, cleanup_func, timeout_callback) \
|
||||
alloc_recv_buffer_func, deal_func, cleanup_func, timeout_callback) \
|
||||
sf_set_parameters_ex(&g_sf_context, header_size, \
|
||||
set_body_length_func, deal_func, \
|
||||
cleanup_func, timeout_callback, NULL)
|
||||
set_body_length_func, alloc_recv_buffer_func, \
|
||||
deal_func, cleanup_func, timeout_callback, NULL)
|
||||
|
||||
static inline void sf_set_deal_task_func_ex(SFContext *sf_context,
|
||||
sf_deal_task_func deal_func)
|
||||
|
|
|
|||
|
|
@ -121,6 +121,7 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
|
|||
ThreadLoopCallback thread_loop_callback,
|
||||
sf_accept_done_callback accept_done_callback,
|
||||
sf_set_body_length_callback set_body_length_func,
|
||||
sf_alloc_recv_buffer_callback alloc_recv_buffer_func,
|
||||
sf_deal_task_func deal_func, TaskCleanUpCallback task_cleanup_func,
|
||||
sf_recv_timeout_callback timeout_callback, const int net_timeout_ms,
|
||||
const int proto_header_size, const int task_arg_size,
|
||||
|
|
@ -141,8 +142,9 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
|
|||
min_buff_size < g_sf_global_vars.max_buff_size;
|
||||
sf_context->accept_done_func = accept_done_callback;
|
||||
sf_set_parameters_ex(sf_context, proto_header_size,
|
||||
set_body_length_func, deal_func, task_cleanup_func,
|
||||
timeout_callback, release_buffer_callback);
|
||||
set_body_length_func, alloc_recv_buffer_func,
|
||||
deal_func, task_cleanup_func, timeout_callback,
|
||||
release_buffer_callback);
|
||||
|
||||
if ((result=sf_init_free_queues(task_arg_size, init_callback)) != 0) {
|
||||
return result;
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ int sf_service_init_ex2(SFContext *sf_context, const char *name,
|
|||
ThreadLoopCallback thread_loop_callback,
|
||||
sf_accept_done_callback accept_done_callback,
|
||||
sf_set_body_length_callback set_body_length_func,
|
||||
sf_alloc_recv_buffer_callback alloc_recv_buffer_func,
|
||||
sf_deal_task_func deal_func, TaskCleanUpCallback task_cleanup_func,
|
||||
sf_recv_timeout_callback timeout_callback, const int net_timeout_ms,
|
||||
const int proto_header_size, const int task_arg_size,
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@
|
|||
typedef void (*sf_accept_done_callback)(struct fast_task_info *task,
|
||||
const bool bInnerPort);
|
||||
typedef int (*sf_set_body_length_callback)(struct fast_task_info *task);
|
||||
typedef char *(*sf_alloc_recv_buffer_callback)(struct fast_task_info *task);
|
||||
typedef int (*sf_deal_task_func)(struct fast_task_info *task, const int stage);
|
||||
typedef int (*sf_recv_timeout_callback)(struct fast_task_info *task);
|
||||
|
||||
|
|
@ -65,6 +66,7 @@ typedef struct sf_context {
|
|||
bool realloc_task_buffer;
|
||||
sf_deal_task_func deal_task;
|
||||
sf_set_body_length_callback set_body_length;
|
||||
sf_alloc_recv_buffer_callback alloc_recv_buffer;
|
||||
sf_accept_done_callback accept_done_func;
|
||||
TaskCleanUpCallback task_cleanup_func;
|
||||
sf_recv_timeout_callback timeout_callback;
|
||||
|
|
|
|||
Loading…
Reference in New Issue