c语言 - 为什么evbuffer_add_printf只接受静态变量而不接受"dynamic"变量?



到目前为止,我已经让我的libev代码成功返回了一个说"OMP OMP"的静态刺痛,但是当我编写一个返回"静态"字符串的函数时,它似乎从未工作过。(旁注:这个想法是将相同的功能转换为动态响应,但仅用于敏捷测试目的,我需要它首先工作)。我的 libev 读取回调代码如下...

void p2pserver_network_buf_read_callback(struct bufferevent *incoming, void *arg){
    //Define function local variables
    struct evbuffer *evreturn;
    char *req;
    //Begin function local logic
    req = evbuffer_readline(incoming->input);
    if (req == NULL){
        return;    
    }

    char *response;
    parse_json_command(req, response);
    //response = "OMP OMP";
    g_print("PARSED");
    evreturn = evbuffer_new();
    evbuffer_add_printf(evreturn, "%s", response);
    bufferevent_write_buffer(incoming,evreturn);
    evbuffer_free(evreturn);
    free(req);
    g_print("%s", response);
}

parse_json_command函数如下...

void parse_json_command(char json_command, char *response){
    //Define Local Variables
    g_print("PARSING");
    response = "YOU KNOW";
    //Print out the recieved message....
    //g_message("%s", json_command);

    /** 
     * TODO: check if the JSON is valid before parsing 
     *          to prevent "Segmentation Defaults" 
     *          and its good sanity checks.
     **/
    //Parse JSON incomming
    /*json_object * jobj = json_tokener_parse(json_command);
    enum json_type type;
    json_object_object_foreach(jobj, key, val){
        g_print("%sn", key);
        if(g_utf8_collate(key, "cmd") >= 0){
            //Looks like the user has sent a "cmd" (command), lets analyze the "val" (value) of that command to see what the caller/client needs to be attending to...
            //Is the client requesting an "Identity Update" (Pings server: if this is the first time ping, the server and client will exachange keys if the relationship exists the server just accepts the encrypted "ping" packet update)
            type = json_object_get_type(val);
            if(type == json_type_string){
                char* cmd_value;
                cmd_value = json_object_get_string(val);
                //g_print("VALUE:%dn", g_utf8_collate(cmd_value, "identupdate"));
                if(g_utf8_collate(cmd_value, "identupdate") == 0){
                    //Call "Identity Update Response"
                        //char return_response = p2pserver_json_identupdate_response(json_command);
                }
            }
        }
    }
    */
    return;
}

如果您想查看完整的代码(在撰写本文时只有几页大),您可以通过以下链接转到源代码:https://github.com/Xenland/P2PCrypt-Server

感谢您的时间和帮助!

c 按值传递参数,而不是按引用传递参数。你的问题在这里:

void parse_json_command(char json_command, char *response){
    [...]
    response = "YOU KNOW";
    [...]
}
char *response;
parse_json_command(req, response);

response 是指向字符串的未初始化指针。您正在将指向静态字符串的指针分配给函数中的response指针,但这不会修改函数外部的response,它只是更改函数内的response。 有不同的方法可以解决此问题。快速修复的最简单方法可能是更改函数的原型以返回char *而不是void

char * parse_json_command(char json_command){
    char *response;
    [...]
    response = "YOU KNOW";
    [...]
    return response;
}
char *response;
response = parse_json_command(req);

此外,json_command参数可能应该是一个char *const char *,而不仅仅是一个char,如果你想在那里传递一个以上的字节。

相关内容

  • 没有找到相关文章

最新更新