Context.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace GatewayWorker\Lib;
  15. use Exception;
  16. /**
  17. * 上下文 包含当前用户 uid, 内部通信 local_ip local_port socket_id,以及客户端 client_ip client_port
  18. */
  19. class Context
  20. {
  21. /**
  22. * 内部通讯 id
  23. *
  24. * @var string
  25. */
  26. public static $local_ip;
  27. /**
  28. * 内部通讯端口
  29. *
  30. * @var int
  31. */
  32. public static $local_port;
  33. /**
  34. * 客户端 ip
  35. *
  36. * @var string
  37. */
  38. public static $client_ip;
  39. /**
  40. * 客户端端口
  41. *
  42. * @var int
  43. */
  44. public static $client_port;
  45. /**
  46. * client_id
  47. *
  48. * @var string
  49. */
  50. public static $client_id;
  51. /**
  52. * 连接 connection->id
  53. *
  54. * @var int
  55. */
  56. public static $connection_id;
  57. /**
  58. * 旧的session
  59. *
  60. * @var string
  61. */
  62. public static $old_session;
  63. /**
  64. * 编码 session
  65. *
  66. * @param mixed $session_data
  67. * @return string
  68. */
  69. public static function sessionEncode($session_data = '')
  70. {
  71. if ($session_data !== '') {
  72. return serialize($session_data);
  73. }
  74. return '';
  75. }
  76. /**
  77. * 解码 session
  78. *
  79. * @param string $session_buffer
  80. * @return mixed
  81. */
  82. public static function sessionDecode($session_buffer)
  83. {
  84. return unserialize($session_buffer);
  85. }
  86. /**
  87. * 清除上下文
  88. *
  89. * @return void
  90. */
  91. public static function clear()
  92. {
  93. self::$local_ip = self::$local_port = self::$client_ip = self::$client_port =
  94. self::$client_id = self::$connection_id = self::$old_session = null;
  95. }
  96. /**
  97. * 通讯地址到 client_id 的转换
  98. *
  99. * @param int $local_ip
  100. * @param int $local_port
  101. * @param int $connection_id
  102. * @return string
  103. */
  104. public static function addressToClientId($local_ip, $local_port, $connection_id)
  105. {
  106. return bin2hex(pack('NnN', $local_ip, $local_port, $connection_id));
  107. }
  108. /**
  109. * client_id 到通讯地址的转换
  110. *
  111. * @param string $client_id
  112. * @return array
  113. * @throws Exception
  114. */
  115. public static function clientIdToAddress($client_id)
  116. {
  117. if (strlen($client_id) !== 20) {
  118. echo new Exception("client_id $client_id is invalid");
  119. return false;
  120. }
  121. return unpack('Nlocal_ip/nlocal_port/Nconnection_id', pack('H*', $client_id));
  122. }
  123. }