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