2

I've set up a C++ program using boost::asio to network two computers on my home network together. One acts as the server and the other acts as a client. The client computer transfers it's player's data(~50chars) to the server, which draws the server computer's player and the client computer's player. The players are squares that are moved around with the arrow keys. The client square on the server computer lags really badly, but the server square renders exactly like one would expect. I outputted the time between the server's asynchronous reads of the client data, and it shows that the reading routinely takes about 1-4 ms, and after about ten reads, it takes >100ms for one read, then goes back to normal. I have no idea where these skips come from. Here are my TCPConnection read methods:

void TCPConnection::readCycle() {
    if(!_socket.is_open())
        return;

    boost::asio::async_read(_socket, boost::asio::buffer(readBuf),
                            boost::bind(&TCPConnection::doneReading, shared_from_this(),
                                        boost::asio::placeholders::error,
                                        boost::asio::placeholders::bytes_transferred,
                                        true));
}

void TCPConnection::doneReading(boost::system::error_code error, size_t bytesTransfered, bool readAgain) {
    if(error == boost::asio::error::eof) {
        _socket.close();
        return;
    }

    double time = SDL_Millis();
    if(lastRead != 0)
        cout << "Time between reads (" << bytesTransfered << " bytes) : " << time - lastRead << endl;

    lastRead = time;

    _readData = string(readBuf.begin(), readBuf.begin() + bytesTransfered);

    if(readAgain) {
        boost::asio::async_read(_socket, boost::asio::buffer(readBuf),
                                boost::bind(&TCPConnection::doneReading, shared_from_this(),
                                            boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred,
                                            true));
    }
}

Here is some sample output:

Time between reads (128 bytes) : 3
Time between reads (128 bytes) : 159
Time between reads (128 bytes) : 4
Time between reads (128 bytes) : 3
Time between reads (128 bytes) : 3
Time between reads (128 bytes) : 3
Time between reads (128 bytes) : 6
Time between reads (128 bytes) : 4
Time between reads (128 bytes) : 3
Time between reads (128 bytes) : 3
Time between reads (128 bytes) : 4
Time between reads (128 bytes) : 5
Time between reads (128 bytes) : 154
Time between reads (128 bytes) : 4
Time between reads (128 bytes) : 3
Time between reads (128 bytes) : 6
Time between reads (128 bytes) : 3
Time between reads (128 bytes) : 3
Time between reads (128 bytes) : 4
Time between reads (128 bytes) : 6
Time between reads (128 bytes) : 4
Time between reads (128 bytes) : 3
Time between reads (128 bytes) : 3
Time between reads (128 bytes) : 7
Time between reads (128 bytes) : 6
Tanner Sansbury
  • 51,153
  • 9
  • 112
  • 169
Biostreem
  • 169
  • 1
  • 2
  • 8
  • 2
    The issue is most likely in some other other place in the code. Make sure that none of your completion handlers takes significant time. – Igor R. Apr 08 '14 at 06:41
  • The only other completion handler that is in my code is for an async write operation, and it does nothing except a simple `cout` – Biostreem Apr 08 '14 at 12:13
  • Consider creating an [sscce](http://www.sscce.org/) client that uses the native socket calls to verify if the delay is within the OS's network stack. For example, the user in [this](http://stackoverflow.com/q/12238375/) question was observing unexpected delays occurring in lower level layers used by Boost.Asio. – Tanner Sansbury Apr 08 '14 at 13:56
  • @TannerSansbury I tried to enable handler tracking via `BOOST_ASIO_ENABLE_HANDLER_TRACKING` and get an exception in win_static_mutex.hpp on win_static_mutex::lock as soon as I call `acceptor.async_accept`. It works fine without handler tracking. Any ideas? – Biostreem Apr 08 '14 at 23:03

0 Answers0