ConnectionInterface.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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;
  15. /**
  16. * ConnectionInterface.
  17. */
  18. #[\AllowDynamicProperties]
  19. abstract class ConnectionInterface
  20. {
  21. /**
  22. * Statistics for status command.
  23. *
  24. * @var array
  25. */
  26. public static $statistics = array(
  27. 'connection_count' => 0,
  28. 'total_request' => 0,
  29. 'throw_exception' => 0,
  30. 'send_fail' => 0,
  31. );
  32. /**
  33. * Emitted when data is received.
  34. *
  35. * @var callable
  36. */
  37. public $onMessage = null;
  38. /**
  39. * Emitted when the other end of the socket sends a FIN packet.
  40. *
  41. * @var callable
  42. */
  43. public $onClose = null;
  44. /**
  45. * Emitted when an error occurs with connection.
  46. *
  47. * @var callable
  48. */
  49. public $onError = null;
  50. /**
  51. * Sends data on the connection.
  52. *
  53. * @param mixed $send_buffer
  54. * @return void|boolean
  55. */
  56. abstract public function send($send_buffer);
  57. /**
  58. * Get remote IP.
  59. *
  60. * @return string
  61. */
  62. abstract public function getRemoteIp();
  63. /**
  64. * Get remote port.
  65. *
  66. * @return int
  67. */
  68. abstract public function getRemotePort();
  69. /**
  70. * Get remote address.
  71. *
  72. * @return string
  73. */
  74. abstract public function getRemoteAddress();
  75. /**
  76. * Get local IP.
  77. *
  78. * @return string
  79. */
  80. abstract public function getLocalIp();
  81. /**
  82. * Get local port.
  83. *
  84. * @return int
  85. */
  86. abstract public function getLocalPort();
  87. /**
  88. * Get local address.
  89. *
  90. * @return string
  91. */
  92. abstract public function getLocalAddress();
  93. /**
  94. * Is ipv4.
  95. *
  96. * @return bool
  97. */
  98. abstract public function isIPv4();
  99. /**
  100. * Is ipv6.
  101. *
  102. * @return bool
  103. */
  104. abstract public function isIPv6();
  105. /**
  106. * Close connection.
  107. *
  108. * @param $data
  109. * @return void
  110. */
  111. abstract public function close($data = null);
  112. }