BusinessWorker.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. *
  4. * 处理具体逻辑
  5. *
  6. * @author walkor <workerman.net>
  7. *
  8. */
  9. define('ROOT_DIR', realpath(__DIR__.'/../'));
  10. require_once ROOT_DIR . '/Protocols/GatewayProtocol.php';
  11. require_once ROOT_DIR . '/Event.php';
  12. class BusinessWorker extends Man\Core\SocketWorker
  13. {
  14. public function dealInput($recv_str)
  15. {
  16. return GatewayProtocol::input($recv_str);
  17. }
  18. public function dealProcess($recv_str)
  19. {
  20. $pack = new GatewayProtocol($recv_str);
  21. Context::$client_ip = $pack->header['client_ip'];
  22. Context::$client_port = $pack->header['client_port'];
  23. Context::$local_ip = $pack->header['local_ip'];
  24. Context::$local_port = $pack->header['local_port'];
  25. Context::$socket_id = $pack->header['socket_id'];
  26. Context::$uid = $pack->header['uid'];
  27. Context::$protocol = $this->protocol;
  28. switch($pack->header['cmd'])
  29. {
  30. case GatewayProtocol::CMD_ON_CONNECTION:
  31. $ret = call_user_func_array(array('Event', 'onConnect'), array($pack->body));
  32. break;
  33. case GatewayProtocol::CMD_ON_MESSAGE:
  34. $ret = call_user_func_array(array('Event', 'onMessage'), array(Context::$uid, $pack->body));
  35. break;
  36. case GatewayProtocol::CMD_ON_CLOSE:
  37. $ret = call_user_func_array(array('Event', 'onClose'), array(Context::$uid));
  38. break;
  39. }
  40. Context::clear();
  41. return $ret;
  42. }
  43. }
  44. /**
  45. * 上下文 包含当前用户uid, 内部通信local_ip local_port socket_id ,以及客户端client_ip client_port
  46. * @author walkor
  47. *
  48. */
  49. class Context
  50. {
  51. public static $series_id;
  52. public static $local_ip;
  53. public static $local_port;
  54. public static $socket_id;
  55. public static $client_ip;
  56. public static $client_port;
  57. public static $uid;
  58. public static $protocol;
  59. public static function clear()
  60. {
  61. self::$series_id = self::$local_ip = self::$local_port = self::$socket_id = self::$client_ip = self::$client_port = self::$uid = null;
  62. }
  63. }