Skip to content

Commit

Permalink
rework connection traits a bit again to prevent having to import two …
Browse files Browse the repository at this point in the history
…traits in most cases
  • Loading branch information
icewind1991 committed Oct 29, 2024
1 parent 32f5098 commit 3c24f7a
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "steam-vent"
version = "0.4.0"
version = "0.3.0"
authors = ["Robin Appelman <[email protected]>"]
edition = "2021"
description = "Interact with the Steam network via rust"
Expand Down
2 changes: 1 addition & 1 deletion examples/auth_ticket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use steam_vent::auth::{
AuthConfirmationHandler, ConsoleAuthConfirmationHandler, DeviceConfirmationHandler,
FileGuardDataStore,
};
use steam_vent::connection::{ConnectionListener, UnAuthenticatedConnection};
use steam_vent::connection::{ReadonlyConnection, UnAuthenticatedConnection};
use steam_vent::ServerList;
use steam_vent_proto::steammessages_clientserver::CMsgClientGameConnectTokens;

Expand Down
3 changes: 1 addition & 2 deletions examples/backpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use steam_vent::auth::{
AuthConfirmationHandler, ConsoleAuthConfirmationHandler, DeviceConfirmationHandler,
FileGuardDataStore,
};
use steam_vent::connection::ConnectionListener;
use steam_vent::{Connection, ConnectionSender, GameCoordinator, ServerList};
use steam_vent::{Connection, ConnectionTrait, GameCoordinator, ServerList};
use steam_vent_proto::tf2::base_gcmessages::CSOEconItem;
use steam_vent_proto::tf2::gcsdk_gcmessages::{
CMsgSOCacheSubscribed, CMsgSOCacheSubscriptionRefresh,
Expand Down
3 changes: 1 addition & 2 deletions examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use steam_vent::auth::{
AuthConfirmationHandler, ConsoleAuthConfirmationHandler, DeviceConfirmationHandler,
FileGuardDataStore,
};
use steam_vent::connection::ConnectionListener;
use steam_vent::proto::steammessages_friendmessages_steamclient::{
CFriendMessages_IncomingMessage_Notification, CFriendMessages_SendMessage_Request,
};
use steam_vent::{Connection, ConnectionSender, ServerList};
use steam_vent::{Connection, ConnectionTrait, ServerList};
use steam_vent_proto::enums::EPersonaStateFlag;
use steam_vent_proto::steammessages_clientserver_friends::CMsgClientChangeStatus;
use steamid_ng::SteamID;
Expand Down
2 changes: 1 addition & 1 deletion examples/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use steam_vent::auth::{
FileGuardDataStore, SharedSecretAuthConfirmationHandler,
};
use steam_vent::proto::steammessages_player_steamclient::CPlayer_GetOwnedGames_Request;
use steam_vent::{Connection, ConnectionSender, ServerList};
use steam_vent::{Connection, ConnectionTrait, ServerList};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
Expand Down
2 changes: 1 addition & 1 deletion examples/product_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use steam_vent::proto::steammessages_clientserver_appinfo::{
cmsg_client_picsproduct_info_request, CMsgClientPICSProductInfoRequest,
CMsgClientPICSProductInfoResponse,
};
use steam_vent::{Connection, ConnectionSender, ServerList};
use steam_vent::{Connection, ConnectionTrait, ServerList};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
Expand Down
2 changes: 1 addition & 1 deletion examples/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::error::Error;
use steam_vent::proto::steammessages_gameservers_steamclient::CGameServers_GetServerList_Request;
use steam_vent::{Connection, ConnectionSender, ServerList};
use steam_vent::{Connection, ConnectionTrait, ServerList};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
Expand Down
28 changes: 22 additions & 6 deletions src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ pub(crate) trait ConnectionImpl: Sync + Debug {
) -> impl Future<Output = Result<()>> + Send;
}

