<commit_before><?php declare(strict_types=1);
/**
 * @author hollodotme
 */

namespace PHPMQ\Server;

use PHPMQ\Server\Protocol\Constants\PacketLength;
use PHPMQ\Server\Protocol\Headers\MessageHeader;
use PHPMQ\Server\Protocol\Headers\PacketHeader;
use PHPMQ\Server\Protocol\Messages\Acknowledgement;
use PHPMQ\Server\Protocol\Messages\ConsumeRequest;
use PHPMQ\Server\Protocol\Messages\MessageBuilder;
use PHPMQ\Server\Protocol\Messages\MessageE2C;
use PHPMQ\Server\Types\QueueName;

require __DIR__ . '/../vendor/autoload.php';

function fread_stream( $fp, $length )
{
	$buffer    = '';
	$chunkSize = (int)min( $length, 1024 );

	while ( $length > 0 )
	{
		$buffer    .= (string)fread( $fp, $chunkSize );
		$length    -= 1024;
		$chunkSize = (int)min( $length, 1024 );
	}

	return $buffer;
}

function fwrite_stream( $fp, $string )
{
	for ( $written = 0, $writtenMax = strlen( $string ); $written < $writtenMax; $written += $fwrite )
	{
		$fwrite = fwrite( $fp, substr( $string, $written ) );
		if ( $fwrite === false )
		{
			return $written;
		}
	}

	return $written;
}

$socket = stream_socket_client( 'tcp://127.0.0.1:9100' );
stream_set_blocking( $socket, false );

$socketName = stream_socket_get_name( $socket, true );
echo 'Connected to server: ' . $socketName . "\n";

$consumeRequest = new ConsumeRequest( new QueueName( $argv[1] ), 5 );

fwrite( $socket, $consumeRequest->toString() );

echo "Sent consume request\n";

sleep( 1 );

$messageBuilder = new MessageBuilder();

while ( true )
{
	$reads  = [$socket];<commit_msg>fixes reading from stream in chunks<commit_after><?php declare(strict_types=1);
/**
 * @author hollodotme
 */

namespace PHPMQ\Server;

use PHPMQ\Server\Protocol\Constants\PacketLength;
use PHPMQ\Server\Protocol\Headers\MessageHeader;
use PHPMQ\Server\Protocol\Headers\PacketHeader;
use PHPMQ\Server\Protocol\Messages\Acknowledgement;
use PHPMQ\Server\Protocol\Messages\ConsumeRequest;
use PHPMQ\Server\Protocol\Messages\MessageBuilder;
use PHPMQ\Server\Protocol\Messages\MessageE2C;
use PHPMQ\Server\Types\QueueName;

require __DIR__ . '/../vendor/autoload.php';

function fread_stream( $fp, $length )
{
	$buffer = '';

	while ( $length > 0 )
	{
		$chunkSize = (int)min( $length, 1024 );
		$buffer    .= (string)fread( $fp, $chunkSize );
		$length    -= $chunkSize;
	}

	return $buffer;
}

function fwrite_stream( $fp, $string )
{
	for ( $written = 0, $writtenMax = strlen( $string ); $written < $writtenMax; $written += $fwrite )
	{
		$fwrite = fwrite( $fp, substr( $string, $written ) );
		if ( $fwrite === false )
		{
			return $written;
		}
	}

	return $written;
}

$socket = stream_socket_client( 'tcp://127.0.0.1:9100' );
stream_set_blocking( $socket, false );

$socketName = stream_socket_get_name( $socket, true );
echo 'Connected to server: ' . $socketName . "\n";

$consumeRequest = new ConsumeRequest( new QueueName( $argv[1] ), 5 );

fwrite( $socket, $consumeRequest->toString() );

echo "Sent consume request\n";

sleep( 1 );

$messageBuilder = new MessageBuilder();

while ( true )
{
	$reads  = [ $socket ];
{'fraction_non_alphanumeric': 0.16088947024198821, 'fraction_numerical': 0.016350555918901243, 'mean_word_length': 4.443060498220641, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4353950', 'n_tokens_mistral': 1150, 'n_tokens_neox': 1086, 'n_words': 230}