Boost asio receive corrupt message - c++

I am made a Server & Client Asynchronous Application. All works perfectly except the message I receive. I am sending image pieces into strings. But when I receive them back, the string is corrupted, I that it's not the same as I send. The length it's the same, and almost all characters. If I compare what I send with what I received I have like 300 characters different from what I sent. I am sending strings of 50.000 characters. Any idea what may be the problem? The most of the code are comments, so you will understand it in seconds. Also, I shrinked it and made it easier for you to read.
I am sending with this.
// Send a message
void StartSendMessage ( MessagePtr msg )
{
// As long as the queue is not empty, the 'sending agent' is still alive
bool writeInProgress =! m_messageQueue.empty() ;
// Queue the message
m_messageQueue.push ( msg ) ;
if ( msg -> BodyLength() != 0 )
{
std:: cout << "Sending :" << msg -> BodyLength() << std:: endl ;
}
// If the 'sending agent' is inactive, start it
if ( !writeInProgress )
{
// Send message asynchronously. We leave the message on the queue
// since it needs to be available during the async read
async_write ( m_socket , boost::asio::buffer ( msg -> HeaderData() , msg -> SendLength () ) ,
boost::bind ( &ASyncConnectionMT::HandleSentMessage , this , boost::asio::placeholders::error , boost::asio::placeholders::bytes_transferred ) ) ;
}
}
// Message was sent
void HandleSentMessage ( const boost::system::error_code& ec , size_t size )
{
// Check the error code
if ( ec )
{
// Transfer error
std:: cout << "Error sending message: " << ec.message() << std:: endl ;
DoStop() ;
return ;
}
// Remove the sent message from queue
m_messageQueue.pop() ;
// If the que is not empty, send next message asynchronously.
// We leave the message on the que since it needs to be available during the async send
if ( !m_messageQueue.empty() )
{
MessagePtr msg = m_messageQueue.front() ;
std:: cout << "Message send lenght "<< msg->SendLength() ;
async_write ( m_socket , boost::asio::buffer ( msg -> HeaderData() , msg -> SendLength () ) ,
boost::bind ( &ASyncConnectionMT:: HandleSentMessage , this , boost::asio::placeholders::error , boost::asio::placeholders::bytes_transferred ) ) ;
}
}
I am reading with this.
void StartReceiving()
{
// Create receive buffer
BufferPtr receiveBuffer ( new Buffer ) ;
// Start async read, must pass 'this' as shared_ptr, else the
// 'this' object will be destroyed after leaving this function
m_socket.async_read_some ( boost::asio::buffer ( *receiveBuffer ) , boost::bind ( &ASyncConnectionMT::HandleReceivedd , shared_from_this() , receiveBuffer ,
boost::asio::placeholders::error , boost::asio::placeholders::bytes_transferred ) );
}
// Handle received data
void HandleReceivedd ( BufferPtr receiveBuffer , const boost::system::error_code& ec , size_t size)
{
if ( !ec )
{
BufferPtr sendBuffer ( new Buffer ) ;
std:: cout << m_socket.remote_endpoint() << ": Message received: " << std:: string (receiveBuffer -> data() , size ) << std:: endl << std:: endl;
std:: cout << "Message lenght received " << size << std:: endl;
// Start receiving next bit
StartReceiving() ;
}
else if ( ec == boost::asio::error::eof)
{
// Client disconnected. Close the socket.
std:: cout << m_socket.remote_endpoint() << ": Connection closed ( handle received )" << std:: endl;
m_socket.close();
}
}

I see several problems in this chunk of code:
1) When you send, you are putting copy of msg into m_messageQueue. But, when you call async_write, your buffer is constructed from pointer taken from msg, not m_messageQueue. So eventually you can send from incorrent buffer.
2) On receive you creating receiveBuffer on stack. When async_read_some immediatelly returns (almost always), your receiveBuffer will be destroyed because you exit from StartReceiving call.
3) Same with sendBuffer

Related

Grpc access violation on client

