Skip to content

Client to Server RPC #29

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 3 commits into
base: master
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
81 changes: 81 additions & 0 deletions LiteEntitySystem/ClientEntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using LiteEntitySystem.Transport;
using LiteEntitySystem.Collections;
using LiteNetLib;
using LiteNetLib.Utils;

namespace LiteEntitySystem
{
Expand Down Expand Up @@ -257,6 +258,86 @@ internal T AddLocalEntity<T>(Action<T> initMethod = null) where T : EntityLogic

return entity;
}
internal void SendServerRPC<T>(ushort entityId, ushort rpcId, T value) where T : unmanaged
{
// Prepare a NetDataWriter
NetDataWriter writer = new NetDataWriter();

// Add packet header and type
writer.Put(HeaderByte); // Header byte for this entity system
writer.Put((byte)InternalPackets.ClientRPC); // Denote it's a client->server RPC

// Add entity ID and RPC ID
writer.Put(entityId);
writer.Put(rpcId);

unsafe
{
byte[] byteArray = new byte[sizeof(T)];
fixed (byte* byteArrayPtr = byteArray)
{
Buffer.MemoryCopy(&value, byteArrayPtr, sizeof(T), sizeof(T));
}

writer.Put(byteArray);
}

_netPeer.SendReliableOrdered(writer.AsReadOnlySpan());
}

// For "RemoteCallSpan<T>"
internal void SendServerRPC<T>(ushort entityId, ushort rpcId, ReadOnlySpan<T> data) where T : unmanaged
{
// Prepare a NetDataWriter
NetDataWriter writer = new NetDataWriter();

// Add packet header and type
writer.Put(HeaderByte);
writer.Put(InternalPackets.ClientRPC);

// Add entity ID and RPC ID
writer.Put(entityId);
writer.Put(rpcId);

byte[] byteArray;
// Convert span to byte array
unsafe
{
byteArray = new byte[data.Length * sizeof(T)];
fixed (T* dataPtr = data)
fixed (byte* bytePtr = byteArray)
{
Buffer.MemoryCopy(dataPtr, bytePtr, byteArray.Length, byteArray.Length);
}
}

// Write the byte array
writer.Put(byteArray);

// Send the data
_netPeer.SendReliableOrdered(writer.AsReadOnlySpan());
}

// For "RemoteCallSerializable<T>"
internal void SendServerRPC(ushort entityId, ushort rpcId, ReadOnlySpan<byte> data)
{
// Prepare a NetDataWriter
NetDataWriter writer = new NetDataWriter();

// Add packet header and type
writer.Put(HeaderByte);
writer.Put(InternalPackets.ClientRPC);

// Add entity ID and RPC ID
writer.Put(entityId);
writer.Put(rpcId);

// Convert span to byte array and write
writer.Put(data.ToArray());

// Send the data
_netPeer.SendReliableOrdered(writer.AsReadOnlySpan());
}

internal EntityLogic FindEntityByPredictedId(ushort tick, ushort parentId, ushort predictedId)
{
Expand Down
Loading