<?php

require_once __DIR__ . "/../../Bootstrap.php";

$server = new TcpServer(TCP_SERVER_HOST, TCP_SERVER_PORT);
$server->start();

class TcpServer
{
    public $swooleServer;

    public function __construct($host, $port)
    {
        echo "server host:$host, port:$port\n";
        $this->swooleServer = new \swoole_server($host, $port, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
        $this->swooleServer->set([
            "pipe_buffer_size" => 1024 * 1024 * 1024,
            'dispatch_mode' => 3,
            'open_tcp_nodelay' => 1,
            'open_cpu_affinity' => 1,
            'net_worker_num' => 1,
            'worker_num' => 1,
			'task_worker_num' => 1,
            'max_request' => 100000,
			'log_file' => '/tmp/swoole_server.log',
        ]);
    }

    public function start()
    {
        $this->swooleServer->on('start', [$this, 'onStart']);
        $this->swooleServer->on('shutdown', [$this, 'onShutdown']);

        $this->swooleServer->on('workerStart', [$this, 'onWorkerStart']);
        $this->swooleServer->on('workerStop', [$this, 'onWorkerStop']);
        $this->swooleServer->on('workerError', [$this, 'onWorkerError']);

        $this->swooleServer->on('connect', [$this, 'onConnect']);
        $this->swooleServer->on('receive', [$this, 'onReceive']);
        $this->swooleServer->on('task', [$this, 'onTask']);
        $this->swooleServer->on('finish', [$this, 'onFinish']);

        $this->swooleServer->on('close', [$this, 'onClose']);

        $this->swooleServer->start();
    }

    public function onConnect(swoole_server $swooleServer, int $fd, int $from_id)
    {
	    debug_log("connecting ......");
    }

    public function onClose(swoole_server $swooleServer, $fd, $reactorId)
    {
        debug_log("closing, fd=$fd, reactorId=$reactorId");
    }

    public function onStart(swoole_server $swooleServer)
    {
        debug_log("server starting .....");
    }

    public function onShutdown(swoole_server $swooleServer)
    {
        debug_log("server shutdown .....");
    }

    public function onWorkerStart(swoole_server $swooleServer, $workerId)
    {
        debug_log("worker #$workerId starting .....");
        if ($workerId == 0) {
            swoole_timer_after(5000, function () {
                //$this->swooleServer->shutdown();
            });
        }
    }

    public function onWorkerStop(swoole_server $swooleServer, $workerId)
    {
        debug_log("worker #$workerId stopping ....");
    }

    public function onWorkerError(swoole_server $swooleServer, $workerId, $workerPid, $exitCode, $sigNo)
    {
        debug_log("worker error happening [workerId=$workerId, workerPid=$workerPid, exitCode=$exitCode, signalNo=$sigNo]...");
    }

    public function onReceive(swoole_server $swooleServer, $fd, $fromId, $data)
    {
        echo "server receive data: $data\n";
		$param = array(
			'fd' => $fd,
			'data' => $data,
		);		
		$swooleServer->task(json_encode($param));
		//echo "send data to task worker.\n";
	}

	public function onTask(swoole_server $swooleServer, $task_id, $fromId, $data)
	{
        $task_data = json_decode($data, true);
        $swooleServer->send($task_data['fd'] , $task_data['data']);
        $swooleServer->finish($task_data['fd']);
	}
	
    public function onFinish(swoole_server $swooleServer, $worker_task_id, $data)
	{
        debug_log("onFinish: worker_task_id=$worker_task_id, fd=$data\n");
	}
}
