English 中文(简体)
使用增强缓冲区进行操作
原标题:Manipulate with boost buffer

I m writing server with streaming protocol, so I need to do things like find end of header, copy it and then parse other stuff in boost buffer. As I found out the best way for manipulation with strings (find string in it, copy/delete using iterators and so on) is std::string. But I m using char array buffer. So I ll need to have two buffers - char array and std::string - every time I ll need to manipulate with buffer I ll need to convert char array to std::string, do my stuff and then convert it back to char array using std::string.c_str(). Another way I found is use streambuf (as I asked in my previous questition) and then create istream/ostream to it and fill content from it to std::string (as showed in documentation). With streambuf I ll need :
streambuf
mutable_buffers_type
istream
ostream
and std::string
But with use of char array and std::string I need just :
char array
std::string

所以我认为使用streambuf是浪费内存(我需要为每个连接创建缓冲区)。我可以使用std::string作为boost缓冲区吗?然而,我认为可能有更好的方法来做到这一点。你能给我一个建议吗?

编辑:

我需要对我的缓冲区做这样的事情,但char数组不提供std::string(erase,substr,…)等功能,所以我需要使用std::string作为缓冲区。将其用作boost::buffer的最佳方式是什么,或者像这样解析代码的最佳方式又是什么?

#include <iostream>

int main(int argc, char* argv[])
{

    //"header" is header
    //"end" is marking that this point is end of header
    //"data" is data after header
    //this all is sent in one packet which I receive to buffer
    //I need to fill "headerend" to std::string header and then remove "headerend" from begining of buffer
    //then continue parsing "data" which stay in buffer

    std::string buffer = "headerenddata"; //I receive something like this
    std::string header; //here I ll fill header (including mark of end of header)

    //find end of header and include also mark of end of header which is "end" (+3)
    int endOfHeader = int(buffer.find("end"))+3; 

    //fill header from buffer to string header
    header = buffer.substr(0, endOfHeader);

    //delete header from input buffer and keep data in it for next parsing
    buffer.erase(buffer.begin(), buffer.begin()+endOfHeader); 
    //will be just "data" becouse header and mark of header are removed
    std::cout << buffer << std::endl; 
    //will be "headerend" which is "header" and mark end of header which is "end"
    std::cout << header << std::endl;


    return 0;
}
最佳回答

您可以使用std::string作为参数来构造boost::asio::bufferstd::vector<;char>;)。我认为处理它更容易,但这可能真的取决于你的其余代码是如何设计的。

您也可以使用aC++11数组。它的行为类似于向量,但它静态地分配空间(即,一旦创建了数组,底层缓冲区的大小就不能改变)。在某些情况下,这可能会给你带来一些性能优势。如果你不能使用C++11,Boost还包括一个非常相似的类。

boost::asio::buffer也接受一个普通的char数组(char-buf[SIZE]

作为参考,这里是boost::asio::buffer

更新:为了避免从char[]转换为string向量来接收和处理缓冲区(使用string

所以,为了接收,你可以这样做:

vector<char> msg(N);
...
asio::read(socket, asio::buffer(msg), asio::transfer_at_least(N), ec);

然后,为了处理数据包并拆分报头和数据,您可以使用迭代器,避免代码中具有O(n)复杂性的操作(substrerase查找(或者在我的例子中搜索

string end_marker = "end";
auto it = search(msg.begin(), msg.end(), end_marker.begin(), end_marker.end());

process_header(msg.begin(), it + 2);
process_data(it + 3, msg.end());
问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?