/// A trait for listening for messages coming from steam
pub trait ConnectionListener {
/// A trait for connections that only allow listening for messages coming from steam
pub trait ReadonlyConnection {
fn on_notification<T: ServiceMethodRequest>(&self) -> impl Stream<Item = Result<T>> + 'static;

/// Wait for one message of a specific kind, also returning the header
Expand All @@ -160,7 +160,25 @@ pub trait ConnectionListener {
}

/// A trait for sending messages to steam
pub trait ConnectionSender {
pub trait ConnectionTrait {
fn on_notification<T: ServiceMethodRequest>(&self) -> impl Stream<Item = Result<T>> + 'static;

/// Wait for one message of a specific kind, also returning the header
fn one_with_header<T: NetMessage + 'static>(
&self,
) -> impl Future<Output = Result<(NetMessageHeader, T)>> + 'static;

/// Wait for one message of a specific kind
fn one<T: NetMessage + 'static>(&self) -> impl Future<Output = Result<T>> + 'static;

/// Listen to messages of a specific kind, also returning the header
fn on_with_header<T: NetMessage + 'static>(
&self,
) -> impl Stream<Item = Result<(NetMessageHeader, T)>> + 'static;

/// Listen to messages of a specific kind
fn on<T: NetMessage + 'static>(&self) -> impl Stream<Item = Result<T>> + 'static;

/// Send a rpc-request to steam, waiting for the matching rpc-response
fn service_method<Msg: ServiceMethodRequest>(
&self,
Expand Down Expand Up @@ -235,7 +253,7 @@ impl ConnectionImpl for Connection {
}
}

impl<C: ConnectionImpl> ConnectionListener for C {
impl<C: ConnectionImpl> ConnectionTrait for C {
fn on_notification<T: ServiceMethodRequest>(&self) -> impl Stream<Item = Result<T>> + 'static {
BroadcastStream::new(self.filter().on_notification(T::REQ_NAME))
.filter_map(|res| res.ok())
Expand Down Expand Up @@ -272,9 +290,7 @@ impl<C: ConnectionImpl> ConnectionListener for C {
self.on_with_header::<T>()
.map(|res| res.map(|(_, msg)| msg))
}
}

impl<C: ConnectionImpl> ConnectionSender for C {
async fn service_method<Msg: ServiceMethodRequest>(&self, msg: Msg) -> Result<Msg::Response> {
let header = self.session().header(true);
let recv = self.filter().on_job_id(header.source_job_id);
Expand Down
4 changes: 2 additions & 2 deletions src/connection/unauthenticated.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::raw::RawConnection;
use super::{ConnectionListener, Result};
use super::{ReadonlyConnection, Result};
use crate::auth::{begin_password_auth, AuthConfirmationHandler, GuardDataStore};
use crate::message::{ServiceMethodMessage, ServiceMethodResponseMessage};
use crate::net::{NetMessageHeader, RawNetMessage};
Expand Down Expand Up @@ -109,7 +109,7 @@ impl UnAuthenticatedConnection {
}

/// Listen for messages before starting authentication
impl ConnectionListener for UnAuthenticatedConnection {
impl ReadonlyConnection for UnAuthenticatedConnection {
fn on_notification<T: ServiceMethodRequest>(&self) -> impl Stream<Item = Result<T>> + 'static {
BroadcastStream::new(self.0.filter.on_notification(T::REQ_NAME))
.filter_map(|res| res.ok())
Expand Down
4 changes: 1 addition & 3 deletions src/game_coordinator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::connection::{
ConnectionImpl, ConnectionListener, ConnectionSender, MessageFilter, MessageSender,
};
use crate::connection::{ConnectionImpl, ConnectionTrait, MessageFilter, MessageSender};
use crate::message::EncodableMessage;
use crate::net::{decode_kind, NetMessageHeader, RawNetMessage};
use crate::session::Session;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod transport;

pub use steam_vent_proto as proto;

pub use connection::{Connection, ConnectionSender};
pub use connection::{Connection, ConnectionTrait, ReadonlyConnection};
pub use eresult::EResult;
pub use game_coordinator::GameCoordinator;
pub use message::NetMessage;
Expand Down
2 changes: 1 addition & 1 deletion src/session.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::auth::{ConfirmationError, ConfirmationMethod};
use crate::connection::raw::RawConnection;
use crate::connection::{ConnectionImpl, ConnectionSender};
use crate::connection::{ConnectionImpl, ConnectionTrait};
use crate::eresult::EResult;
use crate::net::{JobId, NetMessageHeader, NetworkError};
use crate::proto::steammessages_base::CMsgIPAddress;
Expand Down

0 comments on commit 3c24f7a

Please sign in to comment.