Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411507 Posts in 69374 Topics- by 58429 Members - Latest Member: Alternalo

April 26, 2024, 12:33:00 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)network packet code organization?
Pages: [1]
Print
Author Topic: network packet code organization?  (Read 571 times)
Orz
Level 1
*


"When nothing is done, nothing is left undone."


View Profile WWW
« on: September 02, 2014, 09:20:16 AM »

I am working on a networked game with an ever-growing number of different packets.   There are packet ids, which are bytes; packets themselves, which are objects;  and packet handlers, which are functions (actually delegates,  since this is c#).  Right now I have packet ids in an enum for convenience. Are there any other tricks I can use to organize the objects and functions?  Or perhaps a better way of organizing the networking code?
Logged
Ashaman73
Level 0
***



View Profile WWW
« Reply #1 on: September 03, 2014, 10:25:52 PM »

I've once read about the networkcode in doom/quake. The basic idea is to have a really large state object for each entity and to transfer the whole object. The real trick is, to only send the binary delta of the object,regardless of the object setup. E.g you have a object like

Code:
struct MyObject {
  char name[50];
  int health;
  float x;
  float y;
  float z;
...
}

Instead of creating special movement, health packets , you only send the whole MyObject as binary delta, something like this:

Code:
MyObject* obj = getPlayerEntity();
byte* lastSendData = getLastSendData();
byte* currentData = (byte*)obj;
byte* deltaData = getDeltaBuffer();
for(int i=0;i<sizeof(MyObject);i++) {
  deltaData[i] = lastSendData ^ currentData; // XOR
}

// simple RLE(run-lentgh-encoding) compression
byte* compressedData = encodeRLE(deltaData);

// send delta
sendPacket(compressedData);

The idea is, that the delta of a large object state will most likely contains a lot of 0 bits (not changing very often), which can be compressed by RLE quite fast and effective.
The decoding would look like this:

Code:
byte* compressedData = receivePacket();
byte* deltaData = decodeRLE(compressedData);

MyObject* obj = getPlayerEntity();
byte* currentData = (byte*)obj;
for(int i=0;i<sizeof(MyObject);i++) {
  currentData [i] = deltaData ^ currentData; // XOR
}

To construct your object like this, it would be best to have a fix length object, but other delta compressions or hybrid solutions would work too.


PS:
If you dont want to change all your objects, it doesnt hurt to create a intermediate object:
Code:
MyComplexeDynamicObject* obj = ...
PlainDataObjectRepresentation plainObj;
plainObj.transformFrom(obj);

.... encoding and sending ...


.... receiving and decoding...
PlainDataObjectRepresentation plainObj = ... received and decoded;
MyComplexeDynamicObject* obj = ...
plainObj.transformTo(obj);




Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic