feat(ssh): Implement SCP protocol handling with ChannelReadWrite (Phase 8 complete)
This commit is contained in:
@@ -397,6 +397,65 @@ impl ScpHandler {
|
||||
pub trait ReadWrite: Read + Write {}
|
||||
impl<T: Read + Write> ReadWrite for T {}
|
||||
|
||||
/// ⭐⭐⭐⭐⭐ Phase 8: Channel wrapper for SCP protocol
|
||||
/// 实现 Read + Write traits,用于 ScpHandler 和 SSH channel 之间传递数据
|
||||
pub struct ChannelReadWrite {
|
||||
input_buffer: Vec<u8>,
|
||||
output_buffer: Vec<u8>,
|
||||
input_pos: usize,
|
||||
}
|
||||
|
||||
impl ChannelReadWrite {
|
||||
pub fn new(input_buffer: Vec<u8>) -> Self {
|
||||
Self {
|
||||
input_buffer,
|
||||
output_buffer: Vec::new(),
|
||||
input_pos: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn feed_input(&mut self, data: &[u8]) {
|
||||
self.input_buffer.extend_from_slice(data);
|
||||
}
|
||||
|
||||
pub fn drain_output(&mut self) -> Vec<u8> {
|
||||
let output = self.output_buffer.clone();
|
||||
self.output_buffer.clear();
|
||||
output
|
||||
}
|
||||
|
||||
pub fn has_remaining_input(&self) -> bool {
|
||||
self.input_pos < self.input_buffer.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for ChannelReadWrite {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
let remaining = self.input_buffer.len() - self.input_pos;
|
||||
let to_read = std::cmp::min(buf.len(), remaining);
|
||||
|
||||
if to_read == 0 {
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
buf[..to_read].copy_from_slice(&self.input_buffer[self.input_pos..self.input_pos + to_read]);
|
||||
self.input_pos += to_read;
|
||||
|
||||
Ok(to_read)
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for ChannelReadWrite {
|
||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||
self.output_buffer.extend_from_slice(buf);
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user