From 5757db111b61b28965abd8d4a6477771f549817d Mon Sep 17 00:00:00 2001 From: Parbez Date: Sun, 13 Aug 2023 11:05:23 +0200 Subject: [PATCH] fix: misc changes --- client.js | 9 ++++++++- src/canvas/functions.rs | 3 +-- src/canvas/mod.rs | 16 +++++----------- src/canvas/request.rs | 6 ++---- src/main.rs | 12 +++++++----- src/mongo/functions.rs | 2 -- src/tcp/message_handler.rs | 31 ++++++++++++++++--------------- src/tcp/mod.rs | 2 +- 8 files changed, 40 insertions(+), 41 deletions(-) diff --git a/client.js b/client.js index 9c88bd8..821c442 100644 --- a/client.js +++ b/client.js @@ -4,6 +4,13 @@ import { Timer } from './Timer.js'; const name = process.argv[2]; +const Job = /**@type {const}*/ ({ + Ping: 0, + Pong: 1, + GenerateImage: 2, + Unknown: 3, +}); + const client = new Client(name, { maximumRetries: Infinity, retryTime: 2_000, @@ -38,7 +45,7 @@ await sleep(5000); const timer = new Timer(); for (let i = 0; i < 100; i++) { // Send a message to the Rust server - const message = { payload: `ping` }; + const message = { payload: `ping`, job: Job.Ping }; let thing = timer.time(); const msg = await client.sendTo('Sofi', message, { receptive: true }); diff --git a/src/canvas/functions.rs b/src/canvas/functions.rs index f30a7e1..9e613f5 100644 --- a/src/canvas/functions.rs +++ b/src/canvas/functions.rs @@ -1,10 +1,9 @@ -use std::{cell::RefCell, fs::File, io::Read, rc::Rc}; +use std::{fs::File, io::Read}; use skia_safe::{ font_style::{Slant, Weight, Width}, Font, FontStyle, Typeface, }; -use tokio; use super::Canvas; // 0.2.21, features = ["macros"] diff --git a/src/canvas/mod.rs b/src/canvas/mod.rs index 4beae5e..f32c562 100644 --- a/src/canvas/mod.rs +++ b/src/canvas/mod.rs @@ -1,13 +1,10 @@ use std::mem; pub mod functions; pub mod request; -use image::EncodableLayout; use skia_safe::{ - image::{BitDepth, CachingHint}, - AlphaType, Color, ColorSpace, ColorType, Data, EncodedImageFormat, Font, ISize, Image, - ImageGenerator, ImageInfo, Paint, PaintStyle, Path, Picture, Point, Rect, Surface, + Color, Data, EncodedImageFormat, Font, Image, ImageGenerator, Paint, PaintStyle, Path, Point, + Rect, Surface, }; -use webp::Encoder; pub struct Canvas { surface: Surface, path: Path, @@ -107,12 +104,9 @@ impl Canvas { #[inline] pub fn webp(&mut self) -> Vec { let image = self.surface.image_snapshot(); - let png_data = image.encode_to_data(EncodedImageFormat::WEBP).unwrap(); - // let image_bytes = image::load_from_memory(png_data.as_bytes()).unwrap(); - - // let encoder = Encoder::from_image(&image_bytes).unwrap(); - - // encoder.encode(100.).to_vec() + let png_data = image + .encode_to_data_with_quality(EncodedImageFormat::WEBP, 80) + .unwrap(); png_data.as_bytes().to_vec() } diff --git a/src/canvas/request.rs b/src/canvas/request.rs index bbd7f06..d35ba19 100644 --- a/src/canvas/request.rs +++ b/src/canvas/request.rs @@ -15,13 +15,11 @@ impl Request { pub async fn fetch_buffer(&self, url: &str) -> Vec { let res = self.client.get(url).send().await.unwrap(); - let buffer = res.bytes().await.unwrap().to_vec(); - buffer + res.bytes().await.unwrap().to_vec() } } pub async fn fetch_buffer(url: &str) -> Vec { let res = reqwest::get(url).await.unwrap(); - let buffer = res.bytes().await.expect("issue").to_vec(); - buffer + res.bytes().await.expect("issue").to_vec() } diff --git a/src/main.rs b/src/main.rs index 8d812e1..9422a8a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,11 +81,11 @@ async fn main() -> Result<(), Box> { let _three_uid = _three_cards.2.get("unique_id").unwrap().as_str().unwrap(); // let one = format!("{_one_uid}:character_cards:buffer"); - let one = "e0d140e3-a33f-45cd-a156-661ddc921ab3:character_cards:buffer".to_string(); + let one = "8a89afd4-36c2-476b-929c-d050e83b0578:character_cards:buffer".to_string(); // let two = format!("{_two_uid}:character_cards:buffer"); - let two = "c09404d4-f4e2-4c9c-ba13-3c2eacf486b1:character_cards:buffer".to_string(); + let two = "3286f1af-e030-474b-9d34-cb4249196952:character_cards:buffer".to_string(); // let three = format!("{_three_uid}:character_cards:buffer"); - let three = "ec153239-a238-4483-81b1-f8c38cc29eb1:character_cards:buffer".to_string(); + let three = "3343cf53-bacd-49d0-8240-6dea019be172:character_cards:buffer".to_string(); let images = connection.mget(vec![one, two, three]).unwrap(); let image_one = deserialize_buffer(&images[0]).buffer; @@ -146,11 +146,13 @@ async fn main() -> Result<(), Box> { 465.0, ); - let drop_image = canvas.webp(); + let drop = canvas.webp(); let name = format!("./out/{}.{}.webp", 1, Utc::now().timestamp_millis()); + let mut file = File::create(name).unwrap(); - file.write_all(&drop_image).unwrap(); + + file.write_all(&drop).unwrap(); println!("Time taken: {:?}", Instant::now() - start); }); diff --git a/src/mongo/functions.rs b/src/mongo/functions.rs index 34a6f40..d26f7e7 100644 --- a/src/mongo/functions.rs +++ b/src/mongo/functions.rs @@ -1,5 +1,3 @@ -use std::time::{Duration, Instant}; - use mongodb::{ bson::{doc, Document}, options::FindOneOptions, diff --git a/src/tcp/message_handler.rs b/src/tcp/message_handler.rs index c9ddcbc..d70716f 100644 --- a/src/tcp/message_handler.rs +++ b/src/tcp/message_handler.rs @@ -1,33 +1,34 @@ use serde::{Deserialize, Serialize}; -// #[derive(Serialize, Deserialize, Debug)] -// pub enum Job { -// Ping, -// Pong, -// GenerateImage, -// } +#[derive(Serialize, Deserialize, Debug)] +pub enum Job { + Ping, + Pong, + GenerateImage, + Unknown, +} #[derive(Serialize, Deserialize, Debug)] pub struct Payload { - // pub job: Job, - pub payload: String, + pub job: Job, + pub data: String, } pub struct IncomingMessage { pub id: u64, pub receptive: bool, - pub data: Payload, + pub payload: Payload, } pub async fn handle_message(message: IncomingMessage) -> Option { - match message.data.payload.as_str() { - "ping" => Some(Payload { - // job: Job::Pong, - payload: "pong".to_string(), + match message.payload.job { + Job::Ping => Some(Payload { + job: Job::Pong, + data: "pong".to_string(), }), _ => Some(Payload { - // job: Job::Pong, - payload: "unknown".to_string(), + job: Job::Unknown, + data: "unknown".to_string(), }), } } diff --git a/src/tcp/mod.rs b/src/tcp/mod.rs index 73bf856..c2ca95e 100644 --- a/src/tcp/mod.rs +++ b/src/tcp/mod.rs @@ -86,7 +86,7 @@ pub async fn handle_connection( let message = IncomingMessage { id: header.id, receptive: header.receptive, - data: message, + payload: message, }; let data = handle_message(message).await;