aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2019-10-02 07:25:14 -0400
committerJesse Luehrs <doy@tozt.net>2019-10-02 07:25:14 -0400
commit26d8e62820435c572a6d062ebe7d0713a858cb80 (patch)
tree86b052ff8cd38d7b60c58d613c1258d2bc5e953d /src
parent4989d0966982168d36caca919a8a58fc950d2219 (diff)
downloadteleterm-26d8e62820435c572a6d062ebe7d0713a858cb80.tar.gz
teleterm-26d8e62820435c572a6d062ebe7d0713a858cb80.zip
don't literally store the session object, just generate it on demand
Diffstat (limited to 'src')
-rw-r--r--src/common.rs7
-rw-r--r--src/main.rs1
-rw-r--r--src/protocol.rs29
-rw-r--r--src/server.rs120
4 files changed, 85 insertions, 72 deletions
diff --git a/src/common.rs b/src/common.rs
deleted file mode 100644
index 6b913fc..0000000
--- a/src/common.rs
+++ /dev/null
@@ -1,7 +0,0 @@
-#[derive(Debug, Clone)]
-pub struct Session {
- pub id: String,
- pub username: String,
- pub term_type: String,
- pub size: (u32, u32),
-}
diff --git a/src/main.rs b/src/main.rs
index 4b04a00..8df25c3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,7 +8,6 @@
mod client;
mod cmd;
-mod common;
mod component_future;
mod process;
mod protocol;
diff --git a/src/protocol.rs b/src/protocol.rs
index 95d590f..8d5092f 100644
--- a/src/protocol.rs
+++ b/src/protocol.rs
@@ -39,6 +39,14 @@ pub enum Error {
pub type Result<T> = std::result::Result<T, Error>;
+#[derive(Debug, Clone)]
+pub struct Session {
+ pub id: String,
+ pub username: String,
+ pub term_type: String,
+ pub size: (u32, u32),
+}
+
pub struct FramedReader(
tokio::codec::FramedRead<
tokio::io::ReadHalf<tokio::net::tcp::TcpStream>,
@@ -95,7 +103,7 @@ pub enum Message {
},
ListSessions,
Sessions {
- sessions: Vec<crate::common::Session>,
+ sessions: Vec<Session>,
},
Disconnected,
Error {
@@ -144,7 +152,7 @@ impl Message {
Self::ListSessions
}
- pub fn sessions(sessions: &[crate::common::Session]) -> Self {
+ pub fn sessions(sessions: &[Session]) -> Self {
Self::Sessions {
sessions: sessions.to_vec(),
}
@@ -272,17 +280,14 @@ impl From<&Message> for Packet {
fn write_str(val: &str, data: &mut Vec<u8>) {
write_bytes(val.as_bytes(), data);
}
- fn write_session(val: &crate::common::Session, data: &mut Vec<u8>) {
+ fn write_session(val: &Session, data: &mut Vec<u8>) {
write_str(&val.id, data);
write_str(&val.username, data);
write_str(&val.term_type, data);
write_u32(val.size.0, data);
write_u32(val.size.1, data);
}
- fn write_sessions(
- val: &[crate::common::Session],
- data: &mut Vec<u8>,
- ) {
+ fn write_sessions(val: &[Session], data: &mut Vec<u8>) {
write_u32(u32_from_usize(val.len()), data);
for s in val {
write_session(s, data);
@@ -389,16 +394,14 @@ impl std::convert::TryFrom<Packet> for Message {
let val = String::from_utf8(bytes).context(ParseString)?;
Ok((val, rest))
}
- fn read_session(
- data: &[u8],
- ) -> Result<(crate::common::Session, &[u8])> {
+ fn read_session(data: &[u8]) -> Result<(Session, &[u8])> {
let (id, data) = read_str(data)?;
let (username, data) = read_str(data)?;
let (term_type, data) = read_str(data)?;
let (cols, data) = read_u32(data)?;
let (rows, data) = read_u32(data)?;
Ok((
- crate::common::Session {
+ Session {
id,
username,
term_type,
@@ -407,9 +410,7 @@ impl std::convert::TryFrom<Packet> for Message {
data,
))
}
- fn read_sessions(
- data: &[u8],
- ) -> Result<(Vec<crate::common::Session>, &[u8])> {
+ fn read_sessions(data: &[u8]) -> Result<(Vec<Session>, &[u8])> {
let mut val = vec![];
let (len, mut data) = read_u32(data)?;
for _ in 0..len {
diff --git a/src/server.rs b/src/server.rs
index 8e0e9ad..2352f34 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -63,30 +63,35 @@ enum WriteSocket {
),
}
+#[derive(Debug, Clone)]
+struct TerminalInfo {
+ term: String,
+ size: (u32, u32),
+}
+
// XXX https://github.com/rust-lang/rust/issues/64362
#[allow(dead_code)]
enum ConnectionState {
- Accepted {
- id: String,
- },
+ Accepted,
LoggedIn {
- session: crate::common::Session,
+ username: String,
+ term_info: TerminalInfo,
},
Casting {
- session: crate::common::Session,
+ username: String,
+ term_info: TerminalInfo,
saved_data: crate::term::Buffer,
},
Watching {
- session: crate::common::Session,
+ username: String,
+ term_info: TerminalInfo,
watch_id: String,
},
}
impl ConnectionState {
fn new() -> Self {
- Self::Accepted {
- id: format!("{}", uuid::Uuid::new_v4()),
- }
+ Self::Accepted
}
fn login(
@@ -96,11 +101,10 @@ impl ConnectionState {
size: (u32, u32),
) -> Self {
match self {
- Self::Accepted { id } => Self::LoggedIn {
- session: crate::common::Session {
- id: id.clone(),
- username: username.to_string(),
- term_type: term_type.to_string(),
+ Self::Accepted => Self::LoggedIn {
+ username: username.to_string(),
+ term_info: TerminalInfo {
+ term: term_type.to_string(),
size,
},
},
@@ -110,8 +114,12 @@ impl ConnectionState {
fn cast(&self) -> Self {
match self {
- Self::LoggedIn { session } => Self::Casting {
- session: session.clone(),
+ Self::LoggedIn {
+ username,
+ term_info,
+ } => Self::Casting {
+ username: username.clone(),
+ term_info: term_info.clone(),
saved_data: crate::term::Buffer::new(),
},
_ => unreachable!(),
@@ -120,8 +128,12 @@ impl ConnectionState {
fn watch(&self, id: &str) -> Self {
match self {
- Self::LoggedIn { session } => Self::Watching {
- session: session.clone(),
+ Self::LoggedIn {
+ username,
+ term_info,
+ } => Self::Watching {
+ username: username.clone(),
+ term_info: term_info.clone(),
watch_id: id.to_string(),
},
_ => unreachable!(),
@@ -130,6 +142,7 @@ impl ConnectionState {
}
struct Connection {
+ id: String,
rsock: Option<ReadSocket>,
wsock: Option<WriteSocket>,
to_send: std::collections::VecDeque<crate::protocol::Message>,
@@ -141,6 +154,7 @@ impl Connection {
fn new(s: tokio::net::tcp::TcpStream) -> Self {
let (rs, ws) = s.split();
Self {
+ id: format!("{}", uuid::Uuid::new_v4()),
rsock: Some(ReadSocket::Connected(
crate::protocol::FramedReader::new(rs),
)),
@@ -153,22 +167,30 @@ impl Connection {
}
}
- fn id(&self) -> &str {
- match &self.state {
- ConnectionState::Accepted { id } => id,
- ConnectionState::LoggedIn { session } => &session.id,
- ConnectionState::Casting { session, .. } => &session.id,
- ConnectionState::Watching { session, .. } => &session.id,
- }
- }
-
- fn session(&self) -> Option<&crate::common::Session> {
- match &self.state {
- ConnectionState::Accepted { .. } => None,
- ConnectionState::LoggedIn { session } => Some(session),
- ConnectionState::Casting { session, .. } => Some(session),
- ConnectionState::Watching { session, .. } => Some(session),
- }
+ fn session(&self) -> Option<crate::protocol::Session> {
+ let (username, term_info) = match &self.state {
+ ConnectionState::Accepted => return None,
+ ConnectionState::LoggedIn {
+ username,
+ term_info,
+ } => (username, term_info),
+ ConnectionState::Casting {
+ username,
+ term_info,
+ ..
+ } => (username, term_info),
+ ConnectionState::Watching {
+ username,
+ term_info,
+ ..
+ } => (username, term_info),
+ };
+ Some(crate::protocol::Session {
+ id: self.id.clone(),
+ username: username.clone(),
+ term_type: term_info.term.clone(),
+ size: term_info.size,
+ })
}
fn close(&mut self, res: Result<()>) {
@@ -246,19 +268,20 @@ impl Server {
conn: &mut Connection,
message: crate::protocol::Message,
) -> Result<()> {
- let (session, saved_data) = if let ConnectionState::Casting {
- session,
+ let (username, saved_data) = if let ConnectionState::Casting {
+ username,
saved_data,
+ ..
} = &mut conn.state
{
- (session, saved_data)
+ (username, saved_data)
} else {
unreachable!()
};
match message {
crate::protocol::Message::Heartbeat => {
- println!("got a heartbeat from {}", session.username);
+ println!("got a heartbeat from {}", username);
conn.to_send
.push_back(crate::protocol::Message::heartbeat());
Ok(())
@@ -269,7 +292,7 @@ impl Server {
for watch_conn in self.watchers_mut() {
match &watch_conn.state {
ConnectionState::Watching { watch_id, .. } => {
- if &session.id == watch_id {
+ if &conn.id == watch_id {
watch_conn.to_send.push_back(
crate::protocol::Message::terminal_output(
&data,
@@ -291,15 +314,15 @@ impl Server {
conn: &mut Connection,
message: crate::protocol::Message,
) -> Result<()> {
- let session =
- if let ConnectionState::Watching { session, .. } = &conn.state {
- session
+ let username =
+ if let ConnectionState::Watching { username, .. } = &conn.state {
+ username
} else {
unreachable!()
};
match message {
crate::protocol::Message::Heartbeat => {
- println!("got a heartbeat from {}", session.username);
+ println!("got a heartbeat from {}", username);
conn.to_send
.push_back(crate::protocol::Message::heartbeat());
Ok(())
@@ -315,11 +338,8 @@ impl Server {
) -> Result<()> {
match message {
crate::protocol::Message::ListSessions => {
- let sessions: Vec<_> = self
- .casters()
- .flat_map(Connection::session)
- .cloned()
- .collect();
+ let sessions: Vec<_> =
+ self.casters().flat_map(Connection::session).collect();
conn.to_send
.push_back(crate::protocol::Message::sessions(&sessions));
Ok(())
@@ -359,7 +379,7 @@ impl Server {
if let ConnectionState::Watching { watch_id, .. } =
&watch_conn.state
{
- if watch_id == conn.id() {
+ if watch_id == &conn.id {
watch_conn.close(Ok(()));
}
} else {
@@ -520,7 +540,7 @@ impl Server {
) -> Result<crate::component_future::Poll<()>> {
match self.sock_stream.poll() {
Ok(futures::Async::Ready(Some(conn))) => {
- self.connections.insert(conn.id().to_string(), conn);
+ self.connections.insert(conn.id.to_string(), conn);
Ok(crate::component_future::Poll::DidWork)
}
Ok(futures::Async::Ready(None)) => {