-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
up: update pipe message logic for handle clean ws,tcp connection
- Loading branch information
Showing
8 changed files
with
189 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Swoft\Server\Swoole; | ||
|
||
use Swoft; | ||
use Swoft\Bean\Annotation\Mapping\Bean; | ||
use Swoft\Log\Helper\CLog; | ||
use Swoft\Server\Contract\PipeMessageInterface; | ||
use Swoft\Server\ServerEvent; | ||
use Swoole\Server; | ||
|
||
/** | ||
* Class PipeMessageListener | ||
* | ||
* @since 2.0.7 | ||
* @Bean() | ||
*/ | ||
class PipeMessageListener implements PipeMessageInterface | ||
{ | ||
/** | ||
* Pipe message event | ||
* | ||
* @param Server $server | ||
* @param int $srcWorkerId | ||
* @param mixed $message | ||
*/ | ||
public function onPipeMessage(Server $server, int $srcWorkerId, $message): void | ||
{ | ||
CLog::debug("PipeMessage: received pipe-message fromWID=$srcWorkerId message=$message"); | ||
|
||
Swoft::trigger(ServerEvent::PIPE_MESSAGE, $message, $srcWorkerId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Swoft\Tcp\Server\Listener; | ||
|
||
use Swoft\Event\Annotation\Mapping\Listener; | ||
use Swoft\Event\EventHandlerInterface; | ||
use Swoft\Event\EventInterface; | ||
use Swoft\Log\Helper\CLog; | ||
use Swoft\Server\ServerEvent; | ||
use Swoft\Session\Session; | ||
|
||
/** | ||
* Class PipeMessageListener | ||
* | ||
* @Listener(ServerEvent::PIPE_MESSAGE) | ||
*/ | ||
class PipeMessageListener implements EventHandlerInterface | ||
{ | ||
/** | ||
* @param EventInterface $event | ||
*/ | ||
public function handle(EventInterface $event): void | ||
{ | ||
if (!$message = $event->getTarget()) { | ||
return; | ||
} | ||
|
||
// Don't handle on data is invalid | ||
$data = (array)json_decode($message, true); | ||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
return; | ||
} | ||
|
||
// Ensure is tcp notify message | ||
if (!isset($data['from']) || $data['from'] !== 'tcpServer') { | ||
return; | ||
} | ||
|
||
// Handle | ||
if (isset($data['event'])) { | ||
$eventName = (string)$data['event']; | ||
|
||
/** @see CloseListener::onClose() */ | ||
if ($eventName === 'onClose') { | ||
$this->handleOnClose($data, $event->getParam(0)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* @param int $srcWID | ||
*/ | ||
protected function handleOnClose(array $data, int $srcWID): void | ||
{ | ||
$sid = $data['sid']; | ||
|
||
if (Session::has($sid)) { | ||
CLog::info("PipeMessage: destroy tcp connection for fd=$sid fromWID=$srcWID"); | ||
Session::destroy($sid); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Swoft\WebSocket\Server\Listener; | ||
|
||
use Swoft\Event\Annotation\Mapping\Listener; | ||
use Swoft\Event\EventHandlerInterface; | ||
use Swoft\Event\EventInterface; | ||
use Swoft\Log\Helper\CLog; | ||
use Swoft\Server\ServerEvent; | ||
use Swoft\Session\Session; | ||
|
||
/** | ||
* Class PipeMessageListener | ||
* | ||
* @Listener(ServerEvent::PIPE_MESSAGE) | ||
*/ | ||
class PipeMessageListener implements EventHandlerInterface | ||
{ | ||
/** | ||
* @param EventInterface $event | ||
*/ | ||
public function handle(EventInterface $event): void | ||
{ | ||
if (!$message = $event->getTarget()) { | ||
return; | ||
} | ||
|
||
$data = (array)json_decode($message, true); | ||
|
||
// Don't handle on data is invalid | ||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
return; | ||
} | ||
|
||
// Ensure is websocket notify message | ||
if (!isset($data['from']) || $data['from'] !== 'wsServer') { | ||
return; | ||
} | ||
|
||
// Handle | ||
if (isset($data['event'])) { | ||
$eventName = (string)$data['event']; | ||
|
||
/** @see \Swoft\WebSocket\Server\Swoole\CloseListener::onClose() */ | ||
if ($eventName === 'onClose') { | ||
$this->handleClose($data, $event->getParam(0)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* @param int $srcWID | ||
*/ | ||
protected function handleClose(array $data, int $srcWID): void | ||
{ | ||
$sid = $data['sid']; | ||
if (Session::has($sid)) { | ||
CLog::info("PipeMessage: destroy ws connection for fd=$sid fromWID=$srcWID"); | ||
Session::destroy($sid); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,6 @@ | |
* Class CloseListener | ||
* | ||
* @since 2.0 | ||
* | ||
* @Bean() | ||
*/ | ||
class CloseListener implements CloseInterface | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters