缓冲区&处理传入数据,按换行符

时间:2017-06-05 14:22:26

标签: c++

我刚刚在我的一些代码中发现了一个解析错误:

while(some_condition){
    char buf[1536] = {};
    len = BIO_read(web, buf, sizeof(buf));
    if(len > 0){
        process(new std::string(buf));
    }
}

传入的数据是json,问题是我希望传入的数据如下所述:

{json packet 1}\n
{json packet 2}\n{json packet 3}\n
{json packet 4}\n
通常是

。但我只是注意到我有时也可以获得以下形式的数据:

{json pack
et 1}\n
{json packet 2}
\n{json packet 3}\n
{json packet 4}\n

编辑:换行符永远不会出现在两个不同的行上(在我的示例中错误地分析了' \&39;并且' n'

当然,当得到不完整的json数据时,它会使进程函数内的json解析器失败。

所以我需要以某种方式缓冲传入的数据,解析换行符,如果找到则发送"完成json包"处理过程函数并从缓冲区中删除处理过的数据。

关于我如何快速和快速地做到这一点的建议有效的方式?

编辑:

没有回复描述任何特定的解决方案,所以我猜我没有足够好地解释这个问题(你通常会想出聪明的"简单"解决我看起来很复杂的问题)

我的下面的版本似乎正在按照我的意愿运作,所以我的后续问题是,如果您看到任何可能的速度改进,或者有任何其他方式的想法,我可以进行基准测试并与此进行比较。没有足够快的"在这种情况下,越快越好。

..或者如果你发现任何可能的错误?

#include <stdio.h>
#include <string>
#include <iostream>

//this is just for simulating incoming data
int dataSimulator(char* buf){
    static unsigned char call_no = 0;
    call_no++;
    switch (call_no){
    case 1:
        memcpy(buf, "111\n", 4);
        return 4;
        break;
    case 2:
        memcpy(buf, "222\n33", 6);
        return 6;
        break;
    case 3:
        memcpy(buf, "3\n444\n", 6);
        return 6;
        break;
    case 4:
    case 5:
    case 6:
        memcpy(buf, "5", 1);
        return 1;
        break;
    case 7:
        memcpy(buf, "\n", 1);
        return 1;
        break;
    case 8:
        memcpy(buf, "666\n777\n888\n", 12);
        return 12;
        break;

    }

    return 0;
}

int main(int argc, char* argv[])
{
    char buff[100] = {};
    int buff_offset = 0;
    int end;
    int start;

    while (1){

        buff_offset = strlen(buff);
        int len = dataSimulator(&buff[buff_offset]);

        if (len > 0)    {

            buff[buff_offset + len] = 0;

            end = 0;
            start = 0;

            while (buff[end]){
                if (buff[end] == '\n'){
                    //end of packet found
                    std::string packet(&buff[start], end - start);
                    std::cout << "packet: " << packet << " received" <<std::endl;
                    //mark old start as processed
                    buff[start] = 0;
                    start = end + 1;
                }
                end++;
            }
            if (start != end){
                //data left in buffer, move remaining stuff to the beginning
                memmove(buff, &buff[start], end - start);
                //make sure it's null terminated
                buff[end - start] = 0;
            }
        }
        else{
            break;
        }
    }
}

输出:

packet: 111 received
packet: 222 received
packet: 333 received
packet: 444 received
packet: 555 received
packet: 666 received
packet: 777 received
packet: 888 received

0 个答案:

没有答案