Skip to content

Commit

Permalink
use
Browse files Browse the repository at this point in the history
Signed-off-by: wthrajat <rajatkhanduri290102@gmail.com>
  • Loading branch information
wthrajat committed Jan 7, 2024
1 parent 07ae3f2 commit ce675da
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ bip39 = {version = "1.0.1", features = ["rand"] }
bitcoin = {version = "0.30", features = ["rand"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_cbor = "0.11.2"
tokio = { version = "1.16.1", features = ["full"] }
log = "^0.4"
env_logger = "0.7"
Expand Down
7 changes: 7 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum NetError {
Json(serde_json::Error),
ReachedEOF,
ConnectionTimedOut,
Cbor(serde_cbor::Error)
}

impl From<std::io::Error> for NetError {
Expand All @@ -23,6 +24,12 @@ impl From<serde_json::Error> for NetError {
}
}

impl From<serde_cbor::Error> for NetError {
fn from(value: serde_cbor::Error) -> Self {
Self::Cbor(value)
}
}

/// Includes all Protocol-level errors.
#[derive(Debug)]
pub enum ProtocolError {
Expand Down
9 changes: 4 additions & 5 deletions src/maker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{
use bitcoin::Network;
use bitcoind::bitcoincore_rpc::RpcApi;
use tokio::{
io::{AsyncBufReadExt, BufReader},
io::{BufReader, AsyncReadExt},
net::TcpListener,
select,
sync::mpsc,
Expand Down Expand Up @@ -191,9 +191,9 @@ pub async fn start_maker_server(maker: Arc<Maker>) -> Result<(), MakerError> {
log::info!("[{}] ===> MakerHello", maker_clone.config.port);

loop {
let mut line = String::new();
let mut line : Vec<u8> = Vec::new();
select! {
readline_ret = reader.read_line(&mut line) => {
readline_ret = reader.read_to_end(&mut line) => {
match readline_ret {
Ok(0) => {
log::info!("[{}] Connection closed by peer", maker_clone.config.port);
Expand All @@ -212,8 +212,7 @@ pub async fn start_maker_server(maker: Arc<Maker>) -> Result<(), MakerError> {
},
};

line = line.trim_end().to_string();
let message: TakerToMakerMessage = serde_json::from_str(&line).unwrap();
let message: TakerToMakerMessage = serde_cbor::from_slice(&line).unwrap();
log::info!("[{}] <=== {} ", maker_clone.config.port, message);

let message_result: Result<Option<MakerToTakerMessage>, MakerError> =
Expand Down
13 changes: 6 additions & 7 deletions src/utill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::{

use serde_json::Value;
use tokio::{
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
io::{AsyncWriteExt, BufReader, AsyncReadExt},
net::tcp::{ReadHalf, WriteHalf},
};

Expand Down Expand Up @@ -80,7 +80,7 @@ pub fn setup_logger() {
.default_filter_or("coinswap=info")
.default_write_style_or("always"),
)
.is_test(true)
//.is_test(true)
.init();
});
}
Expand All @@ -90,8 +90,7 @@ pub async fn send_message(
socket_writer: &mut WriteHalf<'_>,
message: &impl serde::Serialize,
) -> Result<(), NetError> {
let mut message_bytes = serde_json::to_vec(message).map_err(std::io::Error::from)?;
message_bytes.push(b'\n');
let message_bytes = serde_cbor::to_vec(message).map_err(NetError::Cbor)?;
socket_writer.write_all(&message_bytes).await?;
Ok(())
}
Expand All @@ -100,12 +99,12 @@ pub async fn send_message(
pub async fn read_message(
reader: &mut BufReader<ReadHalf<'_>>,
) -> Result<MakerToTakerMessage, NetError> {
let mut line = String::new();
let n = reader.read_line(&mut line).await?;
let mut line: Vec<u8> = Vec::new();
let n = reader.read_to_end(&mut line).await?;
if n == 0 {
return Err(NetError::ReachedEOF);
}
let message: MakerToTakerMessage = serde_json::from_str(&line)?;
let message: MakerToTakerMessage = serde_cbor::from_slice(&line)?;
log::debug!("<== {:#?}", message);
Ok(message)
}
Expand Down

0 comments on commit ce675da

Please sign in to comment.