void Connection::parsePacket(const boost::system::error_code& error) { m_connectionLock.lock(); m_readTimer.cancel(); if(error) handleReadError(error); if(m_connectionState != CONNECTION_STATE_OPEN || m_readError) { close(); m_connectionLock.unlock(); return; } --m_pendingRead; uint32_t length = m_msg.size() - m_msg.position() - 4, checksumReceived = m_msg.get<uint32_t>(true), checksum = 0; if(length > 0) checksum = adlerChecksum((uint8_t*)(m_msg.buffer() + m_msg.position() + 4), length); bool checksumEnabled = false; if(checksumReceived == checksum) { m_msg.skip(4); checksumEnabled = true; } if(!m_receivedFirst) { // First message received m_receivedFirst = true; if(!m_protocol) { // Game protocol has already been created at this point m_protocol = m_servicePort->makeProtocol(checksumEnabled, m_msg); if(!m_protocol) { close(); m_connectionLock.unlock(); return; } m_protocol->setConnection(shared_from_this()); } else m_msg.skip(1); // Skip protocol m_protocol->onRecvFirstMessage(m_msg); } else m_protocol->onRecvMessage(m_msg); // Send the packet to the current protocol try { ++m_pendingRead; m_readTimer.expires_from_now(boost::posix_time::seconds(Connection::readTimeout)); m_readTimer.async_wait(boost::bind(&Connection::handleReadTimeout, boost::weak_ptr<Connection>(shared_from_this()), boost::asio::placeholders::error)); // Wait to the next packet boost::asio::async_read(getHandle(), boost::asio::buffer(m_msg.buffer(), NETWORK_HEADER_SIZE), boost::bind(&Connection::parseHeader, shared_from_this(), boost::asio::placeholders::error)); } catch(std::exception& e) { if(m_logError) { LOG_MESSAGE(LOGTYPE_ERROR, e.what(), "NETWORK"); m_logError = false; close(); } } m_connectionLock.unlock(); }
void Connection::parsePacket(const boost::system::error_code& error) { m_connectionLock.lock(); m_readTimer.cancel(); if(error){ handleReadError(error); } if(m_connectionState != CONNECTION_STATE_OPEN || m_readError){ closeConnection(); m_connectionLock.unlock(); return; } --m_pendingRead; //Check packet checksum uint32_t recvChecksum = m_msg.PeekU32(); uint32_t checksum = 0; int32_t len = m_msg.getMessageLength() - m_msg.getReadPos() - 4; if(len > 0){ checksum = adlerChecksum((uint8_t*)(m_msg.getBuffer() + m_msg.getReadPos() + 4), len); } if(recvChecksum == checksum) // remove the checksum m_msg.GetU32(); if(!m_receivedFirst){ m_receivedFirst = true; // First message received if(!m_protocol){ // Game protocol has already been created at this point m_protocol = m_service_port->make_protocol(recvChecksum == checksum, m_msg); if(!m_protocol){ closeConnection(); m_connectionLock.unlock(); return; } m_protocol->setConnection(shared_from_this()); } else{ // Skip protocol ID m_msg.GetByte(); } m_protocol->onRecvFirstMessage(m_msg); } else{ // Send the packet to the current protocol m_protocol->onRecvMessage(m_msg); } try{ ++m_pendingRead; m_readTimer.expires_from_now(boost::posix_time::seconds(Connection::read_timeout)); m_readTimer.async_wait( boost::bind(&Connection::handleReadTimeout, boost::weak_ptr<Connection>(shared_from_this()), boost::asio::placeholders::error)); // Wait to the next packet boost::asio::async_read(getHandle(), boost::asio::buffer(m_msg.getBuffer(), NetworkMessage::header_length), boost::bind(&Connection::parseHeader, shared_from_this(), boost::asio::placeholders::error)); } catch(boost::system::system_error& e){ if(m_logError){ LOG_MESSAGE("NETWORK", LOGTYPE_ERROR, 1, e.what()); m_logError = false; closeConnection(); } } m_connectionLock.unlock(); }
void Connection::parsePacket(const boost::system::error_code& error) { std::lock_guard<std::recursive_mutex> lockClass(m_connectionLock); m_readTimer.cancel(); if (error) { handleReadError(error); } if (m_connectionState != CONNECTION_STATE_OPEN || m_readError) { close(); return; } //Check packet checksum uint32_t checksum; int32_t len = m_msg.getLength() - m_msg.getBufferPosition() - 4; if (len > 0) { checksum = adlerChecksum(m_msg.getBuffer() + m_msg.getBufferPosition() + 4, len); } else { checksum = 0; } uint32_t recvChecksum = m_msg.get<uint32_t>(); if (recvChecksum != checksum) { // it might not have been the checksum, step back m_msg.skipBytes(-4); } if (!m_receivedFirst) { // First message received m_receivedFirst = true; if (!m_protocol) { // Game protocol has already been created at this point m_protocol = m_service_port->make_protocol(recvChecksum == checksum, m_msg); if (!m_protocol) { close(); return; } m_protocol->setConnection(shared_from_this()); } else { m_msg.getByte(); // Skip protocol ID } m_protocol->onRecvFirstMessage(m_msg); } else { m_protocol->onRecvMessage(m_msg); // Send the packet to the current protocol } try { m_readTimer.expires_from_now(boost::posix_time::seconds(Connection::read_timeout)); m_readTimer.async_wait( std::bind(&Connection::handleReadTimeout, std::weak_ptr<Connection>(shared_from_this()), std::placeholders::_1)); // Wait to the next packet boost::asio::async_read(getHandle(), boost::asio::buffer(m_msg.getBuffer(), NetworkMessage::header_length), std::bind(&Connection::parseHeader, shared_from_this(), std::placeholders::_1)); } catch (boost::system::system_error& e) { if (m_logError) { std::cout << "[Network error - Connection::parsePacket] " << e.what() << std::endl; m_logError = false; } close(); } }