diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..b04907b --- /dev/null +++ b/flake.lock @@ -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 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..06c87d8 --- /dev/null +++ b/flake.nix @@ -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 + ]; + }); + }; +} diff --git a/src/main.rs b/src/main.rs index 3a76740..cd2bb43 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,12 @@ #![feature(test)] +#![feature(sync_unsafe_cell)] use std::{ + cell::SyncUnsafeCell, io::{self, Error, ErrorKind}, iter::{self, once}, - vec, + sync::Arc, + usize, vec, }; use tokio::{ @@ -94,7 +97,7 @@ fn get_pixel(grids: &mut [FlutGrid], canvas: u8, x: u16, y: u16) -> Option< async fn process_msg( stream: &mut T, - grids: &mut [FlutGrid], + grids: &mut [FlutGrid; GRID_LENGTH], ) -> io::Result<()> { match stream.read_u8().await { Ok(i) => match i { @@ -209,7 +212,7 @@ async fn process_msg( async fn process_socket( socket: T, - grids: &mut [FlutGrid], + grids: &mut [FlutGrid; GRID_LENGTH], ) -> io::Result<()> { let mut stream = BufStream::new(socket); loop { @@ -220,19 +223,26 @@ async fn process_socket( } } +const GRID_LENGTH: usize = 1; + #[tokio::main] 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 asuc = Arc::new(SyncUnsafeCell::new(grids)); loop { let (socket, _) = flut_listener.accept().await?; - match process_socket(socket, &mut grids).await { - Ok(_) => println!("socket closed okay"), - Err(err) => { - eprintln!("received illegal command: {}", err) + let asuc = asuc.clone(); + let _ = tokio::spawn(async move { + let grids = unsafe { asuc.get().as_mut().unwrap() }; + match process_socket(socket, grids).await { + Ok(()) => return Ok(()), + Err(err) => return Err(err), } - } + }); } }