Skip to content

Commit

Permalink
del code
Browse files Browse the repository at this point in the history
  • Loading branch information
jxy918 committed Aug 23, 2019
1 parent d5be768 commit 3833da2
Show file tree
Hide file tree
Showing 70 changed files with 115 additions and 6,459 deletions.
39 changes: 0 additions & 39 deletions app/Common/DbSelector.php

This file was deleted.

59 changes: 0 additions & 59 deletions app/Common/RpcProvider.php

This file was deleted.

112 changes: 112 additions & 0 deletions app/Common/TcpReceiveListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php declare(strict_types=1);

namespace App\Common;

use ReflectionException;
use Swoft;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Context\Context;
use Swoft\Server\Contract\ReceiveInterface;
use Swoft\Session\Session;
use Swoft\SwoftEvent;
use Swoft\Tcp\Server\Context\TcpReceiveContext;
use Swoft\Tcp\Server\Exception\TcpResponseException;
use Swoft\Tcp\Server\Request;
use Swoft\Tcp\Server\Response;
use Swoft\Tcp\Server\TcpErrorDispatcher;
use Swoft\Tcp\Server\TcpServerEvent;
use Swoole\Server;
use Throwable;

//自己定义的服务器逻辑
use App\WebSocket\Game\Core\Packet;
use App\WebSocket\Game\Core\Dispatch;
use App\WebSocket\Game\Core\Log;

/**
* Class TcpReceiveListener
*
* @since 2.0
* @Bean()
*/
class TcpReceiveListener implements ReceiveInterface
{
/**
* @param Server $server
* @param int $fd
* @param int $reactorId
* @param string $data
*
* @throws ContainerException
* @throws ReflectionException
* @throws TcpResponseException
*/
public function onReceive(Server $server, int $fd, int $reactorId, string $data): void
{
$response = Response::new($fd);
$request = Request::new($fd, $data, $reactorId);

server()->log("Receive: conn#{$fd} received client request, begin init context", [], 'debug');
$sid = (string)$fd;
$ctx = TcpReceiveContext::new($fd, $request, $response);

// Storage context
Context::set($ctx);
Session::bindCo($sid);

try {
Log::show(" Receive: client #{$fd} send success Mete: \n{");
if(!empty($data)) {
$data = Packet::packDecode($data);
$back = '';
if (isset($data['code']) && $data['code'] == 0 && isset($data['msg']) && $data['msg'] == 'OK') {
Log::show('Recv <<< cmd=' . $data['cmd'] . ' scmd=' . $data['scmd'] . ' len=' . $data['len'] . ' data=' . json_encode($data['data']));
//转发请求,代理模式处理,websocket路由到相关逻辑
$data['serv'] = $server;
$back = $this->dispatch($data);
Log::show('Tcp Strategy <<< data=' . $back);
} else {
Log::show($data['msg']);
}
if (!empty($back)) {
$server->send($fd, $back);
}
}
Log::split('}');
} catch (Throwable $e) {
server()->log("Receive: conn#{$fd} error: " . $e->getMessage(), [], 'error');
Swoft::trigger(TcpServerEvent::RECEIVE_ERROR, $e, $fd);

/** @var TcpErrorDispatcher $errDispatcher */
$errDispatcher = Swoft::getSingleton(TcpErrorDispatcher::class);

// Dispatching error handle
$response = $errDispatcher->receiveError($e, $response);
$response->send($server);
} finally {
// Defer
Swoft::trigger(SwoftEvent::COROUTINE_DEFER);

// Destroy
Swoft::trigger(SwoftEvent::COROUTINE_COMPLETE);

// Unbind cid => sid(fd)
Session::unbindCo();
}
}

/**
* 根据路由策略处理逻辑,并返回数据
* @param $data
* @return string
*/
public function dispatch($data) {
$obj = new Dispatch($data);
$back = "<center><h1>404 Not Found </h1></center><hr><center>swoft</center>\n";
if(!empty($obj->getStrategy())) {
$back = $obj->exec();
}
return $back;
}
}
Empty file added app/Console/.gitkeep
Empty file.
Empty file added app/Console/Command/.gitkeep
Empty file.
84 changes: 0 additions & 84 deletions app/Console/Command/AgentCommand.php

This file was deleted.

38 changes: 0 additions & 38 deletions app/Console/Command/DemoCommand.php

This file was deleted.

Loading

0 comments on commit 3833da2

Please sign in to comment.