<commit_before>#include "server.hh"
#include "serializable.hh"
#include "networkEvents.hh"
#include "../logger.hh"

#include <atomic>
#include <memory>
#include <sstream>

using namespace std;


static atomic<unsigned int> clientIdCounter( 0 ); // Should be good enough.


Client::Client( tcp::socket socket ) : m_socket( move( socket ) )
{
	m_clientId = ++clientIdCounter;
	memset( m_data, 0, maxLength );
}


void Client::SetRead()
{
	auto self( shared_from_this() );
	m_socket.async_read_some(
		asio::buffer( m_data, maxLength ),
		[this, self]( boost::system::error_code ec, size_t length )
		{
			char buffer[USHRT_MAX];

			// Check for errors
			if( ec.value() )
			{
				LOG_ERROR( "Client::Read() got error " << ec.value() << ": '" << ec.message() << "'" );

				auto partEvent      = new PartEvent();
				partEvent->type     = NETWORK_EVENT;
				partEvent->subType  = NETWORK_PART;
				partEvent->clientId = 0;
				eventQueue->AddEvent( partEvent );

				m_socket.close();
				return;
			}

			// Apped the received data to the readStream
			readStream.write( m_data, length );

			// Read more if we don't have enough data even
			// for the header alone
			size_t streamLength = readStream.str().length();
			if( streamLength < sizeof( unsigned short ) )
			{
				SetRead();
				return;
			}

			// Read the package byte count
			size_t packetLength = UnserializeUint16( readStream );

			// Reverse the reading point back and
			// wait for more data if we don't have enough
			if( streamLength < packetLength )
			{
				long pos = static_cast<long>( readStream.tellg() );
				readStream.seekg( pos - sizeof( unsigned short ) );
				SetRead();<commit_msg>Oops, surely we want to know who disconnected.<commit_after>#include "server.hh"
#include "serializable.hh"
#include "networkEvents.hh"
#include "../logger.hh"

#include <atomic>
#include <memory>
#include <sstream>

using namespace std;


static atomic<unsigned int> clientIdCounter( 0 ); // Should be good enough.


Client::Client( tcp::socket socket ) : m_socket( move( socket ) )
{
	m_clientId = ++clientIdCounter;
	memset( m_data, 0, maxLength );
}


void Client::SetRead()
{
	auto self( shared_from_this() );
	m_socket.async_read_some(
		asio::buffer( m_data, maxLength ),
		[this, self]( boost::system::error_code ec, size_t length )
		{
			char buffer[USHRT_MAX];

			// Check for errors
			if( ec.value() )
			{
				LOG_ERROR( "Client::Read() got error " << ec.value() << ": '" << ec.message() << "'" );

				auto partEvent      = new PartEvent();
				partEvent->type     = NETWORK_EVENT;
				partEvent->subType  = NETWORK_PART;
				partEvent->clientId = m_clientId;
				eventQueue->AddEvent( partEvent );

				m_socket.close();
				return;
			}

			// Apped the received data to the readStream
			readStream.write( m_data, length );

			// Read more if we don't have enough data even
			// for the header alone
			size_t streamLength = readStream.str().length();
			if( streamLength < sizeof( unsigned short ) )
			{
				SetRead();
				return;
			}

			// Read the package byte count
			size_t packetLength = UnserializeUint16( readStream );

			// Reverse the reading point back and
			// wait for more data if we don't have enough
			if( streamLength < packetLength )
			{
				long pos = static_cast<long>( readStream.tellg() );
				readStream.seekg( pos - sizeof( unsigned short ) );
				SetRead();
{'fraction_non_alphanumeric': 0.13313609467455623, 'fraction_numerical': 0.0026627218934911242, 'mean_word_length': 3.3070063694267517, 'pattern_counts': {'":': 2, '<': 33, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3180385', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1068, 'n_words': 306}