Search code examples
c++audioboost-asiobeep

Boost Asio async_send produces beep sound


Trying to write a client for a type of server which behaves like an echo server. When I try to send something, it produces a beep sound. I've found that the problem arise at line 356 (on my editor) of win_iocp_socket_service_base.ipp file. The code is:

int result = ::WSASend(impl.socket_, buffers,
        static_cast<DWORD>(buffer_count), &bytes_transferred, flags, op, 0);

Then I made some research.

Continuous boost::asio reads here in the comment of the answer, someone said when a binary data is being written to std::cout.

Why does the following code make my computer beep? here, it seems like the problem is same. They concluded that a '\a' is what produced the sound.

Debugging the application, the buffer of the API function I mentioned earlier, contains the following:

"asd@ıııı2•Z\x1aP"

here "asd@" is my string, the rest changes every time when I debug and I don't know what are they. Now, this is probably the part which the sound is produced, but my question how do I prevent it?

I have the following implementation of a one-round "Correspond" function. Note that I also have a isolated Send() function implementation, which can be paired with ReadUntil() explicitly to achieve same result. Using Send() and ReadUntil() together, the beep sound is still produced. Normally, this shouldn't happen as I am not dealing with low-level API functions, but Boost.Asio. Am I doing something wrong?

CODES

void Correspond(const std::string &data,
                const std::string &delim,
                std::vector<char> &response)
{
    std::shared_ptr<std::vector<char>> localbuffer = std::make_shared<std::vector<char>>(data.begin(), data.end());

    // pass shared_ptr to callback function to prolong the buffer's lifetime
    // !!! perhaps a better buffer management is needed
    socket.async_send(boost::asio::buffer(*localbuffer),
                      std::bind(&TCPClient::on_correspond,
                                this,
                                std::placeholders::_1,
                                std::placeholders::_2,
                                delim,
                                std::ref(response),
                                localbuffer));
}

and following callback implementation

void on_correspond(const boost::system::error_code& errorcode,
                   std::size_t sent,
                   const std::string &delim,
                   std::vector<char> &response,
                   const std::shared_ptr<std::vector<char>> &buffer)
{
    if(errorcode) {
        SLOGERROR(mutex, errorcode.message(), "on_correspond()");
    }

    if(sent == 0) {
        SLOG(mutex, "0 bytes sent w/o errors", "on_correspond()");
    }

    ReadUntil(delim, response);
}

After debugging deep into API, I've found this issue is not related with read function, but I will post it here just to be sure.

void ReadUntil(const std::string &delim, std::vector<char> &response)
{
    boost::asio::async_read_until(socket,
                                  buffer,
                                  delim,
                                  std::bind(&TCPClient::on_readuntil,
                                            this,
                                            std::placeholders::_1,
                                            std::placeholders::_2,
                                            std::ref(response)));
}
void on_readuntil(const boost::system::error_code& errorcode,
                  std::size_t received,
                  std::vector<char> &response)
{
    SLOG(mutex, "on_readuntil invoked", "on_readuntil()");
    SLOG(mutex, received, "on_readuntil");
    // !!! needs error handling, short-read handling and whatnot
    if(errorcode) {
        SLOGERROR(mutex, errorcode.message(), "on_readuntil()");
        return;
    }

    if(received == 0) {
        SLOG(mutex, "0 bytes received w/o errors", "on_readuntil()");
    }

    response.reserve(received);
    std::copy(boost::asio::buffers_begin(buffer.data()),
              boost::asio::buffers_begin(buffer.data()) + received,
              std::back_inserter(response));
    buffer.consume(received);
}

Solution

  • here "asd@" is my string, the rest changes every time when I debug

    The buffer is read without caring about the size of it, welcome to C++. Give the boost::asio::buffer the buffers size.