Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkedDestiny committed Aug 22, 2016
1 parent 958699d commit d0ce82e
Show file tree
Hide file tree
Showing 60 changed files with 3,447 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/course/hprose/app/Async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Created by PhpStorm.
* User: lancelot
* Date: 16-8-6
* Time: 下午4:02
*/
require_once "../vendor/autoload.php";

use Hprose\Future;

$p2 = Future\sync(function() {
sleep(1);
return array(Future\value(1), Future\value(2));
});
$p2->then(function($value) {
var_dump($value);
});
var_dump("hello");
78 changes: 78 additions & 0 deletions src/course/hprose/app/SwooleServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Created by PhpStorm.
* User: lancelot
* Date: 16-8-6
* Time: 下午4:26
*/
require_once "../vendor/autoload.php";

use Hprose\Swoole\Server;
use Hprose\Swoole\Socket\Service;

class SwooleServer
{
private $serv;
public static $PDO;

public function __construct() {
$this->serv = new Server("http://0.0.0.0:9501");
$this->serv->set(array(
'worker_num' => 1,
'daemonize' => false,
'max_request' => 10000,
'dispatch_mode' => 2,
));

$this->serv->on('Start', array($this, 'onStart'));
$this->serv->on('WorkerStart', array($this, 'onWorkerStart'));

// 加载配置文件
$config = require "config.php";

// 在这里注册对外服务接口
foreach($config['api_list'] as $controller)
{
require_once __DIR__ . "/ctrl/$controller.php";
$this->serv->add( new $controller() );
}

$port = $this->serv->listen("0.0.0.0", 9502, SWOOLE_SOCK_TCP);
$rpc_service = new Service();
$rpc_service->socketHandle($port);
foreach($config['api_list'] as $controller)
{
require_once __DIR__ . "/ctrl/$controller.php";
$rpc_service->add( new $controller() );
}

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

public function onStart( $serv ) {
echo "Start\n";
//INIT

}

public function onWorkerStart( $serv, $worker_id ) {
echo "Worker {$worker_id} start\n";

// TODO 在这里加载可能的全局变量
if(!$serv->taskworker){
SwooleServer::$PDO = new PDO(
"mysql:host=localhost;port=3306;dbname=Test",
"root",
"123456",
array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8';",
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_PERSISTENT => true
)
);
echo "Worker\n";
}
}
}

new SwooleServer();
57 changes: 57 additions & 0 deletions src/course/hprose/app/TcpFDClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
require_once "../vendor/autoload.php";

use \Hprose\Future;
use \Hprose\Swoole\Client;
use \HProse\Http\Client;
use \Hprose\Socket\Client;

$test = new Client("tcp://127.0.0.1:1314");
$test->fullDuplex = true;
$var_dump = Future\wrap("var_dump");

Future\co(function() use ($test) {
try {
var_dump((yield $test->hello("yield world1")));
var_dump((yield $test->hello("yield world2")));
var_dump((yield $test->hello("yield world3")));
var_dump((yield $test->hello("yield world4")));
var_dump((yield $test->hello("yield world5")));
var_dump((yield $test->hello("yield world6")));
}
catch (\Exception $e) {
echo ($e);
}
});

$var_dump($test->hello("async world1"));
$var_dump($test->hello("async world2"));
$var_dump($test->hello("async world3"));
$var_dump($test->hello("async world4"));
$var_dump($test->hello("async world5"));
$var_dump($test->hello("async world6"));

$test->sum(1,2)
->then(function($result) use ($test) {
var_dump($result);
return $test->sum($result , 1);
})
->then(function($result) use ($test) {
var_dump($result);
return $test->sum($result , 1);
})
->then(function($result) use ($test) {
var_dump($result);
return $test->sum($result, 1);
})
->then(function($result) use ($test) {
var_dump($result);
return $test->sum($result , 1);
})
->then(function($result) use ($test) {
var_dump($result);
return $test->sum($result , 1);
})
->then(function($result) {
var_dump($result);
});
35 changes: 35 additions & 0 deletions src/course/hprose/app/TcpServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
require_once "../vendor/autoload.php";

use Hprose\Swoole\Server;

function hello($name) {
return "Hello $name!";
}

class Controller
{
public function sum($a, $b)
{
if( !is_int($a) )
{
return null;
}
if( !is_int($b) )
{
return null;
}
return $a + $b;
}

public function sub(){

}
}

$server = new Server("tcp://0.0.0.0:1314");
$server->setErrorTypes(E_ALL);
$server->setDebugEnabled();
$server->addFunction('hello');
$server->add(new Controller());
$server->start();
27 changes: 27 additions & 0 deletions src/course/hprose/app/client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
require_once "../vendor/autoload.php";

use \Hprose\Future;
use \Hprose\Swoole\Client;
use \Hprose\Http\Client as HttpClient;

$test = new Client("http://127.0.0.1:9501");

$http = new HttpClient("http://127.0.0.1:9501", false);


var_dump($http->sum(1,2));
$test->sum(1,2)
->then(function($result) use ($test) {
var_dump($result);
//swoole_event_exit();
});
$test->getResult("show tables")
->then(function($result) use ($test) {
var_dump($result);
swoole_event_exit();
});

$test->subscribe($topic, function($msg){

})
13 changes: 13 additions & 0 deletions src/course/hprose/app/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Created by PhpStorm.
* User: lancelot
* Date: 16-8-6
* Time: 下午4:55
*/

return [
'api_list' => [
'Api'
]
];
35 changes: 35 additions & 0 deletions src/course/hprose/app/ctrl/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Created by PhpStorm.
* User: lancelot
* Date: 16-8-6
* Time: 下午4:45
*/

require_once __DIR__."/../SwooleServer.php";

class Api
{
public function sum($a, $b)
{
return $a + $b;
}

public function sub($a, $b)
{
return $a - $b;
}

public function getResult($sql)
{
$statement = SwooleServer::$PDO->prepare($sql);
$statement->execute();
return $statement->fetchAll(\PDO::FETCH_ASSOC);
}

public function publish($topic, $id, $msg, $offset)
{
// offset , msg
$serv->push($topic, $id, $msg);
}
}
8 changes: 8 additions & 0 deletions src/course/hprose/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"require": {

"hprose/hprose": "dev-master",
"hprose/hprose-swoole": "dev-master"

}
}
Loading

0 comments on commit d0ce82e

Please sign in to comment.