0

到目前为止,我已经让我的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("%s\n", 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:%d\n", 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

感谢您的时间和帮助!

4

1 回答 1

2

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

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

char *response;
parse_json_command(req, response);

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

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

char *response;
response = parse_json_command(req);

此外,如果您想在那里传递多个字节,则json_command参数可能应该是 a char *or const char *,而不仅仅是单个。char

于 2013-01-27T16:11:03.107 回答