mirror of https://github.com/MedzikUser/servers
feat(tcp client): add fn `peer_addr`
This commit is contained in:
parent
3fb0a1132a
commit
fc5afe56a1
|
@ -55,7 +55,7 @@ impl Event for PluginTest {
|
|||
/// Event function
|
||||
async fn execute(&self, client: &mut Client) -> Result<()> {
|
||||
client
|
||||
.send(&format!("Welcome {}", client.stream.peer_addr().unwrap()))
|
||||
.send(format!("Welcome {}", client.peer_addr().unwrap()))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -51,7 +51,6 @@ async fn start_tcp_server(host: String, port: String) -> anyhow::Result<()> {
|
|||
tokio::spawn(async move {
|
||||
// get ip address of the client
|
||||
let ip = client
|
||||
.stream
|
||||
.peer_addr()
|
||||
.expect("failed to get peer address");
|
||||
|
||||
|
|
|
@ -131,7 +131,7 @@
|
|||
//! /// Event function
|
||||
//! async fn execute(&self, client: &mut Client) -> Result<()> {
|
||||
//! client
|
||||
//! .send(&format!("Welcome {}", client.stream.peer_addr()?))
|
||||
//! .send(format!("Welcome {}", client.peer_addr()?))
|
||||
//! .await?;
|
||||
//!
|
||||
//! Ok(())
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
use core::fmt;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use tokio::{
|
||||
io::{self, AsyncReadExt, AsyncWriteExt},
|
||||
net::TcpStream,
|
||||
net::{TcpStream},
|
||||
};
|
||||
|
||||
/// Max size of a TCP packet
|
||||
|
@ -40,7 +41,7 @@ impl Client {
|
|||
/// Send message to the client
|
||||
pub async fn send<S>(&mut self, content: S) -> io::Result<()>
|
||||
where
|
||||
S: ToString + fmt::Debug + fmt::Display,
|
||||
S: ToString + fmt::Display,
|
||||
{
|
||||
// add a new line at the end of the content
|
||||
let content = format!("{content}\n\r");
|
||||
|
@ -48,4 +49,8 @@ impl Client {
|
|||
// send message
|
||||
self.stream.write_all(content.as_bytes()).await
|
||||
}
|
||||
|
||||
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
||||
self.stream.peer_addr()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ pub async fn handle_connection(
|
|||
mut client: Client,
|
||||
plugin_manager: PluginManagerType,
|
||||
) -> anyhow::Result<()> {
|
||||
info!("New Client: {}", client.stream.peer_addr()?);
|
||||
info!("New Client: {}", client.peer_addr()?);
|
||||
|
||||
// run `onConnect` events from plugins
|
||||
check_event(&mut client, &plugin_manager, "onConnect").await?;
|
||||
|
@ -51,7 +51,7 @@ pub async fn handle_connection(
|
|||
Err(err) => {
|
||||
error!("failed to execute command `{cmd}`, error message = `{err}`");
|
||||
|
||||
client.send(&format!("error: {err}")).await?;
|
||||
client.send(format!("error: {err}")).await?;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -87,7 +87,7 @@ async fn check_event(
|
|||
Err(err) => {
|
||||
error!("failed to execute event `{event_name}`, error message = `{err}`");
|
||||
|
||||
client.send(&format!("error: {err}")).await?;
|
||||
client.send(format!("error: {err}")).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue