27
27
} ,
28
28
indexmap:: IndexMap ,
29
29
std:: sync:: Arc ,
30
+ std:: time:: Duration ,
30
31
//crate::utils::print::print,
31
32
streamhub:: define:: StreamHubEventSender ,
32
33
tokio:: { net:: TcpStream , sync:: Mutex } ,
@@ -67,6 +68,7 @@ pub enum ClientSessionType {
67
68
}
68
69
pub struct ClientSession {
69
70
io : Arc < Mutex < Box < dyn TNetIO + Send + Sync > > > ,
71
+ timeout : Option < Duration > ,
70
72
common : Common ,
71
73
handshaker : SimpleHandshakeClient ,
72
74
unpacketizer : ChunkUnpacketizer ,
@@ -115,6 +117,7 @@ impl ClientSession {
115
117
116
118
Self {
117
119
io : Arc :: clone ( & net_io) ,
120
+ timeout : None ,
118
121
common,
119
122
handshaker : SimpleHandshakeClient :: new ( Arc :: clone ( & net_io) ) ,
120
123
unpacketizer : ChunkUnpacketizer :: new ( ) ,
@@ -129,6 +132,10 @@ impl ClientSession {
129
132
gop_num,
130
133
}
131
134
}
135
+
136
+ pub fn set_timeout ( & mut self , timeout : Duration ) {
137
+ self . timeout = Some ( timeout)
138
+ }
132
139
133
140
pub async fn run ( & mut self ) -> Result < ( ) , SessionError > {
134
141
loop {
@@ -169,7 +176,10 @@ impl ClientSession {
169
176
ClientSessionState :: WaitStateChange => { }
170
177
}
171
178
172
- let data = self . io . lock ( ) . await . read ( ) . await ?;
179
+ let data = match self . timeout {
180
+ None => self . io . lock ( ) . await . read ( ) . await ?,
181
+ Some ( t) => self . io . lock ( ) . await . read_timeout ( t) . await ?,
182
+ } ;
173
183
self . unpacketizer . extend_data ( & data[ ..] ) ;
174
184
175
185
loop {
0 commit comments