I'm trying to write a simple Redis client for publishing data to a Redis server. This is a project for me to learn Pawn/AMXX basics.
I come from a JavaScript background so things are a bit strange to me. I wrote the following function to format a command for RESP (Redis protocol) in about 2 minutes in JS:
PHP Code:
function FormatCommandRESP(commandString) {
const __newline = "\\r\\n";
var commandArray = commandString.trim().split(/\s+/);
var commandArraySize = commandArray.length;
return '*' + commandArraySize + __newline + commandArray.map(function(string) {
return '$' + string.length + __newline + string;
}).join(__newline) + __newline;
}
but I am struggling to write something similar as part of an AMXX plugin:
PHP Code:
stock FormatCommandRESP(commandString[256])
{
new respCommand[256]
new commandArray[64][256]
new commandArraySize
commandArraySize = explode_string(commandString, " ", commandArray, 64, 127)
for (new i=0; i<commandArraySize; i++)
{
format(commandArray[i], 255, "$%d\r\n%s", strlen(commandArray[i]), commandArray[i])
}
implode_strings(commandArray, commandArraySize, "\r\n", respCommand, 255)
format(respCommand, 255, "*%d\r\n%s\r\n", commandArraySize, respCommand)
return respCommand
}
Could someone help me understand better how to do this correctly/optimally?
For those unfamiliar with JS or Redis, here is how the protocol works:
http://redis.io/topics/protocol