Skip to content

Commit

Permalink
MessageType impl send/recv async
Browse files Browse the repository at this point in the history
  • Loading branch information
alembiq committed Jun 29, 2024
1 parent 3d25e9e commit a1fa5df
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 22 deletions.
2 changes: 2 additions & 0 deletions lesson15/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
files/*
images/*
16 changes: 0 additions & 16 deletions lesson15/files/Cargo.toml

This file was deleted.

Binary file removed lesson15/images/1719588157.png
Binary file not shown.
Binary file removed lesson15/images/1719588650.png
Binary file not shown.
Binary file removed lesson15/images/1719588730.png
Binary file not shown.
55 changes: 49 additions & 6 deletions lesson15/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// use anyhow::{Context, Result}; //FIXME remove
// use bincode;
use image::codecs::png::PngEncoder;
use image::ImageEncoder;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
// use tokio::net::TcpStream;

use std::error::Error;
use std::fs::{self};
use std::io::{Cursor, Read, Write};
use std::net::{IpAddr, TcpStream};
use std::marker::Unpin;
use std::net::{IpAddr, TcpStream as TcpSync};
use std::path::Path;
use std::process;

Expand Down Expand Up @@ -66,7 +70,49 @@ pub enum MessageType {
Text(String),
}

pub fn message_incoming(stream: &mut TcpStream) -> Result<MessageType, ErrorMessage> {
impl MessageType {
pub async fn recv<T: AsyncReadExt + Unpin>(stream: &mut T) -> Result<Self, ErrorMessage> {
let mut length_bytes = [0; 4];

stream
.read_exact(&mut length_bytes)
.await
.expect("{timestamp()} Failed to read length");

let length = u32::from_be_bytes(length_bytes);

let mut buf = vec![0; length as usize];

stream
.read_exact(&mut buf)
.await
.expect("{timestamp()} Failed to read message");

Ok(bincode::deserialize(&buf).expect("{timestamp()} Unable to deserialize message."))
}

pub async fn send<T: AsyncWriteExt + Unpin + AsyncReadExt>(
&self,
stream: &mut T,
) -> Result<(), ErrorMessage> {
let serialized: Vec<u8> =
bincode::serialize(&self).expect("{timestamp()} Failed to serialize message");

let length = (serialized.len() as u32).to_be_bytes();

stream
.write_all(&length)
.await
.expect("{timestamp()} Failed to send length");
stream
.write_all(&serialized)
.await
.expect("{timestamp()} Failed to send message");
Ok(())
}
}

pub fn message_incoming(stream: &mut TcpSync) -> Result<MessageType, ErrorMessage> {
let mut len_bytes = [0; 4];
stream.read_exact(&mut len_bytes)?;
let len = u32::from_be_bytes(len_bytes) as usize;
Expand All @@ -76,10 +122,7 @@ pub fn message_incoming(stream: &mut TcpStream) -> Result<MessageType, ErrorMess
Ok(ciborium::from_reader(&mut &buffer[..])?)
}

pub fn message_outgoing(
stream: &mut TcpStream,
message: &MessageType,
) -> Result<(), Box<dyn Error>> {
pub fn message_outgoing(stream: &mut TcpSync, message: &MessageType) -> Result<(), Box<dyn Error>> {
let mut buffer = Vec::new();
ciborium::into_writer(message, &mut buffer)?;
let len = buffer.len() as u32;
Expand Down

0 comments on commit a1fa5df

Please sign in to comment.