chore(fmt): add legacy stubs and strip trailing whitespace to unblock cargo fmt

This commit is contained in:
Selfhosting Dev
2025-09-17 07:43:07 +09:00
parent fcf8ce1f3c
commit adbb0201a9
385 changed files with 35622 additions and 15004 deletions

View File

@ -1,23 +1,23 @@
/*! 🔌 SocketBox - TCP/UDP Socket networking
*
*
* ## 📝 概要
* Rustの std::net を基盤とした高性能ネットワーキング Box
* TCP サーバー・クライアント両対応、HTTPサーバー基盤として利用
*
*
* ## 🛠️ 利用可能メソッド
* ### TCP Server
* - `bind(address, port)` - TCP ソケット bind
* - `listen(backlog)` - 接続待機開始
* - `accept()` - クライアント接続受諾
*
*
* ### TCP Client
* - `connect(address, port)` - サーバーへ接続
*
*
* ### IO Operations
* - `read()` - データ読み取り
* - `write(data)` - データ送信
* - `close()` - ソケット閉鎖
*
*
* ## 💡 使用例
* ```nyash
* // TCP Server
@ -25,7 +25,7 @@
* server.bind("0.0.0.0", 8080)
* server.listen(128)
* client = server.accept()
*
*
* // TCP Client
* client = new SocketBox()
* client.connect("127.0.0.1", 8080)
@ -34,11 +34,11 @@
* ```
*/
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
use std::any::Any;
use std::io::{BufRead, BufReader, Write};
use std::net::{TcpListener, TcpStream};
use std::io::{Write, BufRead, BufReader};
use std::sync::{Arc, RwLock}; // Arc追加
use std::sync::{Arc, RwLock}; // Arc追加
use std::time::Duration;
/// TCP/UDP ソケット操作を提供するBox
@ -46,25 +46,25 @@ use std::time::Duration;
pub struct SocketBox {
base: BoxBase,
// TCP Server
listener: Arc<RwLock<Option<TcpListener>>>, // Arc追加
listener: Arc<RwLock<Option<TcpListener>>>, // Arc追加
// TCP Client/Connected Socket
stream: Arc<RwLock<Option<TcpStream>>>, // Arc追加
stream: Arc<RwLock<Option<TcpStream>>>, // Arc追加
// Connection state
is_server: Arc<RwLock<bool>>, // Arc追加
is_connected: Arc<RwLock<bool>>, // Arc追加
is_server: Arc<RwLock<bool>>, // Arc追加
is_connected: Arc<RwLock<bool>>, // Arc追加
}
impl Clone for SocketBox {
fn clone(&self) -> Self {
// ディープコピー(独立インスタンス)
// ディープコピー(独立インスタンス)
let is_server_val = *self.is_server.read().unwrap();
let is_connected_val = *self.is_connected.read().unwrap();
Self {
base: BoxBase::new(), // New unique ID for clone
listener: Arc::new(RwLock::new(None)), // 新しいArc
stream: Arc::new(RwLock::new(None)), // 新しいArc
is_server: Arc::new(RwLock::new(is_server_val)), // 状態のみコピー
base: BoxBase::new(), // New unique ID for clone
listener: Arc::new(RwLock::new(None)), // 新しいArc
stream: Arc::new(RwLock::new(None)), // 新しいArc
is_server: Arc::new(RwLock::new(is_server_val)), // 状態のみコピー
is_connected: Arc::new(RwLock::new(is_connected_val)), // 状態のみコピー
}
}
@ -74,41 +74,41 @@ impl SocketBox {
pub fn new() -> Self {
Self {
base: BoxBase::new(),
listener: Arc::new(RwLock::new(None)), // Arc::new追加
stream: Arc::new(RwLock::new(None)), // Arc::new追加
is_server: Arc::new(RwLock::new(false)), // Arc::new追加
listener: Arc::new(RwLock::new(None)), // Arc::new追加
stream: Arc::new(RwLock::new(None)), // Arc::new追加
is_server: Arc::new(RwLock::new(false)), // Arc::new追加
is_connected: Arc::new(RwLock::new(false)), // Arc::new追加
}
}
/// TCP ソケットをアドレス・ポートにバインド
pub fn bind(&self, address: Box<dyn NyashBox>, port: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
let addr_str = address.to_string_box().value;
let port_str = port.to_string_box().value;
let socket_addr = format!("{}:{}", addr_str, port_str);
eprintln!("🔥 SOCKETBOX DEBUG: bind() called");
eprintln!("🔥 Socket ID = {}", self.base.id);
eprintln!("🔥 Address = {}", socket_addr);
eprintln!("🔥 Arc pointer = {:p}", &self.is_server);
match TcpListener::bind(&socket_addr) {
Ok(listener) => {
eprintln!("✅ TCP bind successful");
// listener設定
match self.listener.write() {
Ok(mut listener_guard) => {
*listener_guard = Some(listener);
eprintln!("✅ Listener stored successfully");
},
}
Err(e) => {
eprintln!("❌ Failed to lock listener mutex: {}", e);
return Box::new(BoolBox::new(false));
}
}
// is_server状態設定 - 徹底デバッグ
match self.is_server.write() {
Ok(mut is_server_guard) => {
@ -116,62 +116,62 @@ impl SocketBox {
eprintln!("🔥 is_server value = {}", *is_server_guard);
eprintln!("🔥 RwLock pointer = {:p}", &self.is_server);
eprintln!("🔥 Guard pointer = {:p}", &*is_server_guard);
// 状態変更
*is_server_guard = true;
eprintln!("🔥 AFTER MUTATION:");
eprintln!("🔥 is_server value = {}", *is_server_guard);
eprintln!("🔥 Value confirmed = {}", *is_server_guard == true);
// 明示的にドロップしてロック解除
drop(is_server_guard);
eprintln!("✅ is_server guard dropped");
// 再確認テスト
match self.is_server.read() {
Ok(check_guard) => {
eprintln!("🔥 RECHECK AFTER DROP:");
eprintln!("🔥 is_server value = {}", *check_guard);
},
}
Err(e) => {
eprintln!("❌ Failed to recheck: {}", e);
}
}
},
}
Err(e) => {
eprintln!("❌ SOCKETBOX: Failed to lock is_server mutex: {}", e);
return Box::new(BoolBox::new(false));
}
}
eprintln!("✅ bind() completed successfully");
Box::new(BoolBox::new(true))
},
}
Err(e) => {
eprintln!("❌ TCP bind failed: {}", e);
Box::new(BoolBox::new(false))
}
}
}
/// 指定した backlog で接続待機開始
pub fn listen(&self, backlog: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
let _backlog_num = backlog.to_string_box().value.parse::<i32>().unwrap_or(128);
// Check if listener exists and is properly bound
let listener_guard = match self.listener.read() {
Ok(guard) => guard,
Err(_) => return Box::new(BoolBox::new(false)),
};
if let Some(ref listener) = *listener_guard {
// Try to get the local address to confirm the listener is working
match listener.local_addr() {
Ok(_addr) => {
// Listener is properly set up and can accept connections
Box::new(BoolBox::new(true))
},
}
Err(_) => {
// Listener exists but has issues
Box::new(BoolBox::new(false))
@ -183,7 +183,7 @@ impl SocketBox {
Box::new(BoolBox::new(false))
}
}
/// クライアント接続を受諾(ブロッキング)
pub fn accept(&self) -> Box<dyn NyashBox> {
let listener_guard = self.listener.write().unwrap();
@ -191,14 +191,14 @@ impl SocketBox {
match listener.accept() {
Ok((stream, _addr)) => {
drop(listener_guard);
// Create new SocketBox for the client connection
let client_socket = SocketBox::new();
*client_socket.stream.write().unwrap() = Some(stream);
*client_socket.is_connected.write().unwrap() = true;
Box::new(client_socket)
},
}
Err(e) => {
eprintln!("🚨 SocketBox accept error: {}", e);
Box::new(BoolBox::new(false))
@ -212,7 +212,9 @@ impl SocketBox {
/// クライアント接続を受諾タイムアウトms、タイムアウト時はvoid
pub fn accept_timeout(&self, timeout_ms: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
let ms = timeout_ms.to_string_box().value.parse::<u64>().unwrap_or(0);
if ms == 0 { return self.accept(); }
if ms == 0 {
return self.accept();
}
let start = std::time::Instant::now();
if let Ok(guard) = self.listener.write() {
@ -227,7 +229,7 @@ impl SocketBox {
*client_socket.stream.write().unwrap() = Some(stream);
*client_socket.is_connected.write().unwrap() = true;
return Box::new(client_socket);
},
}
Err(e) => {
if e.kind() == std::io::ErrorKind::WouldBlock {
if start.elapsed() >= Duration::from_millis(ms) {
@ -248,32 +250,36 @@ impl SocketBox {
}
Box::new(crate::box_trait::VoidBox::new())
}
/// サーバーに接続(クライアントモード)
pub fn connect(&self, address: Box<dyn NyashBox>, port: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
pub fn connect(
&self,
address: Box<dyn NyashBox>,
port: Box<dyn NyashBox>,
) -> Box<dyn NyashBox> {
let addr_str = address.to_string_box().value;
let port_str = port.to_string_box().value;
let socket_addr = format!("{}:{}", addr_str, port_str);
match TcpStream::connect(&socket_addr) {
Ok(stream) => {
// Set timeout for read/write operations
let _ = stream.set_read_timeout(Some(Duration::from_secs(30)));
let _ = stream.set_write_timeout(Some(Duration::from_secs(30)));
*self.stream.write().unwrap() = Some(stream);
*self.is_connected.write().unwrap() = true;
*self.is_server.write().unwrap() = false;
Box::new(BoolBox::new(true))
},
}
Err(e) => {
eprintln!("🚨 SocketBox connect error: {}", e);
Box::new(BoolBox::new(false))
}
}
}
/// データを読み取り(改行まで or EOF
pub fn read(&self) -> Box<dyn NyashBox> {
let stream_guard = self.stream.write().unwrap();
@ -282,10 +288,10 @@ impl SocketBox {
match stream.try_clone() {
Ok(stream_clone) => {
drop(stream_guard);
let mut reader = BufReader::new(stream_clone);
let mut buffer = String::new();
match reader.read_line(&mut buffer) {
Ok(_) => {
// Remove trailing newline
@ -296,13 +302,13 @@ impl SocketBox {
}
}
Box::new(StringBox::new(buffer))
},
}
Err(e) => {
eprintln!("🚨 SocketBox read error: {}", e);
Box::new(StringBox::new("".to_string()))
}
}
},
}
Err(e) => {
eprintln!("🚨 SocketBox stream clone error: {}", e);
Box::new(StringBox::new("".to_string()))
@ -328,12 +334,16 @@ impl SocketBox {
Ok(_) => {
if buffer.ends_with('\n') {
buffer.pop();
if buffer.ends_with('\r') { buffer.pop(); }
if buffer.ends_with('\r') {
buffer.pop();
}
}
Box::new(StringBox::new(&buffer))
}
Err(e) => {
if e.kind() == std::io::ErrorKind::WouldBlock || e.kind() == std::io::ErrorKind::TimedOut {
if e.kind() == std::io::ErrorKind::WouldBlock
|| e.kind() == std::io::ErrorKind::TimedOut
{
return Box::new(StringBox::new(""));
}
eprintln!("🚨 SocketBox recv_timeout error: {}", e);
@ -350,7 +360,7 @@ impl SocketBox {
Box::new(StringBox::new(""))
}
}
/// HTTP request を読み取り(ヘッダーまで含む)
pub fn read_http_request(&self) -> Box<dyn NyashBox> {
let stream_guard = self.stream.write().unwrap();
@ -358,11 +368,11 @@ impl SocketBox {
match stream.try_clone() {
Ok(stream_clone) => {
drop(stream_guard);
let mut reader = BufReader::new(stream_clone);
let mut request = String::new();
let mut line = String::new();
// Read HTTP request line by line until empty line
loop {
line.clear();
@ -374,16 +384,16 @@ impl SocketBox {
if line.trim().is_empty() {
break;
}
},
}
Err(e) => {
eprintln!("🚨 SocketBox HTTP read error: {}", e);
break;
}
}
}
Box::new(StringBox::new(request))
},
}
Err(e) => {
eprintln!("🚨 SocketBox stream clone error: {}", e);
Box::new(StringBox::new("".to_string()))
@ -393,21 +403,19 @@ impl SocketBox {
Box::new(StringBox::new("".to_string()))
}
}
/// データを送信
pub fn write(&self, data: Box<dyn NyashBox>) -> Box<dyn NyashBox> {
let data_str = data.to_string_box().value;
let mut stream_guard = self.stream.write().unwrap();
if let Some(ref mut stream) = *stream_guard {
match stream.write_all(data_str.as_bytes()) {
Ok(_) => {
match stream.flush() {
Ok(_) => Box::new(BoolBox::new(true)),
Err(e) => {
eprintln!("🚨 SocketBox flush error: {}", e);
Box::new(BoolBox::new(false))
}
Ok(_) => match stream.flush() {
Ok(_) => Box::new(BoolBox::new(true)),
Err(e) => {
eprintln!("🚨 SocketBox flush error: {}", e);
Box::new(BoolBox::new(false))
}
},
Err(e) => {
@ -419,7 +427,7 @@ impl SocketBox {
Box::new(BoolBox::new(false))
}
}
/// ソケット閉鎖
pub fn close(&self) -> Box<dyn NyashBox> {
*self.stream.write().unwrap() = None;
@ -428,18 +436,18 @@ impl SocketBox {
*self.is_server.write().unwrap() = false;
Box::new(BoolBox::new(true))
}
/// 接続状態確認
pub fn is_connected(&self) -> Box<dyn NyashBox> {
Box::new(BoolBox::new(*self.is_connected.write().unwrap()))
}
/// サーバーモード確認
pub fn is_server(&self) -> Box<dyn NyashBox> {
eprintln!("🔥 SOCKETBOX DEBUG: is_server() called");
eprintln!("🔥 Socket ID = {}", self.base.id);
eprintln!("🔥 RwLock pointer = {:p}", &self.is_server);
match self.is_server.read() {
Ok(is_server_guard) => {
let is_server_value = *is_server_guard;
@ -447,11 +455,14 @@ impl SocketBox {
eprintln!("🔥 is_server value = {}", is_server_value);
eprintln!("🔥 Guard pointer = {:p}", &*is_server_guard);
eprintln!("🔥 Returning BoolBox with value = {}", is_server_value);
Box::new(BoolBox::new(is_server_value))
},
}
Err(e) => {
eprintln!("❌ SOCKETBOX: Failed to lock is_server mutex in is_server(): {}", e);
eprintln!(
"❌ SOCKETBOX: Failed to lock is_server mutex in is_server(): {}",
e
);
Box::new(BoolBox::new(false))
}
}
@ -462,45 +473,48 @@ impl NyashBox for SocketBox {
fn clone_box(&self) -> Box<dyn NyashBox> {
Box::new(self.clone())
}
/// 🎯 状態共有の核心実装 - SocketBox状態保持問題の根本解決
fn share_box(&self) -> Box<dyn NyashBox> {
let new_instance = SocketBox {
base: BoxBase::new(), // 新しいID
listener: Arc::clone(&self.listener), // 状態共有
stream: Arc::clone(&self.stream), // 状態共有
is_server: Arc::clone(&self.is_server), // 状態共有
is_connected: Arc::clone(&self.is_connected), // 状態共有
base: BoxBase::new(), // 新しいID
listener: Arc::clone(&self.listener), // 状態共有
stream: Arc::clone(&self.stream), // 状態共有
is_server: Arc::clone(&self.is_server), // 状態共有
is_connected: Arc::clone(&self.is_connected), // 状態共有
};
Box::new(new_instance)
}
fn to_string_box(&self) -> StringBox {
eprintln!("🔥 SOCKETBOX to_string_box() called - Socket ID = {}", self.base.id);
eprintln!(
"🔥 SOCKETBOX to_string_box() called - Socket ID = {}",
self.base.id
);
eprintln!("🔥 RwLock pointer = {:p}", &self.is_server);
let is_server = match self.is_server.read() {
Ok(guard) => {
eprintln!("✅ is_server.read() successful");
*guard
},
}
Err(e) => {
eprintln!("❌ is_server.read() failed: {}", e);
false // デフォルト値
}
};
let is_connected = match self.is_connected.read() {
Ok(guard) => {
eprintln!("✅ is_connected.read() successful");
*guard
},
}
Err(e) => {
eprintln!("❌ is_connected.read() failed: {}", e);
false // デフォルト値
}
};
let status = if is_server {
"Server"
} else if is_connected {
@ -508,8 +522,11 @@ impl NyashBox for SocketBox {
} else {
"Disconnected"
};
StringBox::new(format!("SocketBox(id: {}, status: {})", self.base.id, status))
StringBox::new(format!(
"SocketBox(id: {}, status: {})",
self.base.id, status
))
}
fn type_name(&self) -> &'static str {
@ -529,14 +546,17 @@ impl BoxCore for SocketBox {
fn box_id(&self) -> u64 {
self.base.id
}
fn parent_type_id(&self) -> Option<std::any::TypeId> {
self.base.parent_type_id
}
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
eprintln!("🔥 SOCKETBOX fmt_box() called - Socket ID = {}", self.base.id);
eprintln!(
"🔥 SOCKETBOX fmt_box() called - Socket ID = {}",
self.base.id
);
let is_server = match self.is_server.read() {
Ok(guard) => *guard,
Err(e) => {
@ -544,7 +564,7 @@ impl BoxCore for SocketBox {
false
}
};
let is_connected = match self.is_connected.read() {
Ok(guard) => *guard,
Err(e) => {
@ -552,7 +572,7 @@ impl BoxCore for SocketBox {
false
}
};
let status = if is_server {
"Server"
} else if is_connected {
@ -560,14 +580,14 @@ impl BoxCore for SocketBox {
} else {
"Disconnected"
};
write!(f, "SocketBox(id: {}, status: {})", self.base.id, status)
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}