Most efficient way to write packets?

04/14/2011 20:36 Secured#1
What is more efficient to use?
Pointers or MemoryStream?
04/14/2011 21:01 KraHen#2
Or serializing structs or classes...
04/14/2011 21:07 Secured#3
Quote:
Originally Posted by KraHen View Post
Or serializing structs or classes...
Oh yea that's right. Just want to know which one would be most efficient to use.
04/15/2011 00:15 _tao4229_#4
Writing them on paper takes the least amount of computational resources.


obv
04/17/2011 21:23 kill_acvc#5
Quote:
Originally Posted by Secured View Post
What is more efficient to use?
Pointers or MemoryStream?
I use a safe writer in my source, which takes 2 args : the value and the offset. It's pretty simple.
04/17/2011 21:57 KraHen#6
Simple!=efficient

Though you can send structs without any extra stuff needed.
04/18/2011 00:32 Secured#7
Quote:
Originally Posted by kill_acvc View Post
I use a safe writer in my source, which takes 2 args : the value and the offset. It's pretty simple.
I'm not asking for what's most simple, but what's most efficient.
04/18/2011 01:35 kill_acvc#8
Quote:
Originally Posted by Secured View Post
I'm not asking for what's most simple, but what's most efficient.
Well if you're looking for eficiency then I suggest you to use pointers. But be careful, you may get "AcessViolationException" sometimes and your server will be more vulnerable to hackers attacks.
04/18/2011 02:47 pro4never#9
Pointers are a lovely thing to use but just make sure you double check all information properly. Don't just blindly accept a packet as being true...

For example if you receive a packet that has a string in it... don't just blindly assume that the string length byte is correct. You could easily run past the end of the actual packet being received and end up reading or even modifying important areas of your memory.

That being said... I personally like using unsafe structures + implicit operators or just using memory marshaling to send the end result.

Personally I'm using unsafe structures + implicit operators for almost all my packets in my new source (thanks to Dev for the suggestion :P) and it's working great. My source also supports just using memory marshaling but maybe it's just me... so far I've liked doing it separately.

Example...

Code:
public unsafe struct WalkPacket
    {
        public uint Direction;
        public uint Id;
        public uint Mode;
        public SystemTime Timestamp;
        public uint Unknown1;

        public static WalkPacket Create(uint id, uint direction, uint mode)
        {
            var packet = new WalkPacket();
            packet.Direction = direction;
            packet.Id = id;
            packet.Mode = mode;
            packet.Timestamp = SystemTime.Now;
            return packet;
        }

        public static implicit operator WalkPacket(byte* ptr)
        {
            var packet = new WalkPacket();
            packet.Direction = *((uint*) (ptr + 4));
            packet.Id = *((uint*) (ptr + 8));
            packet.Mode = *((uint*) (ptr + 12));
            packet.Timestamp = *((SystemTime*) (ptr + 16));
            packet.Unknown1 = *((uint*) (ptr + 20));
            return packet;
        }

        public static implicit operator byte[](WalkPacket packet)
        {
            var buffer = new byte[24 + 8];
            fixed (byte* ptr = buffer)
            {
                PacketBuilder.AppendHeader(ptr, buffer.Length, 10005);
                *((uint*) (ptr + 4)) = packet.Direction;
                *((uint*) (ptr + 8)) = packet.Id;
                *((uint*) (ptr + 12)) = packet.Mode;
                *((SystemTime*) (ptr + 16)) = packet.Timestamp;
                *((uint*) (ptr + 20)) = packet.Unknown1;
            }
            return buffer;
        }
    }
04/18/2011 04:58 _tao4229_#10
WalkPacket* walk = (WalkPacket*)ptr; ??
04/18/2011 05:07 Lateralus#11
Quote:
Originally Posted by _tao4229_ View Post
Writing them on paper takes the least amount of computational resources.


obv
HILARIOUS! A+++!