The data between these two functions is becoming garbled. Inspecting the variables on each side show that the data is definitely different. The message size signal does work. It's the second read/write function that has the problem. Here is my write function and its counter read function:

string Pipe::RecvCompressedMessage()
{
int message_size = 0;
DWORD dwBytesRead = 0;
string buf, data;
byte size_in[sizeof(int)];

if (!ReadFile(_h, &message_size, sizeof(int), &dwBytesRead, NULL))
{
    debug->DebugMessage(Error::GetErrorMessageW(GetLastError()));
    Close();
    return "";
}

if (message_size < 0)
    return "";

buf.resize(message_size);

int total_bytes_read = 0;

while(total_bytes_read < message_size)
{
    if (!ReadFile(_h, (LPVOID) &buf, message_size - total_bytes_read, &dwBytesRead, NULL))
    {
        int err = GetLastError();

        debug->DebugMessage(Error::GetErrorMessageW(GetLastError()));
        Close();
        return "";
    }

    data.append(buf);

    total_bytes_read += dwBytesRead;
}

std::string decompressed;
boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
out.push(boost::iostreams::bzip2_decompressor());
out.push(boost::iostreams::back_inserter(decompressed));
boost::iostreams::copy(boost::make_iterator_range(buf), out);

return decompressed;
}


void Pipe::SendCompressedMessage(string message)
{
std::string compressed;
boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
out.push(boost::iostreams::bzip2_compressor());
out.push(boost::iostreams::back_inserter(compressed));
boost::iostreams::copy(boost::make_iterator_range(message), out);

DWORD dwBytesWritten;
int message_size = compressed.length();

if (WriteFile(_h, &message_size, sizeof(int), &dwBytesWritten, NULL) == 0)
{
    debug->DebugMessage(Error::GetErrorMessageW(GetLastError()));

    Close();
    return;
}

if (WriteFile(_h, (LPVOID *) &compressed, message_size, &dwBytesWritten, NULL) == 0)
{
    debug->DebugMessage(Error::GetErrorMessageW(GetLastError()));

    Close();
    return;
}
}
有帮助吗?

解决方案

You are writing a std::string instead a char *:

if (WriteFile(_h, (LPVOID *) (compressed.c_str()), message_size, &dwBytesWritten, NULL) == 0)

其他提示

string buf, data;
...
if (!ReadFile(_h, (LPVOID) &buf, message_size - total_bytes_read, &dwBytesRead, NULL))

You pass pointer to string object as lpBuffer to ReadFile. Replace string with vector<char>:

vector<char> buf;
...
buf.resize(message_size - total_bytes_read);
if (!ReadFile(_h, &buf[0], message_size - total_bytes_read, &dwBytesRead, NULL))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top