Skip to content

Commit

Permalink
impl send & reply to use BinaryStream
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Aug 16, 2024
1 parent 3620022 commit 07345f6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,10 @@ export declare class Datagram {
* The size of the buffer.
*/
size: number
/**
* The time between the last packet and the current packet.
*/
deltaTime: number
reply(stream: BinaryStream): void
}
export declare class Socket {
Expand Down
11 changes: 8 additions & 3 deletions src/socket/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ pub struct Datagram {
*/
pub size: u32,

/**
* The time between the last packet and the current packet.
*/
pub delta_time: u32,

#[napi(skip)]
pub socket: UdpSocket,
}
Expand All @@ -35,15 +40,15 @@ impl Datagram {
/**
* Create a new instance of the Datagram struct.
*/
pub fn new(identifier: NetworkIdentifier, buffer: Buffer, size: u32, socket: UdpSocket) -> Self {
pub fn new(identifier: NetworkIdentifier, buffer: Buffer, size: u32, socket: UdpSocket, delta_time: u32) -> Self {
// Create a new BinaryStream from the buffer.
let stream = BinaryStream::new(Some(buffer), None);

Self { identifier, stream, size, socket }
Self { identifier, stream, size, socket , delta_time}
}

#[napi]
pub fn reply(&self, stream: BinaryStream) -> Result<()> {
pub fn reply(&self, stream: &BinaryStream) -> Result<()> {
match self.socket.send_to(&stream.binary, self.identifier.to_addr()) {
Ok(_) => Ok(()),
Err(err) => Err(err.into())
Expand Down
14 changes: 11 additions & 3 deletions src/socket/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,16 @@ impl Socket {
// Create a buffer to store the incoming packet
let mut buf = [0; 2048];

let mut time = std::time::Instant::now();

// Loop to receive packets
loop {
// Calculate the delta time
let delta_time = time.elapsed().as_millis() as u32;

// Reset the time
time = std::time::Instant::now();

// Lock the alive mutex
let alive = alive.lock().unwrap();

Expand Down Expand Up @@ -120,7 +128,7 @@ impl Socket {
};

// Create a new Datagram instance
let datagram = Datagram::new(identifier, buffer, size as u32, socket);
let datagram = Datagram::new(identifier, buffer, size as u32, socket, delta_time);

// Call the recv function
recv.call(Ok(datagram), napi::threadsafe_function::ThreadsafeFunctionCallMode::NonBlocking);
Expand All @@ -143,11 +151,11 @@ impl Socket {
}

#[napi]
pub fn send(&self, identifier: NetworkIdentifier, stream: BinaryStream) -> Result<()> {
pub fn send(&self, identifier: NetworkIdentifier, stream: &BinaryStream) -> Result<()> {
// Lock the socket
let socket = self.socket.lock().unwrap();
let addr = identifier.to_addr();
let buf = stream.binary;
let buf = &stream.binary;

// Send the packet
match socket.send_to(&buf, addr) {
Expand Down

0 comments on commit 07345f6

Please sign in to comment.