PacketBuilder

11/30/2011 21:23 Arco.#1
Alrighty, well I made this packetbuilder, I feel as if it can be improved though. Anyone have any ideas, any suggestions etc?
12/01/2011 00:16 CptSky#2
Quote:
Originally Posted by Arco. View Post
Alrighty, well I made this packetbuilder, I feel as if it can be improved though. Anyone have any ideas, any suggestions etc?[...]
Seems really similar to the one I did for you. But, you need to free the memory and everything... Take a look at what I did. You'll be able to improve it. Oh, and as I said when I did the builder for you, you should use structures and pointers to them, it's faster.

If you no longer have mine.
12/01/2011 04:52 Arco.#3
Quote:
Originally Posted by CptSky View Post
Seems really similar to the one I did for you. But, you need to free the memory and everything... Take a look at what I did. You'll be able to improve it. Oh, and as I said when I did the builder for you, you should use structures and pointers to them, it's faster.

If you no longer have mine.
Yeah, the one you made, I took that and read it over, learned about pointers, and used it as a reference to make a builder. Simplified it a good bit, also explicitly declare offsets, return a ByteArray. Thanks to your builder, I learned a good bit about pointers <3
12/01/2011 06:27 Lateralus#4
Even better - pin the byte pointer so it's not swiped by the GC, and assign the internal pointer in the "packet" base class to the pointer to the byte array in a constructor *for packets to process* with the finalizer always freeing the unmanaged memory. Write a bunch of functions in the packet class to split up the fields by pointer arithmetic, derive all packets off of the packet class, and in these derived classes, use accessors to use the base class read and write functions. Tada, you're using just a tiny bit more memory and have a very slight performance degrade for 1028739x neater and more flexible packet handling than pointers to structures.

I've done all this except pinning the byte pointer and assigning it for processing packets (right now, we're copying the data over with memcpy... but hey, I'm never satisfied so what the hell), but there's probably something stupidly wrong with doing this... Now I'm going to go implement that. I'll let you know if what I said works or not. If someone knows it does/doesn't work, please tell me before I start wasting time. ;d
12/01/2011 15:05 miketheking#5
i just realised that my packetbuilder is crap
12/01/2011 20:46 Spirited#6
I don't use a packet builder. I make them using structs and send them to the client in one line of code. I never have to copy it... or process it... or waste processor time doing comparisons in the wrapper.
12/01/2011 20:52 Lateralus#7
Quote:
Originally Posted by Fаng View Post
I don't use a packet builder. I make them using structs and send them to the client in one line of code. I never have to copy it... or process it... or waste processor time doing comparisons in the wrapper.
Doing it that way is ugly, time consuming, and not very object oriented.
12/01/2011 21:03 Spirited#8
Quote:
Originally Posted by Lateralus View Post
Doing it that way is ugly, time consuming, and not very object oriented.
Lol, if you say so o.o
I personally think it's much more organized than anything else I've seen. In addition to that, it's much faster than traditional packet building. I've tested it. =p
12/01/2011 21:23 Arco.#9
Quote:
Originally Posted by Fаng View Post
Lol, if you say so o.o
I personally think it's much more organized than anything else I've seen. In addition to that, it's much faster than traditional packet building. I've tested it. =p
Cause you've used every single method of packetbuilding known to man right?
Forgot, Fang is pro coder, my bad.
12/01/2011 21:46 Spirited#10
Quote:
Originally Posted by Arco. View Post
Cause you've used every single method of packetbuilding known to man right?
Forgot, Fang is pro coder, my bad.
Yes Arco. I actually have used every possible *good* and base way to code a packet in C#. It's called experience.
12/01/2011 22:20 Lateralus#11
Quote:
Originally Posted by Fаng View Post
Lol, if you say so o.o
I personally think it's much more organized than anything else I've seen. In addition to that, it's much faster than traditional packet building. I've tested it. =p
How is your method "much faster" than mine? Using pointers to structures has exactly the same function as my packet class, except mine is much more flexible and object oriented. I'm pretty sure small accessors are inlined in my case, so what I have is unsafe code only in the packet class and beautiful code in the derived classes and everywhere that uses them that has the same performance as, if not faster than, your method, is much easier to work with, and follows proper OOP guidelines - not an opinion.
12/01/2011 22:40 Spirited#12
Quote:
Originally Posted by Lateralus View Post
How is your method "much faster" than mine? Using pointers to structures has exactly the same function as my packet class, except mine is much more flexible and object oriented. I'm pretty sure small accessors are inlined in my case, so what I have is unsafe code only in the packet class and beautiful code in the derived classes and everywhere that uses them that has the same performance as, if not faster than, your method, is much easier to work with, and follows proper OOP guidelines - not an opinion.
I was comparing it to traditional packet building. You don't have any stupid comparisons like Impulse's wrapper does. It should be fine. And as far as a difference between structs and pointer wrappers like yours... there is no difference (performance wise). I honestly like structs better. It's just a preference of mine.

Edit: Jack says there probably is a difference between structs and wrappers like yours - but it's so small that it wouldn't make a big difference.
12/01/2011 23:02 Lateralus#13
Quote:
Originally Posted by Fаng View Post
Edit: Jack says there probably is a difference between structs and wrappers like yours - but it's so small that it wouldn't make a big difference.
If the compiler inlines small functions and accessors like mine, then there isn't a difference. As of right now, because I copy packets with memcpy into the internal pointer, yeah, there's a difference.
12/03/2011 23:30 mateo136#14
I know this is stupid question but what exactly is a packetBuilder?
12/03/2011 23:43 KraHen#15
PHP Code:
public static byte[] Serialize(object Data)
        {
            
int size Marshal.SizeOf(Data);
            
byte[] bdata = new byte[size];
            
GCHandle handle GCHandle.Alloc(bdataGCHandleType.Pinned);
            
Marshal.StructureToPtr(Datahandle.AddrOfPinnedObject(), false);
            
handle.Free();
            return 
bdata;
        }