diff --git a/Platform/common/rpc/rpc_conn.h b/Platform/common/rpc/rpc_conn.h index f78c0bd38..e9d5398dc 100755 --- a/Platform/common/rpc/rpc_conn.h +++ b/Platform/common/rpc/rpc_conn.h @@ -48,4 +48,5 @@ void rpc_return_error(rpc_conn *conn, ret_code err_code, char* err_message); void rpc_send_message(rpc_conn *conn); +void rpc_conn_freeiovdata(rpc_conn *c, pointer data1, pointer data2, pointer data3); #endif /* RPC_CONN_H_ */ diff --git a/Platform/common/rpc/rpc_request.h b/Platform/common/rpc/rpc_request.h index 98b13df31..8df4b743a 100755 --- a/Platform/common/rpc/rpc_request.h +++ b/Platform/common/rpc/rpc_request.h @@ -29,6 +29,6 @@ rpc_request* rpc_request_copy_head(rpc_request *req); rpc_parse_result rpc_request_parse(rpc_conn *c, rpc_request **req); -void rpc_request_format(const rpc_request *request, rpc_conn *c); +ret_code rpc_request_format(const rpc_request *request, rpc_conn *c); #endif /* RPC_REQUEST_H_ */ diff --git a/Platform/common/rpc/rpc_util.h b/Platform/common/rpc/rpc_util.h index 5d2fd59fb..53c1a052b 100755 --- a/Platform/common/rpc/rpc_util.h +++ b/Platform/common/rpc/rpc_util.h @@ -45,6 +45,12 @@ __func__, __LINE__, ##__VA_ARGS__); \ } while (0) +#define rpc_json_print(json_obj) \ + do { \ + char *print_str = cJSON_Print(json_obj); \ + rpc_log_info("json input:\n %s\n", print_str); \ + free(print_str); \ + } while (0) #define ASSERT_RETCONN_VOID(conn,ret) \ do { \ diff --git a/Platform/user/configm/config-server/netconfig/bridge/brconfig.c b/Platform/user/configm/config-server/netconfig/bridge/brconfig.c index a84df7d6c..050c981ed 100644 --- a/Platform/user/configm/config-server/netconfig/bridge/brconfig.c +++ b/Platform/user/configm/config-server/netconfig/bridge/brconfig.c @@ -409,7 +409,7 @@ ret_code br_config_json_parse(pointer input, uint *conf_type, char *br_name) json_obj = cJSON_Parse(input); ASSERT_PTR(json_obj, RET_NULLP); - rpc_log_info("json input:\n %s\n", cJSON_Print(json_obj)); + rpc_json_print(json_obj); s2j_create_struct_obj(br_config, br_config_string_t); @@ -555,7 +555,7 @@ ret_code br_if_config_json_parse(pointer input, uint *conf_type, br_config_t *br json_obj = cJSON_Parse(input); ASSERT_PTR(json_obj, RET_NULLP); - rpc_log_info("json input:\n %s\n", cJSON_Print(json_obj)); + rpc_json_print(json_obj); s2j_create_struct_obj(br_if_conf, br_if_config_string_t); @@ -971,7 +971,7 @@ ret_code br_fdb_config_json_parse(pointer input, uint *conf_type, br_fdb_config_ json_obj = cJSON_Parse(input); ASSERT_PTR(json_obj, RET_NULLP); - rpc_log_info("json input:\n %s\n", cJSON_Print(json_obj)); + rpc_json_print(json_obj); s2j_create_struct_obj(br_fdb_conf, br_fdb_string_t); diff --git a/Platform/user/configm/config-server/netconfig/ipconfig/ipconfig.c b/Platform/user/configm/config-server/netconfig/ipconfig/ipconfig.c index 83d2bcd70..6f18d0788 100644 --- a/Platform/user/configm/config-server/netconfig/ipconfig/ipconfig.c +++ b/Platform/user/configm/config-server/netconfig/ipconfig/ipconfig.c @@ -72,7 +72,7 @@ ret_code ip_config_json_parse(pointer input, uint *conf_type, ip_config_t *confi return RET_INPUTERR; } - rpc_log_info("json input:\n %s \n", cJSON_Print(json_obj)); + rpc_json_print(json_obj); s2j_create_struct_obj(ip_config, ip_config_string_t); @@ -119,7 +119,7 @@ ret_code ip_config_json_parse_array(pointer input, uint *conf_type, return RET_INPUTERR; } - rpc_log_info("json input:%s \n", cJSON_Print(json_obj)); + rpc_json_print(json_obj); iCount = cJSON_GetArraySize(json_obj); @@ -228,12 +228,12 @@ ret_code ip_config_format_json_array(ip_config_t *config_buff, return RET_NOMEM; } - cJSON_AddItemToArray(json_array, json_obj); - s2j_json_set_basic_element(json_obj, ip_config, string, ifname); s2j_json_set_basic_element(json_obj, ip_config, int, family); s2j_json_set_basic_element(json_obj, ip_config, string, ipaddr); s2j_json_set_basic_element(json_obj, ip_config, int, prefixlen); + + cJSON_AddItemToArray(json_array, json_obj); } diff --git a/Platform/user/configm/config-server/static_routing_config/static_routing_config.c b/Platform/user/configm/config-server/static_routing_config/static_routing_config.c index e81c29c1d..bc92235b4 100644 --- a/Platform/user/configm/config-server/static_routing_config/static_routing_config.c +++ b/Platform/user/configm/config-server/static_routing_config/static_routing_config.c @@ -75,13 +75,13 @@ ret_code routing_to_json(int count, routing_t *buff_total, pointer output, int * cJSON_AddNumberToObject(json_obj,"metric",buff_total[i].metric); cJSON_AddItemToArray(json_array, json_obj); - rpc_log_info("json_obj: %s\n",cJSON_Print(json_obj)); + rpc_json_print(json_obj); } cJSON_AddItemToObject(ret_root,"route",json_array); } - rpc_log_info("check: %s\n",cJSON_Print(ret_root)); + rpc_json_print(ret_root); json_routing = cJSON_PrintUnformatted(ret_root); *output_len = strlen(json_routing) + 1; @@ -107,7 +107,7 @@ ret_code routing_json_parse(pointer input, uint *conf_type, routing_t *conf_buff return RET_EMPTY_STRING; } - rpc_log_info("json input:\n %s \n", cJSON_Print(json_obj)); + rpc_json_print(json_obj); operate = cJSON_GetObjectItem(json_obj,"operate"); diff --git a/Platform/user/configm/config-server/web_config/authfree.c b/Platform/user/configm/config-server/web_config/authfree.c index c9d917894..f96edaba4 100644 --- a/Platform/user/configm/config-server/web_config/authfree.c +++ b/Platform/user/configm/config-server/web_config/authfree.c @@ -311,7 +311,7 @@ ret_code authfree_config_json_parse_array(pointer input, uint *conf_type, freeau return RET_INPUTERR; } - rpc_log_info("json input:%s \n", cJSON_Print(cjson)); + rpc_json_print(cjson); /*获取免认证规则的data部分 */ data = cJSON_GetObjectItem(cjson, "data"); diff --git a/Platform/user/rpc/rpc_client.c b/Platform/user/rpc/rpc_client.c index 2faaacd8d..0b6608bb8 100755 --- a/Platform/user/rpc/rpc_client.c +++ b/Platform/user/rpc/rpc_client.c @@ -292,12 +292,7 @@ ret_code rpc_client_call_async(rpc_client* client, char* service_name, rpc_log_dbg("rpc_client_call_async:send_message\n"); //write data - rpc_request_format(req, conn); - - if(conn->sfd == -1 || rpc_client_send(conn) == FALSE) - { - ret = RET_SENDERR; - } + ret = rpc_request_format(req, conn); pthread_mutex_unlock(&th->mutex); diff --git a/Platform/user/rpc/rpc_conn.c b/Platform/user/rpc/rpc_conn.c index 2bca9a407..65bac1518 100755 --- a/Platform/user/rpc/rpc_conn.c +++ b/Platform/user/rpc/rpc_conn.c @@ -424,6 +424,29 @@ void rpc_conn_addiov(rpc_conn *c, pointer data, size_t data_len) { c->iovused++; } +void rpc_conn_freeiovdata(rpc_conn *c, pointer data1, pointer data2, pointer data3) { + int i = 0; + + if(c == NULL || data1 == NULL + || data2 == NULL || data3 == NULL){ + return; + } + + for(i = 0; i < c->iovused; i++){ + if(c->iov[i].iov_base == data1 + || c->iov[i].iov_base == data2 + || c->iov[i].iov_base == data3){ + c->iov[i].iov_base = NULL; + } + } + + rpc_free(data1); + rpc_free(data2); + rpc_free(data3); + + c->iovused = 0; +} + boolean rpc_client_send(rpc_conn *conn) { struct msghdr m; boolean ret = TRUE; @@ -460,7 +483,7 @@ boolean rpc_client_send(rpc_conn *conn) { } } //TODO free resource - conn->iovused = 0; + //conn->iovused = 0; return ret; } @@ -492,34 +515,37 @@ void rpc_send_message(rpc_conn *conn) { } else { perror("failed to write"); rpc_conn_close(conn); + return; } } else { break; } } //TODO free resource - conn->iovused = 0; + //conn->iovused = 0; } static inline void rpc_send_response(rpc_conn *conn, ret_code code, pointer output, int output_len) { //status line - char *data; - data = rpc_vsprintf("%s %d %s\n", RPC_VERSION, (int) code, rpc_code_format( + char *data1, *data2, *data3; + data1 = rpc_vsprintf("%s %d %s\n", RPC_VERSION, (int) code, rpc_code_format( code)); - rpc_conn_addiov(conn, data, strlen(data)); + rpc_conn_addiov(conn, data1, strlen(data1)); //seq - data = rpc_vsprintf("seq:%d\n", conn->curr_seq); - rpc_conn_addiov(conn, data, strlen(data)); + data2 = rpc_vsprintf("seq:%d\n", conn->curr_seq); + rpc_conn_addiov(conn, data2, strlen(data2)); //body-len - data = rpc_vsprintf("body-len:%d\n\n", output_len); - rpc_conn_addiov(conn, data, strlen(data)); + data3 = rpc_vsprintf("body-len:%d\n\n", output_len); + rpc_conn_addiov(conn, data3, strlen(data3)); //body if (output) { rpc_conn_addiov(conn, output, output_len); } - rpc_log_dbg("rpc_send_response: %s\n", data); + rpc_log_dbg("rpc_send_response: %s\n", data3); rpc_send_message(conn); + + rpc_conn_freeiovdata(conn, data1, data2, data3); } void rpc_return(rpc_conn *conn, pointer output, int output_len) { diff --git a/Platform/user/rpc/rpc_request.c b/Platform/user/rpc/rpc_request.c index 2ba7d4374..69b62075a 100755 --- a/Platform/user/rpc/rpc_request.c +++ b/Platform/user/rpc/rpc_request.c @@ -160,23 +160,33 @@ rpc_parse_result rpc_request_parse(rpc_conn *c, rpc_request **req) { return RPC_Parse_Error; } -void rpc_request_format(const rpc_request *request, rpc_conn *c) { +ret_code rpc_request_format(const rpc_request *request, rpc_conn *c) { + ret_code ret = RET_OK; + char *data1, *data2, *data3; assert(request !=NULL); assert(c != NULL); - char *data; //request line - data = rpc_vsprintf("CALL %s@%s %s\n", request->method_name, + data1 = rpc_vsprintf("CALL %s@%s %s\n", request->method_name, request->service_name, RPC_VERSION); - rpc_conn_addiov(c, data, strlen(data)); + rpc_conn_addiov(c, data1, strlen(data1)); //seq - data = rpc_vsprintf("seq:%d\n", request->seq); - rpc_conn_addiov(c, data, strlen(data)); + data2 = rpc_vsprintf("seq:%d\n", request->seq); + rpc_conn_addiov(c, data2, strlen(data2)); //body len - data = rpc_vsprintf("body-len:%d\n\n", request->input_len); - rpc_conn_addiov(c, data, strlen(data)); + data3 = rpc_vsprintf("body-len:%d\n\n", request->input_len); + rpc_conn_addiov(c, data3, strlen(data3)); - rpc_log_dbg("request data:%s\n", data); + rpc_log_dbg("request data:%s\n", data3); //body if (request->input) rpc_conn_addiov(c, request->input, request->input_len); + + if(c->sfd == -1 || rpc_client_send(c) == FALSE) + { + ret = RET_SENDERR; + } + + rpc_conn_freeiovdata(c, data1, data2, data3); + + return ret; } diff --git a/Platform/user/rpc/rpc_thread.c b/Platform/user/rpc/rpc_thread.c index c31f79663..202124b8d 100755 --- a/Platform/user/rpc/rpc_thread.c +++ b/Platform/user/rpc/rpc_thread.c @@ -422,7 +422,7 @@ static void* thread_write_hander(void* data) { //write data rpc_request_format(req, conn); - rpc_client_send(conn); + //rpc_client_send(conn); } } return NULL;