Skip to main content

GnsSocket

Struct GnsSocket 

Source
pub struct GnsSocket<S> { /* private fields */ }
Expand description

A network socket, and the main type of this library.

Use GnsSocket::connect to create a client socket and GnsSocket::listen to create a server socket. Every operation on a socket is safe.

Dropping a socket frees everything that belongs to it. It does not free the GnsGlobal instance.

Implementations§

Source§

impl<S> GnsSocket<S>
where S: IsReady,

Source

pub fn get_connection_real_time_status( &self, GnsConnection: GnsConnection, nb_of_lanes: u32, ) -> GnsResult<(GnsConnectionRealTimeStatus, Vec<GnsConnectionRealTimeLaneStatus>)>

Returns the status of a connection and of its lanes.

Configure the lanes with Self::configure_connection_lanes before you call this.

Source

pub fn get_connection_info( &self, GnsConnection: GnsConnection, ) -> Option<GnsConnectionInfo>

Source

pub fn get_detailed_connection_status( &self, GnsConnection: GnsConnection, ) -> Option<String>

Returns a verbose human-readable description of the state of a connection, intended for diagnostics and debug dumps.

The format is subject to change between GameNetworkingSockets versions, so do not parse it. Returns None if the connection handle is invalid.

Source

pub fn get_connection_name( &self, GnsConnection: GnsConnection, ) -> Option<String>

Returns the debug name of a connection, previously set with Self::set_connection_name.

Returns None if the connection handle is invalid.

Source

pub fn set_connection_name( &self, GnsConnection: GnsConnection, name: &str, ) -> GnsResult<()>

Sets the debug name of a connection.

The name shows up in diagnostics such as Self::get_detailed_connection_status and the debug output, which makes multi-connection logs much easier to read.

§Errors

Returns GnsError::Config if name contains an interior NUL byte.

Source

pub fn flush_messages_on_connection( &self, GnsConnection: GnsConnection, ) -> GnsResult<()>

Source

pub fn close_connection( &self, GnsConnection: GnsConnection, reason: u32, debug: Option<&CStr>, linger: bool, ) -> GnsResult<()>

Closes a connection.

The wrapper forwards debug to the peer when you pass Some. Pass None to send no diagnostic string and avoid allocating.

§Errors

Returns GnsError::Close if the connection handle is invalid, for example because the connection is already closed.

Source

pub fn receive_messages<const K: usize>(&self) -> GnsResult<ReceivedMessages<K>>

Receives up to K messages and returns an iterator over the ones that were available.

Each message is yielded by value, so you can keep it, forward it, or let it drop, which releases it. Any message left in the iterator is released when the iterator is dropped.

The K-slot pointer buffer lives inline in the returned iterator, so this call allocates nothing and copies no payload. Use receive_messages_into to reuse one buffer across calls and avoid moving the inline array.

§Errors

Returns GnsError::Receive if the connection or poll group handle is invalid.

Source

pub fn receive_messages_into<'a>( &self, buffer: &'a mut [MessageSlot], ) -> GnsResult<ReceivedMessagesInto<'a>>

Receives up to buffer.len() messages into a buffer you own, and returns an iterator over the ones that were available.

This is the variant of receive_messages that neither allocates nor moves the buffer. GameNetworkingSockets fills buffer in place and the returned iterator borrows it, so reusing one buffer across a polling loop costs nothing per call.

§Errors

Returns GnsError::Receive if the connection or poll group handle is invalid.

Source

pub fn receive_events(&self) -> impl Iterator<Item = GnsConnectionEvent> + '_

Returns an iterator that drains the pending connection events.

Unlike receive_messages, you supply no buffer. Events arrive on an internal lock-free queue that the connection-status callback fills, and this call pops from that queue.

Source

pub fn configure_connection_lanes( &self, GnsConnection: GnsConnection, lanes: &[GnsLane], ) -> GnsResult<()>

Source

pub fn send_message( &self, message: GnsNetworkMessage<ToSend>, ) -> GnsResult<GnsMessageNumber>

Sends a single message to its target connection.

This is a convenience wrapper over send_messages for the common one-message case.

Source

pub fn send_messages( &self, messages: impl IntoIterator<Item = GnsNetworkMessage<ToSend>>, ) -> Vec<SendOutcome>

Sends each message to its target connection.

The returned Vec holds one SendOutcome per input message, in the same order.

Source§

impl GnsSocket<IsCreated>

Source

pub fn new(global: &'static GnsGlobal) -> Self

Creates a socket in the IsCreated state.

Source

pub fn listen( self, address: IpAddr, port: u16, ) -> GnsResult<GnsSocket<IsServer>>

Listens for incoming connections.

This moves the socket from IsCreated to IsServer, which gives you the server operations.

Source

pub fn connect( self, address: IpAddr, port: u16, ) -> GnsResult<GnsSocket<IsClient>>

Connects to a remote host.

This moves the socket from IsCreated to IsClient, which gives you the client operations.

Source

pub fn socket_pair( self, use_network_loopback: bool, ) -> GnsResult<(GnsSocket<IsClient>, GnsSocket<IsClient>)>

Creates a pair of connections that talk to each other, mainly for tests and loopback communication between parts of one process.

With use_network_loopback set, the traffic goes through the local network stack over 127.0.0.1. Without it, the payloads take an internal shortcut. See ISteamNetworkingSockets::CreateSocketPair for the trade-offs.

Both sockets come back in the IsClient state and already connected. The connections are created before the wrapper can attach its connection-state callback, so the initial transition to the connected state never shows up in GnsSocket::receive_events. Later events, such as the peer closing the connection, are delivered normally.

Source§

impl GnsSocket<IsServer>

Source

pub fn accept(&self, connection: GnsConnection) -> GnsResult<()>

Accepts an incoming connection. Only a socket in the IsServer state has this operation.

Source

pub fn get_listen_socket_address(&self) -> Option<(IpAddr, u16)>

Returns the address the listen socket is bound to.

The address part is the unspecified address when the socket listens on every interface, which is what an all-zeros IP passed to GnsSocket::listen requests.

Source

pub fn set_listen_socket_config_value( &self, typ: ESteamNetworkingConfigValue, value: GnsConfig<'_>, ) -> GnsResult<()>

Sets a configuration value on the listen socket, for example a connection option that every accepted connection inherits as its default.

Source

pub fn get_listen_socket_config_value( &self, typ: ESteamNetworkingConfigValue, ) -> GnsResult<GnsConfigValue>

Reads a configuration value back from the listen socket.

Source§

impl GnsSocket<IsClient>

Source

pub fn connection(&self) -> GnsConnection

Returns the socket connection. Only a socket in the IsClient state has this operation.

Auto Trait Implementations§

§

impl<S> Freeze for GnsSocket<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for GnsSocket<S>
where S: RefUnwindSafe,

§

impl<S> Send for GnsSocket<S>
where S: Send,

§

impl<S> Sync for GnsSocket<S>
where S: Sync,

§

impl<S> Unpin for GnsSocket<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for GnsSocket<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for GnsSocket<S>
where S: UnwindSafe,

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>,

Source§

type Error = !

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>,

Source§

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.