4
4
//
5
5
6
6
use nix:: unistd:: close;
7
- use protobuf:: { CodedInputStream , CodedOutputStream , Message } ;
8
7
use std:: collections:: HashMap ;
8
+ use std:: convert:: TryInto ;
9
9
use std:: os:: unix:: io:: RawFd ;
10
10
use std:: sync:: { Arc , Mutex } ;
11
11
12
12
use crate :: common:: client_connect;
13
13
use crate :: error:: { Error , Result } ;
14
- use crate :: proto:: { Code , Request , Response , MESSAGE_TYPE_RESPONSE } ;
14
+ use crate :: proto:: { Code , Codec , GenMessage , Message , Request , Response , MESSAGE_TYPE_RESPONSE } ;
15
15
16
- use crate :: asynchronous:: stream:: { receive, to_req_buf} ;
17
16
use crate :: r#async:: utils;
18
17
use tokio:: {
19
18
self ,
20
- io:: { split, AsyncWriteExt } ,
19
+ io:: split,
21
20
sync:: mpsc:: { channel, Receiver , Sender } ,
22
21
sync:: Notify ,
23
22
} ;
24
23
25
- type RequestSender = Sender < ( Vec < u8 > , Sender < Result < Vec < u8 > > > ) > ;
26
- type RequestReceiver = Receiver < ( Vec < u8 > , Sender < Result < Vec < u8 > > > ) > ;
24
+ type RequestSender = Sender < ( GenMessage , Sender < Result < Vec < u8 > > > ) > ;
25
+ type RequestReceiver = Receiver < ( GenMessage , Sender < Result < Vec < u8 > > > ) > ;
27
26
28
27
type ResponseSender = Sender < Result < Vec < u8 > > > ;
29
28
type ResponseReceiver = Receiver < Result < Vec < u8 > > > ;
@@ -57,17 +56,17 @@ impl Client {
57
56
let request_sender = tokio:: spawn ( async move {
58
57
let mut stream_id: u32 = 1 ;
59
58
60
- while let Some ( ( body , resp_tx) ) = rx. recv ( ) . await {
59
+ while let Some ( ( mut msg , resp_tx) ) = rx. recv ( ) . await {
61
60
let current_stream_id = stream_id;
61
+ msg. header . set_stream_id ( current_stream_id) ;
62
62
stream_id += 2 ;
63
63
64
64
{
65
65
let mut map = req_map2. lock ( ) . unwrap ( ) ;
66
66
map. insert ( current_stream_id, resp_tx. clone ( ) ) ;
67
67
}
68
68
69
- let buf = to_req_buf ( current_stream_id, body) ;
70
- if let Err ( e) = writer. write_all ( & buf) . await {
69
+ if let Err ( e) = msg. write_to ( & mut writer) . await {
71
70
error ! ( "write_message got error: {:?}" , e) ;
72
71
73
72
{
@@ -97,41 +96,42 @@ impl Client {
97
96
_ = notify2. notified( ) => {
98
97
break ;
99
98
}
100
- res = receive ( & mut reader) => {
99
+ res = GenMessage :: read_from ( & mut reader) => {
101
100
match res {
102
- Ok ( ( header, body) ) => {
101
+ Ok ( msg) => {
102
+ trace!( "Got Message body {:?}" , msg. payload) ;
103
103
let req_map = req_map. clone( ) ;
104
104
tokio:: spawn( async move {
105
105
let resp_tx2;
106
106
{
107
107
let mut map = req_map. lock( ) . unwrap( ) ;
108
- let resp_tx = match map. get( & header. stream_id) {
108
+ let resp_tx = match map. get( & msg . header. stream_id) {
109
109
Some ( tx) => tx,
110
110
None => {
111
111
debug!(
112
- "Receiver got unknown packet {:?} {:?} " ,
113
- header , body
112
+ "Receiver got unknown packet {:?}" ,
113
+ msg
114
114
) ;
115
115
return ;
116
116
}
117
117
} ;
118
118
119
119
resp_tx2 = resp_tx. clone( ) ;
120
- map. remove( & header. stream_id) ; // Forget the result, just remove.
120
+ map. remove( & msg . header. stream_id) ; // Forget the result, just remove.
121
121
}
122
122
123
- if header. type_ != MESSAGE_TYPE_RESPONSE {
123
+ if msg . header. type_ != MESSAGE_TYPE_RESPONSE {
124
124
resp_tx2
125
125
. send( Err ( Error :: Others ( format!(
126
- "Recver got malformed packet {:?} {:?} " ,
127
- header , body
126
+ "Recver got malformed packet {:?}" ,
127
+ msg
128
128
) ) ) )
129
129
. await
130
130
. unwrap_or_else( |_e| error!( "The request has returned" ) ) ;
131
131
return ;
132
132
}
133
133
134
- resp_tx2. send( Ok ( body ) ) . await . unwrap_or_else( |_e| error!( "The request has returned" ) ) ;
134
+ resp_tx2. send( Ok ( msg . payload ) ) . await . unwrap_or_else( |_e| error!( "The request has returned" ) ) ;
135
135
} ) ;
136
136
}
137
137
Err ( e) => {
@@ -165,26 +165,24 @@ impl Client {
165
165
166
166
/// Requsts a unary request and returns with response.
167
167
pub async fn request ( & self , req : Request ) -> Result < Response > {
168
- let mut buf = Vec :: with_capacity ( req. compute_size ( ) as usize ) ;
169
- {
170
- let mut s = CodedOutputStream :: vec ( & mut buf) ;
171
- req. write_to ( & mut s) . map_err ( err_to_others_err ! ( e, "" ) ) ?;
172
- s. flush ( ) . map_err ( err_to_others_err ! ( e, "" ) ) ?;
173
- }
168
+ let timeout_nano = req. timeout_nano ;
169
+ let msg: GenMessage = Message :: new_request ( 0 , req)
170
+ . try_into ( )
171
+ . map_err ( |e : protobuf:: error:: ProtobufError | Error :: Others ( e. to_string ( ) ) ) ?;
174
172
175
173
let ( tx, mut rx) : ( ResponseSender , ResponseReceiver ) = channel ( 100 ) ;
176
174
self . req_tx
177
- . send ( ( buf , tx) )
175
+ . send ( ( msg , tx) )
178
176
. await
179
177
. map_err ( |e| Error :: Others ( format ! ( "Send packet to sender error {:?}" , e) ) ) ?;
180
178
181
- let result = if req . timeout_nano == 0 {
179
+ let result = if timeout_nano == 0 {
182
180
rx. recv ( )
183
181
. await
184
182
. ok_or_else ( || Error :: Others ( "Receive packet from receiver error" . to_string ( ) ) ) ?
185
183
} else {
186
184
tokio:: time:: timeout (
187
- std:: time:: Duration :: from_nanos ( req . timeout_nano as u64 ) ,
185
+ std:: time:: Duration :: from_nanos ( timeout_nano as u64 ) ,
188
186
rx. recv ( ) ,
189
187
)
190
188
. await
@@ -193,10 +191,8 @@ impl Client {
193
191
} ;
194
192
195
193
let buf = result?;
196
- let mut s = CodedInputStream :: from_bytes ( & buf) ;
197
- let mut res = Response :: new ( ) ;
198
- res. merge_from ( & mut s)
199
- . map_err ( err_to_others_err ! ( e, "Unpack response error " ) ) ?;
194
+ let res =
195
+ Response :: decode ( & buf) . map_err ( err_to_others_err ! ( e, "Unpack response error " ) ) ?;
200
196
201
197
let status = res. get_status ( ) ;
202
198
if status. get_code ( ) != Code :: OK {
0 commit comments