SerializeTrait.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 Workerman\Connection\Traits;
  15. trait SerializeTrait
  16. {
  17. public function jsonSerialize()
  18. {
  19. return [
  20. 'transport' => $this->transport,
  21. 'id' => $this->id,
  22. 'status' => $this->getStatus(),
  23. 'getRemoteIp' => $this->getRemoteIp(),
  24. 'remotePort' => $this->getRemotePort(),
  25. 'getRemoteAddress' => $this->getRemoteAddress(),
  26. 'getLocalIp' => $this->getLocalIp(),
  27. 'getLocalPort' => $this->getLocalPort(),
  28. 'getLocalAddress' => $this->getLocalAddress(),
  29. 'isIpV4' => $this->isIpV4(),
  30. 'isIpV6' => $this->isIpV6(),
  31. ];
  32. }
  33. public function serialize()
  34. {
  35. return serialize($this->jsonSerialize());
  36. }
  37. public function unserialize(string $data)
  38. {
  39. // 仅仅打印信息,不做操作,进程数据不可进行改变
  40. var_export(sprintf("unserialize %s \n", get_class($this)));
  41. var_export(unserialize($data));
  42. }
  43. }