Raised This Month: $ Target: $400
 0% 

Formatting command string for sending over TCP socket


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
tenub
Junior Member
Join Date: Aug 2013
Old 04-21-2016 , 15:33   Formatting command string for sending over TCP socket
Reply With Quote #1

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

Last edited by tenub; 04-21-2016 at 19:15.
tenub is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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