I don't think we can use a queue here. The data that comes in might not be only a part of the whole message, so we need to buffer it somewhere. Also, I don't think std::string does a complete string copy when you do a substring command (at least, a good implementation shouldn't).

At any rate, we'll probably only process about 1KB of data TOTAL using this function, so I personally believe that code simplicity is preferable to ultimate efficiency.