I'm in the process of messing around with GRPC. Currently I'm using a C# web application as my GRPC server and I'm using a C++ console application as the client.
I was able to successfully connect and communicate with the server with no issue. The problem arises when
I exit the C++ console client application. Upon exiting an Access Violation is thrown.
Stack trace
MeterReaderClientCpp.exe!`anonymous namespace'::ThreadInternalsWindows::thread_body
MeterReaderClientCpp.exe!__acrt_lock
ntdll.dll!RtlpWaitOnCriticalSection()
ntdll.dll!RtlpEnterCriticalSectionContended()
ntdll.dll!RtlEnterCriticalSection()
MeterReaderClientCpp.exe!__acrt_lock(__acrt_lock_id _Lock) Line 55
MeterReaderClientCpp.exe!_free_dbg(void * block, int block_use) Line 1019
MeterReaderClientCpp.exe!free(void * block) Line 32
MeterReaderClientCpp.exe!gpr_free(void * p) Line 53
MeterReaderClientCpp.exe!`anonymous namespace'::ThreadInternalsWindows::destroy_thread() Line 142
MeterReaderClientCpp.exe!`anonymous namespace'::ThreadInternalsWindows::Join() Line 112
MeterReaderClientCpp.exe!grpc_core::Thread::Join() Line 147
MeterReaderClientCpp.exe!gc_completed_threads() Line 74
MeterReaderClientCpp.exe!stop_threads() Line 331
MeterReaderClientCpp.exe!grpc_timer_manager_set_threading(bool threaded) Line 351
MeterReaderClientCpp.exe!grpc_shutdown_internal_locked() Line 175
MeterReaderClientCpp.exe!grpc_shutdown_internal(void * __formal) Line 208
MeterReaderClientCpp.exe!`anonymous namespace'::ThreadInternalsWindows::thread_body(void * v) Line 128
GRPC Client
int main( )
{
using namespace MeterReaderWeb::Services;
using namespace google::protobuf::util;
using namespace google::protobuf;
std::cout << "Press enter\n";
std::cin.ignore( );
std::cout << "Calling Grpc service\n";
std::fstream file{ R"(C:\Certificates\certificate.cer)", std::ios::in | std::ios::beg };
if ( !file.is_open( ) )
{
std::cerr << "Failed to open file\n";
return 1;
}
std::stringstream buffer;
buffer << file.rdbuf( );
grpc::SslCredentialsOptions options;
options.pem_root_certs = buffer.str( );
auto credentials{ grpc::SslCredentials( options ) };
auto channel{ grpc::CreateChannel( "localhost:5001", credentials ) };
auto stub{ MeterReadingService::NewStub( channel ) };
ReadingPacket packet;
packet.set_status( ReadingStatus::METER_READER_SUCCESS );
packet.set_notes( "Here are some random notes" );
auto message{ packet.add_readings( ) };
message->set_customer_id( 1 );
message->set_reading_value( 10001 );
auto timestamp{ message->mutable_reading_time( ) };
timestamp->CopyFrom( TimeUtil::GetCurrentTime( ) );
grpc::ClientContext context;
StatusMessage response;
if ( auto status{ stub->AddReading( &context, packet, &response ) }; status.ok( ) )
{
std::cout << "Added reading successfully\n";
auto responseStatus{ response.status( ) };
if ( responseStatus == ReadingStatus::METER_READER_SUCCESS )
{
std::cout << "Server status: success\n"
<< "Message: " << response.message( ) << '\n';
}
}
else
{
std::cerr << "Error: " << status.error_message( ) << '\n';
std::cerr << "Error Details: " << status.error_details( ) << '\n';
}
std::cin.ignore( );
}
I heavily used the GRPC route_guide_client.cc as a guide to help me write the above application.
I've tried adding calls to both grpc_init( ) and grpc_shutdown( ) even though their client examples don't contain either calls. But adding those had no effect.
What (if anything) am I missing here? Did I forget to call/populate something that the framework is attempting to clean up upon application exit?
OK I believe I've found what was causing the issue.
In my original post I said:
I've tried adding calls to both grpc_init( ) and grpc_shutdown( ) even though
the client examples don't contain either calls. But
adding those had no effect."
This was true, but after re-reading the documentation for grpc_shutdown( ) i noticed this (emphasis mine):
The last call to grpc_shutdown will initiate cleaning up of grpc
library internals, which can happen in another thread. Once the
clean-up is done, no memory is used by grpc, nor are any instructions
executing within the grpc library. Prior to calling, all application
owned grpc objects must have been destroyed.
This is where I think I went wrong. I was calling grpc_shutdown() while I still had grpc objects in scope. To correct I scoped the grpc objects and then called grpc_shutdown() once that scope was exited. This seems to have corrected the issue.
New Grpc Client
int main( )
{
std::cout << "Press enter\n";
std::cin.ignore( );
std::cout << "Calling Grpc service\n";
grpc_init( );
{ // <- Intentionally added scope here.
grpc::SslCredentialsOptions options;
if ( auto certificate{ ReadCertificate( ) } )
options.pem_root_certs = std::move( certificate ).value( );
else return 1;
auto credentials{ grpc::SslCredentials( options ) };
auto channel{ grpc::CreateChannel( "localhost:5001", credentials ) };
auto stub{ MeterReadingService::NewStub( channel ) };
std::cout << "Sending single packet\n";
SendPacket( stub.get( ), 8000 );
std::cout << "Sending multiple packets\n";
StreamDiagnostics( stub.get( ), 3 );
}
std::cout << "Shutting down library\n";
grpc_shutdown_blocking( );
std::cout << "Shut down complete press enter to exit\n";
std::cin.ignore( );
}

