feat: true async

This commit is contained in:
Noa Aarts 2024-07-12 15:59:27 +02:00
parent 330568ec2f
commit c8df06aa7e
4 changed files with 72 additions and 9 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake

27
flake.lock generated Normal file
View file

@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1720542800,
"narHash": "sha256-ZgnNHuKV6h2+fQ5LuqnUaqZey1Lqqt5dTUAiAnqH0QQ=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "feb2849fdeb70028c70d73b848214b00d324a497",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

25
flake.nix Normal file
View file

@ -0,0 +1,25 @@
{
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
outputs = {self, nixpkgs, ...}:
let
allSystems = [
"x86_64-linux" # 64-bit Intel/AMD Linux
"aarch64-linux" # 64-bit ARM Linux
"x86_64-darwin" # 64-bit Intel macOS
"aarch64-darwin" # 64-bit ARM macOS
];
forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f {
inherit system;
pkgs = import nixpkgs { inherit system; };
});
in
{
devShell = forAllSystems ({ system, pkgs }:
pkgs.mkShell {
buildInputs = [
pkgs.rustup
];
});
};
}

View file

@ -1,9 +1,12 @@
#![feature(test)] #![feature(test)]
#![feature(sync_unsafe_cell)]
use std::{ use std::{
cell::SyncUnsafeCell,
io::{self, Error, ErrorKind}, io::{self, Error, ErrorKind},
iter::{self, once}, iter::{self, once},
vec, sync::Arc,
usize, vec,
}; };
use tokio::{ use tokio::{
@ -94,7 +97,7 @@ fn get_pixel(grids: &mut [FlutGrid<u32>], canvas: u8, x: u16, y: u16) -> Option<
async fn process_msg<T: AsyncReadExt + AsyncWriteExt + std::marker::Unpin>( async fn process_msg<T: AsyncReadExt + AsyncWriteExt + std::marker::Unpin>(
stream: &mut T, stream: &mut T,
grids: &mut [FlutGrid<u32>], grids: &mut [FlutGrid<u32>; GRID_LENGTH],
) -> io::Result<()> { ) -> io::Result<()> {
match stream.read_u8().await { match stream.read_u8().await {
Ok(i) => match i { Ok(i) => match i {
@ -209,7 +212,7 @@ async fn process_msg<T: AsyncReadExt + AsyncWriteExt + std::marker::Unpin>(
async fn process_socket<T: AsyncRead + AsyncWrite + std::marker::Unpin>( async fn process_socket<T: AsyncRead + AsyncWrite + std::marker::Unpin>(
socket: T, socket: T,
grids: &mut [FlutGrid<u32>], grids: &mut [FlutGrid<u32>; GRID_LENGTH],
) -> io::Result<()> { ) -> io::Result<()> {
let mut stream = BufStream::new(socket); let mut stream = BufStream::new(socket);
loop { loop {
@ -220,19 +223,26 @@ async fn process_socket<T: AsyncRead + AsyncWrite + std::marker::Unpin>(
} }
} }
const GRID_LENGTH: usize = 1;
#[tokio::main] #[tokio::main]
async fn main() -> io::Result<()> { async fn main() -> io::Result<()> {
let mut grids = [FlutGrid::init(800, 600, 0xff00ff)]; let grids = [FlutGrid::init(800, 600, 0xff00ff)];
assert_eq!(grids.len(), GRID_LENGTH);
let flut_listener = TcpListener::bind("0.0.0.0:7791").await?; let flut_listener = TcpListener::bind("0.0.0.0:7791").await?;
let asuc = Arc::new(SyncUnsafeCell::new(grids));
loop { loop {
let (socket, _) = flut_listener.accept().await?; let (socket, _) = flut_listener.accept().await?;
match process_socket(socket, &mut grids).await { let asuc = asuc.clone();
Ok(_) => println!("socket closed okay"), let _ = tokio::spawn(async move {
Err(err) => { let grids = unsafe { asuc.get().as_mut().unwrap() };
eprintln!("received illegal command: {}", err) match process_socket(socket, grids).await {
Ok(()) => return Ok(()),
Err(err) => return Err(err),
} }
} });
} }
} }