Make packet reading slighly faster by reading strings in-place instead of copying to temp buffer

This commit is contained in:
UnknownShadow200 2019-07-13 12:26:02 +10:00
parent 3725fe0585
commit 267345e47e
2 changed files with 41 additions and 65 deletions

View File

@ -758,14 +758,14 @@ static void Http_AddHeader(const char* key, const String* value) {
} }
/* Processes a HTTP header downloaded from the server */ /* Processes a HTTP header downloaded from the server */
static JNIEXPORT void JNICALL Java_com_classicube_MainActivity_httpParseHeader(JNIEnv* env, jclass c, jstring header) { JNIEXPORT void JNICALL Java_com_classicube_MainActivity_httpParseHeader(JNIEnv* env, jclass c, jstring header) {
String line = JavaGetString(env, header); String line = JavaGetString(env, header);
Http_ParseHeader(java_req, &line); Http_ParseHeader(java_req, &line);
(*env)->ReleaseStringUTFChars(env, header, line.buffer); (*env)->ReleaseStringUTFChars(env, header, line.buffer);
} }
/* Processes a chunk of data downloaded from the web server */ /* Processes a chunk of data downloaded from the web server */
static JNIEXPORT void JNICALL Java_com_classicube_MainActivity_httpAppendData(JNIEnv* env, jclass c, jbyteArray arr, jint len) { JNIEXPORT void JNICALL Java_com_classicube_MainActivity_httpAppendData(JNIEnv* env, jclass c, jbyteArray arr, jint len) {
jbyte* src = (*env)->GetByteArrayElements(env, NULL, 0); jbyte* src = (*env)->GetByteArrayElements(env, NULL, 0);
struct HttpRequest* req = java_req; struct HttpRequest* req = java_req;

View File

@ -83,6 +83,16 @@ if (cpe_extBlocks) {\
} else { *data++ = (BlockRaw)value; } } else { *data++ = (BlockRaw)value; }
#endif #endif
static String Protocol_UNSAFE_GetString(uint8_t* data) {
int i, length = 0;
for (i = STRING_SIZE - 1; i >= 0; i--) {
char code = data[i];
if (code == '\0' || code == ' ') continue;
length = i + 1; break;
}
return String_Init((char*)data, length, STRING_SIZE);
}
static void Protocol_ReadString(uint8_t** ptr, String* str) { static void Protocol_ReadString(uint8_t** ptr, String* str) {
int i, length = 0; int i, length = 0;
uint8_t* data = *ptr; uint8_t* data = *ptr;
@ -651,10 +661,7 @@ static void Classic_Message(uint8_t* data) {
static void Classic_Kick(uint8_t* data) { static void Classic_Kick(uint8_t* data) {
static const String title = String_FromConst("&eLost connection to the server"); static const String title = String_FromConst("&eLost connection to the server");
String reason; char reasonBuffer[STRING_SIZE]; String reason = Protocol_UNSAFE_GetString(data);
String_InitArray(reason, reasonBuffer);
Protocol_ReadString(&data, &reason);
Game_Disconnect(&title, &reason); Game_Disconnect(&title, &reason);
} }
@ -847,27 +854,20 @@ static void CPE_SendCpeExtInfoReply(void) {
static void CPE_ExtInfo(uint8_t* data) { static void CPE_ExtInfo(uint8_t* data) {
static const String d3Server = String_FromConst("D3 server"); static const String d3Server = String_FromConst("D3 server");
String appName; char appNameBuffer[STRING_SIZE]; String appName = Protocol_UNSAFE_GetString(&data[0]);
String_InitArray(appName, appNameBuffer);
Protocol_ReadString(&data, &appName);
Chat_Add1("Server software: %s", &appName);
cpe_needD3Fix = String_CaselessStarts(&appName, &d3Server); cpe_needD3Fix = String_CaselessStarts(&appName, &d3Server);
Chat_Add1("Server software: %s", &appName);
/* Workaround for old MCGalaxy that send ExtEntry sync but ExtInfo async. This means /* Workaround for old MCGalaxy that send ExtEntry sync but ExtInfo async. */
ExtEntry may sometimes arrive before ExtInfo, thus have to use += instead of = */ /* Means ExtEntry may sometimes arrive before ExtInfo, so use += instead of = */
cpe_serverExtensionsCount += Stream_GetU16_BE(data); cpe_serverExtensionsCount += Stream_GetU16_BE(&data[64]);
CPE_SendCpeExtInfoReply(); CPE_SendCpeExtInfoReply();
} }
static void CPE_ExtEntry(uint8_t* data) { static void CPE_ExtEntry(uint8_t* data) {
String ext; char extNameBuffer[STRING_SIZE]; String ext = Protocol_UNSAFE_GetString(&data[0]);
int extVersion; int version = data[67];
Platform_Log2("cpe ext: %s, %i", &ext, &version);
String_InitArray(ext, extNameBuffer);
Protocol_ReadString(&data, &ext);
extVersion = Stream_GetU32_BE(data);
Platform_Log2("cpe ext: %s, %i", &ext, &extVersion);
cpe_serverExtensionsCount--; cpe_serverExtensionsCount--;
CPE_SendCpeExtInfoReply(); CPE_SendCpeExtInfoReply();
@ -884,16 +884,16 @@ static void CPE_ExtEntry(uint8_t* data) {
} else if (String_CaselessEqualsConst(&ext, "PlayerClick")) { } else if (String_CaselessEqualsConst(&ext, "PlayerClick")) {
Server.SupportsPlayerClick = true; Server.SupportsPlayerClick = true;
} else if (String_CaselessEqualsConst(&ext, "EnvMapAppearance")) { } else if (String_CaselessEqualsConst(&ext, "EnvMapAppearance")) {
cpe_envMapVer = extVersion; cpe_envMapVer = version;
if (extVersion == 1) return; if (version == 1) return;
Net_PacketSizes[OPCODE_ENV_SET_MAP_APPEARANCE] += 4; Net_PacketSizes[OPCODE_ENV_SET_MAP_APPEARANCE] += 4;
} else if (String_CaselessEqualsConst(&ext, "LongerMessages")) { } else if (String_CaselessEqualsConst(&ext, "LongerMessages")) {
Server.SupportsPartialMessages = true; Server.SupportsPartialMessages = true;
} else if (String_CaselessEqualsConst(&ext, "FullCP437")) { } else if (String_CaselessEqualsConst(&ext, "FullCP437")) {
Server.SupportsFullCP437 = true; Server.SupportsFullCP437 = true;
} else if (String_CaselessEqualsConst(&ext, "BlockDefinitionsExt")) { } else if (String_CaselessEqualsConst(&ext, "BlockDefinitionsExt")) {
cpe_blockDefsExtVer = extVersion; cpe_blockDefsExtVer = version;
if (extVersion == 1) return; if (version == 1) return;
Net_PacketSizes[OPCODE_DEFINE_BLOCK_EXT] += 3; Net_PacketSizes[OPCODE_DEFINE_BLOCK_EXT] += 3;
} else if (String_CaselessEqualsConst(&ext, "ExtEntityPositions")) { } else if (String_CaselessEqualsConst(&ext, "ExtEntityPositions")) {
Net_PacketSizes[OPCODE_ENTITY_TELEPORT] += 6; Net_PacketSizes[OPCODE_ENTITY_TELEPORT] += 6;
@ -956,19 +956,13 @@ static void CPE_HoldThis(uint8_t* data) {
} }
static void CPE_SetTextHotkey(uint8_t* data) { static void CPE_SetTextHotkey(uint8_t* data) {
String action; char actionBuffer[STRING_SIZE]; /* First 64 bytes are label string */
uint32_t keyCode; String action = Protocol_UNSAFE_GetString(&data[64]);
uint8_t keyMods; uint32_t keyCode = Stream_GetU32_BE(&data[128]);
uint8_t keyMods = data[132];
Key key; Key key;
data += STRING_SIZE; /* skip label */
String_InitArray(action, actionBuffer);
Protocol_ReadString(&data, &action);
keyCode = Stream_GetU32_BE(data); data += 4;
keyMods = *data;
if (keyCode > 255) return; if (keyCode > 255) return;
key = Hotkeys_LWJGL[keyCode]; key = Hotkeys_LWJGL[keyCode];
if (!key) return; if (!key) return;
Platform_Log3("CPE hotkey added: %c, %b: %s", Key_Names[key], &keyMods, &action); Platform_Log3("CPE hotkey added: %c, %b: %s", Key_Names[key], &keyMods, &action);
@ -984,22 +978,12 @@ static void CPE_SetTextHotkey(uint8_t* data) {
} }
static void CPE_ExtAddPlayerName(uint8_t* data) { static void CPE_ExtAddPlayerName(uint8_t* data) {
String playerName; char playerNameBuffer[STRING_SIZE]; EntityID id = data[1]; /* 16 bit id */
String listName; char listNameBuffer[STRING_SIZE]; String playerName = Protocol_UNSAFE_GetString(&data[2]);
String groupName; char groupNameBuffer[STRING_SIZE]; String listName = Protocol_UNSAFE_GetString(&data[66]);
uint8_t groupRank; String groupName = Protocol_UNSAFE_GetString(&data[130]);
EntityID id; uint8_t groupRank = data[194];
String_InitArray(playerName, playerNameBuffer);
String_InitArray(listName, listNameBuffer);
String_InitArray(groupName, groupNameBuffer);
id = data[1]; data += 2;
Protocol_ReadString(&data, &playerName);
Protocol_ReadString(&data, &listName);
Protocol_ReadString(&data, &groupName);
groupRank = *data;
String_StripCols(&playerName);
Protocol_RemoveEndPlus(&playerName); Protocol_RemoveEndPlus(&playerName);
Protocol_RemoveEndPlus(&listName); Protocol_RemoveEndPlus(&listName);
@ -1091,16 +1075,12 @@ static void CPE_SetBlockPermission(uint8_t* data) {
} }
static void CPE_ChangeModel(uint8_t* data) { static void CPE_ChangeModel(uint8_t* data) {
String model; char modelBuffer[STRING_SIZE];
struct Entity* entity; struct Entity* entity;
EntityID id; EntityID id = data[0];
String_InitArray(model, modelBuffer); String model = Protocol_UNSAFE_GetString(&data[1]);
id = *data++;
Protocol_ReadString(&data, &model);
entity = Entities.List[id]; entity = Entities.List[id];
if (entity) { Entity_SetModel(entity, &model); } if (entity) Entity_SetModel(entity, &model);
} }
static void CPE_EnvSetMapAppearance(uint8_t* data) { static void CPE_EnvSetMapAppearance(uint8_t* data) {
@ -1217,10 +1197,7 @@ static void CPE_SetTextColor(uint8_t* data) {
} }
static void CPE_SetMapEnvUrl(uint8_t* data) { static void CPE_SetMapEnvUrl(uint8_t* data) {
String url; char urlBuffer[STRING_SIZE]; String url = Protocol_UNSAFE_GetString(data);
String_InitArray(url, urlBuffer);
Protocol_ReadString(&data, &url);
if (!url.length || Utils_IsUrlPrefix(&url, 0)) { if (!url.length || Utils_IsUrlPrefix(&url, 0)) {
Server_RetrieveTexturePack(&url); Server_RetrieveTexturePack(&url);
@ -1378,7 +1355,7 @@ static void CPE_Tick(void) {
static void BlockDefs_OnBlockUpdated(BlockID block, bool didBlockLight) { static void BlockDefs_OnBlockUpdated(BlockID block, bool didBlockLight) {
if (!World.Blocks) return; if (!World.Blocks) return;
/* Need to refresh lighting when a block's light blocking state changes */ /* Need to refresh lighting when a block's light blocking state changes */
if (Blocks.BlocksLight[block] != didBlockLight) { Lighting_Refresh(); } if (Blocks.BlocksLight[block] != didBlockLight) Lighting_Refresh();
} }
static TextureLoc BlockDefs_Tex(uint8_t** ptr) { static TextureLoc BlockDefs_Tex(uint8_t** ptr) {
@ -1395,7 +1372,7 @@ static TextureLoc BlockDefs_Tex(uint8_t** ptr) {
} }
static BlockID BlockDefs_DefineBlockCommonStart(uint8_t** ptr, bool uniqueSideTexs) { static BlockID BlockDefs_DefineBlockCommonStart(uint8_t** ptr, bool uniqueSideTexs) {
String name; char nameBuffer[STRING_SIZE]; String name;
BlockID block; BlockID block;
bool didBlockLight; bool didBlockLight;
float speedLog2; float speedLog2;
@ -1406,8 +1383,7 @@ static BlockID BlockDefs_DefineBlockCommonStart(uint8_t** ptr, bool uniqueSideTe
didBlockLight = Blocks.BlocksLight[block]; didBlockLight = Blocks.BlocksLight[block];
Block_ResetProps(block); Block_ResetProps(block);
String_InitArray(name, nameBuffer); name = Protocol_UNSAFE_GetString(data); data += STRING_SIZE;
Protocol_ReadString(&data, &name);
Block_SetName(block, &name); Block_SetName(block, &name);
Block_SetCollide(block, *data++); Block_SetCollide(block, *data++);