RevenantX / LiteNetLib

Lite reliable UDP library for Mono and .NET
https://revenantx.github.io/LiteNetLib/index.html
MIT License
3.05k stars 495 forks source link

Hi. Im deeply learning the API and coul'nt find a way to write data in PooledPacket. But in NetPeer Documentation there is a method to send PooledPacket. " SendPooledPacket(PooledPacket, int)" How to insert data in pooledPacket?Can you provide an example? #562

Closed SOHAMXBR closed 2 weeks ago

SOHAMXBR commented 2 weeks ago

Information about bug or feat

RevenantX commented 2 weeks ago

you need to create it first

var packet = peer.CreatePacketFromPool(DeliveryMethod.Unreliable, 0);
packet.Data[packet.UserDataOffset] = 5; //or create NetDataWriter/orSomeAnotherSerializer from packet.Data and set start position as packet.UserDataOffset
peer.SendPooledPacket(packet, 1 /* size of your data */);
SOHAMXBR commented 2 weeks ago

Hi, Thank you ample for the response mail. But I still didn't understand how to insert data into the pooledPacket.There are no available methods.Please tell me an example code to insert data, like bytearray or string inside the pooledpacket.

On Tue, 3 Sept 2024 at 13:35, Ruslan Pyrch @.***> wrote:

Closed #562 https://github.com/RevenantX/LiteNetLib/issues/562 as completed.

— Reply to this email directly, view it on GitHub https://github.com/RevenantX/LiteNetLib/issues/562#event-14110985122, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO5HQ5O2H2LHV2RJI3D2BZDZUVUVVAVCNFSM6AAAAABNROGJDWVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJUGEYTAOJYGUYTEMQ . You are receiving this because you authored the thread.Message ID: @.***>

RevenantX commented 1 week ago

@SOHAMXBR there is preallocated ByteArray(byte[]) property named Data inside. Maximum allowed size is packet.MaxUserDataSize You just put bytes inside. You can use byte copy like Buffer.BlockCopy or some serializer like NetDataWriter.

var pp = _peer.CreatePacketFromPool(DeliveryMethod.Unreliable, 0);

//create example writer but this can be anything else including direct byteArrayManipulation
//but this is slow in real life - because NetDataWriter allocating data so PooledPacket has no sense here
var exampleWriter = new NetDataWriter();
exampleWriter.Put("SomeTestString");
Buffer.BlockCopy(exampleWriter.Data, 0, pp.Data, pp.UserDataOffset, exampleWriter.Length);
//exampleWriter.Length should be <= pp.MaxUserDataSize
_peer.SendPooledPacket(pp, exampleWriter.Length);

Another variant (more RAW)

var pp2 = _peer.CreatePacketFromPool(DeliveryMethod.Unreliable, 0);
//another variant
byte[] intBytes = BitConverter.GetBytes(32);
//int - 4 bytes
Buffer.BlockCopy(intBytes, 0, pp2.Data, pp2.UserDataOffset, sizeof(int));

_peer.SendPooledPacket(pp2, sizeof(int));

This thing is needed in most cases when you write into data using raw methods like unsafe or BitConverter and you need to eliminate one additional copy before send for epic speed. If you don't know how to write into ByteArray in C#, just ignore this methods currently. You won't loose much speed using default Send

SOHAMXBR commented 1 week ago

Thanks for the tip! I loved it

On Wed, 4 Sept, 2024, 1:41 pm Ruslan Pyrch, @.***> wrote:

@SOHAMXBR https://github.com/SOHAMXBR there is preallocated ByteArray(byte[]) property named Data inside of MTU (MaximumTransferUnit) size. Maximum allowed size is packet.MaxUserDataSize You just put bytes inside. You can use byte copy like Buffer.BlockCopy or some serializer like NetDataWriter.

var pp = _peer.CreatePacketFromPool(DeliveryMethod.Unreliable, 0); //create example writer but this can be anything else including direct byteArrayManipulation//but this is slow in real life - because NetDataWriter allocating data so PooledPacket has no sense herevar exampleWriter = new NetDataWriter(); exampleWriter.Put("SomeTestString"); Buffer.BlockCopy(exampleWriter.Data, 0, pp.Data, pp.UserDataOffset, exampleWriter.Length);//exampleWriter.Length should be <= pp.MaxUserDataSize _peer.SendPooledPacket(pp, exampleWriter.Length);

Another variant (more RAW)

var pp2 = _peer.CreatePacketFromPool(DeliveryMethod.Unreliable, 0);//another variantbyte[] intBytes = BitConverter.GetBytes(32);//int - 4 bytes Buffer.BlockCopy(intBytes, 0, pp2.Data, pp2.UserDataOffset, sizeof(int));

_peer.SendPooledPacket(pp2, sizeof(int));

This thing is needed in most cases when you write into data using raw methods like unsafe or BitConverter and you need to eliminate one additional copy before send for epic speed. If you don't know how to write into ByteArray in C#, just ignore this methods currently. You won't lose much speed using default Send

— Reply to this email directly, view it on GitHub https://github.com/RevenantX/LiteNetLib/issues/562#issuecomment-2328199987, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO5HQ5LS72F3FTI4CKIEO7DZU26BPAVCNFSM6AAAAABNROGJDWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRYGE4TSOJYG4 . You are receiving this because you were mentioned.Message ID: @.***>