java - How to handle the different packets in both the server and client? -
in gameserver & client i'm planning make, i'm gonna have lot of packets in long run have handle. i'm wondering best-practices on how handle different packets.
my packets payload start packetid.
would idea create seperate class extending packet , handle logics there? example packet001login? give me lot of classes in long run.
is better make gigantic switch statement? doubt it.
is best way didn't think of?
any advice appreciated.
if have enougth calculation time on server side should work on technique create prototypes different package types.
to illustrate point , idea of give uml class descriptions
uml:
class packetprototype + packetprototype(packettype) + adddata(datatype, bitlength, ...) + deserilize(bitstream stream) : receivedpacket + serilizethis() : tosendpacket + getpackettype() : int
you need class have packetprototypes , decides on type of each packetprototype object prototype should used deserilize data.
you need 1 class knows every packetprototype, call packetprototypeholder
class packetprototypeholder + register(packetprototype) + getprototypeforpackettype(int type) : packetprototype
the protocol on setup time follows
packetenemy00 = new packetprototype(0) packetenemy00.adddata(types.int, 5) // health packetenemy00.adddata(types.string, 16) // name ...
this means packet of type 0 consists out of int 5 bits long , string have maximal length of 16 characters.
we have add packetprototype after setup our packetprototypeholder
packetholder.register(packetenemy00)
if server or client receives read type of packet, (you can read data bitstream)
type = bitstream.readint(5) prototype = packetholder.getprototypeforpackettype(type) receivedpacket ofreceivedpacket = prototype.deserilize(bitstream) // need here switch/if statement determine how handle reading of data switch(type) { case 0: // packetenemy00
the prototype.deserilize
call reads data datastream , puts receivedpacket object, there can access data either index operations or named access.
as example indices
int unithealth = ofreceivedpacket.getint(/* index, want 1st int */0); string unitname = ofreceivedpacket.getstring(/* index, want 1st string */0); , on... break; ... }
so effectivly moved switch statement inside network layer application/usage layer.
to remove switch need data driven approach. in engine more complicated realize hardcoded approach.
Comments
Post a Comment