Skip to content

Commit

Permalink
add create command
Browse files Browse the repository at this point in the history
  • Loading branch information
skidu committed Dec 16, 2016
1 parent 56b272c commit e24a155
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 3 deletions.
112 changes: 112 additions & 0 deletions examples/GatewayWorker/start.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
namespace App\Workerman\{AppName};

use \Workerman\Worker;
use \Workerman\Connection\AsyncTcpConnection;

// 自动加载类
define('STAGE_INIT', 0);
define('STAGE_ADDR', 1);
define('STAGE_UDP_ASSOC', 2);
define('STAGE_DNS', 3);
define('STAGE_CONNECTING', 4);
define('STAGE_STREAM', 5);
define('STAGE_DESTROYED', -1);
define('CMD_CONNECT', 1);
define('CMD_BIND', 2);
define('CMD_UDP_ASSOCIATE', 3);
define('ADDRTYPE_IPV4', 1);
define('ADDRTYPE_IPV6', 4);
define('ADDRTYPE_HOST', 3);

$worker = new Worker('tcp://0.0.0.0:1080');
$worker->onConnect = function($connection)
{
$connection->stage = STAGE_INIT;
};

$worker->onMessage = function($connection, $buffer)
{
switch($connection->stage)
{
case STAGE_INIT:
$connection->send("\x05\x00");
$connection->stage = STAGE_ADDR;
return;
case STAGE_ADDR:
$cmd = ord($buffer[1]);
if($cmd != CMD_CONNECT)
{
echo "bad cmd $cmd\n";
$connection->close();
return;
}
$header_data = parse_socket5_header($buffer);
if(!$header_data)
{
$connection->close();
return;
}
$connection->stage = STAGE_CONNECTING;
$remote_connection = new AsyncTcpConnection('tcp://'.$header_data[1].':'.$header_data[2]);
$remote_connection->onConnect = function($remote_connection)use($connection)
{
$connection->state = STAGE_STREAM;
$connection->send("\x05\x00\x00\x01\x00\x00\x00\x00\x10\x10");
$connection->pipe($remote_connection);
$remote_connection->pipe($connection);
};
$remote_connection->connect();
}
};

function parse_socket5_header($buffer)
{
$addr_type = ord($buffer[3]);
switch($addr_type)
{
case ADDRTYPE_IPV4:
if(strlen($buffer) < 10)
{
echo bin2hex($buffer)."\n";
echo "buffer too short\n";
return false;
}
$dest_addr = ord($buffer[4]).'.'.ord($buffer[5]).'.'.ord($buffer[6]).'.'.ord($buffer[7]);
$port_data = unpack('n', substr($buffer, -2));
$dest_port = $port_data[1];
$header_length = 10;
break;
case ADDRTYPE_HOST:
$addrlen = ord($buffer[4]);
if(strlen($buffer) < $addrlen + 5)
{
echo $buffer."\n";
echo bin2hex($buffer)."\n";
echo "buffer too short\n";
return false;
}
$dest_addr = substr($buffer, 5, $addrlen);
$port_data = unpack('n', substr($buffer, -2));
$dest_port = $port_data[1];
$header_length = $addrlen + 7;
break;
case ADDRTYPE_IPV6:
if(strlen($buffer) < 22)
{
echo "buffer too short\n";
return false;
}
echo "todo ipv6\n";
return false;
default:
echo "unsupported addrtype $addr_type\n";
return false;
}
return array($addr_type, $dest_addr, $dest_port, $header_length);
}

if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
21 changes: 21 additions & 0 deletions examples/Workerman/start.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace App\Workerman\{AppName};

use Workerman\Worker;

// 创建一个Worker监听2345端口,使用http协议通讯
$http_worker = new Worker("http://0.0.0.0:2345");

// 启动4个进程对外提供服务
$http_worker->count = 4;

// 接收到浏览器发送的数据时回复hello world给浏览器
$http_worker->onMessage = function($connection, $data)
{
// 向浏览器发送hello world
$connection->send('hello world');
};

if(!defined('GLOBAL_START')) {
Worker::runAll();
}
3 changes: 2 additions & 1 deletion src/Commands/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ protected function loadRuntimeConfig()
return true;
}

protected function initLogFile($logFile=null) {
protected function initLogFile($logFile=null)
{
if (!$logFile) {
$logFile = config('workerman.log_file') .DIRECTORY_SEPARATOR. 'workerman.log';
}
Expand Down
34 changes: 34 additions & 0 deletions src/Commands/CreateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace Skidu\LaraWorker\Commands;

class CreateCommand extends BaseCommand
{
protected $signature = 'worker:create {name} {--type=1}';
protected $description = 'create workerman app';

public function fire()
{
$workerName = $this->argument('name');
$config = config('workerman');
$appPath = $config['app_path'];
if (!is_dir($appPath) && !mkdir($appPath)) {
throw new \Exception("create app directory failed: {$appPath}");
}

$workspace = $appPath . DIRECTORY_SEPARATOR . ucfirst($workerName);
if (is_dir($workspace)) {
throw new \Exception("directory exists: {$workspace}");
}
if (!mkdir($workspace)) {
throw new \Exception("create app directory failed: {$workspace}");
}

$type = $this->option('type') == 1 ? 'Workerman' : 'GatewayWorker';
$tpl = dirname(dirname(dirname(__FILE__))) .DIRECTORY_SEPARATOR.'examples' .DIRECTORY_SEPARATOR. $type .DIRECTORY_SEPARATOR. 'start.tpl';
$content = str_replace('{AppName}', ucfirst($workerName), file_get_contents($tpl));
file_put_contents($workspace.DIRECTORY_SEPARATOR.'start.php', $content);

return true;
}

}
4 changes: 2 additions & 2 deletions src/Commands/StartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class StartCommand extends BaseCommand

public function fire()
{
$this->checkException();
$this->checkExtensions();
$this->initLogFile($this->option('logfile'));
$this->initPidFile($this->option('pidfile'));
$this->initRunConfFile();
$this->initWorkspace();
$this->startWorker();
}

protected function checkException()
protected function checkExtensions()
{
if (!extension_loaded('pcntl')) {
throw new \Exception("Please install pcntl extension");
Expand Down
9 changes: 9 additions & 0 deletions src/LaraWorkerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ public function boot()
*/
public function register()
{
$this->registerCmdCreate();
$this->registerCmdStart();
$this->registerCmdRestart();
$this->registerCmdStop();
$this->registerCmdReload();
$this->registerCmdStatus();
}

protected function registerCmdCreate()
{
$this->app->singleton('command.worker.create', function($app){
return $app['\Skidu\LaraWorker\Commands\CreateCommand'];
});
$this->commands('command.worker.create');
}

protected function registerCmdStart()
{
$this->app->singleton('command.worker.start', function($app){
Expand Down

0 comments on commit e24a155

Please sign in to comment.