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