Will calling a blocking snd_pcm_writei after decoding audio cause weird playback?

Forgive me if my question title is terrible. My wife is always telling me I'm not good at phrasing things.
I've written some code that reads a buffer that's filled by another thread. The buffer is filled with audio data encoded by the opus codec. VoIP data is received from the remote side 20ms at a time. In an attempt to play audio as quickly as possible, in a loop, I take 20ms of data at a time out of the buffer, and then decode it, then send it straight to play on snd_pcm_writei.
I've looked around on Google for some examples on using snd_pcm_writei with previously encoded audio to see how others are doing it. I haven't had much luck.
My thought is, if I'm waiting on a mutex and waiting for encoding I can't logically see the audio being "smooth." I'd imagine that between each 20ms frame there be gaps of time where no audio is being sent to the speakers. Are my suspicions correct that this will likely create imperfect audio?
My code relating to this:
while( true )
{
// We need a positive lock
if( !buffer_lock )
buffer_lock.lock();
LOG_DEBUG( *logger_ ) << "After the mutex lock.";
LOG_DEBUG( *logger_ ) << "Buffer size: " << current_audio->buffer_size_;
LOG_DEBUG( *logger_ ) << "Read pointer: " << current_audio->read_pointer_;
opus_int32 payload_size;
LOG_DEBUG( *logger_ ) << "calling audioCanDecodeChunk()";
// Now fisticuffs do we have enouffs?
if( audioCanDecodeChunk( current_audio, payload_size ) )
{
LOG_DEBUG( *logger_ ) << "We have enough current_audio buffer.";
// Are we dank?
if( payload_size<0 or payload_size>MAX_PACKET )
{
LOG_ERROR( *logger_ ) << "Decoding error, payload size (" << payload_size << ") is outsize range.";
break; // Terminal
}
// We have enough!
// Advance the read pointer
current_audio->read_pointer_+= 4;
// Copy it out
memcpy( payload_buffer, current_audio->buffer_+current_audio->read_pointer_, payload_size );
// Release it
buffer_lock.unlock();
// Now thingify it
int samples_decoded = opus_decode( opus_decoder_,
(const unsigned char *)payload_buffer,
payload_size,
(opus_int16 *)pcm_buffer,
MAX_FRAME_SIZE,
0 );
// How did we do?
if( samples_decoded<0 )
{
// What hap?
LOG_ERROR( *logger_ ) << "Error decoding samples: " << opus_strerror( samples_decoded );
break;
}
else
{
// Now we have our PCM!
int bytes_decoded = current_audio->recording_.channels*sizeof( opus_int16 )*samples_decoded;
LOG_DEBUG( *logger_ ) << "We have decoded " << bytes_decoded << " bytes payload: " << payload_size;
// Now write
if( (error = snd_pcm_writei( playback_handle_, pcm_buffer, samples_decoded ))!=samples_decoded )
{
LOG_ERROR( *logger_ ) << "snd_pcm_writei error: " << snd_strerror( error );
}
}
// Advance pointer
current_audio->read_pointer_+= payload_size;
} // If we don't have enough let it slide and unlock
else if( current_audio->done_ ) // Were we issued a flush?
{
LOG_DEBUG( *logger_ ) << "We are done.";
// We are done with this loop
break;
}
else
{
// Wait for it (an update)
LOG_DEBUG( *logger_ ) << "Before wait_buffer wait. Done: " << ( current_audio->done_ ? "true" : "false" ) <<
"Size: " << current_audio->buffer_size_
<< ", Read: " << current_audio->read_pointer_;
current_audio->wait_buffer_.wait( buffer_lock );
LOG_DEBUG( *logger_ ) << "After wait_buffer wait";
}
} // End while( true )
If the time between writing 20-ms chunks is exactly 20 ms, then the device's buffer will be empty when you are writing a new chunk. Even the smallest delay will then result in an underrun.
To prevent underrungs, you must keep the buffer as full as possible. This means that at the start, you must fill it without waiting between chunks.
When the sender's clock runs faster than the device's clock, the stream will underrun eventually.This can be avoided by measuring the clock difference, and either changing the sender's transmit rate, or resampling the data dynamically.

