Commit 1733bb7f by BANice

将口子的逻辑地址保存至数据中,不再保存至redis中,口子状态redis维护不止在线和离线状态,异常等状态

parent ae14f02f
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PublishConfigData">
<component name="PublishConfigData" serverName="cdz">
<serverData>
<paths name="cdz">
<serverdata>
......
......@@ -5,14 +5,20 @@
</component>
<component name="ChangeListManager">
<list default="true" id="00edf348-0453-4317-8d2b-bf5eb8ea4b7a" name="Default" comment="">
<change afterPath="$PROJECT_DIR$/Application/Model/OrderModel.php" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Application/Middleware.php" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Application/Middleware/AuthMiddleware.php" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Application/Middleware/HasMacMiddleware.php" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Lib/Gateway.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/deployment.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/deployment.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Application/Controller.php" beforeDir="false" afterPath="$PROJECT_DIR$/Application/Controller.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Application/Controller/AccessController.php" beforeDir="false" afterPath="$PROJECT_DIR$/Application/Controller/AccessController.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Application/Controller/PortController.php" beforeDir="false" afterPath="$PROJECT_DIR$/Application/Controller/PortController.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Application/Controller/PowerController.php" beforeDir="false" afterPath="$PROJECT_DIR$/Application/Controller/PowerController.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Application/Events.php" beforeDir="false" afterPath="$PROJECT_DIR$/Application/Events.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Application/Model.php" beforeDir="false" afterPath="$PROJECT_DIR$/Application/Model.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Lib/Log.php" beforeDir="false" afterPath="$PROJECT_DIR$/Lib/Log.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/start.php" beforeDir="false" afterPath="$PROJECT_DIR$/start.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Lib/port/Port.php" beforeDir="false" afterPath="$PROJECT_DIR$/Lib/port/Port.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Route/Api.php" beforeDir="false" afterPath="$PROJECT_DIR$/Route/Api.php" afterDir="false" />
<change beforePath="$PROJECT_DIR$/vendor/workerman/workerman/Protocols/Bintohex.php" beforeDir="false" afterPath="$PROJECT_DIR$/vendor/workerman/workerman/Protocols/Bintohex.php" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="TRACKING_ENABLED" value="true" />
......@@ -29,102 +35,101 @@
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file leaf-file-name="PowerController.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Application/Controller/PowerController.php">
<file leaf-file-name="Api.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Route/Api.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="195">
<caret line="19" column="17" selection-start-line="19" selection-start-column="17" selection-end-line="19" selection-end-column="17" />
<state relative-caret-position="5">
<caret line="18" column="31" lean-forward="true" selection-start-line="18" selection-start-column="31" selection-end-line="18" selection-end-column="31" />
<folding>
<element signature="e#126#153#0#PHP" expanded="true" />
<element signature="e#113#155#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file leaf-file-name="OrderModel.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Application/Model/OrderModel.php">
<file leaf-file-name="Controller.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Application/Controller.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="240">
<caret line="21" column="1" selection-start-line="21" selection-start-column="1" selection-end-line="21" selection-end-column="1" />
<state relative-caret-position="365">
<caret line="325" lean-forward="true" selection-start-line="325" selection-end-line="325" />
<folding>
<element signature="e#119#131#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file leaf-file-name="RedisKey.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Lib/redis/RedisKey.php">
<file leaf-file-name="PortController.php" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/Application/Controller/PortController.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="435">
<caret line="34" column="1" selection-start-line="34" selection-start-column="1" selection-end-line="34" selection-end-column="1" />
<state relative-caret-position="413">
<caret line="103" column="28" lean-forward="true" selection-start-line="103" selection-start-column="28" selection-end-line="103" selection-end-column="28" />
<folding>
<element signature="e#131#158#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file leaf-file-name="Events.php" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/Application/Events.php">
<file leaf-file-name="RedisKey.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Lib/redis/RedisKey.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="372">
<caret line="71" column="8" selection-start-line="71" selection-start-column="8" selection-end-line="71" selection-end-column="8" />
<folding>
<element signature="e#94#106#0#PHP" expanded="true" />
</folding>
<state relative-caret-position="270">
<caret line="23" column="10" selection-start-line="23" selection-start-column="10" selection-end-line="23" selection-end-column="10" />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="Log.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Lib/Log.php">
<file leaf-file-name="Port.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Lib/port/Port.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="567">
<caret line="53" column="74" lean-forward="true" selection-start-line="53" selection-start-column="74" selection-end-line="53" selection-end-column="74" />
<folding>
<element signature="e#111#151#0#PHP" expanded="true" />
</folding>
<state relative-caret-position="275">
<caret line="81" column="9" lean-forward="true" selection-start-line="81" selection-start-column="9" selection-end-line="81" selection-end-column="9" />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="start.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/start.php">
<file leaf-file-name="Model.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Application/Model.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="507">
<caret line="55" column="40" lean-forward="true" selection-start-line="55" selection-start-column="40" selection-end-line="55" selection-end-column="40" />
<state relative-caret-position="230">
<caret line="83" column="66" lean-forward="true" selection-start-line="83" selection-start-column="66" selection-end-line="83" selection-end-column="66" />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="Logger.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/vendor/monolog/monolog/src/Monolog/Logger.php">
<file leaf-file-name="AccessController.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Application/Controller/AccessController.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="300">
<caret line="322" column="12" selection-start-line="322" selection-start-column="12" selection-end-line="322" selection-end-column="12" />
<state relative-caret-position="763">
<caret line="125" lean-forward="true" selection-start-line="125" selection-end-line="125" />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="mbstring.php" pinned="false" current-in-tab="false">
<entry file="jar://$APPLICATION_HOME_DIR$/plugins/php/lib/php.jar!/stubs/mbstring/mbstring.php">
<file leaf-file-name="Bintohex.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/vendor/workerman/workerman/Protocols/Bintohex.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="525">
<caret line="35" column="9" selection-start-line="35" selection-start-column="9" selection-end-line="35" selection-end-column="9" />
<state relative-caret-position="390">
<caret line="31" column="46" lean-forward="true" selection-start-line="31" selection-start-column="46" selection-end-line="31" selection-end-column="46" />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="Model.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Application/Model.php">
<file leaf-file-name="PortModel.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Application/Model/PortModel.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1500">
<caret line="105" selection-start-line="105" selection-end-line="105" />
<state relative-caret-position="120">
<caret line="13" column="28" selection-start-line="13" selection-start-column="28" selection-end-line="13" selection-end-column="28" />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="README.md" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/vendor/workerman/mysql/README.md">
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
<state split_layout="SPLIT">
<first_editor />
<second_editor />
<file leaf-file-name="CertifyController.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Application/Controller/CertifyController.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="405">
<caret line="33" column="10" selection-start-line="33" selection-start-column="10" selection-end-line="33" selection-end-column="10" />
</state>
</provider>
</entry>
......@@ -137,10 +142,12 @@
<find>getInstance()-&gt;</find>
<find>lenght</find>
<find>deb</find>
<find>outTradeNo</find>
</findStrings>
<replaceStrings>
<replace>Env</replace>
<replace>length</replace>
<replace>orderId</replace>
</replaceStrings>
</component>
<component name="Git.Settings">
......@@ -163,23 +170,28 @@
<option value="$PROJECT_DIR$/Application/Model/PortModel.php" />
<option value="$PROJECT_DIR$/Application/Controller/CapacityController.php" />
<option value="$PROJECT_DIR$/Lib/redis/RedisKey.php" />
<option value="$PROJECT_DIR$/vendor/workerman/workerman/Protocols/Bintohex.php" />
<option value="$PROJECT_DIR$/Lib/Response.php" />
<option value="$PROJECT_DIR$/Application/Controller.php" />
<option value="$PROJECT_DIR$/Lib/port/Port.php" />
<option value="$PROJECT_DIR$/Route/Api.php" />
<option value="$PROJECT_DIR$/Application/Controller/PortController.php" />
<option value="$PROJECT_DIR$/Application/Controller/powerController.php" />
<option value="$PROJECT_DIR$/.env" />
<option value="$PROJECT_DIR$/.example" />
<option value="$PROJECT_DIR$/.gitignore" />
<option value="$PROJECT_DIR$/Application/Controller/AccessController.php" />
<option value="$PROJECT_DIR$/Application/Model/OrderModel.php" />
<option value="$PROJECT_DIR$/Application/Model.php" />
<option value="$PROJECT_DIR$/Application/Controller/PowerController.php" />
<option value="$PROJECT_DIR$/start.php" />
<option value="$PROJECT_DIR$/Lib/Log.php" />
<option value="$PROJECT_DIR$/Lib/Gateway.php" />
<option value="$PROJECT_DIR$/Application/Controller/PowerController.php" />
<option value="$PROJECT_DIR$/Application/Middleware.php" />
<option value="$PROJECT_DIR$/Application/Middleware/AuthMiddleware.php" />
<option value="$PROJECT_DIR$/Application/Middleware/hasMacMiddleware.php" />
<option value="$PROJECT_DIR$/Application/Events.php" />
<option value="$PROJECT_DIR$/Application/Middleware/HasMacMiddleware.php" />
<option value="$PROJECT_DIR$/Route/Api.php" />
<option value="$PROJECT_DIR$/vendor/workerman/workerman/Protocols/Bintohex.php" />
<option value="$PROJECT_DIR$/Lib/port/Port.php" />
<option value="$PROJECT_DIR$/Application/Controller/AccessController.php" />
<option value="$PROJECT_DIR$/Application/Controller.php" />
<option value="$PROJECT_DIR$/Application/Controller/PortController.php" />
</list>
</option>
</component>
......@@ -203,8 +215,8 @@
</include_path>
</component>
<component name="ProjectFrameBounds">
<option name="x" value="90" />
<option name="y" value="46" />
<option name="x" value="170" />
<option name="y" value="26" />
<option name="width" value="1597" />
<option name="height" value="1057" />
</component>
......@@ -237,13 +249,18 @@
<path>
<item name="cdz_gateway" type="b2602c69:ProjectViewProjectNode" />
<item name="cdz_gateway" type="462c0819:PsiDirectoryNode" />
<item name="Application" type="462c0819:PsiDirectoryNode" />
<item name="Model" type="462c0819:PsiDirectoryNode" />
<item name="Lib" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="cdz_gateway" type="b2602c69:ProjectViewProjectNode" />
<item name="cdz_gateway" type="462c0819:PsiDirectoryNode" />
<item name="Lib" type="462c0819:PsiDirectoryNode" />
<item name="port" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="cdz_gateway" type="b2602c69:ProjectViewProjectNode" />
<item name="cdz_gateway" type="462c0819:PsiDirectoryNode" />
<item name="Route" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
......@@ -254,19 +271,20 @@
</component>
<component name="PropertiesComponent">
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="php.override.implement.member.chooser.php.doc" value="NONE" />
<property name="run.code.analysis.last.selected.profile" value="pProject Default" />
<property name="settings.editor.selected.configurable" value="reference.settings.ide.settings.file-colors" />
<property name="settings.editor.selected.configurable" value="reference.webide.settings.project.settings.php" />
</component>
<component name="RecentsManager">
<key name="MoveFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/Application" />
<recent name="$PROJECT_DIR$" />
</key>
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/Lib" />
<recent name="$PROJECT_DIR$/vendor/workerman/workerman/Protocols" />
<recent name="$PROJECT_DIR$/Application" />
</key>
<key name="MoveFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/Application" />
<recent name="$PROJECT_DIR$" />
</key>
</component>
<component name="RunDashboard">
<option name="ruleStates">
......@@ -297,12 +315,12 @@
<workItem from="1526831317731" duration="547000" />
<workItem from="1526862727769" duration="6095000" />
<workItem from="1526871407971" duration="34639000" />
<workItem from="1527058489501" duration="3453000" />
<workItem from="1527058489501" duration="32851000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="87623000" />
<option name="totallyTimeSpent" value="117021000" />
</component>
<component name="TodoView">
<todo-panel id="selected-file">
......@@ -314,19 +332,20 @@
</todo-panel>
</component>
<component name="ToolWindowManager">
<frame x="90" y="46" width="1597" height="1057" extended-state="0" />
<frame x="170" y="26" width="1597" height="1057" extended-state="0" />
<editor active="true" />
<layout>
<window_info anchor="bottom" id="TODO" order="6" weight="0.32977587" />
<window_info anchor="bottom" id="Event Log" order="7" side_tool="true" />
<window_info anchor="bottom" id="Database Changes" order="8" show_stripe_button="false" />
<window_info anchor="bottom" id="File Transfer" order="8" visible="true" weight="0.32977587" />
<window_info anchor="bottom" id="File Transfer" order="8" weight="0.32977587" />
<window_info anchor="bottom" id="Version Control" order="7" />
<window_info anchor="bottom" id="Run" order="2" />
<window_info anchor="bottom" id="Terminal" order="7" weight="0.3211679" />
<window_info anchor="bottom" id="Terminal" order="7" weight="0.32017076" />
<window_info active="true" content_ui="combo" id="Project" order="0" sideWeight="0.9242262" visible="true" weight="0.26495177" />
<window_info anchor="bottom" id="Docker" order="8" show_stripe_button="false" />
<window_info anchor="right" id="Database" order="3" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info id="Favorites" order="2" sideWeight="0.075773746" side_tool="true" weight="0.26173633" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
......@@ -336,7 +355,6 @@
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Find" order="1" />
</layout>
</component>
<component name="TypeScriptGeneratedFilesManager">
......@@ -347,38 +365,10 @@
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<option name="time" value="1" />
<option name="time" value="2" />
</breakpoint-manager>
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/start.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="495">
<caret line="39" lean-forward="true" selection-start-line="39" selection-end-line="39" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Controller/CertifyController.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="315">
<caret line="26" selection-start-line="26" selection-end-line="26" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Lib/Log.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1020">
<caret line="75" selection-start-line="75" selection-end-line="75" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Lib/log/LogRecord.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="90">
<caret line="11" column="15" selection-start-line="11" selection-start-column="15" selection-end-line="11" selection-end-column="15" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Controller.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1890">
......@@ -420,13 +410,6 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/start_businessworker.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="270">
<caret line="30" lean-forward="true" selection-start-line="30" selection-end-line="30" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/start_register.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="195">
......@@ -434,13 +417,6 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Lib/Route.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="743">
<caret line="124" selection-start-line="124" selection-end-line="124" />
</state>
</provider>
</entry>
<entry file="jar://$APPLICATION_HOME_DIR$/plugins/php/lib/php.jar!/stubs/standard/standard_8.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="294">
......@@ -455,13 +431,6 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Lib/Env.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-364">
<caret line="11" column="6" selection-start-line="11" selection-start-column="6" selection-end-line="11" selection-end-column="6" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Lib/Request.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="720">
......@@ -500,13 +469,6 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Model/PortModel.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="120">
<caret line="13" column="25" lean-forward="true" selection-start-line="13" selection-start-column="25" selection-end-line="13" selection-end-column="25" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/start_gateway.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="300">
......@@ -528,190 +490,258 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Lib/port/Port.php">
<entry file="file://$PROJECT_DIR$/Lib/Response.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-130">
<caret line="53" lean-forward="true" selection-start-line="53" selection-end-line="53" />
<state relative-caret-position="304">
<caret line="82" lean-forward="true" selection-start-line="82" selection-end-line="82" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Route/Api.php">
<entry file="file://$PROJECT_DIR$/Lib/Crc.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="270">
<caret line="23" column="52" lean-forward="true" selection-start-line="23" selection-start-column="52" selection-end-line="23" selection-end-column="52" />
<state relative-caret-position="1140">
<caret line="81" column="59" selection-start-line="81" selection-start-column="59" selection-end-line="81" selection-end-column="59" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Controller/PortController.php">
<entry file="file://$PROJECT_DIR$/.example">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="375">
<caret line="30" column="11" selection-start-line="30" selection-start-column="11" selection-end-line="30" selection-end-column="11" />
<state relative-caret-position="210">
<caret line="14" column="17" selection-start-line="14" selection-start-column="17" selection-end-line="14" selection-end-column="17" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Lib/Response.php">
<entry file="file://$PROJECT_DIR$/Application/Controller/CapacityController.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="304">
<caret line="82" lean-forward="true" selection-start-line="82" selection-end-line="82" />
<state relative-caret-position="375">
<caret line="30" column="10" selection-start-line="30" selection-start-column="10" selection-end-line="30" selection-end-column="10" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Lib/Crc.php">
<entry file="file://$PROJECT_DIR$/vendor/workerman/mysql/src/Connection.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1140">
<caret line="81" column="59" selection-start-line="81" selection-start-column="59" selection-end-line="81" selection-end-column="59" />
<state relative-caret-position="135">
<caret line="11" column="6" selection-start-line="11" selection-start-column="6" selection-end-line="11" selection-end-column="6" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/vendor/workerman/workerman/Protocols/Bintohex.php">
<entry file="file://$PROJECT_DIR$/vendor/workerman/mysql/README.md">
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
<state split_layout="SPLIT">
<first_editor />
<second_editor />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Model/OrderModel.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="704">
<caret line="72" selection-start-line="72" selection-end-line="72" />
<state relative-caret-position="240">
<caret line="21" column="1" selection-start-line="21" selection-start-column="1" selection-end-line="21" selection-end-column="1" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/composer.json">
<entry file="jar://$APPLICATION_HOME_DIR$/plugins/php/lib/php.jar!/stubs/mbstring/mbstring.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="195">
<caret line="13" selection-start-line="13" selection-end-line="13" />
<state relative-caret-position="525">
<caret line="35" column="9" selection-start-line="35" selection-start-column="9" selection-end-line="35" selection-end-column="9" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/.example">
<entry file="file://$PROJECT_DIR$/vendor/monolog/monolog/src/Monolog/Logger.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="210">
<caret line="14" column="17" selection-start-line="14" selection-start-column="17" selection-end-line="14" selection-end-column="17" />
<state relative-caret-position="300">
<caret line="322" column="12" selection-start-line="322" selection-start-column="12" selection-end-line="322" selection-end-column="12" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/.gitignore">
<entry file="file://$PROJECT_DIR$/Lib/Log.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="30">
<caret line="2" column="6" selection-start-line="2" selection-start-column="6" selection-end-line="2" selection-end-column="6" />
<state relative-caret-position="567">
<caret line="53" column="74" selection-start-line="53" selection-start-column="74" selection-end-line="53" selection-end-column="74" />
<folding>
<element signature="e#111#151#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Controller/AccessController.php">
<entry file="file://$PROJECT_DIR$/Lib/Env.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="516">
<caret line="110" column="29" lean-forward="true" selection-start-line="110" selection-start-column="29" selection-end-line="110" selection-end-column="29" />
<state relative-caret-position="15">
<caret line="11" column="6" selection-start-line="11" selection-start-column="6" selection-end-line="11" selection-end-column="6" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/.env">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="180">
<caret line="12" column="23" selection-start-line="12" selection-start-column="23" selection-end-line="12" selection-end-column="23" />
<state relative-caret-position="30">
<caret line="2" column="5" selection-start-line="2" selection-start-column="5" selection-end-line="2" selection-end-column="5" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Controller/CapacityController.php">
<entry file="file://$PROJECT_DIR$/Application/start_businessworker.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="375">
<caret line="30" column="10" selection-start-line="30" selection-start-column="10" selection-end-line="30" selection-end-column="10" />
<state relative-caret-position="285">
<caret line="30" lean-forward="true" selection-start-line="30" selection-end-line="30" />
<folding>
<element signature="e#429#451#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Controller/CertifyController.php">
<entry file="file://$PROJECT_DIR$/Lib/Gateway.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="405">
<caret line="33" column="10" selection-start-line="33" selection-start-column="10" selection-end-line="33" selection-end-column="10" />
<state relative-caret-position="273">
<caret line="28" column="44" lean-forward="true" selection-start-line="28" selection-start-column="44" selection-end-line="28" selection-end-column="44" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/vendor/workerman/mysql/src/Connection.php">
<entry file="file://$PROJECT_DIR$/Application/Controller/PowerController.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="135">
<caret line="11" column="6" selection-start-line="11" selection-start-column="6" selection-end-line="11" selection-end-column="6" />
<state relative-caret-position="378">
<caret line="73" selection-start-line="73" selection-end-line="73" />
<folding>
<element signature="e#126#153#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Controller.php">
<entry file="file://$PROJECT_DIR$/start.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="2100">
<caret line="149" column="8" selection-start-line="149" selection-start-column="8" selection-end-line="149" selection-end-column="8" />
<state relative-caret-position="230">
<caret line="56" column="64" selection-start-line="56" selection-start-column="64" selection-end-line="56" selection-end-column="64" />
<folding>
<element signature="e#119#131#0#PHP" expanded="true" />
<element signature="e#94#106#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/vendor/workerman/mysql/README.md">
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
<state split_layout="SPLIT">
<first_editor />
<second_editor />
<entry file="file://$PROJECT_DIR$/composer.json">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="180">
<caret line="12" column="3" lean-forward="true" selection-end-line="14" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Model.php">
<entry file="file://$PROJECT_DIR$/.gitignore">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1500">
<caret line="105" selection-start-line="105" selection-end-line="105" />
<state relative-caret-position="30">
<caret line="2" selection-start-line="2" selection-end-line="2" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Model/OrderModel.php">
<entry file="file://$PROJECT_DIR$/Application/Middleware.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="240">
<caret line="21" column="1" selection-start-line="21" selection-start-column="1" selection-end-line="21" selection-end-column="1" />
<caret line="21" column="1" lean-forward="true" selection-start-line="21" selection-start-column="1" selection-end-line="21" selection-end-column="1" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Controller/PowerController.php">
<entry file="file://$PROJECT_DIR$/Application/Middleware/AuthMiddleware.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="195">
<caret line="19" column="17" selection-start-line="19" selection-start-column="17" selection-end-line="19" selection-end-column="17" />
<state relative-caret-position="270">
<caret line="23" column="1" lean-forward="true" selection-start-line="23" selection-start-column="1" selection-end-line="23" selection-end-column="1" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Lib/Route.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1335">
<caret line="94" column="20" selection-start-line="94" selection-start-column="20" selection-end-line="94" selection-end-column="20" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Events.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="226">
<caret line="102" lean-forward="true" selection-start-line="102" selection-end-line="102" />
<folding>
<element signature="e#126#153#0#PHP" expanded="true" />
<element signature="e#94#106#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Middleware/HasMacMiddleware.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="225">
<caret line="20" lean-forward="true" selection-start-line="20" selection-end-line="20" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Route/Api.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="5">
<caret line="18" column="31" lean-forward="true" selection-start-line="18" selection-start-column="31" selection-end-line="18" selection-end-column="31" />
<folding>
<element signature="e#113#155#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Controller/CertifyController.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="405">
<caret line="33" column="10" selection-start-line="33" selection-start-column="10" selection-end-line="33" selection-end-column="10" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Lib/redis/RedisKey.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="435">
<caret line="34" column="1" selection-start-line="34" selection-start-column="1" selection-end-line="34" selection-end-column="1" />
<state relative-caret-position="270">
<caret line="23" column="10" selection-start-line="23" selection-start-column="10" selection-end-line="23" selection-end-column="10" />
</state>
</provider>
</entry>
<entry file="jar://$APPLICATION_HOME_DIR$/plugins/php/lib/php.jar!/stubs/mbstring/mbstring.php">
<entry file="file://$PROJECT_DIR$/Application/Model/PortModel.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="525">
<caret line="35" column="9" selection-start-line="35" selection-start-column="9" selection-end-line="35" selection-end-column="9" />
<state relative-caret-position="120">
<caret line="13" column="28" selection-start-line="13" selection-start-column="28" selection-end-line="13" selection-end-column="28" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/start.php">
<entry file="file://$PROJECT_DIR$/Application/Model.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="507">
<caret line="55" column="40" lean-forward="true" selection-start-line="55" selection-start-column="40" selection-end-line="55" selection-end-column="40" />
<state relative-caret-position="230">
<caret line="83" column="66" lean-forward="true" selection-start-line="83" selection-start-column="66" selection-end-line="83" selection-end-column="66" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/vendor/monolog/monolog/src/Monolog/Logger.php">
<entry file="file://$PROJECT_DIR$/vendor/workerman/workerman/Protocols/Bintohex.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="300">
<caret line="322" column="12" selection-start-line="322" selection-start-column="12" selection-end-line="322" selection-end-column="12" />
<state relative-caret-position="390">
<caret line="31" column="46" lean-forward="true" selection-start-line="31" selection-start-column="46" selection-end-line="31" selection-end-column="46" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Lib/Log.php">
<entry file="file://$PROJECT_DIR$/Lib/port/Port.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="567">
<caret line="53" column="74" lean-forward="true" selection-start-line="53" selection-start-column="74" selection-end-line="53" selection-end-column="74" />
<state relative-caret-position="275">
<caret line="81" column="9" lean-forward="true" selection-start-line="81" selection-start-column="9" selection-end-line="81" selection-end-column="9" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Controller/AccessController.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="763">
<caret line="125" lean-forward="true" selection-start-line="125" selection-end-line="125" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Controller.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="365">
<caret line="325" lean-forward="true" selection-start-line="325" selection-end-line="325" />
<folding>
<element signature="e#111#151#0#PHP" expanded="true" />
<element signature="e#119#131#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Application/Events.php">
<entry file="file://$PROJECT_DIR$/Application/Controller/PortController.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="372">
<caret line="71" column="8" selection-start-line="71" selection-start-column="8" selection-end-line="71" selection-end-column="8" />
<state relative-caret-position="413">
<caret line="103" column="28" lean-forward="true" selection-start-line="103" selection-start-column="28" selection-end-line="103" selection-end-column="28" />
<folding>
<element signature="e#94#106#0#PHP" expanded="true" />
<element signature="e#131#158#0#PHP" expanded="true" />
</folding>
</state>
</provider>
......
......@@ -340,6 +340,16 @@ class Controller
$this->plc = $plc;
}
/**
* 获取网关下的口子mac地址和id的数组
* @return array
*/
protected function getPortMacId()
{
return isset($_SESSION['port_mac_id']) ? $_SESSION['port_mac_id'] : [];
}
}
\ No newline at end of file
......@@ -37,8 +37,6 @@ class AccessController extends Controller
*/
public function access()
{
if ($_SESSION['certified'] === true) {
$gatewayModel = new GatewayModel();
......@@ -48,16 +46,17 @@ class AccessController extends Controller
if ($gatewayId != false) {
//tcp绑定uid
Gateway::bindUid($this->clientId, $mac);
$_SESSION['gateway_id'] = $gatewayId;
$portModel = new PortModel();
//网关id下所有的口子mac地址和id
$portMacId = $portModel->getAllColumns(['mac_address', 'id'], 'gateway_id = ' . $gatewayId);
if (count($portMacId) !== 0) {
if (count($portMacId) !== 0) { //网关下的口子mac和id缓存到session中
//网关下的端口id和mac注入到session当中,静态缓存
foreach ($portMacId as $item) {
......@@ -68,6 +67,7 @@ class AccessController extends Controller
$sessionId = '';
for ($i = 0; $i < 4; $i++) {
$hex = dechex(rand(0, 255));
$sessionId .= $hex;
......@@ -80,11 +80,6 @@ class AccessController extends Controller
}
} else {
Gateway::closeClient($this->clientId);
}
}
/**
......@@ -96,94 +91,103 @@ class AccessController extends Controller
$startLogicNum = $this->get('start_num');
$count = hexdec($this->get('count'));
$totalCount = $this->get('total_count');
$macs = substr($this->message, $this->plc, $count * 16);
$startLogicNumDec = hexdec($startLogicNum);
$this->plc += $count * 16;
$count = $this->get('count');
$macsState = substr($this->message, $this->plc);
$countDec = hexdec($count);
$macsState = str_split($macsState, 2);
$totalCount = $this->get('total_count');
$macsStateArray = [];
$macs = substr($this->message, $this->plc, $countDec * 16);
foreach ($macsState as $value) {
array_push($macsStateArray, hexdec($value));
}
$this->plc += $countDec * 16;
$macs = str_split($macs, 16);
$macsState = substr($this->message, $this->plc);
$ids = [];
$stateArray = $this->parseState($macsState);
$onlineIds = [];
$macLogicArray = $this->parseMacs($macs, $startLogicNumDec);
$offlineIds = [];
$portModel = new PortModel();
$i = 0;
//依据mac地址和session中存储的mac和id的数组获取到对应的id
//根据状态为设置端口id的状态
$portMacId = isset($_SESSION['port_mac_id']) ? $_SESSION['port_mac_id'] : [];
foreach ($macs as $mac) {
$port = new port();
if (isset($_SESSION['port_mac_id'][$mac])) {
//$macIdArray[$mac] = $_SESSION['port_mac_id'][$mac];
$portIdState = [];
$id = $_SESSION['port_mac_id'][$mac];
foreach ($macLogicArray as $mac => $logic)//将端口状态和端口逻辑地址更新至数据库中
{
array_push($ids, $id);
//在线离线状态判断
if ($macsStateArray[$i++] == 1) {
array_push($onlineIds, $id);
$portModel->update([
} else {
'state' => $stateArray[$i],
'logic_num' => $logic,
], 'mac_address = ' . "'$mac'");
array_push($offlineIds, $id);
if(isset($portMacId[$mac]))
{
$portIdState[$portMacId[$mac]] = $stateArray[$i];
}
}
$i++;
}
$idLogicArray = [];
$port->setPortState($portIdState); //改变口子状态
$logicId = $startLogicNum;
//端口id计算逻辑id
foreach ($ids as $id) {
if(isset($_SESSION['port_mac_id'])) {
$portMacId = $_SESSION['port_mac_id'];
$idLogicArray[$id] = $logicId;
//根据起始逻辑地址计算逻辑地址
$logicId = sprintf('%02x', hexdec($logicId) + 1);
}
$port = new Port();
return $this->response->send('03', $startLogicNum . $count . $totalCount);
$port->pipe();
}
$port->register($idLogicArray, $_SESSION['gateway_id']);
$port->online($onlineIds);
/**
* @param string $macs
* @param int $logicStart
* @return array
*/
private function parseMacs(string $macs, int $logicStart)
{
$port->offline($offlineIds);
$macs = str_split($macs, 16);
$port->exec();
$macLogic = [];
$count = sprintf('%02x',$count);
foreach ($macs as $mac) {
$macLogic[$mac] = sprintf('%02x', $logicStart++);
}
var_dump($count);
return $macLogic;
var_dump($totalCount);
}
$this->response->send('03', $startLogicNum . $count . $totalCount);
/**
* 解析口子状态
* @param string $state
* @return array
*/
private function parseState(string $state)
{
$state = str_split($state, 2);
return array_map(function ($hex) {
return hexdec($hex);
}, $state);
}
......
......@@ -28,41 +28,44 @@ class PortController extends Controller
/**
* command 08
* 端口上线
*/
public function online()
{
$count = $this->get('count');
$portsLogicNum = str_split(substr($this->message,$this->plc,$count),2);
$countDec = hexdec($count) ;
$this->plc+=$count;
$portLogic =substr($this->message,$this->plc,$countDec*2);
$macs = str_split(substr($this->message,$this->plc),16);
$this->plc += $countDec*2;
$portMac = substr($this->message,$this->plc);
$portsIdLogic = [];
$portLogicArray = str_split($portLogic,2);
for($i=0;$i<$count;$i++)
{
if(isset($_SESSION['port_mac_id'][$macs[$i]]))
{
$id = $_SESSION['port_mac_id'][$macs[$i]];
$portMacArray = str_split($portMac,16);
$portsIdLogic[$id] = $portsLogicNum[$i];
}
}
$portMacId = $this->getPortMacId();
$port = new Port();
$port->pipe();
$onlinePortId = [];
$port->register($portsIdLogic,$_SESSION['gateway_id']);
foreach ($portMacArray as $mac)
{
$port->online(array_keys($portsIdLogic));
//todo 更新逻辑地址数据中
if(isset($portMacId[$mac]))
{
array_push($onlinePortId,$portMacId[$mac]);
}
$port->exec();
}
$port->online($onlinePortId);
}
......@@ -72,19 +75,42 @@ class PortController extends Controller
public function offline()
{
$gatewayId = $_SESSION['gateway_id'];
$count = $this->get('count');
$logics = substr($this->message,$this->plc);
$countDec = hexdec($count) ;
$portLogic =substr($this->message,$this->plc,$countDec*2);
$this->plc += $countDec*2;
$portsLogic = str_split($logics,2);
$portMac = substr($this->message,$this->plc);
$portLogicArray = str_split($portLogic,2);
$portMacArray = str_split($portMac,16);
$portMacId = $this->getPortMacId();
$port = new Port();
$portsId = $port->getPortId($portsLogic,$gatewayId);
$offlinePortId = [];
foreach ($portMacArray as $mac)
{
//todo 更新逻辑地址数据中
if(isset($portMacId[$mac]))
{
array_push($onlinePortId,$portMacId[$mac]);
}
}
$port->offline($offlinePortId);
var_dump($portsId);
}
......
......@@ -21,11 +21,13 @@ class PowerController extends Controller
'type' => 1,
'logic' => 1,
'outTradeNo' => 6,
'orderId' => 6,
'capacity' => 4,
'state' => 1,
],
'05' => [
'outTradeNo' => 6,
'orderId' => 6,
'logic' => 1,
'state' => 1,
'capacity' => 4
......@@ -48,23 +50,31 @@ class PowerController extends Controller
$type = $this->get('type');
$orderId = $this->get('orderId');
if($type == 1)
{
$logic = $this->get('logic');
$outTradeNo = $this->get('outTradeNo');
$power = $this->get('capacity');
//fixme 修改订单表中,订单的状态位
$state = $this->get('state');
$orderModel = new OrderModel();
$row = $orderModel->update(['state' => 1],'out_trade_no = '.$outTradeNo);
$id = hexdec($orderId);
$row = $orderModel->update(['is_charger' => $state,'start_capacity' => $power],'id = '.$id);
//todo websocket推送给前段开始充电信息,如果未发现当前订单id连接则存入消息队列当中
var_dump($row);
}
$this->response->send('14',$outTradeNo);
$this->response->send('14',$orderId);
}
......@@ -77,7 +87,9 @@ class PowerController extends Controller
{
$outTradeNo = $this->get('outTradeNo');
$orderId = $this->get('orderId');
$id = hexdec($orderId);
$logic = $this->get('logic');
......@@ -85,10 +97,12 @@ class PowerController extends Controller
$capacity = $this->get('capacity');
$orderModel = new OrderModel();
$orderModel->update(['is_stop' => $state, 'end_capacity' => $capacity],' id = '.$id);
//todo 存储数据库
$this->response->send('15',$outTradeNo);
$this->response->send('15',$orderId);
}
......@@ -108,7 +122,6 @@ class PowerController extends Controller
$powerData = str_split($powerData,4);
$this->plc+=hexdec($count) * 4;
$capacity = substr($this->message,$this->plc);
......
......@@ -93,11 +93,8 @@ class Events
$command = substr($message,6,2);
var_dump($command);
$route = Api::getRoute($command);
if( $route instanceof Route)
{
$controllerName = $route->getController();
......@@ -111,6 +108,20 @@ class Events
$message = mb_strtoupper($message);
$middleware = $route->getMiddleware();
foreach ($middleware as $item) {
if($item instanceof \Application\Middleware)
{
if($item->handle($clientId,$message) === false)
{
Gateway::closeCurrentClient();
}
}
}
$controller = new $controllerName($clientId,$message,$command);
$controller->$method();
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/5/25
* Time: 下午3:24
*/
namespace Application;
abstract class Middleware
{
/**
* @param $clientId
* @param $message
* @return bool
*/
abstract public function handle($clientId,$message) : bool ;
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/5/25
* Time: 下午3:35
*/
namespace Application\Middleware;
use Application\Middleware;
class AuthMiddleware extends Middleware
{
public function handle($clientId, $message): bool
{
return isset($_SESSION['certified']);
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/5/25
* Time: 下午3:37
*/
namespace Application\Middleware;
use Application\Middleware;
class HasMacMiddleware extends Middleware
{
public function handle($clientId, $message): bool
{
return isset($_SESSION['gateway_mac']);
}
}
\ No newline at end of file
<?php
namespace Lib;
//namespace Lib;
//use \Exception;
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* 数据发送相关
* @version 3.0.0
*/
class Gateway
{
/**
* gateway 实例
*
* @var object
*/
protected static $businessWorker = null;
/**
* 注册中心地址
*
* @var string
*/
//public static $registerAddress = '10.200.2.117:1239';
public static $registerAddress = 'webapppush.default:1240';
//public static $registerAddress = 'cdzregister.default.svc.cluster.local:1239';
/**
* 秘钥
* @var string
*/
public static $secretKey = '';
/**
* 链接超时时间
* @var int
*/
public static $connectTimeout = 3;
/**
* 与Gateway是否是长链接
* @var bool
*/
public static $persistentConnection = false;
public static function getUidFirstClientId($uid)
{
$result = false;
if(Gateway::isUidOnline($uid))
{
$clientsId = Gateway::getClientIdByUid($uid);
$result = $clientsId[0];
}
return $result;
}
/**
* 向所有客户端连接(或者 client_id_array 指定的客户端连接)广播消息
*
* @param string $message 向客户端发送的消息
* @param array $client_id_array 客户端 id 数组
* @param array $exclude_client_id 不给这些client_id发
* @param bool $raw 是否发送原始数据(即不调用gateway的协议的encode方法)
* @return void
*/
public static function sendToAll($message, $client_id_array = null, $exclude_client_id = null, $raw = false)
{
$gateway_data = GatewayProtocol::$empty;
$gateway_data['cmd'] = GatewayProtocol::CMD_SEND_TO_ALL;
$gateway_data['body'] = $message;
if ($raw) {
$gateway_data['flag'] |= GatewayProtocol::FLAG_NOT_CALL_ENCODE;
}
if ($exclude_client_id) {
if (!is_array($exclude_client_id)) {
$exclude_client_id = array($exclude_client_id);
}
if ($client_id_array) {
$exclude_client_id = array_flip($exclude_client_id);
}
}
if ($client_id_array) {
if (!is_array($client_id_array)) {
echo new \Exception('bad $client_id_array:'.var_export($client_id_array, true));
return;
}
$data_array = array();
foreach ($client_id_array as $client_id) {
if (isset($exclude_client_id[$client_id])) {
continue;
}
$address = Context::clientIdToAddress($client_id);
$key = long2ip($address['local_ip']) . ":{$address['local_port']}";
$data_array[$key][$address['connection_id']] = $address['connection_id'];
}
foreach ($data_array as $addr => $connection_id_list) {
$the_gateway_data = $gateway_data;
$the_gateway_data['ext_data'] = json_encode(array('connections' => $connection_id_list));
self::sendToGateway($addr, $the_gateway_data);
}
return;
} elseif (empty($client_id_array) && is_array($client_id_array)) {
return;
}
if (!$exclude_client_id) {
return self::sendToAllGateway($gateway_data);
}
$address_connection_array = self::clientIdArrayToAddressArray($exclude_client_id);
// 如果有businessWorker实例,说明运行在workerman环境中,通过businessWorker中的长连接发送数据
if (self::$businessWorker) {
foreach (self::$businessWorker->gatewayConnections as $address => $gateway_connection) {
$gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
json_encode(array('exclude'=> $address_connection_array[$address])) : '';
/** @var TcpConnection $gateway_connection */
$gateway_connection->send($gateway_data);
}
} // 运行在其它环境中,通过注册中心得到gateway地址
else {
$all_addresses = self::getAllGatewayAddressesFromRegister();
if (!$all_addresses) {
throw new Exception('Gateway::getAllGatewayAddressesFromRegister() with registerAddress:' .
self::$registerAddress . ' return ' . var_export($all_addresses, true));
}
foreach ($all_addresses as $address) {
$gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
json_encode(array('exclude'=> $address_connection_array[$address])) : '';
self::sendToGateway($address, $gateway_data);
}
}
}
/**
* 向某个客户端连接发消息
*
* @param int $client_id
* @param string $message
* @return bool
*/
public static function sendToClient($client_id, $message)
{
return self::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_SEND_TO_ONE, $message);
}
/**
* 向当前客户端连接发送消息
*
* @param string $message
* @return bool
*/
public static function sendToCurrentClient($message)
{
return self::sendCmdAndMessageToClient(null, GatewayProtocol::CMD_SEND_TO_ONE, $message);
}
/**
* 判断某个uid是否在线
*
* @param string $uid
* @return int 0|1
*/
public static function isUidOnline($uid)
{
return (int)self::getClientIdByUid($uid);
}
/**
* 判断某个客户端连接是否在线
*
* @param int $client_id
* @return int 0|1
*/
public static function isOnline($client_id)
{
$address_data = Context::clientIdToAddress($client_id);
$address = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
if (isset(self::$businessWorker)) {
if (!isset(self::$businessWorker->gatewayConnections[$address])) {
return 0;
}
}
$gateway_data = GatewayProtocol::$empty;
$gateway_data['cmd'] = GatewayProtocol::CMD_IS_ONLINE;
$gateway_data['connection_id'] = $address_data['connection_id'];
return (int)self::sendAndRecv($address, $gateway_data);
}
/**
* 获取所有在线用户的session,client_id为 key
*
* @param string $group
* @return array
*/
public static function getAllClientInfo($group = null)
{
return self::getAllClientSessions($group);
}
/**
* 获取所有在线用户的session,client_id为 key
*
* @param string $group
* @return array
*/
public static function getAllClientSessions($group = null)
{
$gateway_data = GatewayProtocol::$empty;
if (!$group) {
$gateway_data['cmd'] = GatewayProtocol::CMD_GET_ALL_CLIENT_INFO;
} else {
$gateway_data['cmd'] = GatewayProtocol::CMD_GET_CLINET_INFO_BY_GROUP;
$gateway_data['ext_data'] = $group;
}
$status_data = array();
$all_buffer_array = self::getBufferFromAllGateway($gateway_data);
foreach ($all_buffer_array as $local_ip => $buffer_array) {
foreach ($buffer_array as $local_port => $data) {
if ($data) {
foreach ($data as $connection_id => $session_buffer) {
$client_id = Context::addressToClientId($local_ip, $local_port, $connection_id);
if ($client_id === Context::$client_id) {
$status_data[$client_id] = (array)$_SESSION;
} else {
$status_data[$client_id] = $session_buffer ? Context::sessionDecode($session_buffer) : array();
}
}
}
}
}
return $status_data;
}
/**
* 获取某个组的连接信息
*
* @param string $group
* @return array
*/
public static function getClientInfoByGroup($group)
{
return self::getAllClientSessions($group);
}
/**
* 获取某个组的连接信息
*
* @param string $group
* @return array
*/
public static function getClientSessionsByGroup($group)
{
return self::getAllClientSessions($group);
}
/**
* 获取所有连接数
*
* @return int
*/
public static function getAllClientCount()
{
return self::getClientCountByGroup();
}
/**
* 获取某个组的在线连接数
*
* @param string $group
* @return int
*/
public static function getClientCountByGroup($group = '')
{
$gateway_data = GatewayProtocol::$empty;
$gateway_data['cmd'] = GatewayProtocol::CMD_GET_CLIENT_COUNT_BY_GROUP;
$gateway_data['ext_data'] = $group;
$total_count = 0;
$all_buffer_array = self::getBufferFromAllGateway($gateway_data);
foreach ($all_buffer_array as $local_ip => $buffer_array) {
foreach ($buffer_array as $local_port => $count) {
if ($count) {
$total_count += $count;
}
}
}
return $total_count;
}
/**
* 获取与 uid 绑定的 client_id 列表
*
* @param string $uid
* @return array
*/
public static function getClientIdByUid($uid)
{
$gateway_data = GatewayProtocol::$empty;
$gateway_data['cmd'] = GatewayProtocol::CMD_GET_CLIENT_ID_BY_UID;
$gateway_data['ext_data'] = $uid;
$client_list = array();
$all_buffer_array = self::getBufferFromAllGateway($gateway_data);
foreach ($all_buffer_array as $local_ip => $buffer_array) {
foreach ($buffer_array as $local_port => $connection_id_array) {
if ($connection_id_array) {
foreach ($connection_id_array as $connection_id) {
$client_list[] = Context::addressToClientId($local_ip, $local_port, $connection_id);
}
}
}
}
return $client_list;
}
/**
* 生成验证包,用于验证此客户端的合法性
*
* @return string
*/
protected static function generateAuthBuffer()
{
$gateway_data = GatewayProtocol::$empty;
$gateway_data['cmd'] = GatewayProtocol::CMD_GATEWAY_CLIENT_CONNECT;
$gateway_data['body'] = json_encode(array(
'secret_key' => self::$secretKey,
));
return GatewayProtocol::encode($gateway_data);
}
/**
* 批量向所有 gateway 发包,并得到返回数组
*
* @param string $gateway_data
* @return array
* @throws Exception
*/
protected static function getBufferFromAllGateway($gateway_data)
{
$gateway_buffer = GatewayProtocol::encode($gateway_data);
$gateway_buffer = self::$secretKey ? self::generateAuthBuffer() . $gateway_buffer : $gateway_buffer;
if (isset(self::$businessWorker)) {
$all_addresses = self::$businessWorker->getAllGatewayAddresses();
if (empty($all_addresses)) {
throw new Exception('businessWorker::getAllGatewayAddresses return empty');
}
} else {
$all_addresses = self::getAllGatewayAddressesFromRegister();
if (empty($all_addresses)) {
return array();
}
}
$client_array = $status_data = $client_address_map = $receive_buffer_array = $recv_length_array = array();
// 批量向所有gateway进程发送请求数据
foreach ($all_addresses as $address) {
$client = stream_socket_client("tcp://$address", $errno, $errmsg, self::$connectTimeout);
if ($client && strlen($gateway_buffer) === stream_socket_sendto($client, $gateway_buffer)) {
$socket_id = (int)$client;
$client_array[$socket_id] = $client;
$client_address_map[$socket_id] = explode(':', $address);
$receive_buffer_array[$socket_id] = '';
}
}
// 超时1秒
$timeout = 1;
$time_start = microtime(true);
// 批量接收请求
while (count($client_array) > 0) {
$write = $except = array();
$read = $client_array;
if (@stream_select($read, $write, $except, $timeout)) {
foreach ($read as $client) {
$socket_id = (int)$client;
$buffer = stream_socket_recvfrom($client, 65535);
if ($buffer !== '' && $buffer !== false) {
$receive_buffer_array[$socket_id] .= $buffer;
$receive_length = strlen($receive_buffer_array[$socket_id]);
if (empty($recv_length_array[$socket_id]) && $receive_length >= 4) {
$recv_length_array[$socket_id] = current(unpack('N', $receive_buffer_array[$socket_id]));
}
if (!empty($recv_length_array[$socket_id]) && $receive_length >= $recv_length_array[$socket_id] + 4) {
unset($client_array[$socket_id]);
}
} elseif (feof($client)) {
unset($client_array[$socket_id]);
}
}
}
if (microtime(true) - $time_start > $timeout) {
break;
}
}
$format_buffer_array = array();
foreach ($receive_buffer_array as $socket_id => $buffer) {
$local_ip = ip2long($client_address_map[$socket_id][0]);
$local_port = $client_address_map[$socket_id][1];
$format_buffer_array[$local_ip][$local_port] = unserialize(substr($buffer, 4));
}
return $format_buffer_array;
}
/**
* 关闭某个客户端
*
* @param int $client_id
* @return bool
*/
public static function closeClient($client_id)
{
if ($client_id === Context::$client_id) {
return self::closeCurrentClient();
} // 不是发给当前用户则使用存储中的地址
else {
$address_data = Context::clientIdToAddress($client_id);
$address = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
return self::kickAddress($address, $address_data['connection_id']);
}
}
/**
* 踢掉当前客户端
*
* @return bool
* @throws Exception
*/
public static function closeCurrentClient()
{
if (!Context::$connection_id) {
throw new Exception('closeCurrentClient can not be called in async context');
}
return self::kickAddress(long2ip(Context::$local_ip) . ':' . Context::$local_port, Context::$connection_id);
}
/**
* 将 client_id 与 uid 绑定
*
* @param int $client_id
* @param int|string $uid
* @return bool
*/
public static function bindUid($client_id, $uid)
{
return self::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_BIND_UID, '', $uid);
}
/**
* 将 client_id 与 uid 解除绑定
*
* @param int $client_id
* @param int|string $uid
* @return bool
*/
public static function unbindUid($client_id, $uid)
{
return self::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_UNBIND_UID, '', $uid);
}
/**
* 将 client_id 加入组
*
* @param int $client_id
* @param int|string $group
* @return bool
*/
public static function joinGroup($client_id, $group)
{
return self::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_JOIN_GROUP, '', $group);
}
/**
* 将 client_id 离开组
*
* @param int $client_id
* @param int|string $group
* @return bool
*/
public static function leaveGroup($client_id, $group)
{
return self::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_LEAVE_GROUP, '', $group);
}
/**
* 向所有 uid 发送
*
* @param int|string|array $uid
* @param string $message
*/
public static function sendToUid($uid, $message)
{
$gateway_data = GatewayProtocol::$empty;
$gateway_data['cmd'] = GatewayProtocol::CMD_SEND_TO_UID;
$gateway_data['body'] = $message;
if (!is_array($uid)) {
$uid = array($uid);
}
$gateway_data['ext_data'] = json_encode($uid);
self::sendToAllGateway($gateway_data);
}
/**
* 向 group 发送
*
* @param int|string|array $group 组(不允许是 0 '0' false null array()等为空的值)
* @param string $message 消息
* @param array $exclude_client_id 不给这些client_id发
* @param bool $raw 发送原始数据(即不调用gateway的协议的encode方法)
*/
public static function sendToGroup($group, $message, $exclude_client_id = null, $raw = false)
{
$gateway_data = GatewayProtocol::$empty;
$gateway_data['cmd'] = GatewayProtocol::CMD_SEND_TO_GROUP;
$gateway_data['body'] = $message;
if ($raw) {
$gateway_data['flag'] |= GatewayProtocol::FLAG_NOT_CALL_ENCODE;
}
if (!is_array($group)) {
$group = array($group);
}
// 分组发送,没有排除的client_id,直接发送
$default_ext_data_buffer = json_encode(array('group'=> $group, 'exclude'=> null));
if (empty($exclude_client_id)) {
$gateway_data['ext_data'] = $default_ext_data_buffer;
return self::sendToAllGateway($gateway_data);
}
// 分组发送,有排除的client_id,需要将client_id转换成对应gateway进程内的connectionId
if (!is_array($exclude_client_id)) {
$exclude_client_id = array($exclude_client_id);
}
$address_connection_array = self::clientIdArrayToAddressArray($exclude_client_id);
// 如果有businessWorker实例,说明运行在workerman环境中,通过businessWorker中的长连接发送数据
if (self::$businessWorker) {
foreach (self::$businessWorker->gatewayConnections as $address => $gateway_connection) {
$gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
json_encode(array('group'=> $group, 'exclude'=> $address_connection_array[$address])) :
$default_ext_data_buffer;
/** @var TcpConnection $gateway_connection */
$gateway_connection->send($gateway_data);
}
} // 运行在其它环境中,通过注册中心得到gateway地址
else {
$all_addresses = self::getAllGatewayAddressesFromRegister();
if (!$all_addresses) {
throw new Exception('Gateway::getAllGatewayAddressesFromRegister() with registerAddress:' .
self::$registerAddress . ' return ' . var_export($all_addresses, true));
}
foreach ($all_addresses as $address) {
$gateway_data['ext_data'] = isset($address_connection_array[$address]) ?
json_encode(array('group'=> $group, 'exclude'=> $address_connection_array[$address])) :
$default_ext_data_buffer;
self::sendToGateway($address, $gateway_data);
}
}
}
/**
* 更新 session,框架自动调用,开发者不要调用
*
* @param int $client_id
* @param string $session_str
* @return bool
*/
public static function setSocketSession($client_id, $session_str)
{
return self::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_SET_SESSION, '', $session_str);
}
/**
* 设置 session,原session值会被覆盖
*
* @param int $client_id
* @param array $session
*/
public static function setSession($client_id, array $session)
{
if (Context::$client_id === $client_id) {
$_SESSION = $session;
Context::$old_session = $_SESSION;
}
return self::setSocketSession($client_id, Context::sessionEncode($session));
}
/**
* 更新 session,实际上是与老的session合并
*
* @param int $client_id
* @param array $session
*/
public static function updateSession($client_id, array $session)
{
if (Context::$client_id === $client_id) {
$_SESSION = $session + (array)$_SESSION;
Context::$old_session = $_SESSION;
}
return self::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_UPDATE_SESSION, '', Context::sessionEncode($session));
}
/**
* 获取某个client_id的session
*
* @param int $client_id
* @return mixed false表示出错、null表示用户不存在、array表示具体的session信息
*/
public static function getSession($client_id)
{
$address_data = Context::clientIdToAddress($client_id);
$address = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
if (isset(self::$businessWorker)) {
if (!isset(self::$businessWorker->gatewayConnections[$address])) {
return null;
}
}
$gateway_data = GatewayProtocol::$empty;
$gateway_data['cmd'] = GatewayProtocol::CMD_GET_SESSION_BY_CLIENT_ID;
$gateway_data['connection_id'] = $address_data['connection_id'];
return self::sendAndRecv($address, $gateway_data);
}
/**
* 想某个用户网关发送命令和消息
*
* @param int $client_id
* @param int $cmd
* @param string $message
* @param string $ext_data
* @return boolean
*/
protected static function sendCmdAndMessageToClient($client_id, $cmd, $message, $ext_data = '')
{
// 如果是发给当前用户则直接获取上下文中的地址
if ($client_id === Context::$client_id || $client_id === null) {
$address = long2ip(Context::$local_ip) . ':' . Context::$local_port;
$connection_id = Context::$connection_id;
} else {
$address_data = Context::clientIdToAddress($client_id);
$address = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
$connection_id = $address_data['connection_id'];
}
$gateway_data = GatewayProtocol::$empty;
$gateway_data['cmd'] = $cmd;
$gateway_data['connection_id'] = $connection_id;
$gateway_data['body'] = $message;
if (!empty($ext_data)) {
$gateway_data['ext_data'] = $ext_data;
}
return self::sendToGateway($address, $gateway_data);
}
/**
* 发送数据并返回
*
* @param int $address
* @param mixed $data
* @return bool
* @throws Exception
*/
protected static function sendAndRecv($address, $data)
{
$buffer = GatewayProtocol::encode($data);
$buffer = self::$secretKey ? self::generateAuthBuffer() . $buffer : $buffer;
$client = stream_socket_client("tcp://$address", $errno, $errmsg, self::$connectTimeout);
if (!$client) {
throw new Exception("can not connect to tcp://$address $errmsg");
}
if (strlen($buffer) === stream_socket_sendto($client, $buffer)) {
$timeout = 1;
// 阻塞读
stream_set_blocking($client, 1);
// 1秒超时
stream_set_timeout($client, 1);
$all_buffer = '';
$time_start = microtime(true);
$pack_len = 0;
while (1) {
$buf = stream_socket_recvfrom($client, 655350);
if ($buf !== '' && $buf !== false) {
$all_buffer .= $buf;
} else {
if (feof($client)) {
throw new Exception("connection close tcp://$address");
} elseif (microtime(true) - $time_start > $timeout) {
break;
}
continue;
}
$recv_len = strlen($all_buffer);
if (!$pack_len && $recv_len >= 4) {
$pack_len= current(unpack('N', $all_buffer));
}
// 回复的数据都是以\n结尾
if (($pack_len && $recv_len >= $pack_len + 4) || microtime(true) - $time_start > $timeout) {
break;
}
}
// 返回结果
return unserialize(substr($all_buffer, 4));
} else {
throw new Exception("sendAndRecv($address, \$bufer) fail ! Can not send data!", 502);
}
}
/**
* 发送数据到网关
*
* @param string $address
* @param array $gateway_data
* @return bool
*/
protected static function sendToGateway($address, $gateway_data)
{
return self::sendBufferToGateway($address, GatewayProtocol::encode($gateway_data));
}
/**
* 发送buffer数据到网关
* @param string $address
* @param string $gateway_buffer
* @return bool
*/
protected static function sendBufferToGateway($address, $gateway_buffer)
{
// 有$businessWorker说明是workerman环境,使用$businessWorker发送数据
if (self::$businessWorker) {
if (!isset(self::$businessWorker->gatewayConnections[$address])) {
return false;
}
return self::$businessWorker->gatewayConnections[$address]->send($gateway_buffer, true);
}
// 非workerman环境
$gateway_buffer = self::$secretKey ? self::generateAuthBuffer() . $gateway_buffer : $gateway_buffer;
$flag = self::$persistentConnection ? STREAM_CLIENT_PERSISTENT | STREAM_CLIENT_CONNECT : STREAM_CLIENT_CONNECT;
$client = stream_socket_client("tcp://$address", $errno, $errmsg, self::$connectTimeout, $flag);
return strlen($gateway_buffer) == stream_socket_sendto($client, $gateway_buffer);
}
/**
* 向所有 gateway 发送数据
*
* @param string $gateway_data
* @throws Exception
*/
protected static function sendToAllGateway($gateway_data)
{
$buffer = GatewayProtocol::encode($gateway_data);
// 如果有businessWorker实例,说明运行在workerman环境中,通过businessWorker中的长连接发送数据
if (self::$businessWorker) {
foreach (self::$businessWorker->gatewayConnections as $gateway_connection) {
/** @var TcpConnection $gateway_connection */
$gateway_connection->send($buffer, true);
}
} // 运行在其它环境中,通过注册中心得到gateway地址
else {
$all_addresses = self::getAllGatewayAddressesFromRegister();
if (!$all_addresses) {
throw new Exception('Gateway::getAllGatewayAddressesFromRegister() with registerAddress:' .
self::$registerAddress . ' return ' . var_export($all_addresses, true));
}
foreach ($all_addresses as $address) {
self::sendBufferToGateway($address, $buffer);
}
}
}
/**
* 踢掉某个网关的 socket
*
* @param string $address
* @param int $connection_id
* @return bool
*/
protected static function kickAddress($address, $connection_id)
{
$gateway_data = GatewayProtocol::$empty;
$gateway_data['cmd'] = GatewayProtocol::CMD_KICK;
$gateway_data['connection_id'] = $connection_id;
return self::sendToGateway($address, $gateway_data);
}
/**
* 将clientid数组转换成address数组
*
* @param array $client_id_array
* @return array
*/
protected static function clientIdArrayToAddressArray(array $client_id_array)
{
$address_connection_array = array();
foreach ($client_id_array as $client_id) {
$address_data = Context::clientIdToAddress($client_id);
$address = long2ip($address_data['local_ip']) .
":{$address_data['local_port']}";
$address_connection_array[$address][$address_data['connection_id']] = $address_data['connection_id'];
}
return $address_connection_array;
}
/**
* 设置 gateway 实例
*
* @param \GatewayWorker\BusinessWorker $business_worker_instance
*/
public static function setBusinessWorker($business_worker_instance)
{
self::$businessWorker = $business_worker_instance;
}
/**
* 获取通过注册中心获取所有 gateway 通讯地址
*
* @return array
* @throws Exception
*/
protected static function getAllGatewayAddressesFromRegister()
{
static $addresses_cache, $last_update;
$time_now = time();
$expiration_time = 1;
if(empty($addresses_cache) || $time_now - $last_update > $expiration_time) {
$client = stream_socket_client('tcp://' . self::$registerAddress, $errno, $errmsg, self::$connectTimeout);
if (!$client) {
throw new Exception('Can not connect to tcp://' . self::$registerAddress . ' ' . $errmsg);
}
fwrite($client, '{"event":"worker_connect","secret_key":"' . self::$secretKey . '"}' . "\n");
stream_set_timeout($client, 1);
$ret = fgets($client, 655350);
if (!$ret || !$data = json_decode(trim($ret), true)) {
throw new Exception('getAllGatewayAddressesFromRegister fail. tcp://' .
self::$registerAddress . ' return ' . var_export($ret, true));
}
$last_update = $time_now;
$addresses_cache = $data['addresses'];
}
return $addresses_cache;
}
}
/**
* 上下文 包含当前用户uid, 内部通信local_ip local_port socket_id ,以及客户端client_ip client_port
*/
class Context
{
/**
* 内部通讯id
* @var string
*/
public static $local_ip;
/**
* 内部通讯端口
* @var int
*/
public static $local_port;
/**
* 客户端ip
* @var string
*/
public static $client_ip;
/**
* 客户端端口
* @var int
*/
public static $client_port;
/**
* client_id
* @var string
*/
public static $client_id;
/**
* 连接connection->id
* @var int
*/
public static $connection_id;
/**
* 旧的session
*
* @var string
*/
public static $old_session;
/**
* 编码session
* @param mixed $session_data
* @return string
*/
public static function sessionEncode($session_data = '')
{
if($session_data !== '')
{
return serialize($session_data);
}
return '';
}
/**
* 解码session
* @param string $session_buffer
* @return mixed
*/
public static function sessionDecode($session_buffer)
{
return unserialize($session_buffer);
}
/**
* 清除上下文
* @return void
*/
public static function clear()
{
self::$local_ip = self::$local_port = self::$client_ip = self::$client_port =
self::$client_id = self::$connection_id = self::$old_session = null;
}
/**
* 通讯地址到client_id的转换
* @return string
*/
public static function addressToClientId($local_ip, $local_port, $connection_id)
{
return bin2hex(pack('NnN', $local_ip, $local_port, $connection_id));
}
/**
* client_id到通讯地址的转换
* @return array
*/
public static function clientIdToAddress($client_id)
{
if(strlen($client_id) !== 20)
{
throw new \Exception("client_id $client_id is invalid");
}
return unpack('Nlocal_ip/nlocal_port/Nconnection_id' ,pack('H*', $client_id));
}
}
/**
* Gateway与Worker间通讯的二进制协议
*
* struct GatewayProtocol
* {
* unsigned int pack_len,
* unsigned char cmd,//命令字
* unsigned int local_ip,
* unsigned short local_port,
* unsigned int client_ip,
* unsigned short client_port,
* unsigned int connection_id,
* unsigned char flag,
* unsigned short gateway_port,
* unsigned int ext_len,
* char[ext_len] ext_data,
* char[pack_length-HEAD_LEN] body//包体
* }
* NCNnNnNCnN
*/
class GatewayProtocol
{
// 发给worker,gateway有一个新的连接
const CMD_ON_CONNECTION = 1;
// 发给worker的,客户端有消息
const CMD_ON_MESSAGE = 3;
// 发给worker上的关闭链接事件
const CMD_ON_CLOSE = 4;
// 发给gateway的向单个用户发送数据
const CMD_SEND_TO_ONE = 5;
// 发给gateway的向所有用户发送数据
const CMD_SEND_TO_ALL = 6;
// 发给gateway的踢出用户
const CMD_KICK = 7;
// 发给gateway,通知用户session更新
const CMD_UPDATE_SESSION = 9;
// 获取在线状态
const CMD_GET_ALL_CLIENT_INFO = 10;
// 判断是否在线
const CMD_IS_ONLINE = 11;
// client_id绑定到uid
const CMD_BIND_UID = 12;
// 解绑
const CMD_UNBIND_UID = 13;
// 向uid发送数据
const CMD_SEND_TO_UID = 14;
// 根据uid获取绑定的clientid
const CMD_GET_CLIENT_ID_BY_UID = 15;
// 加入组
const CMD_JOIN_GROUP = 20;
// 离开组
const CMD_LEAVE_GROUP = 21;
// 向组成员发消息
const CMD_SEND_TO_GROUP = 22;
// 获取组成员
const CMD_GET_CLINET_INFO_BY_GROUP = 23;
// 获取组成员数
const CMD_GET_CLIENT_COUNT_BY_GROUP = 24;
// worker连接gateway事件
const CMD_WORKER_CONNECT = 200;
// 心跳
const CMD_PING = 201;
// GatewayClient连接gateway事件
const CMD_GATEWAY_CLIENT_CONNECT = 202;
// 根据client_id获取session
const CMD_GET_SESSION_BY_CLIENT_ID = 203;
// 发给gateway,覆盖session
const CMD_SET_SESSION = 204;
// 包体是标量
const FLAG_BODY_IS_SCALAR = 0x01;
// 通知gateway在send时不调用协议encode方法,在广播组播时提升性能
const FLAG_NOT_CALL_ENCODE = 0x02;
/**
* 包头长度
*
* @var int
*/
const HEAD_LEN = 28;
public static $empty = array(
'cmd' => 0,
'local_ip' => 0,
'local_port' => 0,
'client_ip' => 0,
'client_port' => 0,
'connection_id' => 0,
'flag' => 0,
'gateway_port' => 0,
'ext_data' => '',
'body' => '',
);
/**
* 返回包长度
*
* @param string $buffer
* @return int return current package length
*/
public static function input($buffer)
{
if (strlen($buffer) < self::HEAD_LEN) {
return 0;
}
$data = unpack("Npack_len", $buffer);
return $data['pack_len'];
}
/**
* 获取整个包的 buffer
*
* @param mixed $data
* @return string
*/
public static function encode($data)
{
$flag = (int)is_scalar($data['body']);
if (!$flag) {
$data['body'] = serialize($data['body']);
}
$data['flag'] |= $flag;
$ext_len = strlen($data['ext_data']);
$package_len = self::HEAD_LEN + $ext_len + strlen($data['body']);
return pack("NCNnNnNCnN", $package_len,
$data['cmd'], $data['local_ip'],
$data['local_port'], $data['client_ip'],
$data['client_port'], $data['connection_id'],
$data['flag'], $data['gateway_port'],
$ext_len) . $data['ext_data'] . $data['body'];
}
/**
* 从二进制数据转换为数组
*
* @param string $buffer
* @return array
*/
public static function decode($buffer)
{
$data = unpack("Npack_len/Ccmd/Nlocal_ip/nlocal_port/Nclient_ip/nclient_port/Nconnection_id/Cflag/ngateway_port/Next_len",
$buffer);
if ($data['ext_len'] > 0) {
$data['ext_data'] = substr($buffer, self::HEAD_LEN, $data['ext_len']);
if ($data['flag'] & self::FLAG_BODY_IS_SCALAR) {
$data['body'] = substr($buffer, self::HEAD_LEN + $data['ext_len']);
} else {
$data['body'] = unserialize(substr($buffer, self::HEAD_LEN + $data['ext_len']));
}
} else {
$data['ext_data'] = '';
if ($data['flag'] & self::FLAG_BODY_IS_SCALAR) {
$data['body'] = substr($buffer, self::HEAD_LEN);
} else {
$data['body'] = unserialize(substr($buffer, self::HEAD_LEN));
}
}
return $data;
}
}
......@@ -41,6 +41,7 @@ class Port
public function register(array $idLogic,int $gatewayId)
{
$this->redis->hMset(RedisKey::PORT_ID_LOGIC_HASH,$idLogic);
$this->redis->hMset('gateway_id_'.":$gatewayId".RedisKey::GATEWAY_PORT_LOGIC_ID_HASH,array_flip($idLogic));
......@@ -68,6 +69,21 @@ class Port
}
public function setPortState(array $portIdState)
{
$tmp = [RedisKey::PORT_STATE_ZSET];
foreach ($portIdState as $id => $state) {
array_push($tmp,$state);
array_push($tmp,$id);
}
call_user_func_array([$this->redis,'zAdd'],$tmp);
}
/**
* 端口id在线
* @param array $portId
......
......@@ -9,6 +9,8 @@
namespace Route;
use Application\Middleware\AuthMiddleware;
use Application\Middleware\HasMacMiddleware;
use Lib\Route;
class Api
......@@ -23,13 +25,13 @@ class Api
static::add('02','AccessController@access');
static::add('03','AccessController@syncPort');
static::add('03','AccessController@syncPort',[new AuthMiddleware(),new HasMacMiddleware()]);
static::add('04','PowerController@charger');
static::add('04','PowerController@charger',[new AuthMiddleware(),new HasMacMiddleware()]);
static::add('08','PortController@online');
static::add('08','PortController@online',[new AuthMiddleware(),new HasMacMiddleware()]);
static::add('09','PortController@offline');
static::add('09','PortController@offline',[new AuthMiddleware(),new HasMacMiddleware()]);
}
......
......@@ -25,8 +25,6 @@ class Bintohex
$data = $data[1];
var_dump(['message' => $data]);
$packageLength = strlen($data);
$packageLength/=2;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment