|
You last visited: Today at 01:12
Advertisement
Pointers vs. Safecode
Discussion on Pointers vs. Safecode within the CO2 Private Server forum part of the Conquer Online 2 category.
03/10/2011, 20:58
|
#1
|
elite*gold: 0
Join Date: Feb 2011
Posts: 335
Received Thanks: 170
|
Pointers vs. Safecode
Really, what is the difference between doing:
Code:
*(ushort*)PTR = (ushort)Buffer.Length;
*((ushort*)(PTR + 2)) = 2030;
*((uint*)(PTR + 4)) = npc.StaticID;
*((ushort*)(PTR + 8)) = (ushort)npc.Cords.Position.X;
*((ushort*)(PTR + 10)) = (ushort)npc.Cords.Position.Y;
*((ushort*)(PTR + 12)) = (ushort)(npc.StaticID + (byte)npc.Direction);
*((ushort*)(PTR + 14)) = npc.Interaction;
And
Code:
PacketBuilder Packet = new PacketBuilder(new byte[20]);
Packet.WriteUInt16(Packet.Packet.Length);
Packet.WriteUInt16(2030);
Packet.WriteUInt32((uint)Type);
Packet.WriteUInt16(X);
Packet.WriteUInt16(Y);
Packet.WriteUInt16(SubType);
Packet.WriteUInt16(Dir);
Packet.WriteUInt32((uint)Flag);
Whats the pros and cons of each one?
|
|
|
03/10/2011, 21:03
|
#2
|
elite*gold: 0
Join Date: Nov 2010
Posts: 1,162
Received Thanks: 370
|
The first one is writing the buffer direct and you can send it direct.
The second is writing to a packetbuilder, which you're sending to the client after.
You can use the 2nd one without writing it in an fixed block, because the fixed block is in the builder, where the first one have to be in a fixed block.
Don't know if I explained it right, but that's how I see it.
|
|
|
03/10/2011, 21:14
|
#3
|
elite*gold: 0
Join Date: Feb 2011
Posts: 335
Received Thanks: 170
|
I know what they do, I want to know the pros v. cons of each.
|
|
|
03/10/2011, 21:16
|
#4
|
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,380
|
Pointers are faster/more efficient.
Even more efficient is writing it using a structure (that way it's just stored in memory) and you can just build a packet based on the memory addresses.
Combine that with a static send buffer and you have yourself no need to ever assign new byte arrays for these new packets and completely skip all that overhead/memory usage.
|
|
|
03/11/2011, 01:06
|
#5
|
elite*gold: 0
Join Date: Jun 2009
Posts: 787
Received Thanks: 314
|
doing something like *(ptr + x) = value directly translates to one assembly instruction (or at least less than using mathematical operators to imitate it).
also doing something like
struct packet {
short type;
short size;
}
p.type = 5;
p.size = 4;
is literally the same thing as *(ptr + offset) = ... at the most basic level. However it should be noted that in C# structs are stored on the stack (well struct s = new stuct()...), and allocating a byte[] is done on the heap.
Fixing a byte[] down into a byte* however isn't the fastest or best operation.
|
|
|
03/11/2011, 03:36
|
#6
|
elite*gold: 0
Join Date: Feb 2011
Posts: 335
Received Thanks: 170
|
Quote:
Originally Posted by _tao4229_
doing something like *(ptr + x) = value directly translates to one assembly instruction (or at least less than using mathematical operators to imitate it).
also doing something like
struct packet {
short type;
short size;
}
p.type = 5;
p.size = 4;
is literally the same thing as *(ptr + offset) = ... at the most basic level. However it should be noted that in C# structs are stored on the stack (well struct s = new stuct()...), and allocating a byte[] is done on the heap.
Fixing a byte[] down into a byte* however isn't the fastest or best operation.
|
Then what do you think would be the best operation?
|
|
|
03/11/2011, 03:49
|
#7
|
elite*gold: 0
Join Date: Jun 2009
Posts: 787
Received Thanks: 314
|
As with everything, it's situational.
|
|
|
03/12/2011, 15:45
|
#8
|
elite*gold: 0
Join Date: Sep 2008
Posts: 1,683
Received Thanks: 506
|
If you handle them propertly, pointers are much faster. On the other hand, if you don't handle them correctly, they can cause all kinds of problems.
I would avoid them, the CLR is pretty **** fast, you may find that you'll never have to resort to unsafe code.
Whether to use pointers or not is the least important thing to worry a bout.
I also think it depends on how much you're going to use pointers, only for packet structures? You really want to go into that much complexity for only packet structures?
Oh and let me add when I would use pointers.
- In image processing.
- When using pinvoke calls.
- If data processing is the bottleneck of your application.
Conclusion:
Don't use pointers in .NET, at this point, there is no need for it in a private server. You'll server is going to be a I/O bound application, not CPU bound.
|
|
|
03/14/2011, 02:24
|
#9
|
elite*gold: 20
Join Date: Jan 2006
Posts: 890
Received Thanks: 241
|
Quote:
Originally Posted by Basser
If you handle them propertly, pointers are much faster. On the other hand, if you don't handle them correctly, they can cause all kinds of problems.
I would avoid them, the CLR is pretty **** fast, you may find that you'll never have to resort to unsafe code.
Whether to use pointers or not is the least important thing to worry a bout.
I also think it depends on how much you're going to use pointers, only for packet structures? You really want to go into that much complexity for only packet structures?
Oh and let me add when I would use pointers.
- In image processing.
- When using pinvoke calls.
- If data processing is the bottleneck of your application.
Conclusion:
Don't use pointers in .NET, at this point, there is no need for it in a private server. You'll server is going to be a I/O bound application, not CPU bound.
|
It depends on what your using the pointers for. If it's a single pointer it's not going to make a significant difference. It'd advice staying away from pointers though considering you made this thread.
|
|
|
 |
Similar Threads
|
About Pointers
01/01/2010 - Cabal Online - 1 Replies
Can someone explain briefly how to make ur do pointers?
thanks and happy new year!
|
pointers
06/22/2009 - Grand Chase Philippines - 4 Replies
im just wondering how to use pointers ?
anyone can help me?
|
Help about pointers
05/17/2009 - Grand Chase Philippines - 5 Replies
can anyone teach me how to do this
para d na poh aq mauli ulit mag scan ng address evertime I open PChack
|
Pointers with CE, help
01/15/2009 - General Coding - 0 Replies
Hello!
Guys im having problems when I try to search for a pointer of a dynamic address using CE 5.4.
Always when I press "Find out what writes to this address" or similar options the game just will crash. I'm not able to go further with this problem.
If anyone knows how to fix this problem, or knows another way to do it give me a tip and i'll try to find it and learn it.
Thanks,
|
Pointers with CE
01/15/2009 - Dekaron - 0 Replies
Hello!
Guys im having problems when I try to search for a pointer of a dynamic address using CE 5.4.
Always when I press "Find out what writes to this address" or similar options the game just will crash. I'm not able to go further with this problem.
If anyone knows how to fix this problem, or knows another way to do it give me a tip and i'll try to find it and learn it.
Thanks,
|
All times are GMT +1. The time now is 01:12.
|
|