mirror of https://github.com/MedzikUser/servers
fix(id): fix add one to next id
- Fixed add one to next ID - Added command /id - Added `id` field to Client struct
This commit is contained in:
parent
d0120a0703
commit
25c2f0baa3
|
@ -32,9 +32,7 @@ impl Command for PluginTest {
|
||||||
}
|
}
|
||||||
/// Command function.
|
/// Command function.
|
||||||
async fn execute(&self, client: &Client, _args: Vec<&str>) -> anyhow::Result<()> {
|
async fn execute(&self, client: &Client, _args: Vec<&str>) -> anyhow::Result<()> {
|
||||||
client.send("successful executed command from dylib")?;
|
client.send("successful executed command from dylib")
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
{
|
||||||
|
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||||
|
"extends": [
|
||||||
|
"config:base",
|
||||||
|
"schedule:weekly",
|
||||||
|
"group:allNonMajor",
|
||||||
|
":semanticCommits"
|
||||||
|
],
|
||||||
|
"labels": [
|
||||||
|
"dependencies"
|
||||||
|
],
|
||||||
|
"automergeType": "pr",
|
||||||
|
"prCreation": "immediate",
|
||||||
|
"packageRules": [
|
||||||
|
{
|
||||||
|
"matchUpdateTypes": [
|
||||||
|
"minor",
|
||||||
|
"patch",
|
||||||
|
"pin",
|
||||||
|
"digest"
|
||||||
|
],
|
||||||
|
"automerge": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -21,9 +21,6 @@ impl Command for Disconnect {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn execute(&self, client: &Client, _args: Vec<&str>) -> anyhow::Result<()> {
|
async fn execute(&self, client: &Client, _args: Vec<&str>) -> anyhow::Result<()> {
|
||||||
// close the connection
|
client.close()
|
||||||
client.close()?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,8 +40,6 @@ impl Command for Help {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
client.send(msg.join("\n"))?;
|
client.send(msg.join("\n"))
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
use crate::plugins::prelude::*;
|
||||||
|
|
||||||
|
pub struct ID;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Command for ID {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"/id"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn aliases(&self) -> Vec<&'static str> {
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn help(&self) -> &'static str {
|
||||||
|
"Get id of the client"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &'static str {
|
||||||
|
"/id"
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute(&self, client: &Client, _args: Vec<&str>) -> anyhow::Result<()> {
|
||||||
|
client.send(client.id)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,10 @@
|
||||||
mod disconnect;
|
mod disconnect;
|
||||||
mod help;
|
mod help;
|
||||||
|
mod id;
|
||||||
|
|
||||||
use self::{disconnect::Disconnect, help::Help};
|
use self::{disconnect::Disconnect, help::Help, id::ID};
|
||||||
use crate::plugins::prelude::*;
|
use crate::plugins::prelude::*;
|
||||||
|
|
||||||
pub fn register_commands() -> Vec<Box<dyn Command>> {
|
pub fn register_commands() -> Vec<Box<dyn Command>> {
|
||||||
vec![Box::new(Help), Box::new(Disconnect)]
|
vec![Box::new(Help), Box::new(Disconnect), Box::new(ID)]
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ pub const MAX_PACKET_LEN: usize = 65536;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
|
pub id: usize,
|
||||||
pub stream: ClientStream,
|
pub stream: ClientStream,
|
||||||
pub map: HashMap<String, ClientMapValue>,
|
pub map: HashMap<String, ClientMapValue>,
|
||||||
pub plugins_manager: PluginsManagerType,
|
pub plugins_manager: PluginsManagerType,
|
||||||
|
@ -47,6 +48,7 @@ pub enum ClientStream {
|
||||||
impl From<TcpStream> for Client {
|
impl From<TcpStream> for Client {
|
||||||
fn from(stream: TcpStream) -> Self {
|
fn from(stream: TcpStream) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
id: 0,
|
||||||
stream: ClientStream::TCP(Arc::new(stream)),
|
stream: ClientStream::TCP(Arc::new(stream)),
|
||||||
map: HashMap::new(),
|
map: HashMap::new(),
|
||||||
plugins_manager: PLUGINS_MANAGER.clone(),
|
plugins_manager: PLUGINS_MANAGER.clone(),
|
||||||
|
@ -57,6 +59,7 @@ impl From<TcpStream> for Client {
|
||||||
impl From<WebSocket<TcpStream>> for Client {
|
impl From<WebSocket<TcpStream>> for Client {
|
||||||
fn from(stream: WebSocket<TcpStream>) -> Self {
|
fn from(stream: WebSocket<TcpStream>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
id: 0,
|
||||||
stream: ClientStream::WebSocket(Arc::new(Mutex::new(stream))),
|
stream: ClientStream::WebSocket(Arc::new(Mutex::new(stream))),
|
||||||
map: HashMap::new(),
|
map: HashMap::new(),
|
||||||
plugins_manager: PLUGINS_MANAGER.clone(),
|
plugins_manager: PLUGINS_MANAGER.clone(),
|
||||||
|
@ -66,15 +69,23 @@ impl From<WebSocket<TcpStream>> for Client {
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
/// Create a new TCP Client instance
|
/// Create a new TCP Client instance
|
||||||
pub fn new_tcp(stream: TcpStream) -> Self {
|
pub fn new_tcp(stream: TcpStream, id: usize) -> Self {
|
||||||
Self::from(stream)
|
let mut client = Self::from(stream);
|
||||||
|
|
||||||
|
client.id = id;
|
||||||
|
|
||||||
|
client
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new WebSocket Client instance
|
/// Create a new WebSocket Client instance
|
||||||
pub fn new_websocket(stream: TcpStream) -> anyhow::Result<Self> {
|
pub fn new_websocket(stream: TcpStream, id: usize) -> anyhow::Result<Self> {
|
||||||
let websocket = accept(stream)?;
|
let websocket = accept(stream)?;
|
||||||
|
|
||||||
Ok(Self::from(websocket))
|
let mut client = Self::from(websocket);
|
||||||
|
|
||||||
|
client.id = id;
|
||||||
|
|
||||||
|
Ok(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Recieve a message from the client
|
/// Recieve a message from the client
|
||||||
|
|
|
@ -88,11 +88,15 @@ async fn start_tcp(host: String) -> anyhow::Result<()> {
|
||||||
for stream in incoming {
|
for stream in incoming {
|
||||||
let stream = stream?;
|
let stream = stream?;
|
||||||
|
|
||||||
task::spawn(async {
|
// get id for the client
|
||||||
let client = Client::new_tcp(stream);
|
let id = *CLIENT_NEXT.lock().unwrap();
|
||||||
|
|
||||||
|
// add one to next id
|
||||||
|
*CLIENT_NEXT.lock().unwrap() += 1;
|
||||||
|
|
||||||
|
task::spawn(async move {
|
||||||
// get id for the client and add one to next id
|
// get id for the client and add one to next id
|
||||||
let id = (*CLIENT_NEXT.lock().unwrap() + 1).clone();
|
let client = Client::new_tcp(stream, id);
|
||||||
|
|
||||||
// insert the cloned client to CLIENTS
|
// insert the cloned client to CLIENTS
|
||||||
CLIENTS.lock().unwrap().insert(id, client.clone());
|
CLIENTS.lock().unwrap().insert(id, client.clone());
|
||||||
|
@ -117,17 +121,20 @@ async fn start_websocket(host: String) -> anyhow::Result<()> {
|
||||||
for stream in incoming {
|
for stream in incoming {
|
||||||
let stream = stream?;
|
let stream = stream?;
|
||||||
|
|
||||||
task::spawn(async {
|
// get id for the client
|
||||||
let client = Client::new_websocket(stream).unwrap();
|
let id = *CLIENT_NEXT.lock().unwrap();
|
||||||
|
|
||||||
// get id for the client and add one to next id
|
// add one to next id
|
||||||
let id = (*CLIENT_NEXT.lock().unwrap() + 1).clone();
|
*CLIENT_NEXT.lock().unwrap() += 1;
|
||||||
|
|
||||||
|
task::spawn(async move {
|
||||||
|
let client = Client::new_websocket(stream, id).unwrap();
|
||||||
|
|
||||||
// insert the cloned client to CLIENTS
|
// insert the cloned client to CLIENTS
|
||||||
CLIENTS.lock().unwrap().insert(id, client.clone());
|
CLIENTS.lock().unwrap().insert(id, client.clone());
|
||||||
|
|
||||||
if let Err(err) = process(client).await {
|
if let Err(err) = process(client).await {
|
||||||
error!("TCP client error: {}", err);
|
error!("WebSocket client error: {}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete the client from CLIENTS map
|
// delete the client from CLIENTS map
|
||||||
|
|
Loading…
Reference in New Issue