Hello, I'm trying to make a server program using boost asio and boost serialization. The client is sending object-based messages. What this piece of code is supposed to do is check if there are messages in any of the iostreams and read them if there are any. The first message from the first client actually does get through correctly, but my otherwise the server just keeps giving segfaults and hanging while waiting for information.
I've tried the documentation but I haven't managed to get any wiser from that.
// TcpIoStream is a typedef of boost::asio::ip::tcp::iostream
//Iterate through a vector of pointers to streams.
for (std::vector<TcpIoStream*>::iterator itr=m_connections.begin(); itr!=m_connections.end(); itr++)
{
TcpIoStream* stream;
stream = *itr;
try
{
boost::archive::text_iarchive archive(*stream);
if (true) // <-- Check if data is available and stream is open, but I have no idea how.
{
int32_t msgType = 0;
archive >> msgType; // An int corresponding to an enum constant to know what message is coming through.
switch (msgType){
case LOGINMESSAGE:
{
LoginMessage loginMsg;
archive >> loginMsg;
ProcessLogin(stream,loginMsg);
}
break;
// Unknown message type, assume something went wrong.
default:
throw std::runtime_error("ERROR! Recieved invalid or corrupt message.");
break;
}
}
}
// Get rid of the stream that causes errors.
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
delete stream;
m_connections.erase(itr);
}
}
How do I check if there's data to be read, prevent the program from getting stuck, etc...
Any other things I really should change about this thing?