Skip to content

Commit 0e83a3c

Browse files
committed
Added timeout to inet socket
1 parent dda89c3 commit 0e83a3c

File tree

3 files changed

+6
-22
lines changed

3 files changed

+6
-22
lines changed

src/connection/inet_socket_connection.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use crate::{
22
connection::{BaseConnection, Buffer},
33
utils::{Error, READ_BUFFER_SIZE},
44
};
5+
use std::time::Duration;
56

67
use async_std::{
8+
io,
79
net::{Shutdown, TcpStream},
810
prelude::*,
911
};
@@ -35,7 +37,8 @@ impl BaseConnection for InetSocketConnection {
3537
if let Err(err) = result {
3638
return Err(Error::Internal(format!("{}", err)));
3739
}
38-
self.client = Some(result.unwrap());
40+
let client = result.unwrap();
41+
self.client = Some(client);
3942

4043
self.connection_setup = true;
4144
Ok(())
@@ -72,9 +75,9 @@ impl BaseConnection for InetSocketConnection {
7275
let mut read_size = READ_BUFFER_SIZE;
7376
while read_size > 0 {
7477
let mut buf = [0u8; READ_BUFFER_SIZE];
75-
let result = client.read(&mut buf).await;
78+
let result = io::timeout(Duration::from_millis(10), client.read(&mut buf)).await;
7679
if result.is_err() {
77-
return None;
80+
return Some(buffer);
7881
}
7982
read_size = result.unwrap();
8083
buffer.extend(buf[..read_size].iter());

src/protocol/base_protocol.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,4 @@ impl BaseProtocol {
126126
_ => panic!("Currently, only JsonProtocol is supported"),
127127
}
128128
}
129-
130-
pub fn decode(&self, data: &[u8]) -> BaseMessage {
131-
match self {
132-
BaseProtocol::JsonProtocol { .. } => json_protocol::decode(&self, data),
133-
_ => panic!("Currently, only JsonProtocol is supported"),
134-
}
135-
}
136129
}

src/protocol/json_protocol.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,6 @@ pub fn get_next_message(protocol: &mut BaseProtocol) -> Option<BaseMessage> {
164164
}
165165
}
166166

167-
pub fn decode(protocol: &BaseProtocol, data: &[u8]) -> BaseMessage {
168-
match protocol {
169-
BaseProtocol::JsonProtocol { .. } => match decode_internal(data) {
170-
Some(message) => message,
171-
None => BaseMessage::Unknown {
172-
request_id: String::default(),
173-
},
174-
},
175-
_ => panic!("BaseProtocol tried to decode a non-JsonProtocol as a JsonProtocol"),
176-
}
177-
}
178-
179167
fn decode_internal(data: &[u8]) -> Option<BaseMessage> {
180168
let result: Result<Value> = from_slice(data);
181169
if result.is_err() {

0 commit comments

Comments
 (0)