<?php
declare(strict_types=1);
namespace Tests\buffer;
use Core\FileSystem\FileException;
use Core\Output;
use PRipple;
use Worker\Socket\TCPConnection;
use Worker\Worker;
include __DIR__ . '/../vendor/autoload.php';
class test_file_server extends Worker
{
public function heartbeat(): void
{
echo '.';
}
public function destroy(): void
{
}
public function initialize(): void
{
$filePath = '/tmp/test_file';
if (!file_exists($filePath)) {
$file = fopen($filePath, 'w');
for ($i = 0; $i < 1024 * 1024 * 10; $i++) {
fwrite($file, 'a');
}
fclose($file);
}
parent::initialize();
}
public function onConnect(TCPConnection $client): void
{
$filePath = '/tmp/test_file';
$file = fopen($filePath, 'r');
while (!feof($file)) {
try {
$client->write(fread($file, 1024));
} catch (FileException $exception) {
Output::printException($exception);
}
}
fclose($file);
echo PHP_EOL . 'test file md5:' . md5_file($filePath) . PHP_EOL;
echo 'test file size: ' . filesize($filePath) . PHP_EOL;
echo PHP_EOL . 'test file send done.' . PHP_EOL;
}
}
$kernel = PRipple::configure([
'RUNTIME_PATH' => '/tmp',
'HTTP_UPLOAD_PATH' => '/tmp',
]);
$server = test_file_server::new('test_file_server')->bind('tcp://127.0.0.1:3002', [SO_REUSEADDR => true]);
PRipple::kernel()->push($server);
$kernel->launch();