swoole_server->sendfile

�����ļ���TCP�ͻ������ӡ�ʹ��ʾ����

bool swoole_server->sendfile(int $fd, string $filename);
$serv->sendfile($fd, __DIR__.'/test.jpg');

sendfile��������OS�ṩ��sendfileϵͳ���ã��ɲ���ϵͳֱ�Ӷ�ȡ�ļ���д��socket��sendfileֻ��2���ڴ濽����ʹ�ô˺������Խ��ͷ��ʹ����ļ�ʱ����ϵͳ��CPU���ڴ�ռ�á�

���ӣ�

<?php
$serv = new swoole_server("0.0.0.0", 9501);
$serv->set(array(
        'worker_num' => 2,
        'dispatch_mode' => 3,
        'daemonize' => false,
        'log_file' => '/tmp/swoole.log'
));
$serv->on('start', function($serv) {
    echo 'start',PHP_EOL;
});
$serv->on('workerStart', function($serv, $worker_id) {
    echo "server start\n";
});
$serv->on('connect', function ($serv, $fd, $from_id){
    echo "[#".posix_getpid()."]\tClient@[$fd:$from_id]: Connect.\n";
    $serv->send($fd, filesize(__DIR__.'/test.jpg'));
});
$serv->on('receive', function (swoole_server $serv, $fd, $from_id, $data) {
    echo "[#".$serv->worker_id."]\tClient[$fd]: $data\n";
    $serv->sendfile($fd, __DIR__.'/test.jpg');
    //$serv->close($fd);
});
$serv->on('close', function ($serv, $fd, $from_id) {
    echo "[#".posix_getpid()."]\tClient@[$fd:$from_id]: Close.\n";
});
$serv->start();
            
�˺�����swoole_server->send������ͻ��˷������ݣ���ͬ����sendfile������������ָ�����ļ���