1use std::io::{Read, Write};
4
5use msrt::endpoint::{ClientEndpoint, EngineConfig, PeerState};
6
7use crate::error::Result;
8use crate::event::AdapterEvent;
9use crate::io::IoState;
10
11#[derive(Debug)]
13pub struct StdFrontend<T> {
14 io: T,
15 endpoint: ClientEndpoint,
16 state: IoState,
17}
18
19impl<T> StdFrontend<T>
20where
21 T: Read + Write,
22{
23 pub fn new(io: T) -> Self {
25 Self::with_config(io, EngineConfig::default())
26 }
27
28 pub fn with_config(io: T, config: EngineConfig) -> Self {
30 Self {
31 io,
32 endpoint: ClientEndpoint::new(config),
33 state: IoState::new(),
34 }
35 }
36
37 pub fn peer_state(&self) -> PeerState {
39 self.endpoint.peer().state()
40 }
41
42 pub const fn io(&self) -> &T {
44 &self.io
45 }
46
47 pub fn io_mut(&mut self) -> &mut T {
49 &mut self.io
50 }
51
52 pub fn into_inner(self) -> T {
54 self.io
55 }
56
57 pub fn connect(&mut self) -> Result<()> {
59 self.endpoint.connect(self.state.now_ms())?;
60 Ok(())
61 }
62
63 pub fn disconnect(&mut self) {
65 self.endpoint.disconnect();
66 }
67
68 pub fn send(&mut self, message: &[u8]) -> Result<bool> {
70 Ok(self.endpoint.send(message)?.is_some())
71 }
72
73 pub fn receive_available(&mut self) -> Result<usize> {
78 self.state.receive_available(&mut self.io, |now_ms, bytes| {
79 self.endpoint.receive(now_ms, bytes)
80 })
81 }
82
83 pub fn poll(&mut self) -> Result<AdapterEvent> {
85 self.state.poll(&mut self.io, |now_ms, tx_buf| {
86 self.endpoint.poll(now_ms, tx_buf)
87 })
88 }
89
90 pub fn tick(&mut self) -> Result<AdapterEvent> {
92 let _ = self.receive_available()?;
93 self.poll()
94 }
95}