Made it a library, as I should have done from the beginning

This commit is contained in:
2020-11-11 15:44:27 +01:00
parent de3c663ccd
commit c294e8033f
4 changed files with 10 additions and 7 deletions

2
src/lib.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod tn3270;
mod encoding;

View File

@@ -1,53 +0,0 @@
use structopt::StructOpt;
use std::time::Duration;
mod tn3270;
mod encoding;
#[derive(StructOpt)]
pub struct Cli {
#[structopt(short="h", long = "host", default_value="[::1]")]
host: String,
#[structopt(short="p", long = "port", default_value="2101")]
port: u16,
}
fn run(mut session: tn3270::Session) -> anyhow::Result<()> {
use tn3270::stream::*;
let mut record = WriteCommand::new(WriteCommandCode::Write, WCC::RESET);
let bufsz = BufferAddressCalculator { width: 80, height: 24 };
record.set_buffer_address(0)
.erase_unprotected_to_address(bufsz.encode_address(79, 23))
.set_buffer_address(bufsz.encode_address(31,1))
.set_attribute(ExtendedFieldAttribute::ForegroundColor(Color::Red))
.send_text("Hello from Rust!");
session.send_record(record)?;
std::thread::sleep(Duration::from_secs(60));
Ok(())
}
fn main() -> anyhow::Result<()> {
let options: Cli = Cli::from_args();
let server = std::net::TcpListener::bind((options.host.as_str(), options.port))?;
for client in server.incoming() {
let client = client?;
std::thread::spawn(move || {
let session = match tn3270::Session::new(client) {
Ok(session) => session,
Err(err) => {
eprintln!("Error accepting session: {}", err);
return;
}
};
run(session);
});
}
println!("Hello, world!");
Ok(())
}

View File

@@ -7,7 +7,6 @@ use libtelnet_rs::{
}
};
use std::net::TcpStream;
use libtelnet_rs::telnet::op_option::EOR;
use std::io::{Write, Read};
use std::time::Duration;
@@ -124,14 +123,14 @@ impl Session {
initial_negotiation.extend(self.parser._do(tn_opt::TTYPE));
initial_negotiation.extend(self.parser._will(tn_opt::TTYPE));
self.process_events(initial_negotiation);
self.process_events(initial_negotiation)?;
// Large enough for a TCP packet
let mut idata = Vec::with_capacity(2000);
idata.resize(idata.capacity(), 0);
// Make sure that negotiation completes quickly
self.stream.set_read_timeout(Some(Duration::from_secs(5)));
self.stream.set_read_timeout(Some(Duration::from_secs(5)))?;
while !self.is_ready() {
let len = self.stream.read(&mut idata[..])?;
@@ -143,6 +142,7 @@ impl Session {
self.process_events(events)?;
}
self.stream.set_read_timeout(None)?;
Ok(true)
}

View File

@@ -160,7 +160,7 @@ bitflags! {
}
#[derive(Copy, Clone, Debug, Hash)]
enum Transparency {
pub enum Transparency {
Default,
Or,
Xor,