pub struct NetworkingMessages<Manager> { /* private fields */ }
Expand description

Access to the steam networking messages interface

Implementations§

source§

impl<Manager: 'static> NetworkingMessages<Manager>

source

pub fn send_message_to_user( &self, user: NetworkingIdentity, send_type: SendFlags, data: &[u8], channel: u32 ) -> Result<(), SteamError>

Sends a message to the specified host.

If we don’t already have a session with that user, a session is implicitly created. There might be some handshaking that needs to happen before we can actually begin sending message data. If this handshaking fails and we can’t get through, an error will be posted via the callback SteamNetworkingMessagesSessionFailed_t. There is no notification when the operation succeeds. (You should have the peer send a reply for this purpose.)

Sending a message to a host will also implicitly accept any incoming connection from that host.

channel is a routing number you can use to help route message to different systems. You’ll have to call ReceiveMessagesOnChannel() with the same channel number in order to retrieve the data on the other end.

Using different channels to talk to the same user will still use the same underlying connection, saving on resources. If you don’t need this feature, use 0. Otherwise, small integers are the most efficient.

It is guaranteed that reliable messages to the same host on the same channel will be be received by the remote host (if they are received at all) exactly once, and in the same order that they were sent.

NO other order guarantees exist! In particular, unreliable messages may be dropped, received out of order with respect to each other and with respect to reliable data, or may be received multiple times. Messages on different channels are not guaranteed to be received in the order they were sent.

A note for those familiar with TCP/IP ports, or converting an existing codebase that opened multiple sockets: You might notice that there is only one channel, and with TCP/IP each endpoint has a port number. You can think of the channel number as the destination port. If you need each message to also include a “source port” (so the recipient can route the reply), then just put that in your message. That is essentially how UDP works!

Returns:

  • k_EREsultOK on success.
  • k_EResultNoConnection will be returned if the session has failed or was closed by the peer, and k_nSteamNetworkingSend_AutoRestartBrokenSession is not used. (You can use GetSessionConnectionInfo to get the details.) In order to acknowledge the broken session and start a new one, you must call CloseSessionWithUser
  • See ISteamNetworkingSockets::SendMessageToConnection for more possible return values
source

pub fn receive_messages_on_channel( &self, channel: u32, batch_size: usize ) -> Vec<NetworkingMessage<Manager>>

Reads the next message that has been sent from another user on the given channel.

batch_size is the maximum number of messages that can be received at once.

§Example
let (client, single) = Client::init().unwrap();

// run_callbacks must be called regularly, or no incoming connections can be received
let callback_loop = std::thread::spawn(move || loop {
    single.run_callbacks();
    std::thread::sleep(Duration::from_millis(10));
});
let networking_messages = client.networking_messages();

// Accept all new connections
networking_messages.session_request_callback(|request| request.accept());

let _received = networking_messages.receive_messages_on_channel(0, 10);
source

pub fn session_request_callback( &self, callback: impl FnMut(SessionRequest<Manager>) + Send + 'static )

Register a callback that will be called whenever a peer requests a connection.

Use the SessionRequest to accept or reject the connection.

Requires regularly calling SingleClient.run_callbacks(). Calling this function more than once will replace the previous callback.

§Example
let (client, single) = Client::init().unwrap();

// run_callbacks must be called regularly, or no incoming connections can be received
let callback_loop = std::thread::spawn(move || loop {
    single.run_callbacks();
    std::thread::sleep(Duration::from_millis(10));
});
let messages = client.networking_messages();

// Accept all incoming connections
messages.session_request_callback(|request| {
    request.accept();
});
source

pub fn session_failed_callback( &self, callback: impl FnMut(NetConnectionInfo) + Send + 'static )

Register a callback that will be called whenever a connection fails to be established.

Requires regularly calling SingleClient.run_callbacks(). Calling this function more than once will replace the previous callback.

Trait Implementations§

source§

impl<Manager> Send for NetworkingMessages<Manager>

source§

impl<Manager> Sync for NetworkingMessages<Manager>

Auto Trait Implementations§

§

impl<Manager> RefUnwindSafe for NetworkingMessages<Manager>
where Manager: RefUnwindSafe,

§

impl<Manager> Unpin for NetworkingMessages<Manager>

§

impl<Manager> UnwindSafe for NetworkingMessages<Manager>
where Manager: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.