|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * シンプルソケットのパラレルクラスのファイル |
| 4 | + * |
| 5 | + * REST-APIサーバーのメイン処理クラスへ渡すためのクラスファイル |
| 6 | + */ |
| 7 | + |
| 8 | +namespace App\ParallelClass; |
| 9 | + |
| 10 | +use SocketManager\Library\SimpleSocketGenerator; |
| 11 | +use SocketManager\Library\SimpleSocketTypeEnum; |
| 12 | +use SocketManager\Library\ISimpleSocketUdp; |
| 13 | + |
| 14 | +use App\ContextClass\ContextForSample; |
| 15 | + |
| 16 | + |
| 17 | +/** |
| 18 | + * シンプルソケットのパラレルクラス |
| 19 | + * |
| 20 | + * REST-APIサーバーのメイン処理クラスへ渡すためのクラス |
| 21 | + */ |
| 22 | +class ParallelForSimpleSocket implements IParallelClass |
| 23 | +{ |
| 24 | + private ContextForSample $context; |
| 25 | + private SimpleSocketGenerator $generator; |
| 26 | + |
| 27 | + /** |
| 28 | + * コンストラクタ |
| 29 | + * |
| 30 | + * @param ContextForSample $p_context コンテキストクラスのインスタンス |
| 31 | + */ |
| 32 | + public function __construct($p_context) |
| 33 | + { |
| 34 | + $this->context = $p_context; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * メイン処理の初期化処理 |
| 39 | + * |
| 40 | + * 初期化処理の依存性注入 |
| 41 | + * |
| 42 | + * @return bool true(成功) or false(失敗) |
| 43 | + */ |
| 44 | + public function initMain(): bool |
| 45 | + { |
| 46 | + $this->generator = new SimpleSocketGenerator(SimpleSocketTypeEnum::UDP, null, null, 1000); |
| 47 | + $this->generator->setKeepRunning(function(?ISimpleSocketUdp $p_simple_socket, ContextForSample $p_context) |
| 48 | + { |
| 49 | + $cids = $p_context->getConnectionIdAll(); |
| 50 | + $all_cnt = count($cids); |
| 51 | + $send_data = |
| 52 | + [ |
| 53 | + [ |
| 54 | + 'service' => 'minecraft-parent', |
| 55 | + 'type' => 'user-cnt', |
| 56 | + 'data' => $all_cnt |
| 57 | + ] |
| 58 | + ]; |
| 59 | + $serialize_data = json_encode($send_data); |
| 60 | + $w_ret = $p_simple_socket->sendto('localhost', 15000, $serialize_data); |
| 61 | + if($w_ret === null) |
| 62 | + { |
| 63 | + return; |
| 64 | + } |
| 65 | + else |
| 66 | + if($w_ret === false) |
| 67 | + { |
| 68 | + return; |
| 69 | + } |
| 70 | + }, $this->context); |
| 71 | + |
| 72 | + $w_ret = $this->generator->generate(); |
| 73 | + if($w_ret === null) |
| 74 | + { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * 周期ドリブン処理 |
| 83 | + * |
| 84 | + * イベントループへの依存性注入 |
| 85 | + * |
| 86 | + * @param int $p_cycle_interval REST-APIの周期インターバルタイム(マイクロ秒) |
| 87 | + * @param int $p_alive_interval REST-APIのアライブチェックインターバルタイム(秒) |
| 88 | + * @return bool true(成功) or false(失敗) |
| 89 | + */ |
| 90 | + public function cycleDriven(int $p_cycle_interval, int $p_alive_interval): bool |
| 91 | + { |
| 92 | + // 周期ドリブン |
| 93 | + $ret = $this->generator->cycleDriven($p_cycle_interval); |
| 94 | + if($ret === false) |
| 95 | + { |
| 96 | + $this->generator->shutdownAll(); |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + return true; |
| 101 | + } |
| 102 | +} |
0 commit comments