Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
LaoLittle committed Jan 3, 2023
1 parent b2ba467 commit a592dc8
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ impl Client {
let id = client.id();
let mut reconnected = false;
loop {
if !crate::service::login::auto_reconnect() {
return;
}

if client.network_status() == OFFLINE_STATUS as u8 {
if reconnected {
error!(
Expand Down
8 changes: 8 additions & 0 deletions src/config/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ use serde::{Deserialize, Serialize};

pub const DEFAULT_CONFIG: &[u8] = include_bytes!("../../default_config/default_login_conf.toml");

/// 登录配置
#[derive(Deserialize, Serialize, Debug, Default)]
pub struct LoginConfig {
/// 默认登录协议
pub default_protocol: Protocol,
/// 是否自动重连
#[serde(default = "true_bool")]
pub auto_reconnect: bool,
/// 所有配置进行登录的客户端
#[serde(rename = "client")]
pub clients: Vec<ClientConfig>,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct ClientConfig {
/// 账号
pub account: i64,
/// 密码
pub password: Option<String>,
/// 登录协议
pub protocol: Option<Protocol>,
/// 是否进行登录
#[serde(default = "true_bool")]
pub auto_login: bool,
}
Expand Down
1 change: 1 addition & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::{Path, PathBuf};

pub mod login;
pub mod plugin;

const SERVICE_CONFIG_PATH: &str = "service";

Expand Down
13 changes: 13 additions & 0 deletions src/config/plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use serde::{Deserialize, Serialize};

/// 对插件产生错误的态度
#[derive(Serialize, Deserialize, Default)]
pub enum FaultAttitude {
#[default]
/// 立即结束程序, 记录堆栈
FastFault,
/// 忽略错误, 关闭产生错误的监听器, 记录堆栈
///
/// 可能导致内存泄露或其他问题
Ignore,
}
1 change: 1 addition & 0 deletions src/plugin/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod log;
pub mod member;
pub mod message;
pub mod rt;
pub mod string;

fn cast_ref<'a, T>(ptr: *const ()) -> &'a T {
unsafe { &*(ptr as *const T) }
Expand Down
6 changes: 0 additions & 6 deletions src/plugin/ffi/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ pub extern "C" fn plugin_manager_block_on(
manager.async_runtime().block_on(future)
}

pub extern "C" fn c_str_cvt(ptr: *const c_char) -> RustString {
let cstr = unsafe { CStr::from_ptr(ptr) };

cstr.to_string_lossy().to_string().into()
}

pub fn future_block_on<F>(manager: *const (), future: F) -> F::Output
where
F: Future,
Expand Down
20 changes: 20 additions & 0 deletions src/plugin/ffi/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use atri_ffi::{RustStr, RustString};
use std::ffi::{c_char, CStr, CString};
use std::ptr::null_mut;

pub extern "C" fn rust_str_cvt(str: RustStr) -> *mut c_char {
let str = str.as_str();
CString::new(str)
.map(CString::into_raw)
.unwrap_or(null_mut())
}

pub extern "C" fn c_str_cvt(ptr: *const c_char) -> RustString {
let cstr = unsafe { CStr::from_ptr(ptr) };

cstr.to_string_lossy().to_string().into()
}

pub extern "C" fn rust_string_drop(str: RustString) {
drop(String::from(str));
}
7 changes: 5 additions & 2 deletions src/plugin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod ffi;

use crate::plugin::ffi::rt::{c_str_cvt, plugin_manager_block_on, plugin_manager_spawn};
use crate::plugin::ffi::rt::{plugin_manager_block_on, plugin_manager_spawn};
use crate::plugin::ffi::string::{c_str_cvt, rust_str_cvt, rust_string_drop};
use ffi::client::{
client_find_friend, client_find_group, client_get_friends, client_get_groups, client_get_id,
client_get_list, client_get_nickname, find_client,
Expand Down Expand Up @@ -148,6 +149,8 @@ pub extern "C" fn plugin_get_function(sig: u16) -> *const () {
30101 => message_chain_from_json,

// ffi
30500 => c_str_cvt,
30500 => rust_str_cvt,
30501 => c_str_cvt,
30502 => rust_string_drop,
}
}
15 changes: 15 additions & 0 deletions src/service/login.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::io;
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

use rand::{thread_rng, Rng};
Expand Down Expand Up @@ -50,6 +51,10 @@ pub async fn login_clients() -> Result<(), RQError> {
}
};

if login_conf.auto_reconnect {
set_auto_reconnect(true);
}

let clients_path = config::clients_dir_path();
if !clients_path.is_dir() {
fs::create_dir(&clients_path).await?;
Expand Down Expand Up @@ -169,3 +174,13 @@ async fn login_client(
}
}
}

static AUTO_RECONNECT: AtomicBool = AtomicBool::new(false);

pub fn auto_reconnect() -> bool {
AUTO_RECONNECT.load(Ordering::Relaxed)
}

pub fn set_auto_reconnect(s: bool) {
AUTO_RECONNECT.store(s, Ordering::Relaxed);
}

0 comments on commit a592dc8

Please sign in to comment.