Context.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /**
  16. * 上下文 包含当前用户uid, 内部通信local_ip local_port socket_id ,以及客户端client_ip client_port
  17. */
  18. class Context
  19. {
  20. /**
  21. * 内部通讯id
  22. * @var string
  23. */
  24. public static $local_ip;
  25. /**
  26. * 内部通讯端口
  27. * @var int
  28. */
  29. public static $local_port;
  30. /**
  31. * 客户端ip
  32. * @var string
  33. */
  34. public static $client_ip;
  35. /**
  36. * 客户端端口
  37. * @var int
  38. */
  39. public static $client_port;
  40. /**
  41. * 用户id
  42. * @var int
  43. */
  44. public static $client_id;
  45. /**
  46. * 编码session
  47. * @param mixed $session_data
  48. * @return string
  49. */
  50. public static function sessionEncode($session_data = '')
  51. {
  52. if($session_data !== '')
  53. {
  54. return serialize($session_data);
  55. }
  56. return '';
  57. }
  58. /**
  59. * 解码session
  60. * @param string $session_buffer
  61. * @return mixed
  62. */
  63. public static function sessionDecode($session_buffer)
  64. {
  65. return unserialize($session_buffer);
  66. }
  67. /**
  68. * 清除上下文
  69. * @return void
  70. */
  71. public static function clear()
  72. {
  73. self::$local_ip = self::$local_port = self::$client_ip = self::$client_port = self::$client_id = null;
  74. }
  75. }