26
26
#include "arpc_request.h"
27
27
#include "arpc_response.h"
28
28
29
+ static const int32_t SEND_RSP_END_MAX_TIME_MS = 2 * 1000 ;
29
30
static int request_msg_async_deal (void * usr_ctx );
30
31
31
32
int process_request_header (struct arpc_connection * con , struct xio_msg * msg )
@@ -60,7 +61,7 @@ int process_request_data(struct arpc_connection *con, struct xio_msg *req, int l
60
61
uint32_t i ;
61
62
struct arpc_vmsg rev_iov ;
62
63
struct arpc_rsp usr_rsp_param ;
63
- struct arpc_common_msg * rsp_hanlde ;
64
+ struct arpc_common_msg * rsp_msg ;
64
65
struct arpc_rsp_handle * rsp_fd_ex ;
65
66
struct arpc_thread_param * async_param ;
66
67
int ret ;
@@ -83,14 +84,14 @@ int process_request_data(struct arpc_connection *con, struct xio_msg *req, int l
83
84
ret = destroy_xio_msg_usr_buf (req , ops -> free_cb , usr_ctx );
84
85
LOG_THEN_RETURN_VAL_IF_TRUE ((ret ), ARPC_ERROR , "destroy_xio_msg_usr_buf fail." );
85
86
86
- rsp_hanlde = get_common_msg (con , ARPC_MSG_TYPE_RSP );
87
- LOG_THEN_RETURN_VAL_IF_TRUE (!rsp_hanlde , ARPC_ERROR , "rsp_hanlde alloc null." );
88
- rsp_fd_ex = (struct arpc_rsp_handle * )rsp_hanlde -> ex_data ;
87
+ rsp_msg = get_common_msg (con , ARPC_MSG_TYPE_RSP );
88
+ LOG_THEN_RETURN_VAL_IF_TRUE (!rsp_msg , ARPC_ERROR , "rsp_msg alloc null." );
89
+ rsp_fd_ex = (struct arpc_rsp_handle * )rsp_msg -> ex_data ;
89
90
rsp_fd_ex -> x_rsp_msg = req ;//保存回复的结构体
90
- rsp_hanlde -> attr .rsp_crc = attr .req_crc ;//请求保存在回复体里
91
- rsp_hanlde -> attr .req_crc = 0 ;
91
+ rsp_msg -> attr .rsp_crc = attr .req_crc ;//请求保存在回复体里
92
+ rsp_msg -> attr .req_crc = 0 ;
92
93
memset (& usr_rsp_param , 0 , sizeof (struct arpc_rsp ));
93
- usr_rsp_param .rsp_fd = (void * )rsp_hanlde ;
94
+ usr_rsp_param .rsp_fd = (void * )rsp_msg ;
94
95
95
96
if (IS_SET (req -> usr_flags , METHOD_ARPC_PROC_SYNC ) && ops -> proc_data_cb ){
96
97
ARPC_LOG_TRACE ("process rx request msg with async." );
@@ -122,7 +123,7 @@ int process_request_data(struct arpc_connection *con, struct xio_msg *req, int l
122
123
async_param -> ops .proc_async_cb = ops -> proc_async_cb ;
123
124
async_param -> ops .release_rsp_cb = ops -> release_rsp_cb ;
124
125
async_param -> ops .proc_oneway_async_cb = NULL ;
125
- async_param -> rsp_ctx = rsp_hanlde ;
126
+ async_param -> rsp_ctx = rsp_msg ;
126
127
async_param -> rev_iov = rev_iov ;
127
128
async_param -> req_msg = NULL ;
128
129
async_param -> usr_ctx = usr_ctx ;
@@ -140,20 +141,19 @@ int process_request_data(struct arpc_connection *con, struct xio_msg *req, int l
140
141
free_msg_xio2arpc (& rev_iov , ops -> free_cb , usr_ctx );
141
142
142
143
do_respone :
143
- /* attach request to response */
144
+ /* 框架内回复,则不需要通知模式,也不需要加锁 */
144
145
ARPC_LOG_TRACE ("do respone request msg." );
145
146
gettimeofday (& tx_time , NULL ); // 线程安全
146
147
147
- rsp_hanlde -> attr .tx_sec = tx_time .tv_sec ;
148
- rsp_hanlde -> attr .tx_usec = tx_time .tv_usec ;
149
- rsp_hanlde -> attr .conn_id = con -> id ;
148
+ rsp_msg -> attr .tx_sec = tx_time .tv_sec ;
149
+ rsp_msg -> attr .tx_usec = tx_time .tv_usec ;
150
+ rsp_msg -> attr .conn_id = con -> id ;
150
151
151
- ret = arpc_init_response (rsp_hanlde );
152
+ ret = arpc_init_response (rsp_msg );
152
153
LOG_ERROR_IF_VAL_TRUE (ret , "arpc_init_response fail." );
153
- if (!ret ){
154
- ret = arpc_connection_async_send (rsp_hanlde -> conn , rsp_hanlde );
155
- LOG_ERROR_IF_VAL_TRUE (ret , "arpc_connection_async_send fail." );
156
- }
154
+ ret = xio_send_response (rsp_msg -> tx_msg );
155
+ LOG_ERROR_IF_VAL_TRUE (ret , "xio_send_response fail." );
156
+
157
157
return ret ;
158
158
}
159
159
@@ -162,17 +162,17 @@ static int request_msg_async_deal(void *usr_ctx)
162
162
struct arpc_thread_param * async = (struct arpc_thread_param * )usr_ctx ;
163
163
struct arpc_rsp rsp ;
164
164
int ret ;
165
- struct arpc_common_msg * rsp_fd ;
165
+ struct arpc_common_msg * rsp_msg ;
166
166
struct arpc_rsp_handle * rsp_fd_ex ;
167
167
struct timeval tx_time ;
168
168
169
169
LOG_THEN_RETURN_VAL_IF_TRUE (!async , ARPC_ERROR , "async null." );
170
170
LOG_THEN_RETURN_VAL_IF_TRUE (!async -> ops .proc_async_cb , ARPC_ERROR , "request proc_async_cb null." );
171
171
LOG_THEN_RETURN_VAL_IF_TRUE (!async -> rsp_ctx , ARPC_ERROR , "request rsp context is null." );
172
172
173
- rsp_fd = (struct arpc_common_msg * )async -> rsp_ctx ;
173
+ rsp_msg = (struct arpc_common_msg * )async -> rsp_ctx ;
174
174
memset (& rsp , 0 , sizeof (struct arpc_rsp ));
175
- rsp .rsp_fd = (void * )rsp_fd ;
175
+ rsp .rsp_fd = (void * )rsp_msg ;
176
176
177
177
ARPC_LOG_TRACE ("process request msg with async." );
178
178
ret = async -> ops .proc_async_cb (& async -> rev_iov , & rsp , async -> usr_ctx );
@@ -185,21 +185,26 @@ static int request_msg_async_deal(void *usr_ctx)
185
185
async -> rev_iov .vec = NULL ;
186
186
}
187
187
188
- rsp_fd_ex = (struct arpc_rsp_handle * )rsp_fd -> ex_data ;
188
+ rsp_fd_ex = (struct arpc_rsp_handle * )rsp_msg -> ex_data ;
189
189
rsp_fd_ex -> rsp_usr_iov = rsp .rsp_iov ;
190
190
rsp_fd_ex -> rsp_usr_ctx = rsp .rsp_ctx ;
191
191
rsp_fd_ex -> release_rsp_cb = async -> ops .release_rsp_cb ;
192
192
193
193
if (!IS_SET (rsp .flags , METHOD_CALLER_ASYNC )) {
194
194
gettimeofday (& tx_time , NULL ); // 线程安全
195
- rsp_fd -> attr .tx_sec = tx_time .tv_sec ;
196
- rsp_fd -> attr .tx_usec = tx_time .tv_usec ;
197
- ret = arpc_init_response (rsp_fd );
198
- LOG_ERROR_IF_VAL_TRUE (ret , "arpc_do_respone fail." );
199
- if (!ret ){
200
- ret = arpc_connection_async_send (rsp_fd -> conn , rsp_fd );
201
- LOG_ERROR_IF_VAL_TRUE (ret , "arpc_do_respone fail." );
195
+ arpc_cond_lock (& rsp_msg -> cond );
196
+ rsp_msg -> attr .tx_sec = tx_time .tv_sec ;
197
+ rsp_msg -> attr .tx_usec = tx_time .tv_usec ;
198
+ ret = arpc_init_response (rsp_msg );
199
+ LOG_ERROR_IF_VAL_TRUE (ret , "arpc_init_response fail." );
200
+ ret = arpc_connection_async_send (rsp_msg -> conn , rsp_msg );
201
+ if (!ret ) {
202
+ ret = arpc_cond_wait_timeout (& rsp_msg -> cond , SEND_RSP_END_MAX_TIME_MS ); // 默认等待
203
+ LOG_ERROR_IF_VAL_TRUE (ret , "rsp send timeout fail, conn[%u], msg[%p]." , rsp_msg -> conn -> id , rsp_msg );
204
+ }else {
205
+ ARPC_LOG_ERROR ("arpc_connection_async_send fail, conn[%u], msg[%p]." , rsp_msg -> conn -> id , rsp_msg );
202
206
}
207
+ arpc_cond_unlock (& rsp_msg -> cond );
203
208
}
204
209
SAFE_FREE_MEM (async -> rev_iov .head );
205
210
SAFE_FREE_MEM (async );
0 commit comments