Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkedDestiny committed Jul 2, 2016
1 parent b02416b commit e6b521b
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 13 deletions.
41 changes: 41 additions & 0 deletions src/course/event/chat_client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

$socket = stream_socket_client("tcp://127.0.0.1:9501", $errno, $errstr, 30);

function onRead()
{
global $socket;
$buffer = stream_socket_recvfrom($socket, 1024);
if( !$buffer )
{
echo "Server closed\n"
swoole_event_del($socket);
}
echo "\nRECV: {$buffer}\n";
fwrite(STDOUT, "Enter Msg:");
}

function onWrite()
{
global $socket;
echo "on Write\n";
}

function onInput()
{
global $socket;
$msg = trim(fgets(STDIN));
if( $msg == 'exit' )
{
swoole_event_exit();
exit();
}
swoole_event_write($socket, $msg);
fwrite(STDOUT, "Enter Msg:");
}

swoole_event_add($socket , 'onRead', 'onWrite');

swoole_event_add(STDIN , 'onInput' );

fwrite(STDOUT, "Enter Msg:");
38 changes: 38 additions & 0 deletions src/course/event/chat_server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

class Server
{
private $serv;
private $test;

public function __construct() {
$this->serv = new swoole_server("0.0.0.0", 9501);
$this->serv->set(array(
'worker_num' => 1,
));
$this->serv->on('Start', array($this, 'onStart'));
$this->serv->on('Connect', array($this, 'onConnect'));
$this->serv->on('Receive', array($this, 'onReceive'));
$this->serv->on('Close', array($this, 'onClose'));

$this->serv->start();
}
public function onStart( $serv ) {
echo "Start\n";
}
public function onConnect( $serv, $fd, $from_id ) {
echo "Client {$fd} connect\n";
}
public function onClose( $serv, $fd, $from_id ) {
echo "Client {$fd} close connection\n";
}

public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
echo "Get Message From Client {$fd}:{$data}\n";
foreach ($serv->connections as $client) {
if( $fd != $client)
$serv->send($client, $data);
}
}
}
$server = new Server();
2 changes: 2 additions & 0 deletions src/course/event/event_loop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php

27 changes: 17 additions & 10 deletions src/course/process/base_process.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ class BaseProcess
public function __construct()
{
$this->process = new swoole_process(array($this, 'run') , false , true);
$this->process->signal(SIGTERM, function($signo) {
echo "{$signo} shutdown.\n";
});
$this->process->daemon(true,true);
var_dump($this->process);
//$this->process->daemon(true,true);
$this->process->start();

swoole_event_add($this->process->pipe, function ($pipe){
$data = $this->process->read();
echo "RECV: " . $data.PHP_EOL;
Expand All @@ -29,12 +25,23 @@ public function __construct()

public function run($worker)
{
swoole_timer_tick(1000, function() {
swoole_timer_tick(1000, function($timer_id ) {
static $index = 0;
$index = $index + 1;
$this->process->write("Hello");
var_dump($index);
if( $index == 10 )
{
swoole_timer_clear($timer_id);
}
});

swoole_process::wait();
}
}

new BaseProcess();
new BaseProcess();
swoole_process::signal(SIGCHLD, function($sig) {
//必须为false,非阻塞模式
while($ret = swoole_process::wait(false)) {
echo "PID={$ret['pid']}\n";
}
});
93 changes: 93 additions & 0 deletions src/course/process/dynamic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

class BaseProcess
{

private $process;

private $process_list = [];
private $process_use = [];
private $min_worker_num = 3;
private $max_worker_num = 6;

private $current_num;

public function __construct()
{
$this->process = new swoole_process(array($this, 'run') , false , 2);
$this->process->start();

swoole_process::wait();
}

public function run()
{
$this->current_num = $this->min_worker_num;

for($i=0;$i<$this->current_num ; $i++){
$process = new swoole_process(array($this, 'task_run') , false , 2);
$pid = $process->start();
$this->process_list[$pid] = $process;
$this->process_use[$pid] = 0;
}
foreach ($this->process_list as $process) {
swoole_event_add($process->pipe, function ($pipe) use($process) {
$data = $process->read();
var_dump($data);
$this->process_use[$data] = 0;
});
}


swoole_timer_tick(1000, function($timer_id) {
static $index = 0;
$index = $index + 1;
$flag = true;
foreach ($this->process_use as $pid => $used) {
if($used == 0)
{
$flag = false;
$this->process_use[$pid] = 1;
$this->process_list[$pid]->write($index . "Hello");
break;
}
}
if( $flag && $this->current_num < $this->max_worker_num )
{
$process = new swoole_process(array($this, 'task_run') , false , 2);
$pid = $process->start();
$this->process_list[$pid] = $process;
$this->process_use[$pid] = 1;
$this->process_list[$pid]->write($index . "Hello");
$this->current_num ++;
}
var_dump($index);
if( $index == 10 )
{
foreach ($this->process_list as $process) {
$process->write("exit");
}
swoole_timer_clear($timer_id);
$this->process->exit();
}
});
}

public function task_run($worker)
{
swoole_event_add($worker->pipe, function ($pipe) use ($worker){
$data = $worker->read();
var_dump($worker->pid . ": " . $data);
if($data == 'exit')
{
$worker->exit();
exit;
}
sleep(5);

$worker->write("" . $worker->pid);
});
}
}

new BaseProcess();
52 changes: 52 additions & 0 deletions src/course/process/msg_queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Created by PhpStorm.
* User: lancelot
* Date: 16-6-29
* Time: 下午8:35
*/

class BaseProcess
{

private $process;

public function __construct()
{
$this->process = new swoole_process(array($this, 'run') , false , true);
if( !$this->process->useQueue( 123 ) )
{
var_dump(swoole_strerror(swoole_errno()));
exit;
}
$this->process->start();

while(true)
{
$data = $this->process->pop();
echo "RECV: " . $data.PHP_EOL;
}
}

public function run($worker)
{
swoole_timer_tick(1000, function($timer_id ) {
static $index = 0;
$index = $index + 1;
$this->process->push("Hello");
var_dump($index);
if( $index == 10 )
{
swoole_timer_clear($timer_id);
}
});
}
}

new BaseProcess();
swoole_process::signal(SIGCHLD, function($sig) {
//必须为false,非阻塞模式
while($ret = swoole_process::wait(false)) {
echo "PID={$ret['pid']}\n";
}
});
26 changes: 23 additions & 3 deletions src/course/process/muti_process.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,18 @@ public function run()
$this->process_list[$i]->start();
}

swoole_timer_tick(1000, function() {
$this->process->push("Hello");
swoole_timer_tick(1000, function($timer_id) {
static $index = 0;
$index = $index + 1;
$this->process->push($index . "Hello");
var_dump($index);
if( $index == 10 )
{
$this->process->push("exit");
$this->process->push("exit");
$this->process->push("exit");
swoole_timer_clear($timer_id);
}
});
}

Expand All @@ -41,9 +51,19 @@ public function task_run($worker)
{
$data = $worker->pop();
var_dump($data);
if($data == 'exit')
{
$worker->exit();
}
sleep(5);
}
}
}

new BaseProcess();
new BaseProcess();
swoole_process::signal(SIGCHLD, function($sig) {
//必须为false,非阻塞模式
while($ret = swoole_process::wait(false)) {
echo "PID={$ret['pid']}\n";
}
});

0 comments on commit e6b521b

Please sign in to comment.