Create tcp server
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
use std::{
|
||||
error::Error,
|
||||
sync::{Arc, Mutex},
|
||||
thread,
|
||||
};
|
||||
use tokio::{
|
||||
io::{AsyncReadExt, AsyncWriteExt as _, BufReader, BufWriter},
|
||||
net::{
|
||||
tcp::{OwnedReadHalf, OwnedWriteHalf},
|
||||
TcpListener, TcpStream,
|
||||
},
|
||||
select,
|
||||
sync::oneshot,
|
||||
};
|
||||
|
||||
use crate::emulator::{EmulatorClient, EmulatorCommand, SimId};
|
||||
|
||||
pub struct GdbServer {
|
||||
sim_id: SimId,
|
||||
client: EmulatorClient,
|
||||
status: Arc<Mutex<GdbServerStatus>>,
|
||||
killer: Option<oneshot::Sender<()>>,
|
||||
}
|
||||
|
||||
impl GdbServer {
|
||||
pub fn new(sim_id: SimId, client: EmulatorClient) -> Self {
|
||||
Self {
|
||||
sim_id,
|
||||
client,
|
||||
status: Arc::new(Mutex::new(GdbServerStatus::Stopped)),
|
||||
killer: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn status(&self) -> GdbServerStatus {
|
||||
self.status.lock().unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn start(&mut self, port: u16) {
|
||||
let sim_id = self.sim_id;
|
||||
let client = self.client.clone();
|
||||
let status = self.status.clone();
|
||||
let (tx, rx) = oneshot::channel();
|
||||
self.killer = Some(tx);
|
||||
thread::spawn(move || {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap()
|
||||
.block_on(async move {
|
||||
select! {
|
||||
_ = run_server(sim_id, client, port, &status) => {}
|
||||
_ = rx => {
|
||||
*status.lock().unwrap() = GdbServerStatus::Stopped;
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
pub fn stop(&mut self) {
|
||||
if let Some(killer) = self.killer.take() {
|
||||
let _ = killer.send(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_server(
|
||||
sim_id: SimId,
|
||||
client: EmulatorClient,
|
||||
port: u16,
|
||||
status: &Mutex<GdbServerStatus>,
|
||||
) {
|
||||
client.send_command(EmulatorCommand::Pause);
|
||||
let Some(stream) = try_connect(port, status).await else {
|
||||
return;
|
||||
};
|
||||
let connection = GdbConnection::new(sim_id, client, stream);
|
||||
match connection.run().await {
|
||||
Ok(()) => {
|
||||
*status.lock().unwrap() = GdbServerStatus::Stopped;
|
||||
}
|
||||
Err(error) => {
|
||||
*status.lock().unwrap() = GdbServerStatus::Error(error.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn try_connect(port: u16, status: &Mutex<GdbServerStatus>) -> Option<TcpStream> {
|
||||
*status.lock().unwrap() = GdbServerStatus::Connecting;
|
||||
let listener = match TcpListener::bind(("127.0.0.1", port)).await {
|
||||
Ok(l) => l,
|
||||
Err(err) => {
|
||||
*status.lock().unwrap() = GdbServerStatus::Error(err.to_string());
|
||||
return None;
|
||||
}
|
||||
};
|
||||
match listener.accept().await {
|
||||
Ok((stream, _)) => {
|
||||
*status.lock().unwrap() = GdbServerStatus::Running;
|
||||
Some(stream)
|
||||
}
|
||||
Err(err) => {
|
||||
*status.lock().unwrap() = GdbServerStatus::Error(err.to_string());
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum GdbServerStatus {
|
||||
Stopped,
|
||||
Connecting,
|
||||
Running,
|
||||
Error(String),
|
||||
}
|
||||
|
||||
impl GdbServerStatus {
|
||||
pub fn running(&self) -> bool {
|
||||
matches!(self, Self::Connecting | Self::Running)
|
||||
}
|
||||
}
|
||||
|
||||
struct GdbConnection {
|
||||
sim_id: SimId,
|
||||
client: EmulatorClient,
|
||||
stream_in: BufReader<OwnedReadHalf>,
|
||||
stream_out: BufWriter<OwnedWriteHalf>,
|
||||
}
|
||||
|
||||
impl GdbConnection {
|
||||
fn new(sim_id: SimId, client: EmulatorClient, stream: TcpStream) -> Self {
|
||||
let (rx, tx) = stream.into_split();
|
||||
Self {
|
||||
sim_id,
|
||||
client,
|
||||
stream_in: BufReader::new(rx),
|
||||
stream_out: BufWriter::new(tx),
|
||||
}
|
||||
}
|
||||
async fn run(mut self) -> Result<(), Box<dyn Error>> {
|
||||
println!("Connected for {}", self.sim_id);
|
||||
self.client.send_command(EmulatorCommand::Resume);
|
||||
loop {
|
||||
let byte = self.read_byte().await?;
|
||||
println!("{byte}");
|
||||
self.stream_out.write_u8(byte).await?;
|
||||
}
|
||||
}
|
||||
|
||||
async fn read_byte(&mut self) -> std::io::Result<u8> {
|
||||
self.stream_in.read_u8().await
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user