Context.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace GatewayWorker\Lib;
  3. /**
  4. * 上下文 包含当前用户uid, 内部通信local_ip local_port socket_id ,以及客户端client_ip client_port
  5. * @author walkor
  6. */
  7. class Context
  8. {
  9. /**
  10. * 内部通讯id
  11. * @var string
  12. */
  13. public static $local_ip;
  14. /**
  15. * 内部通讯端口
  16. * @var int
  17. */
  18. public static $local_port;
  19. /**
  20. * 客户端ip
  21. * @var string
  22. */
  23. public static $client_ip;
  24. /**
  25. * 客户端端口
  26. * @var int
  27. */
  28. public static $client_port;
  29. /**
  30. * 用户id
  31. * @var int
  32. */
  33. public static $client_id;
  34. /**
  35. * 编码session
  36. * @param mixed $session_data
  37. * @return string
  38. */
  39. public static function sessionEncode($session_data = '')
  40. {
  41. if($session_data !== '')
  42. {
  43. return serialize($session_data);
  44. }
  45. return '';
  46. }
  47. /**
  48. * 解码session
  49. * @param string $session_buffer
  50. * @return mixed
  51. */
  52. public static function sessionDecode($session_buffer)
  53. {
  54. return unserialize($session_buffer);
  55. }
  56. /**
  57. * 清除上下文
  58. * @return void
  59. */
  60. public static function clear()
  61. {
  62. self::$local_ip = self::$local_port = self::$client_ip = self::$client_port = self::$client_id = null;
  63. }
  64. }