11use crate :: common:: {
2- MAX_F32_SAMPLES_TO_SEND , PROGRAM_NAME , VizUdpClient , VizUdpServer ,
2+ MAX_F32_SAMPLES_TO_SEND , PROGRAM_NAME , SendStatus , VizUdpClient ,
3+ VizUdpServer , samples_from_ne_bytes, samples_to_ne_bytes,
34} ;
45use std:: {
56 net:: { SocketAddr , ToSocketAddrs } ,
@@ -19,8 +20,8 @@ impl Default for Config {
1920 Self {
2021 width : 100 ,
2122 height : 100 ,
22- red : 0 ,
23- green : 255 ,
23+ red : 127 ,
24+ green : 0 ,
2425 blue : 0 ,
2526 }
2627 }
@@ -36,7 +37,7 @@ fn start_client_app(
3637 command. args ( [
3738 format ! ( "--server={}" , server_addr. to_string( ) ) ,
3839 format ! ( "--title={}" , title. to_string( ) ) ,
39- "oscilloscope " . to_string ( ) ,
40+ "blink " . to_string ( ) ,
4041 format ! ( "--width={}" , config. width) ,
4142 format ! ( "--height={}" , config. height) ,
4243 format ! ( "--red={}" , config. red) ,
@@ -45,3 +46,85 @@ fn start_client_app(
4546 ] ) ;
4647 Ok ( command. spawn ( ) ?)
4748}
49+
50+ pub struct Server {
51+ raw : VizUdpServer ,
52+ buf : Vec < f32 > ,
53+ }
54+
55+ fn send_samples (
56+ server : & mut VizUdpServer ,
57+ samples : & [ f32 ] ,
58+ ) -> anyhow:: Result < SendStatus > {
59+ for samples_chunk in samples. chunks ( MAX_F32_SAMPLES_TO_SEND ) {
60+ samples_to_ne_bytes ( samples_chunk, & mut server. buf ) ;
61+ if !server. send_buf ( ) ? {
62+ return Ok ( SendStatus :: Disconnected ) ;
63+ }
64+ }
65+ Ok ( SendStatus :: Success )
66+ }
67+
68+ impl Server {
69+ pub fn new ( title : & str , config : Config ) -> anyhow:: Result < Self > {
70+ Ok ( Self {
71+ raw : VizUdpServer :: new ( |server_addr| {
72+ let _client_process = start_client_app (
73+ server_addr,
74+ PROGRAM_NAME ,
75+ title,
76+ & config,
77+ ) ?;
78+ Ok ( ( ) )
79+ } ) ?,
80+ buf : Vec :: new ( ) ,
81+ } )
82+ }
83+
84+ pub fn send_samples (
85+ & mut self ,
86+ samples : & [ f32 ] ,
87+ ) -> anyhow:: Result < SendStatus > {
88+ send_samples ( & mut self . raw , samples)
89+ }
90+
91+ pub fn send_samples_batched (
92+ & mut self ,
93+ samples : & [ f32 ] ,
94+ ) -> anyhow:: Result < ( ) > {
95+ self . buf . extend_from_slice ( samples) ;
96+ if self . buf . len ( ) >= MAX_F32_SAMPLES_TO_SEND {
97+ send_samples ( & mut self . raw , & self . buf ) ?;
98+ self . buf . clear ( ) ;
99+ }
100+ Ok ( ( ) )
101+ }
102+ }
103+
104+ pub struct Client {
105+ raw : VizUdpClient ,
106+ buf : Vec < f32 > ,
107+ }
108+
109+ impl Client {
110+ pub fn new < A : ToSocketAddrs > ( server_address : A ) -> anyhow:: Result < Self > {
111+ Ok ( Self {
112+ raw : VizUdpClient :: new ( server_address) ?,
113+ buf : Vec :: new ( ) ,
114+ } )
115+ }
116+
117+ pub fn recv_sample ( & mut self ) -> anyhow:: Result < bool > {
118+ Ok ( match self . raw . recv ( ) ? {
119+ Some ( buf_raw) => {
120+ samples_from_ne_bytes ( buf_raw, & mut self . buf ) ;
121+ true
122+ }
123+ None => false ,
124+ } )
125+ }
126+
127+ pub fn iter ( & self ) -> impl Iterator < Item = f32 > {
128+ self . buf . iter ( ) . cloned ( )
129+ }
130+ }
0 commit comments