[Discussion] Packet writing methods

08/01/2010 19:10 InfamousGeek#1
I noticed alot of sources that have been released to public use the accessor method, which was been presented by Hybrid back then. I'm curious which method you personally use, and for what reason. My personal choice is structured packets. Example;
Code:
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        struct LoginResponse
        {
            public PacketHeader Header;
            public UInt32 Key;
            public UInt32 ID;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
            public String IP;
            public UInt16 Port;
            public UInt16 Blank;
        }

        public byte[] ToArray()
        {
            Filler = new PacketFiller(Marshal.SizeOf(Response));
            Filler.FillPacket<LoginResponse>(Response);
            return Filler.ToArray();
        }
Looking forward to your feedback.
08/01/2010 19:13 _tao4229_#2
or Send(&Response, Response.Header.Size/Marshal.SizeOf(Response));
08/01/2010 19:18 InfamousGeek#3
Quote:
Originally Posted by _tao4229_ View Post
or Send(&Response, Response.Header.Size/Marshal.SizeOf(Response));
Code:
        public PacketFiller(Int32 Size) //When I call the constructor I use Marshal.SizeOf
        {
            Packet = new byte[Size];
        }

        public void FillPacket<TStruct>(TStruct Struct)
        {
            IntPtr Ptr = Marshal.AllocHGlobal(Packet.Length);
            Marshal.StructureToPtr(Struct, Ptr, false);

            fixed (byte* pckt = this.Packet)
            {
                Native.memcpy(pckt, Ptr.ToPointer(), Packet.Length);
            }
        }
If you don't mind if I ask, which method do you use?
08/01/2010 20:21 _tao4229_#4
Quote:
Originally Posted by InfamousGeek View Post
Code:
        public PacketFiller(Int32 Size) //When I call the constructor I use Marshal.SizeOf
        {
            Packet = new byte[Size];
        }

        public void FillPacket<TStruct>(TStruct Struct)
        {
            IntPtr Ptr = Marshal.AllocHGlobal(Packet.Length);
            Marshal.StructureToPtr(Struct, Ptr, false);

            fixed (byte* pckt = this.Packet)
            {
                Native.memcpy(pckt, Ptr.ToPointer(), Packet.Length);
            }
        }
If you don't mind if I ask, which method do you use?
I import WSASend/send (from ws2_32) and just pass a pointer to the structure.
08/01/2010 21:45 © Haydz#5
Quote:
Originally Posted by _tao4229_ View Post
I import WSASend/send (from ws2_32) and just pass a pointer to the structure.
Same as above.
08/01/2010 22:11 InfamousGeek#6
Quote:
Originally Posted by _tao4229_ View Post
I import WSASend/send (from ws2_32) and just pass a pointer to the structure.
I considered writing a winsocket, though my personal opinion is that there is no need to reinvent the wheel.
08/01/2010 23:56 Korvacs#7
Quote:
Originally Posted by InfamousGeek View Post
I considered writing a winsocket, though my personal opinion is that there is no need to reinvent the wheel.
If you can improve on the original wheel then why not?
08/02/2010 00:03 _tao4229_#8
Quote:
Originally Posted by InfamousGeek View Post
I considered writing a winsocket, though my personal opinion is that there is no need to reinvent the wheel.
If you don't feel like reinventing the wheel, you can just import the function and pass a System.Net.Socket.Handle as the SOCKET s param.
08/02/2010 14:52 InfamousGeek#9
Quote:
Originally Posted by Korvacs View Post
If you can improve on the original wheel then why not?
Then it's not reinventing the wheel.. anyhow, we could discuss this forever but there is no point since every programmer has his own methods.

Quote:
Originally Posted by _tao4229_ View Post
If you don't feel like reinventing the wheel, you can just import the function and pass a System.Net.Socket.Handle as the SOCKET s param.
I might give it a shot..
08/03/2010 16:34 Nullable#10
Same method, but i usually don't program in managed. It is a lot easier in native.