AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Formatting command string for sending over TCP socket (https://forums.alliedmods.net/showthread.php?t=281820)

tenub 04-21-2016 15:33

Formatting command string for sending over TCP socket
 
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" "commandArray64127)

    for (new 
i=0i<commandArraySizei++)
    {
        
format(commandArray[i], 255"$%d\r\n%s"strlen(commandArray[i]), commandArray[i])
    }

    
implode_strings(commandArraycommandArraySize"\r\n"respCommand255)

    
format(respCommand255"*%d\r\n%s\r\n"commandArraySizerespCommand)

    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

Black Rose 04-22-2016 14:17

Re: Formatting command string for sending over TCP socket
 
I don't really understand. Are you trying to send CRLF or "\r\n" in text form?
It seems to be wrong in both JS and Pawn example.
The control char in Pawn is ^, not \.


All times are GMT -4. The time now is 18:41.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.