Le seed est pas bon, mais ON A LES DROITS!
This commit is contained in:
		| @@ -7,7 +7,7 @@ | ||||
| #define SRV_PORT 10000 | ||||
| #define CLI_PORT 10001 | ||||
|  | ||||
| #define BUFFER_LENGTH 150 | ||||
| #define BUFFER_LENGTH 1500 | ||||
|  | ||||
| #define CHUNK_SIZE_X  4 | ||||
| #define CHUNK_SIZE_Y  64 | ||||
|   | ||||
| @@ -4,38 +4,38 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) { | ||||
| 	*buf[0] = (char)netprot::PACKET_TYPE::INPUT; | ||||
|  | ||||
| 	uint64_t time = in->timestamp; | ||||
| 	uint8_t time8[sizeof(uint64_t)] = {(uint8_t)((time >> 56) & 0xFF), | ||||
| 	uint8_t time8[sizeof(uint64_t)] = { (uint8_t)((time >> 56) & 0xFF), | ||||
| 									   (uint8_t)((time >> 48) & 0xFF), | ||||
| 									   (uint8_t)((time >> 40) & 0xFF), | ||||
| 									   (uint8_t)((time >> 32) & 0xFF), | ||||
| 									   (uint8_t)((time >> 24) & 0xFF), | ||||
| 									   (uint8_t)((time >> 16) & 0xFF), | ||||
| 									   (uint8_t)((time >> 8 ) & 0xFF), | ||||
| 									    (uint8_t)(time		  & 0xFF)}; | ||||
| 									   (uint8_t)((time >> 8) & 0xFF), | ||||
| 										(uint8_t)(time & 0xFF) }; | ||||
|  | ||||
| 	memcpy(*buf + 1, time8, sizeof(uint64_t)); | ||||
|  | ||||
| 	uint64_t sid = in->sid; | ||||
| 	uint8_t sid8[sizeof(uint64_t)] = {(uint8_t)((sid >> 56) & 0xFF), | ||||
| 	uint8_t sid8[sizeof(uint64_t)] = { (uint8_t)((sid >> 56) & 0xFF), | ||||
| 									  (uint8_t)((sid >> 48) & 0xFF), | ||||
| 									  (uint8_t)((sid >> 40) & 0xFF), | ||||
| 									  (uint8_t)((sid >> 32) & 0xFF), | ||||
| 									  (uint8_t)((sid >> 24) & 0xFF), | ||||
| 									  (uint8_t)((sid >> 16) & 0xFF), | ||||
| 									  (uint8_t)((sid >> 8 ) & 0xFF), | ||||
| 									  (uint8_t)( sid		& 0xFF)}; | ||||
| 									  (uint8_t)((sid >> 8) & 0xFF), | ||||
| 									  (uint8_t)(sid & 0xFF) }; | ||||
|  | ||||
| 	memcpy(*buf + sizeof(uint64_t) + 1, sid8, sizeof(uint64_t)); | ||||
|  | ||||
| 	Keys keys = in->keys; | ||||
| 	uint8_t keys8 = // Reste un bit. | ||||
| 		keys.forward  & 0b10000000 | | ||||
| 		keys.forward & 0b10000000 | | ||||
| 		keys.backward & 0b01000000 | | ||||
| 		keys.left     & 0b00100000 | | ||||
| 		keys.right    & 0b00010000 | | ||||
| 		keys.jump     & 0b00001000 | | ||||
| 		keys.shoot    & 0b00000100 | | ||||
| 		keys.block    & 0b00000010 ; | ||||
| 		keys.left & 0b00100000 | | ||||
| 		keys.right & 0b00010000 | | ||||
| 		keys.jump & 0b00001000 | | ||||
| 		keys.shoot & 0b00000100 | | ||||
| 		keys.block & 0b00000010; | ||||
|  | ||||
| 	memcpy(*buf + sizeof(uint64_t) * 2 + 1, &keys8, sizeof(uint8_t)); | ||||
|  | ||||
| @@ -54,7 +54,7 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) { | ||||
| 					 (uint8_t)((vec[2] >> 24) & 0xFF), | ||||
| 					 (uint8_t)((vec[2] >> 16) & 0xFF), | ||||
| 					(uint8_t)((vec[2] >> 8) & 0xFF), | ||||
| 					 (uint8_t)(vec[2] & 0xFF)}; | ||||
| 					 (uint8_t)(vec[2] & 0xFF) }; | ||||
|  | ||||
| 	memcpy(*buf + sizeof(uint64_t) * 2 + 2, vec8, sizeof(uint32_t) * 3); | ||||
|  | ||||
| @@ -104,7 +104,7 @@ void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) { | ||||
| 										(uint8_t)(time & 0xFF) }; | ||||
|  | ||||
| 	memcpy(*buf + 1, stamp8, sizeof(uint64_t)); | ||||
| 	 | ||||
|  | ||||
| 	uint64_t sid = sync->sid; | ||||
| 	uint8_t sid8[sizeof(uint64_t)] = { (uint8_t)((sid >> 56) & 0xFF), | ||||
| 									  (uint8_t)((sid >> 48) & 0xFF), | ||||
| @@ -118,11 +118,11 @@ void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) { | ||||
| 	memcpy(*buf + sizeof(uint64_t) + 1, sid8, sizeof(uint64_t)); | ||||
|  | ||||
| 	uint32_t timer = sync->timer; | ||||
| 	uint8_t time8[sizeof(uint32_t)] = {(uint8_t)((timer >> 24) & 0xFF), | ||||
| 	uint8_t time8[sizeof(uint32_t)] = { (uint8_t)((timer >> 24) & 0xFF), | ||||
| 									   (uint8_t)((timer >> 16) & 0xFF), | ||||
| 									   (uint8_t)((timer >> 8) & 0xFF), | ||||
| 										(uint8_t)(timer & 0xFF) }; | ||||
| 	 | ||||
|  | ||||
| 	memcpy(*buf + sizeof(uint64_t) * 2 + 1, time8, sizeof(uint32_t)); | ||||
|  | ||||
| 	uint16_t ammo = sync->ammo; | ||||
| @@ -169,8 +169,8 @@ void netprot::Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen) { | ||||
| 									   (uint8_t)((tid >> 32) & 0xFF), | ||||
| 									   (uint8_t)((tid >> 24) & 0xFF), | ||||
| 									   (uint8_t)((tid >> 16) & 0xFF), | ||||
| 									   (uint8_t)((tid >> 8)  & 0xFF), | ||||
| 									    (uint8_t)(tid 		 & 0xFF) | ||||
| 									   (uint8_t)((tid >> 8) & 0xFF), | ||||
| 										(uint8_t)(tid & 0xFF) | ||||
| 	}; | ||||
|  | ||||
| 	memcpy(*buf + namesize + 2, tid8, sizeof(uint64_t)); | ||||
| @@ -182,18 +182,18 @@ void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) { | ||||
| 	*buf[0] = (char)netprot::PACKET_TYPE::LOGINF; | ||||
|  | ||||
| 	size_t namesize = std::strlen(linfo->name) + 1; | ||||
| 	 | ||||
|  | ||||
| 	memcpy(*buf + 1, &linfo->name, namesize); | ||||
| 	uint64_t sid = linfo->sid; | ||||
| 	uint8_t sid8[sizeof(uint64_t)] = {  | ||||
| 	uint8_t sid8[sizeof(uint64_t)] = { | ||||
| 									   (uint8_t)((sid >> 56) & 0xFF), | ||||
| 									   (uint8_t)((sid >> 48) & 0xFF), | ||||
| 									   (uint8_t)((sid >> 40) & 0xFF), | ||||
| 									   (uint8_t)((sid >> 32) & 0xFF), | ||||
| 									   (uint8_t)((sid >> 24) & 0xFF), | ||||
| 									   (uint8_t)((sid >> 16) & 0xFF), | ||||
| 									   (uint8_t)((sid >> 8)  & 0xFF), | ||||
| 									    (uint8_t)(sid 		 & 0xFF) | ||||
| 									   (uint8_t)((sid >> 8) & 0xFF), | ||||
| 										(uint8_t)(sid & 0xFF) | ||||
| 	}; | ||||
|  | ||||
| 	memcpy(*buf + namesize + 2, sid8, sizeof(uint64_t)); | ||||
| @@ -206,8 +206,8 @@ void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) { | ||||
| 									   (uint8_t)((tid >> 32) & 0xFF), | ||||
| 									   (uint8_t)((tid >> 24) & 0xFF), | ||||
| 									   (uint8_t)((tid >> 16) & 0xFF), | ||||
| 									   (uint8_t)((tid >> 8)  & 0xFF), | ||||
| 									   (uint8_t)(tid 		 & 0xFF) | ||||
| 									   (uint8_t)((tid >> 8) & 0xFF), | ||||
| 									   (uint8_t)(tid & 0xFF) | ||||
| 	}; | ||||
|  | ||||
| 	memcpy(*buf + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t)); | ||||
| @@ -229,8 +229,8 @@ void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) { | ||||
| 									   (uint8_t)((id >> 32) & 0xFF), | ||||
| 									   (uint8_t)((id >> 24) & 0xFF), | ||||
| 									   (uint8_t)((id >> 16) & 0xFF), | ||||
| 									   (uint8_t)((id >> 8)  & 0xFF), | ||||
| 									    (uint8_t)(id 		& 0xFF) | ||||
| 									   (uint8_t)((id >> 8) & 0xFF), | ||||
| 										(uint8_t)(id & 0xFF) | ||||
| 	}; | ||||
|  | ||||
| 	memcpy(*buf + namesize + 2, id8, sizeof(uint64_t)); | ||||
| @@ -243,8 +243,8 @@ void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) { | ||||
| 									   (uint8_t)((tid >> 32) & 0xFF), | ||||
| 									   (uint8_t)((tid >> 24) & 0xFF), | ||||
| 									   (uint8_t)((tid >> 16) & 0xFF), | ||||
| 									   (uint8_t)((tid >> 8)  & 0xFF), | ||||
| 									    (uint8_t)(tid 		 & 0xFF)  | ||||
| 									   (uint8_t)((tid >> 8) & 0xFF), | ||||
| 										(uint8_t)(tid & 0xFF) | ||||
| 	}; | ||||
|  | ||||
| 	memcpy(*buf + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t)); | ||||
| @@ -263,8 +263,8 @@ void netprot::Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen) { | ||||
| 									   (uint8_t)((game >> 32) & 0xFF), | ||||
| 									   (uint8_t)((game >> 24) & 0xFF), | ||||
| 									   (uint8_t)((game >> 16) & 0xFF), | ||||
| 									   (uint8_t)((game >> 8)  & 0xFF), | ||||
| 									    (uint8_t)(game 		  & 0xFF) | ||||
| 									   (uint8_t)((game >> 8) & 0xFF), | ||||
| 										(uint8_t)(game & 0xFF) | ||||
| 	}; | ||||
|  | ||||
| 	memcpy(*buf + 1, seed8, sizeof(uint64_t)); | ||||
| @@ -272,27 +272,27 @@ void netprot::Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen) { | ||||
| 	game = ginfo->countdown; | ||||
| 	uint8_t count8[sizeof(uint64_t)] = { | ||||
| 										(uint8_t)((game >> 56) & 0xFF), | ||||
| 									    (uint8_t)((game >> 48) & 0xFF), | ||||
| 									    (uint8_t)((game >> 40) & 0xFF), | ||||
| 									    (uint8_t)((game >> 32) & 0xFF), | ||||
| 									    (uint8_t)((game >> 24) & 0xFF), | ||||
| 									    (uint8_t)((game >> 16) & 0xFF), | ||||
| 										(uint8_t)((game >> 8)  & 0xFF), | ||||
| 									     (uint8_t)(game 	   & 0xFF) | ||||
| 										(uint8_t)((game >> 48) & 0xFF), | ||||
| 										(uint8_t)((game >> 40) & 0xFF), | ||||
| 										(uint8_t)((game >> 32) & 0xFF), | ||||
| 										(uint8_t)((game >> 24) & 0xFF), | ||||
| 										(uint8_t)((game >> 16) & 0xFF), | ||||
| 										(uint8_t)((game >> 8) & 0xFF), | ||||
| 										 (uint8_t)(game & 0xFF) | ||||
| 	}; | ||||
|  | ||||
| 	memcpy(*buf + sizeof(uint64_t) + 1, count8, sizeof(uint64_t)); | ||||
|  | ||||
| 	game = ginfo->countdown; | ||||
| 	uint8_t gtype8[sizeof(uint64_t)] = { | ||||
| 									     (uint8_t)((game >> 56) & 0xFF), | ||||
| 										 (uint8_t)((game >> 56) & 0xFF), | ||||
| 									   (uint8_t)((game >> 48) & 0xFF), | ||||
| 									   (uint8_t)((game >> 40) & 0xFF), | ||||
| 									   (uint8_t)((game >> 32) & 0xFF), | ||||
| 									   (uint8_t)((game >> 24) & 0xFF), | ||||
| 									   (uint8_t)((game >> 16) & 0xFF), | ||||
| 									   (uint8_t)((game >> 8)  & 0xFF), | ||||
| 									    (uint8_t)(game 		  & 0xFF) | ||||
| 									   (uint8_t)((game >> 8) & 0xFF), | ||||
| 										(uint8_t)(game & 0xFF) | ||||
| 	}; | ||||
|  | ||||
| 	memcpy(*buf + sizeof(uint64_t) + 1, gtype8, sizeof(uint64_t)); | ||||
| @@ -311,8 +311,8 @@ void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) { | ||||
| 									   (uint8_t)((src >> 32) & 0xFF), | ||||
| 									   (uint8_t)((src >> 24) & 0xFF), | ||||
| 									   (uint8_t)((src >> 16) & 0xFF), | ||||
| 									   (uint8_t)((src >> 8)  & 0xFF), | ||||
| 									    (uint8_t)(src 		 & 0xFF) | ||||
| 									   (uint8_t)((src >> 8) & 0xFF), | ||||
| 										(uint8_t)(src & 0xFF) | ||||
| 	}; | ||||
|  | ||||
| 	memcpy(*buf + 1, src8, sizeof(uint64_t)); | ||||
| @@ -325,8 +325,8 @@ void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) { | ||||
| 									   (uint8_t)((dst >> 32) & 0xFF), | ||||
| 									   (uint8_t)((dst >> 24) & 0xFF), | ||||
| 									   (uint8_t)((dst >> 16) & 0xFF), | ||||
| 									   (uint8_t)((dst >> 8)  & 0xFF), | ||||
| 									    (uint8_t)(dst 		 & 0xFF) | ||||
| 									   (uint8_t)((dst >> 8) & 0xFF), | ||||
| 										(uint8_t)(dst & 0xFF) | ||||
| 	}; | ||||
|  | ||||
| 	memcpy(*buf + 1 + sizeof(uint64_t), dst8, sizeof(uint64_t)); | ||||
| @@ -339,10 +339,10 @@ void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) { | ||||
| 									   (uint8_t)((dstteam >> 32) & 0xFF), | ||||
| 									   (uint8_t)((dstteam >> 24) & 0xFF), | ||||
| 									   (uint8_t)((dstteam >> 16) & 0xFF), | ||||
| 									   (uint8_t)((dstteam >> 8)  & 0xFF), | ||||
| 									    (uint8_t)(dstteam 		 & 0xFF) | ||||
| 									   (uint8_t)((dstteam >> 8) & 0xFF), | ||||
| 										(uint8_t)(dstteam & 0xFF) | ||||
| 	}; | ||||
| 	 | ||||
|  | ||||
| 	memcpy(*buf + 1 + sizeof(uint64_t) * 2, dstt8, sizeof(uint64_t)); | ||||
|  | ||||
| 	size_t messize = std::strlen(chat->mess) + 1; | ||||
| @@ -366,8 +366,8 @@ void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) { | ||||
|  | ||||
|  | ||||
|  | ||||
| bool netprot::Deserialize(Input* in, char* buf, const uint32_t buflen) { | ||||
| 	if (buflen <= sizeof(Input)) | ||||
| bool netprot::Deserialize(Input* in, char* buf, uint32_t *buflen) { | ||||
| 	if (*buflen <= sizeof(Input)) | ||||
| 		return false; | ||||
|  | ||||
| 	uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 }; | ||||
| @@ -395,13 +395,13 @@ bool netprot::Deserialize(Input* in, char* buf, const uint32_t buflen) { | ||||
|  | ||||
| 	uint8_t keys = 0; | ||||
| 	memcpy(&keys, &buf[1 + sizeof(uint64_t) * 2], sizeof(uint8_t)); | ||||
| 	in->keys.forward	= keys & 0b10000000; | ||||
| 	in->keys.backward	= keys & 0b01000000; | ||||
| 	in->keys.left		= keys & 0b00100000; | ||||
| 	in->keys.right		= keys & 0b00010000; | ||||
| 	in->keys.jump		= keys & 0b00001000; | ||||
| 	in->keys.shoot		= keys & 0b00000100; | ||||
| 	in->keys.block		= keys & 0b00000010; | ||||
| 	in->keys.forward = keys & 0b10000000; | ||||
| 	in->keys.backward = keys & 0b01000000; | ||||
| 	in->keys.left = keys & 0b00100000; | ||||
| 	in->keys.right = keys & 0b00010000; | ||||
| 	in->keys.jump = keys & 0b00001000; | ||||
| 	in->keys.shoot = keys & 0b00000100; | ||||
| 	in->keys.block = keys & 0b00000010; | ||||
|  | ||||
| 	uint8_t subvec[3 * sizeof(uint32_t)] = { 0,0,0,0,0,0,0,0,0,0,0,0 }; | ||||
| 	memcpy(subvec, &buf[2 + sizeof(uint64_t) * 2], sizeof(uint8_t) * 12); | ||||
| @@ -417,19 +417,21 @@ bool netprot::Deserialize(Input* in, char* buf, const uint32_t buflen) { | ||||
| 				(uint32_t)subvec[8] << 24 | | ||||
| 				(uint32_t)subvec[9] << 16 | | ||||
| 				(uint32_t)subvec[10] << 8 | | ||||
| 				(uint32_t)subvec[11]}; | ||||
| 				(uint32_t)subvec[11] }; | ||||
|  | ||||
| 	memcpy(&in->direction, vec, sizeof(uint32_t) * 3); | ||||
|  | ||||
| 	*buflen = sizeof(uint64_t) * 2 + 2 + sizeof(uint32_t) * 3; | ||||
|  | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| bool netprot::Deserialize(Output* out, char* buf, const uint32_t buflen) { | ||||
| bool netprot::Deserialize(Output* out, char* buf, uint32_t *buflen) { | ||||
| 	return false; | ||||
| } | ||||
|  | ||||
| bool netprot::Deserialize(Sync* sync, char* buf, const uint32_t buflen) { | ||||
| 	if (buflen <= sizeof(Sync)) | ||||
| bool netprot::Deserialize(Sync* sync, char* buf, uint32_t *buflen) { | ||||
| 	if (*buflen <= sizeof(Sync)) | ||||
| 		return false; | ||||
|  | ||||
| 	uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 }; | ||||
| @@ -487,11 +489,13 @@ bool netprot::Deserialize(Sync* sync, char* buf, const uint32_t buflen) { | ||||
|  | ||||
| 	memcpy(&sync->position, vec, sizeof(uint32_t) * 3); | ||||
|  | ||||
| 	*buflen = sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t) + 2; | ||||
|  | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| bool netprot::Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen) { | ||||
| 	if (buflen <= sizeof(LoginInfo)) | ||||
| bool netprot::Deserialize(TeamInfo* tinfo, char* buf, uint32_t *buflen) { | ||||
| 	if (*buflen <= sizeof(LoginInfo)) | ||||
| 		return false; | ||||
|  | ||||
| 	size_t namesize = std::strlen(buf) + 1; | ||||
| @@ -513,11 +517,13 @@ bool netprot::Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen) { | ||||
| 		(uint64_t)diff[6] << 8 | | ||||
| 		(uint64_t)diff[7]; | ||||
|  | ||||
| 	*buflen = namesize + sizeof(uint64_t) + 2; | ||||
|  | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| bool netprot::Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen) { | ||||
| 	if (buflen <= sizeof(LoginInfo)) | ||||
| bool netprot::Deserialize(LoginInfo* linfo, char* buf, uint32_t *buflen) { | ||||
| 	if (*buflen <= sizeof(LoginInfo)) | ||||
| 		return false; | ||||
|  | ||||
| 	size_t namesize = std::strlen(buf) + 1; | ||||
| @@ -550,11 +556,13 @@ bool netprot::Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen) { | ||||
| 		(uint64_t)diff[6] << 8 | | ||||
| 		(uint64_t)diff[7]; | ||||
|  | ||||
| 	*buflen = namesize + sizeof(uint64_t) * 2 + 2; | ||||
|  | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen) { | ||||
| 	if (buflen <= sizeof(PlayerInfo)) | ||||
| bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, uint32_t *buflen) { | ||||
| 	if (*buflen <= sizeof(PlayerInfo)) | ||||
| 		return false; | ||||
|  | ||||
| 	size_t namesize = std::strlen(buf) + 1; | ||||
| @@ -587,11 +595,13 @@ bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen) { | ||||
| 		(uint64_t)diff[6] << 8 | | ||||
| 		(uint64_t)diff[7]; | ||||
|  | ||||
| 	*buflen = namesize + sizeof(uint64_t) * 2 + 2; | ||||
|  | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| bool netprot::Deserialize(GameInfo* ginfo, char* buf, const uint32_t buflen) { | ||||
| 	if (buflen <= sizeof(GameInfo)) | ||||
| bool netprot::Deserialize(GameInfo* ginfo, char* buf, uint32_t *buflen) { | ||||
| 	if (*buflen <= sizeof(GameInfo)) | ||||
| 		return false; | ||||
|  | ||||
| 	uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 }; | ||||
| @@ -628,11 +638,13 @@ bool netprot::Deserialize(GameInfo* ginfo, char* buf, const uint32_t buflen) { | ||||
| 		(uint64_t)diff[6] << 8 | | ||||
| 		(uint64_t)diff[7]; | ||||
|  | ||||
| 	*buflen = sizeof(uint64_t) * 3 + 1; | ||||
|  | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| bool netprot::Deserialize(Chat* chat, char* buf, const uint32_t buflen) { | ||||
| 	if (buflen <= sizeof(Chat)) | ||||
| bool netprot::Deserialize(Chat* chat, char* buf, uint32_t *buflen) { | ||||
| 	if (*buflen <= sizeof(Chat)) | ||||
| 		return false; | ||||
|  | ||||
| 	uint8_t src[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 }; | ||||
| @@ -677,11 +689,14 @@ bool netprot::Deserialize(Chat* chat, char* buf, const uint32_t buflen) { | ||||
| 		return false; | ||||
|  | ||||
| 	memcpy(&chat->mess, &buf[1 + sizeof(uint64_t) * 3], messsize); | ||||
|  | ||||
| 	*buflen = messsize + sizeof(uint64_t) * 3 + 2; | ||||
| 	 | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| bool netprot::Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen) { | ||||
| 	if (buflen <= sizeof(ErrorLog)) | ||||
| bool netprot::Deserialize(ErrorLog* errlog, char* buf, uint32_t *buflen) { | ||||
| 	if (*buflen <= sizeof(ErrorLog)) | ||||
| 		return false; | ||||
|  | ||||
| 	size_t messsize = std::strlen(buf) + 1; | ||||
| @@ -692,18 +707,20 @@ bool netprot::Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen) { | ||||
| 	memcpy(&errlog->mess, &buf[1], messsize); | ||||
| 	memcpy(&errlog->is_fatal, &buf[1 + messsize], sizeof(bool)); | ||||
|  | ||||
| 	*buflen = messsize + sizeof(uint64_t) * 3 + 2; | ||||
|  | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| netprot::PACKET_TYPE netprot::getType(char* buf, const uint32_t buflen) { | ||||
| 	if (buflen < 1 ||  | ||||
| 		buf[0] >= (char)netprot::PACKET_TYPE::LAST_PACK ||  | ||||
| 	if (buflen < 1 || | ||||
| 		buf[0] >= (char)netprot::PACKET_TYPE::LAST_PACK || | ||||
| 		buf[0] <= (char)netprot::PACKET_TYPE::ERR) | ||||
| 		return netprot::PACKET_TYPE::ERR; | ||||
| 	return (netprot::PACKET_TYPE)buf[0]; | ||||
| } | ||||
|  | ||||
| netprot::Packet netprot::getPack(char* buf, uint32_t buflen) { | ||||
| netprot::Packet netprot::getPack(char* buf, uint32_t *buflen) { | ||||
| 	Packet pck = { nullptr, PACKET_TYPE::ERR }; | ||||
| 	Input* in = nullptr; | ||||
| 	Output* out = nullptr; | ||||
| @@ -713,7 +730,7 @@ netprot::Packet netprot::getPack(char* buf, uint32_t buflen) { | ||||
| 	ErrorLog* errlog = nullptr; | ||||
| 	LoginInfo* loginf = nullptr; | ||||
|  | ||||
| 	switch (getType(buf, buflen)) { | ||||
| 	switch (getType(buf, *buflen)) { | ||||
| 	case PACKET_TYPE::INPUT: | ||||
| 		in = new Input(); | ||||
| 		if (netprot::Deserialize(in, buf, buflen)) { | ||||
| @@ -769,8 +786,8 @@ netprot::Packet netprot::getPack(char* buf, uint32_t buflen) { | ||||
| 	return pck; | ||||
| } | ||||
|  | ||||
| netprot::Packet netprot::getPack(netprot::Buffer* buf) {  | ||||
| 	return netprot::getPack(buf->ptr, buf->len);  | ||||
| netprot::Packet netprot::getPack(netprot::Buffer* buf) { | ||||
| 	return netprot::getPack(buf->ptr, &buf->len); | ||||
| } | ||||
|  | ||||
| bool netprot::emptyPack(netprot::Packet pck) { | ||||
| @@ -808,138 +825,148 @@ netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) { | ||||
| 	return pck; | ||||
| } | ||||
|  | ||||
| std::vector<netprot::Packet> netprot::recvPacks(SOCKET sock, Buffer* buf) { | ||||
| 	std::vector<Packet> lsPck; | ||||
| std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf) { | ||||
| 	std::vector<char*> lsPck; | ||||
| 	int len = 0, end = 0; | ||||
| 	bool pck_received = false; | ||||
| 	char * cursor = nullptr, * next = buf->ptr, * last = buf->ptr; | ||||
| 	const char footer[sizeof(uint64_t) + sizeof(uint8_t)] = { '\0', '\0', '\0', '\0', '\0' }; | ||||
|  | ||||
| 	while (true) { | ||||
| 		while (!pck_received) { | ||||
| 			int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0); | ||||
| 			if (bytes < 0) // si recv() retourne -1; ça veut dire qu'il y a plus rien a lire. | ||||
| 				return lsPck; | ||||
| 			len += bytes; | ||||
| 			// TODO: Voir si on trouve un footer (5 '\0' d'affilee). | ||||
| 			//		 Si oui, mettre l'index du debut du footer dans  | ||||
| 			//		 la variable "end" et mettre pck_received a true. | ||||
| 			 | ||||
| 		int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0); | ||||
| 		if (bytes <= 0) // si recv() retourne -1 ou 0; ça veut dire qu'il y a plus rien a lire qui n'a pas déjà été traité. | ||||
| 			return lsPck; | ||||
| 		len += bytes; | ||||
| 		end = len; | ||||
|  | ||||
| 		for (int x = 0; x < len; ++x) | ||||
| 			std::putchar(buf->ptr[x]); | ||||
| 		std::puts(""); | ||||
|  | ||||
| 			//buf->ptr | ||||
| 		while (true) { | ||||
| 			int cmp = 0; | ||||
|  | ||||
| 			//end = len; | ||||
| 			//pck_received = true; | ||||
| 		} | ||||
| 		char* cursor = nullptr; | ||||
| 			if (cursor) | ||||
| 				end -= (cursor - buf->ptr); | ||||
|  | ||||
| 		cursor = &buf->ptr[end]; | ||||
| 			if (end < 0) | ||||
| 				break; | ||||
|  | ||||
| 		lsPck.push_back(getPack(buf)); | ||||
| 			cursor = (char*)memchr(next, '\0', end); | ||||
| 			next = cursor + 1; | ||||
|  | ||||
| 		while (*cursor == '\0')  | ||||
| 			cursor++, end++; | ||||
|  | ||||
| 		if (cursor) { // mettre le début du prochain paquet au début du buffer. | ||||
| 			memcpy(buf->ptr, cursor, buf->len - end); // et voir s'il y a en un autre | ||||
| 			len = 0; | ||||
| 			pck_received = false; | ||||
| 			if (cursor) { | ||||
| 				cmp = memcmp(cursor, &footer, sizeof(uint64_t) + sizeof(uint8_t)); | ||||
| 				if (cmp == 0) { | ||||
| 					lsPck.push_back(last); | ||||
| 					cursor += sizeof(uint64_t) + sizeof(uint8_t) + 1; | ||||
| 					last = cursor; | ||||
| 					next = cursor + 1; | ||||
| 				} | ||||
| 			} | ||||
| 			else { | ||||
| 				cursor = &buf->ptr[len]; | ||||
| 				next = cursor + 1; | ||||
| 				break; | ||||
| 			}; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| } | ||||
|  | ||||
| template <> | ||||
| void netprot::sendPack<netprot::Packet>(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen) { | ||||
| 	switch (pack->type) { | ||||
|         case PACKET_TYPE::INPUT: | ||||
| 			sendPack<Input>(sock, (Input*)pack->ptr, buf, buflen); | ||||
| 			return; | ||||
|         case PACKET_TYPE::OUTPUT: | ||||
| 			sendPack<Output>(sock, (Output*)pack->ptr, buf, buflen); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::SYNC: | ||||
| 			sendPack<Sync>(sock, (Sync*)pack->ptr, buf, buflen); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::TEAMINF: | ||||
| 			sendPack<TeamInfo>(sock, (TeamInfo*)pack->ptr, buf, buflen); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::PLAYINF: | ||||
| 			sendPack<PlayerInfo>(sock, (PlayerInfo*)pack->ptr, buf, buflen); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::LOGINF: | ||||
| 			sendPack<LoginInfo>(sock, (LoginInfo*)pack->ptr, buf, buflen); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::CHUNKMOD: | ||||
| 			//sendPack<ChunkMod>(sock, (ChunkMod*)pack->ptr, buf, buflen); | ||||
| 		case PACKET_TYPE::PLAYERMOD: | ||||
| 			//sendPack<PlayerMod>(sock, (PlayerMod*)pack->ptr, buf, buflen); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::PICKUPMOD: | ||||
| 			//sendPack<PickupMod>(sock, (PickupMod*)pack->ptr, buf, buflen); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::GAMEINFO: | ||||
| 			sendPack<GameInfo>(sock, (GameInfo*)pack->ptr, buf, buflen); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::ENDINFO: | ||||
| 			//sendPack<EndInfo>(sock, (EndInfo*)pack->ptr, buf, buflen); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::CHAT: | ||||
| 			sendPack<Chat>(sock, (Chat*)pack->ptr, buf, buflen); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::ERRLOG: | ||||
| 			sendPack<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf, buflen); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::LAST_PACK: [[fallthrough]]; | ||||
|         case PACKET_TYPE::ERR: [[fallthrough]]; | ||||
| 		default: | ||||
| 			return; | ||||
|         } | ||||
| 	case PACKET_TYPE::INPUT: | ||||
| 		sendPack<Input>(sock, (Input*)pack->ptr, buf, buflen); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::OUTPUT: | ||||
| 		sendPack<Output>(sock, (Output*)pack->ptr, buf, buflen); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::SYNC: | ||||
| 		sendPack<Sync>(sock, (Sync*)pack->ptr, buf, buflen); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::TEAMINF: | ||||
| 		sendPack<TeamInfo>(sock, (TeamInfo*)pack->ptr, buf, buflen); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::PLAYINF: | ||||
| 		sendPack<PlayerInfo>(sock, (PlayerInfo*)pack->ptr, buf, buflen); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::LOGINF: | ||||
| 		sendPack<LoginInfo>(sock, (LoginInfo*)pack->ptr, buf, buflen); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::CHUNKMOD: | ||||
| 		//sendPack<ChunkMod>(sock, (ChunkMod*)pack->ptr, buf, buflen); | ||||
| 	case PACKET_TYPE::PLAYERMOD: | ||||
| 		//sendPack<PlayerMod>(sock, (PlayerMod*)pack->ptr, buf, buflen); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::PICKUPMOD: | ||||
| 		//sendPack<PickupMod>(sock, (PickupMod*)pack->ptr, buf, buflen); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::GAMEINFO: | ||||
| 		sendPack<GameInfo>(sock, (GameInfo*)pack->ptr, buf, buflen); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::ENDINFO: | ||||
| 		//sendPack<EndInfo>(sock, (EndInfo*)pack->ptr, buf, buflen); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::CHAT: | ||||
| 		sendPack<Chat>(sock, (Chat*)pack->ptr, buf, buflen); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::ERRLOG: | ||||
| 		sendPack<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf, buflen); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::LAST_PACK: [[fallthrough]]; | ||||
| 	case PACKET_TYPE::ERR: [[fallthrough]]; | ||||
| 	default: | ||||
| 		return; | ||||
| 	} | ||||
| } | ||||
|  | ||||
| template <> | ||||
| void netprot::sendPackTo<netprot::Packet>(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad) { | ||||
| 	switch (pack->type) { | ||||
|         case PACKET_TYPE::INPUT: | ||||
| 			sendPackTo<Input>(sock, (Input*)pack->ptr, buf, buflen, sockad); | ||||
| 			return; | ||||
|         case PACKET_TYPE::OUTPUT: | ||||
| 			sendPackTo<Output>(sock, (Output*)pack->ptr, buf, buflen, sockad); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::SYNC: | ||||
| 			sendPackTo<Sync>(sock, (Sync*)pack->ptr, buf, buflen, sockad); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::TEAMINF: | ||||
| 			sendPackTo<TeamInfo>(sock, (TeamInfo*)pack->ptr, buf, buflen, sockad); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::PLAYINF: | ||||
| 			sendPackTo<PlayerInfo>(sock, (PlayerInfo*)pack->ptr, buf, buflen, sockad); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::LOGINF: | ||||
| 			sendPackTo<LoginInfo>(sock, (LoginInfo*)pack->ptr, buf, buflen, sockad); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::CHUNKMOD: | ||||
| 			//sendPackTo<ChunkMod>(sock, (ChunkMod*)pack->ptr, buf, buflen, sockad); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::PLAYERMOD: | ||||
| 			//sendPackTo<PlayerMod>(sock, (PlayerMod*)pack->ptr, buf, buflen, sockad); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::PICKUPMOD: | ||||
| 			//sendPackTo<PickupMod>(sock, (PickupMod*)pack->ptr, buf, buflen, sockad); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::GAMEINFO: | ||||
| 			sendPackTo<GameInfo>(sock, (GameInfo*)pack->ptr, buf, buflen, sockad); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::ENDINFO: | ||||
| 			//sendPackTo<EndInfo>(sock, (EndInfo*)pack->ptr, buf, buflen, sockad); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::CHAT: | ||||
| 			sendPackTo<Chat>(sock, (Chat*)pack->ptr, buf, buflen, sockad); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::ERRLOG: | ||||
| 			sendPackTo<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf, buflen, sockad); | ||||
| 			return; | ||||
| 		case PACKET_TYPE::LAST_PACK: [[fallthrough]]; | ||||
|         case PACKET_TYPE::ERR: [[fallthrough]]; | ||||
| 		default: | ||||
| 			return; | ||||
|         } | ||||
| 	case PACKET_TYPE::INPUT: | ||||
| 		sendPackTo<Input>(sock, (Input*)pack->ptr, buf, buflen, sockad); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::OUTPUT: | ||||
| 		sendPackTo<Output>(sock, (Output*)pack->ptr, buf, buflen, sockad); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::SYNC: | ||||
| 		sendPackTo<Sync>(sock, (Sync*)pack->ptr, buf, buflen, sockad); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::TEAMINF: | ||||
| 		sendPackTo<TeamInfo>(sock, (TeamInfo*)pack->ptr, buf, buflen, sockad); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::PLAYINF: | ||||
| 		sendPackTo<PlayerInfo>(sock, (PlayerInfo*)pack->ptr, buf, buflen, sockad); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::LOGINF: | ||||
| 		sendPackTo<LoginInfo>(sock, (LoginInfo*)pack->ptr, buf, buflen, sockad); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::CHUNKMOD: | ||||
| 		//sendPackTo<ChunkMod>(sock, (ChunkMod*)pack->ptr, buf, buflen, sockad); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::PLAYERMOD: | ||||
| 		//sendPackTo<PlayerMod>(sock, (PlayerMod*)pack->ptr, buf, buflen, sockad); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::PICKUPMOD: | ||||
| 		//sendPackTo<PickupMod>(sock, (PickupMod*)pack->ptr, buf, buflen, sockad); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::GAMEINFO: | ||||
| 		sendPackTo<GameInfo>(sock, (GameInfo*)pack->ptr, buf, buflen, sockad); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::ENDINFO: | ||||
| 		//sendPackTo<EndInfo>(sock, (EndInfo*)pack->ptr, buf, buflen, sockad); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::CHAT: | ||||
| 		sendPackTo<Chat>(sock, (Chat*)pack->ptr, buf, buflen, sockad); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::ERRLOG: | ||||
| 		sendPackTo<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf, buflen, sockad); | ||||
| 		return; | ||||
| 	case PACKET_TYPE::LAST_PACK: [[fallthrough]]; | ||||
| 	case PACKET_TYPE::ERR: [[fallthrough]]; | ||||
| 	default: | ||||
| 		return; | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -135,19 +135,19 @@ namespace netprot { | ||||
| 	void Serialize(Chat* chat, char* buf[], uint32_t* buflen); // cli/srv | ||||
| 	void Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen); // srv | ||||
|  | ||||
| 	bool Deserialize(Input* in, char* buf, const uint32_t buflen); // srv | ||||
| 	bool Deserialize(Output* out, char* buf, const uint32_t buflen); // cli | ||||
| 	bool Deserialize(Sync* sync, char* buf, const uint32_t buflen); // cli | ||||
| 	bool Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen); // cli/srv | ||||
| 	bool Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen); // cli/srv | ||||
| 	bool Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen); // cli | ||||
| 	bool Deserialize(GameInfo* ginfo, char* buf, const uint32_t buflen); // cli | ||||
| 	bool Deserialize(Chat* chat, char* buf, const uint32_t buflen); // srv/cli | ||||
| 	bool Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen); // srv | ||||
| 	bool Deserialize(Input* in, char* buf, uint32_t* buflen); // srv | ||||
| 	bool Deserialize(Output* out, char* buf, uint32_t* buflen); // cli | ||||
| 	bool Deserialize(Sync* sync, char* buf, uint32_t* buflen); // cli | ||||
| 	bool Deserialize(TeamInfo* tinfo, char* buf, uint32_t* buflen); // cli/srv | ||||
| 	bool Deserialize(LoginInfo* linfo, char* buf, uint32_t* buflen); // cli/srv | ||||
| 	bool Deserialize(PlayerInfo* pinfo, char* buf, uint32_t* buflen); // cli | ||||
| 	bool Deserialize(GameInfo* ginfo, char* buf, uint32_t* buflen); // cli | ||||
| 	bool Deserialize(Chat* chat, char* buf, uint32_t* buflen); // srv/cli | ||||
| 	bool Deserialize(ErrorLog* errlog, char* buf, uint32_t* buflen); // srv | ||||
|  | ||||
| 	PACKET_TYPE getType(char* buf, uint32_t buflen); | ||||
|  | ||||
| 	Packet getPack(char* buf, uint32_t buflen); | ||||
| 	Packet getPack(char* buf, uint32_t *buflen); | ||||
| 	Packet getPack(Buffer* buf); | ||||
|  | ||||
| 	bool emptyPack(Packet pck); | ||||
| @@ -160,7 +160,7 @@ namespace netprot { | ||||
| 	template <class T> void sendPack(SOCKET sock, T* pack, Buffer* buf); | ||||
| 	template <class T> void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad); | ||||
| 	 | ||||
| 	std::vector<Packet> recvPacks(SOCKET sock, Buffer* buf); | ||||
| 	std::vector<char*> recvPacks(SOCKET sock, Buffer* buf); | ||||
|  | ||||
| 	template <class T> | ||||
| 	void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user