aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2019-09-01 22:15:28 -0400
committerJesse Luehrs <doy@tozt.net>2019-09-01 22:16:02 -0400
commit9479692af28d82e496dde9609f3bd308a8b9a7ff (patch)
tree3dda010a942e135ee80dca1960977cde553da1c0
parent82f3a81589da723497d5aa1ffa7f8b26acf3520c (diff)
downloadteleterm-9479692af28d82e496dde9609f3bd308a8b9a7ff.tar.gz
teleterm-9479692af28d82e496dde9609f3bd308a8b9a7ff.zip
actually, remove protobufs too
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock38
-rw-r--r--Cargo.toml7
-rw-r--r--build.rs11
-rw-r--r--proto/termcast.proto17
-rw-r--r--src/cmd/cast.rs15
-rw-r--r--src/cmd/server.rs15
-rw-r--r--src/main.rs2
-rw-r--r--src/pb.rs1
-rw-r--r--src/protocol.rs115
10 files changed, 153 insertions, 69 deletions
diff --git a/.gitignore b/.gitignore
index 8528847..53eaa21 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,2 @@
/target
**/*.rs.bk
-/src/pb/*
diff --git a/Cargo.lock b/Cargo.lock
index 11bf942..dcfbe97 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -175,6 +175,14 @@ version = "0.1.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "futures01"
+version = "0.1.28"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "futures 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "iovec"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -326,28 +334,6 @@ dependencies = [
]
[[package]]
-name = "protobuf"
-version = "2.8.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "protobuf-codegen"
-version = "2.8.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "protobuf 2.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "protobuf-codegen-pure"
-version = "2.8.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "protobuf 2.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "protobuf-codegen 2.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
name = "quote"
version = "0.6.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -512,6 +498,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"backtrace 0.3.35 (registry+https://github.com/rust-lang/crates.io-index)",
"doc-comment 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "futures01 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)",
"snafu-derive 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -550,8 +537,7 @@ name = "termcast"
version = "0.1.0"
dependencies = [
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "protobuf 2.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "protobuf-codegen-pure 2.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "futures 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)",
"snafu 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -810,6 +796,7 @@ dependencies = [
"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
"checksum futures 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "45dc39533a6cae6da2b56da48edae506bb767ec07370f86f70fc062e9d435869"
+"checksum futures01 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "600729d07c912e06659601fd1494a82e5b3b8ecf88ccb84faf90e052f4bfefd1"
"checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
@@ -827,9 +814,6 @@ dependencies = [
"checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337"
"checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9"
"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
-"checksum protobuf 2.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8aefcec9f142b524d98fc81d07827743be89dd6586a1ba6ab21fa66a500b3fa5"
-"checksum protobuf-codegen 2.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "31539be8028d6b9e8e1b3b7c74e2fa3555302e27b2cc20dbaee6ffba648f75e2"
-"checksum protobuf-codegen-pure 2.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "00993dc5fbbfcf9d8a005f6b6c29fd29fd6d86deba3ae3f41fd20c624c414616"
"checksum quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1"
"checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca"
"checksum rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef"
diff --git a/Cargo.toml b/Cargo.toml
index 961cb15..b73dde7 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,9 +6,6 @@ edition = "2018"
[dependencies]
clap = "2"
-protobuf = "2"
-snafu = "0.4"
+futures = "0.1"
+snafu = { version = "0.4", features = ["futures-01"] }
tokio = "0.1"
-
-[build-dependencies]
-protobuf-codegen-pure = "2"
diff --git a/build.rs b/build.rs
deleted file mode 100644
index fe3b945..0000000
--- a/build.rs
+++ /dev/null
@@ -1,11 +0,0 @@
-fn main() {
- protobuf_codegen_pure::run(protobuf_codegen_pure::Args {
- out_dir: "src/pb",
- input: &["proto/termcast.proto"],
- includes: &["proto"],
- customize: protobuf_codegen_pure::Customize {
- ..Default::default()
- },
- })
- .expect("protobuf_codegen_pure");
-}
diff --git a/proto/termcast.proto b/proto/termcast.proto
deleted file mode 100644
index 44550f1..0000000
--- a/proto/termcast.proto
+++ /dev/null
@@ -1,17 +0,0 @@
-syntax = "proto3";
-
-message ListRequest {
- // NEXT: 1
-}
-
-message ListResponse {
- // NEXT: 1
-}
-
-message WatchRequest {
- // NEXT: 1
-}
-
-message WatchProgress {
- // NEXT: 1
-}
diff --git a/src/cmd/cast.rs b/src/cmd/cast.rs
index bea3655..042ebd8 100644
--- a/src/cmd/cast.rs
+++ b/src/cmd/cast.rs
@@ -1,7 +1,13 @@
use snafu::ResultExt as _;
#[derive(Debug, snafu::Snafu)]
-pub enum Error {}
+pub enum Error {
+ #[snafu(display("failed to connect: {}", source))]
+ Connect { source: std::io::Error },
+
+ #[snafu(display("failed to write message: {}", source))]
+ Write { source: crate::protocol::Error },
+}
pub type Result<T> = std::result::Result<T, Error>;
@@ -14,5 +20,10 @@ pub fn run<'a>(_matches: &clap::ArgMatches<'a>) -> super::Result<()> {
}
fn run_impl() -> Result<()> {
- unimplemented!()
+ let sock =
+ std::net::TcpStream::connect("127.0.0.1:8000").context(Connect)?;
+ let msg = crate::protocol::Message::start_casting("doy");
+ msg.write(sock).context(Write)?;
+ println!("wrote message successfully");
+ Ok(())
}
diff --git a/src/cmd/server.rs b/src/cmd/server.rs
index 9433e83..f994ee7 100644
--- a/src/cmd/server.rs
+++ b/src/cmd/server.rs
@@ -25,12 +25,19 @@ fn run_impl() -> Result<()> {
let listener = tokio::net::TcpListener::bind(&addr).context(Bind)?;
let server = listener
.incoming()
- .for_each(|_sock| {
- println!("got a connection");
- Ok(())
- })
.map_err(|e| {
eprintln!("accept failed: {}", e);
+ })
+ .for_each(|sock| {
+ crate::protocol::Message::read_async(sock)
+ .map(|msg| match msg {
+ crate::protocol::Message::StartCasting { username } => {
+ println!("got a connection from {}", username);
+ }
+ })
+ .map_err(|e| {
+ eprintln!("failed to read message: {}", e);
+ })
});
tokio::run(server);
Ok(())
diff --git a/src/main.rs b/src/main.rs
index b2fb129..6a15d83 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,5 @@
mod cmd;
-mod pb;
+mod protocol;
mod util;
fn main() {
diff --git a/src/pb.rs b/src/pb.rs
deleted file mode 100644
index 7503dfc..0000000
--- a/src/pb.rs
+++ /dev/null
@@ -1 +0,0 @@
-pub mod termcast;
diff --git a/src/protocol.rs b/src/protocol.rs
new file mode 100644
index 0000000..bcddc6a
--- /dev/null
+++ b/src/protocol.rs
@@ -0,0 +1,115 @@
+use futures::future::Future as _;
+use snafu::futures01::FutureExt as _;
+use snafu::ResultExt as _;
+use std::convert::{TryFrom as _, TryInto as _};
+
+#[derive(Debug, snafu::Snafu)]
+pub enum Error {
+ #[snafu(display("failed to read packet: {}", source))]
+ ReadAsync { source: tokio::io::Error },
+
+ #[snafu(display("failed to write packet: {}", source))]
+ Write { source: std::io::Error },
+
+ #[snafu(display("invalid StartCasting message: {}", source))]
+ ParseStartCastingMessage { source: std::string::FromUtf8Error },
+
+ #[snafu(display("invalid message type: {}", ty))]
+ InvalidMessageType { ty: u32 },
+}
+
+pub type Result<T> = std::result::Result<T, Error>;
+
+pub enum Message {
+ StartCasting { username: String },
+}
+
+impl Message {
+ pub fn start_casting(username: &str) -> Message {
+ Message::StartCasting {
+ username: username.to_string(),
+ }
+ }
+
+ pub fn read_async<R: tokio::io::AsyncRead>(
+ r: R,
+ ) -> impl futures::future::Future<Item = Self, Error = Error> {
+ Packet::read_async(r).and_then(Self::try_from)
+ }
+
+ pub fn write<W: std::io::Write>(&self, w: W) -> Result<()> {
+ Packet::from(self).write(w)
+ }
+}
+
+struct Packet {
+ ty: u32,
+ data: Vec<u8>,
+}
+
+impl Packet {
+ fn read_async<R: tokio::io::AsyncRead>(
+ r: R,
+ ) -> impl futures::future::Future<Item = Self, Error = Error> {
+ let header_buf = [0u8; std::mem::size_of::<u32>() * 2];
+ tokio::io::read_exact(r, header_buf)
+ .and_then(|(r, buf)| {
+ let (len_buf, ty_buf) =
+ buf.split_at(std::mem::size_of::<u32>());
+ let len = u32::from_le_bytes(len_buf.try_into().unwrap());
+ let ty = u32::from_le_bytes(ty_buf.try_into().unwrap());
+ futures::future::ok((r, len, ty))
+ })
+ .and_then(|(r, len, ty)| {
+ let body_buf = vec![0u8; len as usize];
+ tokio::io::read_exact(r, body_buf)
+ .map(move |(_, buf)| (ty, buf))
+ })
+ .and_then(|(ty, buf)| {
+ futures::future::ok(Packet {
+ ty,
+ data: buf.to_vec(),
+ })
+ })
+ .context(ReadAsync)
+ }
+
+ fn write<W: std::io::Write>(&self, mut w: W) -> Result<()> {
+ Ok(w.write_all(&self.as_bytes()).context(Write)?)
+ }
+
+ fn as_bytes(&self) -> Vec<u8> {
+ let len = (self.data.len() as u32).to_le_bytes();
+ let ty = self.ty.to_le_bytes();
+ len.iter()
+ .chain(ty.iter())
+ .chain(self.data.iter())
+ .cloned()
+ .collect()
+ }
+}
+
+impl From<&Message> for Packet {
+ fn from(msg: &Message) -> Self {
+ match msg {
+ Message::StartCasting { username } => Packet {
+ ty: 0,
+ data: username.as_bytes().to_vec(),
+ },
+ }
+ }
+}
+
+impl std::convert::TryFrom<Packet> for Message {
+ type Error = Error;
+
+ fn try_from(packet: Packet) -> Result<Self> {
+ match packet.ty {
+ 0 => Ok(Message::StartCasting {
+ username: std::string::String::from_utf8(packet.data)
+ .context(ParseStartCastingMessage)?,
+ }),
+ _ => Err(Error::InvalidMessageType { ty: packet.ty }),
+ }
+ }
+}