diff --git a/CMakeLists.txt b/CMakeLists.txt index ed61ecd471a9ca76eb93dab223c625144eea69a0..2e4c57375cd92d2df401df5eb521412b5f6bc3e4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ project(cellframe-sdk C) cmake_minimum_required(VERSION 2.8) set(CMAKE_C_STANDARD 11) -set(CELLFRAME_SDK_NATIVE_VERSION "2.6-28") +set(CELLFRAME_SDK_NATIVE_VERSION "2.6-29") add_definitions ("-DCELLFRAME_SDK_VERSION=\"${CELLFRAME_SDK_NATIVE_VERSION}\"") set(DAPSDK_MODULES "") diff --git a/modules/app-cli/dap_app_cli_net.c b/modules/app-cli/dap_app_cli_net.c index 54579c77eb56641bd8a9f16f9e216af3c1e42df8..a472241c029a3b5d19c2547def93fd2cb00a5dac 100644 --- a/modules/app-cli/dap_app_cli_net.c +++ b/modules/app-cli/dap_app_cli_net.c @@ -40,6 +40,7 @@ #else #include <sys/socket.h> #include <sys/un.h> +#include <sys/stat.h> #endif #include "dap_common.h" @@ -52,12 +53,20 @@ static int s_status; -//callback function to receive http data -static void dap_app_cli_http_read(uint64_t *socket, dap_app_cli_cmd_state_t *l_cmd) +//staic function to receive http data +static void dap_app_cli_http_read(dap_app_cli_connect_param_t *socket, dap_app_cli_cmd_state_t *l_cmd) { ssize_t l_recv_len = recv(*socket, &l_cmd->cmd_res[l_cmd->cmd_res_cur], DAP_CLI_HTTP_RESPONSE_SIZE_MAX, 0); + if (l_recv_len == 0) { + s_status = DAP_CLI_ERROR_INCOMPLETE; + return; + } if (l_recv_len == -1) { - s_status = DAP_CLI_ERROR_SOCKET; + if (errno == EAGAIN || errno == EWOULDBLOCK) { + s_status = DAP_CLI_ERROR_TIMEOUT; + } else { + s_status = DAP_CLI_ERROR_SOCKET; + } return; } l_cmd->cmd_res_cur +=(size_t) l_recv_len; @@ -69,16 +78,10 @@ static void dap_app_cli_http_read(uint64_t *socket, dap_app_cli_cmd_state_t *l_c l_cmd->cmd_res_len = atoi(l_str_ptr + strlen(l_cont_len_str)); if (l_cmd->cmd_res_len == 0) { s_status = DAP_CLI_ERROR_FORMAT; + break; } else { s_status++; - // resize buffer for received data - if (l_cmd->cmd_res_len > l_cmd->cmd_res_len_max) { - size_t l_len_max = l_cmd->cmd_res_len_max; - l_cmd->cmd_res_len_max = l_cmd->cmd_res_len + 1; - l_cmd->cmd_res = DAP_REALLOC(l_cmd->cmd_res, l_cmd->cmd_res_len_max); - memset(l_cmd->cmd_res + l_len_max, 0, l_cmd->cmd_res_len_max - l_len_max); - } } } else { break; @@ -102,6 +105,8 @@ static void dap_app_cli_http_read(uint64_t *socket, dap_app_cli_cmd_state_t *l_c if (l_cmd->cmd_res_cur == l_cmd->cmd_res_len) { l_cmd->cmd_res[l_cmd->cmd_res_cur] = 0; s_status = 0; + } else { + s_status = DAP_CLI_ERROR_FORMAT; } } break; } @@ -125,6 +130,8 @@ dap_app_cli_connect_param_t* dap_app_cli_connect(const char *a_socket_path) SOCKET l_socket = socket(AF_INET, SOCK_STREAM, 0); setsockopt((SOCKET)l_socket, SOL_SOCKET, SO_SNDBUF, (char *)&buffsize, sizeof(int) ); setsockopt((SOCKET)l_socket, SOL_SOCKET, SO_RCVBUF, (char *)&buffsize, sizeof(int) ); + DWORD l_to = DAP_CLI_HTTP_TIMEOUT; + setsockopt((SOCKET)l_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&l_to, sizeof(l_to)); #else if (!a_socket_path) { return NULL; @@ -134,8 +141,10 @@ dap_app_cli_connect_param_t* dap_app_cli_connect(const char *a_socket_path) if (l_socket < 0) { return NULL; } + struct timeval l_to = {DAP_CLI_HTTP_TIMEOUT, 0}; setsockopt(l_socket, SOL_SOCKET, SO_SNDBUF, (void*) &buffsize, sizeof(buffsize)); setsockopt(l_socket, SOL_SOCKET, SO_RCVBUF, (void*) &buffsize, sizeof(buffsize)); + setsockopt(l_socket, SOL_SOCKET, SO_RCVTIMEO, (void *)&l_to, sizeof(l_to)); #endif // connect int l_addr_len; @@ -160,7 +169,7 @@ dap_app_cli_connect_param_t* dap_app_cli_connect(const char *a_socket_path) closesocket(l_socket); return NULL; } - uint64_t *l_ret = DAP_NEW(uint64_t); + dap_app_cli_connect_param_t *l_ret = DAP_NEW(dap_app_cli_connect_param_t); *l_ret = l_socket; return l_ret; } @@ -188,7 +197,6 @@ int dap_app_cli_post_command( dap_app_cli_connect_param_t *a_socket, dap_app_cli assert(0); return -1; } - s_status = 1; a_cmd->cmd_res = DAP_NEW_Z_SIZE(char, DAP_CLI_HTTP_RESPONSE_SIZE_MAX); a_cmd->cmd_res_cur = 0; dap_string_t *l_cmd_data = dap_string_new(a_cmd->cmd_name); @@ -218,10 +226,11 @@ int dap_app_cli_post_command( dap_app_cli_connect_param_t *a_socket, dap_app_cli //wait for command execution time_t l_start_time = time(NULL); + s_status = 1; while(s_status > 0) { dap_app_cli_http_read(a_socket, a_cmd); if (time(NULL) - l_start_time > DAP_CLI_HTTP_TIMEOUT) - return DAP_CLI_ERROR_TIMEOUT; + s_status = DAP_CLI_ERROR_TIMEOUT; } // process result if (a_cmd->cmd_res && !s_status) { diff --git a/modules/app-cli/include/dap_app_cli.h b/modules/app-cli/include/dap_app_cli.h index 0acfe18fa6350f89c291b74a406305b0312b2046..944bb9c96ca46a3d0e3276dd07813e12a5ac7c98 100644 --- a/modules/app-cli/include/dap_app_cli.h +++ b/modules/app-cli/include/dap_app_cli.h @@ -34,7 +34,6 @@ typedef struct dap_app_cli_cmd_state { int ret_code; // for reply char *cmd_res; - size_t cmd_res_len_max; size_t cmd_res_len; size_t cmd_res_cur; } dap_app_cli_cmd_state_t; diff --git a/modules/app-cli/include/dap_app_cli_net.h b/modules/app-cli/include/dap_app_cli_net.h index a07fa6be8ca856566f1fd0d9a389f22c8b0ca122..c4ffbd06d0d1a1cf996a3ccb0b8757f57553f4a2 100644 --- a/modules/app-cli/include/dap_app_cli_net.h +++ b/modules/app-cli/include/dap_app_cli_net.h @@ -32,6 +32,7 @@ #define DAP_CLI_ERROR_FORMAT -1 #define DAP_CLI_ERROR_TIMEOUT -2 #define DAP_CLI_ERROR_SOCKET -3 +#define DAP_CLI_ERROR_INCOMPLETE -4 // connection description typedef uint64_t dap_app_cli_connect_param_t;