Skip to content

Channel Support for Networking #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 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
Expand Up @@ -11,7 +11,7 @@ napi = { version = "2.16.8", features = ["tokio_rt", "napi6", "serde-json"] }
napi-derive = "2.16.9"
lazy_static = "1"
tokio = { version = "1", features = ["sync", "time"] }
steamworks = { git = "https://github.com/Noxime/steamworks-rs/", rev = "fbb79635b06b4feea8261e5ca3e8ea3ef42facf9", features = ["serde"] }
steamworks = { git = "https://github.com/Noxime/steamworks-rs/", rev = "5fc8ef13d52e82068f031535446d786cf0bd60a8", features = ["serde"] }
serde = "1"
serde_json = "1"

Expand Down
3 changes: 3 additions & 0 deletions client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,11 @@ export declare namespace networking {
ReliableWithBuffering = 3
}
export function sendP2PPacket(steamId64: bigint, sendType: SendType, data: Buffer): boolean
export function sendP2PPacketOnChannel(steamId64: bigint, sendType: SendType, data: Buffer, channel: number): boolean
export function isP2PPacketAvailable(): number
export function isP2PPacketAvailableOnChannel(channel: number): number
export function readP2PPacket(size: number): P2PPacket
export function readP2PPacketFromChannel(size: number, channel: number): P2PPacket
export function acceptP2PSession(steamId64: bigint): void
}
export declare namespace overlay {
Expand Down
27 changes: 24 additions & 3 deletions src/api/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,19 @@ pub mod networking {
steam_id64: BigInt,
send_type: SendType,
data: Buffer,
) -> Result<bool, Error> {
self::send_p2p_packet_on_channel(steam_id64, send_type, data, 0)
}

#[napi]
pub fn send_p2p_packet_on_channel(
steam_id64: BigInt,
send_type: SendType,
data: Buffer,
channel: i32,
) -> Result<bool, Error> {
let client = crate::client::get_client();
let result = client.networking().send_p2p_packet(
let result = client.networking().send_p2p_packet_on_channel(
SteamId::from_raw(steam_id64.get_u64().1),
match send_type {
SendType::Unreliable => steamworks::SendType::Unreliable,
Expand All @@ -52,27 +62,38 @@ pub mod networking {
SendType::ReliableWithBuffering => steamworks::SendType::ReliableWithBuffering,
},
&data,
channel
);
Ok(result)
}

#[napi]
pub fn is_p2p_packet_available() -> i32 {
self::is_p2p_packet_available_on_channel(0)
}

#[napi]
pub fn is_p2p_packet_available_on_channel(channel: i32) -> i32 {
let client = crate::client::get_client();
client
.networking()
.is_p2p_packet_available()
.is_p2p_packet_available_on_channel(channel)
.unwrap_or_default() as i32
}

#[napi]
pub fn read_p2p_packet(size: i32) -> Result<P2PPacket, Error> {
self::read_p2p_packet_from_channel(size, 0)
}

#[napi]
pub fn read_p2p_packet_from_channel(size: i32, channel: i32) -> Result<P2PPacket, Error> {
let client = crate::client::get_client();
let mut buffer = vec![0; size as usize];

client
.networking()
.read_p2p_packet(&mut buffer)
.read_p2p_packet_from_channel(&mut buffer, channel)
.map(|(steam_id, read_size)| P2PPacket {
data: buffer.into(),
size: read_size as i32,
Expand Down