Table of Contents

Class SteamP2PManager

Namespace
SteamNetworkLib.Core
Assembly
SteamNetworkLib.dll

Manages Steam P2P networking for direct player-to-player communication. Provides functionality for sending messages, managing sessions, and handling P2P events.

public class SteamP2PManager : IDisposable
Inheritance
SteamP2PManager
Implements
Inherited Members

Constructors

SteamP2PManager(SteamLobbyManager)

Initializes a new instance of the SteamP2PManager class.

public SteamP2PManager(SteamLobbyManager lobbyManager)

Parameters

lobbyManager SteamLobbyManager

The lobby manager instance to use for lobby operations.

Exceptions

ArgumentNullException

Thrown when lobbyManager is null.

SteamNetworkException

Thrown when Steam is not initialized.

Properties

ActiveSessionCount

Gets the number of currently active P2P sessions.

public int ActiveSessionCount { get; }

Property Value

int

IsActive

Gets a value indicating whether the P2P manager is active and ready to send/receive data.

public bool IsActive { get; }

Property Value

bool

MaxPacketSize

Gets the maximum packet size in bytes that can be sent via P2P communication.

public int MaxPacketSize { get; }

Property Value

int

Methods

AcceptSession(CSteamID)

Accepts a P2P session request from another player.

public bool AcceptSession(CSteamID playerId)

Parameters

playerId CSteamID

The Steam ID of the player to accept a session with.

Returns

bool

True if the session was accepted successfully, false otherwise.

BroadcastMessage(P2PMessage, int, EP2PSend)

Broadcasts a message to all players in the current lobby.

public void BroadcastMessage(P2PMessage message, int channel = 0, EP2PSend sendType = 2)

Parameters

message P2PMessage

The message to broadcast.

channel int

The communication channel to use (default: 0).

sendType EP2PSend

The send type for reliability and ordering (default: reliable).

Exceptions

P2PException

Thrown when not in a lobby.

BroadcastPacket(byte[], int, EP2PSend)

Broadcasts raw packet data to all players in the current lobby.

public void BroadcastPacket(byte[] data, int channel = 0, EP2PSend sendType = 2)

Parameters

data byte[]

The raw data to broadcast.

channel int

The communication channel to use (default: 0).

sendType EP2PSend

The send type for reliability and ordering (default: reliable).

Exceptions

P2PException

Thrown when not in a lobby.

CleanupInactiveSessions(TimeSpan)

Closes P2P sessions that have been inactive for longer than the specified threshold.

public void CleanupInactiveSessions(TimeSpan inactiveThreshold)

Parameters

inactiveThreshold TimeSpan

The time threshold for considering a session inactive.

CloseSession(CSteamID)

Closes a P2P session with a specific player.

public void CloseSession(CSteamID playerId)

Parameters

playerId CSteamID

The Steam ID of the player to close the session with.

Dispose()

Releases all resources used by the SteamP2PManager.

public void Dispose()

GetActiveSessions()

Gets a list of all currently active P2P sessions.

public List<CSteamID> GetActiveSessions()

Returns

List<CSteamID>

A list of Steam IDs representing active P2P sessions.

GetSessionState(CSteamID)

Gets the current state of a P2P session with a specific player.

public P2PSessionState_t GetSessionState(CSteamID playerId)

Parameters

playerId CSteamID

The Steam ID of the player to get session state for.

Returns

P2PSessionState_t

The current P2P session state information.

ProcessIncomingPackets()

Processes all incoming P2P packets. Call this regularly (e.g., in Update()) to handle received data.

public void ProcessIncomingPackets()

RegisterMessageHandler<T>(Action<T, CSteamID>)

Registers a handler for a specific message type.

public void RegisterMessageHandler<T>(Action<T, CSteamID> handler) where T : P2PMessage, new()

Parameters

handler Action<T, CSteamID>

The handler function that will be called when messages of this type are received.

Type Parameters

T

The type of message to handle.

SendMessageAsync(CSteamID, P2PMessage, int, EP2PSend)

Sends a message to a specific player via P2P communication.

public Task<bool> SendMessageAsync(CSteamID targetId, P2PMessage message, int channel = 0, EP2PSend sendType = 2)

Parameters

targetId CSteamID

The Steam ID of the target player.

message P2PMessage

The message to send.

channel int

The communication channel to use (default: 0).

sendType EP2PSend

The send type for reliability and ordering (default: reliable).

Returns

Task<bool>

A task that represents the asynchronous operation. The task result indicates whether the message was sent successfully.

Exceptions

P2PException

Thrown when the P2P manager is not active, target ID is invalid, or sending fails.

SendPacketAsync(CSteamID, byte[], int, EP2PSend)

Sends raw packet data to a specific player via P2P communication.

public Task<bool> SendPacketAsync(CSteamID targetId, byte[] data, int channel = 0, EP2PSend sendType = 2)

Parameters

targetId CSteamID

The Steam ID of the target player.

data byte[]

The raw data to send.

channel int

The communication channel to use (default: 0).

sendType EP2PSend

The send type for reliability and ordering (default: reliable).

Returns

Task<bool>

A task that represents the asynchronous operation. The task result indicates whether the packet was sent successfully.

Exceptions

P2PException

Thrown when the P2P manager is not active, target ID is invalid, packet is too large, or sending fails.

UnregisterMessageHandler<T>()

Unregisters all handlers for a specific message type.

public void UnregisterMessageHandler<T>() where T : P2PMessage, new()

Type Parameters

T

The type of message to unregister handlers for.

Events

OnMessageReceived

Occurs when a SteamNetworkLib message is received and deserialized.

public event EventHandler<P2PMessageReceivedEventArgs>? OnMessageReceived

Event Type

EventHandler<P2PMessageReceivedEventArgs>

OnPacketReceived

Occurs when a raw P2P packet is received from another player.

public event EventHandler<P2PPacketReceivedEventArgs>? OnPacketReceived

Event Type

EventHandler<P2PPacketReceivedEventArgs>

OnPacketSent

Occurs when a P2P packet is sent (provides send result information).

public event EventHandler<P2PPacketSentEventArgs>? OnPacketSent

Event Type

EventHandler<P2PPacketSentEventArgs>

OnSessionConnectFail

Occurs when a P2P session connection fails.

public event EventHandler<P2PSessionConnectFailEventArgs>? OnSessionConnectFail

Event Type

EventHandler<P2PSessionConnectFailEventArgs>

OnSessionRequested

Occurs when another player requests a P2P session with the local player.

public event EventHandler<P2PSessionRequestEventArgs>? OnSessionRequested

Event Type

EventHandler<P2PSessionRequestEventArgs>