Boost async server buffer error

I am trying to make a server & client in boost asio. Currently I am receiving this error. Can you point me what I am doing wrong?
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
// Forward declaration
class ASyncConnectionMT;
class ASyncEchoServerMT;
// Typedef for the buffer type (shared_ptr)
typedef boost::array < char , 65536 > Buffer;
typedef boost::shared_ptr < Buffer > BufferPtr;
// Typedef for the ASyncConnectionMT shared_ptr
// Typedef for the ASyncEchoServerMT shared_ptr
// Derived from "enable_shared_from_this" so the 'this' object can
// be passed as shared_ptr to the callback function
typedef boost::shared_ptr < ASyncConnectionMT > ASyncConnectionMTPtr;
typedef boost::shared_ptr < ASyncEchoServerMT > ASyncEchoServerMTPtr;
// Class the handles the client
class ASyncConnectionMT : public::boost::enable_shared_from_this<ASyncConnectionMT>
{
private: // The socket class for communication.
boost::asio::ip::tcp::socket m_socket;
// Strand object to synchronise calling handlers. Multiple threads might acces the socked
// at the same time since send and recieve are started asynchronously at the same time
boost::asio::strand m_strand;
public: // Constructor with the IO service to use
ASyncConnectionMT ( boost::asio::io_service& ios) : m_socket(ios), m_strand (ios)
{
}
// Retrieve the socked used by this connection
// Need to be passed to acceptor.accept() function
boost::asio::ip::tcp::socket& Socket()
{
return m_socket;
}
// Start handling the connection
void Start()
{
std:: cout << m_socket.remote_endpoint() << ": Connection accepted" << std:: endl ;
StartReceiving() ;
}
// Start receiving data
void StartReceiving()
{
// Create receive buffer
BufferPtr receiveBuffer ( new Buffer ) ;
// Start async read, must pass 'this' as shared_ptr, else the
// 'this' object will be destroyed after leaving this function
async_write ( m_socket, boost::asio::buffer ( *receiveBuffer ) , m_strand.wrap ( boost::bind ( &ASyncConnectionMT::HandleReceived, shared_from_this() ,
receiveBuffer , boost::asio::placeholders::error ,
boost::asio::placeholders::bytes_transferred ) ) ) ;
}
// Handle received data
void HandleReceived ( BufferPtr receiveBuffer , const boost::system::error_code& ec , size_t size)
{
if (!ec)
{
// Print received message
std:: cout << m_socket.remote_endpoint() << ": Message received: " << std:: string (receiveBuffer -> data() , size ) << std:: endl ;
// Convert to uppercare. We can't use the same buffer because that could be
// overwritten by another recieve
// UPD -> boost shared_ptr<TBuffer> sendBuffer(new TBuffer());
BufferPtr sendBuffer ( new Buffer ) ;
for ( size_t i=0 ; i!=size ; i++ )
{
(( &sendBuffer )[i]) = toupper (( &receiveBuffer )[i]) ;
}
// Start sending reply, must pass 'this' as shared_ptr, else the 'this' object will be
// destroyed after leaving this function. We pass the buffer as shared_ptr to the handler
// so the buffer is still in memory after sending is complete. Without it, the buffer could
// be deleted before the send operation is complete. The Handle Set is now synchronised via the strand.
async_write ( m_socket, boost::asio::buffer ( *sendBuffer , size ) , m_strand.wrap ( boost::bind ( &ASyncConnectionMT::HandleSent ,
shared_from_this() , sendBuffer ,
boost::asio::placeholders::error ,
boost::asio::placeholders::bytes_transferred ) ) ) ;
// Start receiving next bit
StartReceiving() ;
}
else if ( ec == boost::asio::error::eof)
{
// Client disconnected. Close the socket.
std:: cout << m_socket.remote_endpoint() << ": Connection closed ( handle received )" << std:: endl;
m_socket.close();
}
}
// Handle for when the data si sent
void HandleSent ( BufferPtr sendBuffer , const boost::system::error_code& ec , size_t size)
{
if (!ec)
{
// Start receiving again
StartReceiving() ;
}
else if ( ec == boost::asio::error::eof)
{
// Client disconnected. Close the socket.
std:: cout << m_socket.remote_endpoint() << ": Connection closed ( handle received )" << std:: endl;
m_socket.close();
}
else
{
std:: cout << "Error: " << ec.message << std:: endl ;
}
}
};
I am receiving the following errors.
C3967 - boost::system::error_code::message': function call missing argument list; use '&'boost::system::error_code::message' to create a pointer to member
C2664 - 'toupper' : cannot convert parametor 1 from 'BufferPtr' to 'int'
C2512 - 'boost::array' : no appropriate default constructor available
C2039 - 'data' : is not a member of 'boost::shared_prt'
C2027 - use of undefined type 'boost::array '
pointer to incomplete class type is not allowed
no suitable conversion function from "BufferPtr" to "int" exists
incomplete type is not allowed
Thanks in advance.
You missed the include
#include <boost/array.hpp>
The toupper loop is wrong
((&sendBuffer)[i]) = toupper((&receiveBuffer)[i]);
should be more like
((*sendBuffer)[i]) = toupper((*receiveBuffer)[i]);
or even more like
std::transform(receiveBuffer->begin(), receiveBuffer->end(), sendBuffer->begin(), static_cast<int(&)(int)>(std::toupper));
Pro tip: consider using unsigned char in the buffer to avoid unwanted sign extension when passing int to_upper
((*sendBuffer)[i]) = toupper(static_cast<unsigned char>((*receiveBuffer)[i]));
like the comment (and the compiler message) says:
std::cout << "Error: " << ec.message << std::endl;
should be
std::cout << "Error: " << ec.message() << std::endl;
Then it compiles. No guarantees it does what you expect (I haven't read the code)

Boost receive data from the tcp socket

I am trying to read specific number of bytes from the socket. My server is sending:
1) byte[0] - length of the message
2) byte[1:N] - the actual message
How do I read the first byte and then read the remaining bytes using boost::asio::ip::tcp::socket::read ? Here is the code snippet:
// receive data through the socket
void TCPTestClient::ReceiveData( )
{
try
{
boost::system::error_code error;
boost::asio::streambuf receivedStreamBuffer;
// reserve 512 bytes in output sequence
boost::asio::streambuf::mutable_buffers_type bufs =receivedStreamBuffer.prepare( 512 );
boost::asio::read( m_socket,
bufs,
error );
// transfer the buffer contents to string
std::istream is( &receivedStreamBuffer );
is >> m_receivedMessageStr;
// throw exception if error occurred
if ( error )
{
throw NetworkTestFailedException( error.message() );
}
}
catch(...)
{
}
}
You'll want to prepare a buffer for the one byte header, then prepare another buffer for the message. A simplified example might be
boost::asio::read(
m_socket,
receivedStreamBuffer.prepare(1),
error
);
if ( error ) {
std::cerr << "Read header failed: " << boost::system::system_error(error).what() << std::endl;
return;
}
receivedStreamBuffer.commit(1);
std::istream header( &receivedStreamBuffer );
uint8_t size;
header >> size;
// reserve message size in output sequence
boost::asio::read(
m_socket,
receivedStreamBuffer.prepare( size ),
bufs,
error
);
if ( error ) {
std::cerr << "Read message failed: " << boost::system::system_error(error).what() << std::endl;
return;
}
receivedStreamBuffer.commit( size );
// transfer the buffer contents to string
std::istream is( &receivedStreamBuffer );
is >> m_receivedMessageStr;

Winsock recv loop not functioning correctly

For some reason when I connect to my C++ server with Putty I get this
Recv: (string I typed)
Recv:
This happens everytime I send characters to my server using Putty
Source follows. If nessicary I will post the rest of my source. TYIA -Roland
void recvthread( void *pParams ) {
char buffer[128]
int err;
bool gonow = true;
while( true ) {
memset( buffer, '\0', 128 );
err = -1;
err = recv( datasock, buffer, 128, 0 );
if( err != -1 ) {
std::cout << "Recv: " << buffer << '\n';
std::cout << "Err = " << err << '\n';
}
Sleep(10);
}
}
I get this:
Recv: (string I typed)
Recv:
You get the string you typed plus whatever else was left over in the buffer from the previous time. If err is positive it is the number of bytes actually received. If it is zero it means the peer has disconnected and you should stop reading. Don't ignore these values.