logging refactor
This commit is contained in:
@@ -33,13 +33,13 @@ impl NetworkConnection {
|
||||
Self::WS(w) => w.protocol_type(),
|
||||
}
|
||||
}
|
||||
pub fn send(&self, message: Vec<u8>) -> SystemPinBoxFuture<Result<(), ()>> {
|
||||
pub fn send(&self, message: Vec<u8>) -> SystemPinBoxFuture<Result<(), String>> {
|
||||
match self {
|
||||
Self::Dummy(d) => d.send(message),
|
||||
Self::WS(w) => w.send(message),
|
||||
}
|
||||
}
|
||||
pub fn recv(&self) -> SystemPinBoxFuture<Result<Vec<u8>, ()>> {
|
||||
pub fn recv(&self) -> SystemPinBoxFuture<Result<Vec<u8>, String>> {
|
||||
match self {
|
||||
Self::Dummy(d) => d.recv(),
|
||||
Self::WS(w) => w.recv(),
|
||||
|
@@ -52,27 +52,32 @@ impl WebsocketNetworkConnection {
|
||||
ProtocolType::WS
|
||||
}
|
||||
}
|
||||
pub fn send(&self, message: Vec<u8>) -> SystemPinBoxFuture<Result<(), ()>> {
|
||||
pub fn send(&self, message: Vec<u8>) -> SystemPinBoxFuture<Result<(), String>> {
|
||||
let inner = self.inner.clone();
|
||||
Box::pin(async move {
|
||||
if message.len() > MAX_MESSAGE_SIZE {
|
||||
return Err(());
|
||||
return Err("sending too large WS message".to_owned());
|
||||
}
|
||||
inner.lock().ws.send_with_u8_array(&message).map_err(drop)
|
||||
inner.lock().ws.send_with_u8_array(&message).map_err(map_to_string)
|
||||
})
|
||||
}
|
||||
pub fn recv(&self) -> SystemPinBoxFuture<Result<Vec<u8>, ()>> {
|
||||
pub fn recv(&self) -> SystemPinBoxFuture<Result<Vec<u8>, String)>> {
|
||||
let inner = self.inner.clone();
|
||||
Box::pin(async move {
|
||||
let out = match inner.lock().ws_stream.next().await {
|
||||
Some(WsMessage::Binary(v)) => v,
|
||||
_ => {
|
||||
trace!("websocket recv failed");
|
||||
return Err(());
|
||||
Some(_) => {
|
||||
return Err("Unexpected WS message type".to_owned());
|
||||
}
|
||||
Some(Err(e)) => {
|
||||
return Err(|e| e.to_string());
|
||||
}
|
||||
None => {
|
||||
return Err("WS stream closed".to_owned());
|
||||
}
|
||||
};
|
||||
if out.len() > MAX_MESSAGE_SIZE {
|
||||
Err(())
|
||||
Err("sending too large WS message".to_owned())
|
||||
} else {
|
||||
Ok(out)
|
||||
}
|
||||
|
@@ -28,6 +28,18 @@ pub fn get_timestamp() -> u64 {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_timestamp_string() -> String {
|
||||
let date = Date::now();
|
||||
let hours = Date::get_utc_hours(date);
|
||||
let minutes = Date::get_utc_minutes(date);
|
||||
let seconds = Date::get_utc_seconds(date);
|
||||
let milliseconds = Date::get_utc_milliseconds(date);
|
||||
format!(
|
||||
"{:02}:{:02}:{:02}.{}",
|
||||
hours, minutes, seconds, milliseconds
|
||||
)
|
||||
}
|
||||
|
||||
pub fn random_bytes(dest: &mut [u8]) -> Result<(), String> {
|
||||
let len = dest.len();
|
||||
let u32len = len / 4;
|
||||
|
Reference in New Issue
Block a user