Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConnectionInterface
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 send
n/a
0 / 0
n/a
0 / 0
0
 getRemoteIp
n/a
0 / 0
n/a
0 / 0
0
 getRemotePort
n/a
0 / 0
n/a
0 / 0
0
 getRemoteAddress
n/a
0 / 0
n/a
0 / 0
0
 getLocalIp
n/a
0 / 0
n/a
0 / 0
0
 getLocalPort
n/a
0 / 0
n/a
0 / 0
0
 getLocalAddress
n/a
0 / 0
n/a
0 / 0
0
 close
n/a
0 / 0
n/a
0 / 0
0
 isIpV4
n/a
0 / 0
n/a
0 / 0
0
 isIpV6
n/a
0 / 0
n/a
0 / 0
0
 error
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
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
15declare(strict_types=1);
16
17namespace Workerman\Connection;
18
19use Throwable;
20use Workerman\Events\Event;
21use Workerman\Events\EventInterface;
22use Workerman\Worker;
23use AllowDynamicProperties;
24
25/**
26 * ConnectionInterface.
27 */
28#[AllowDynamicProperties]
29abstract class ConnectionInterface
30{
31    /**
32     * Connect failed.
33     *
34     * @var int
35     */
36    public const CONNECT_FAIL = 1;
37
38    /**
39     * Send failed.
40     *
41     * @var int
42     */
43    public const SEND_FAIL = 2;
44
45    /**
46     * Statistics for status command.
47     *
48     * @var array
49     */
50    public static array $statistics = [
51        'connection_count' => 0,
52        'total_request' => 0,
53        'throw_exception' => 0,
54        'send_fail' => 0,
55    ];
56
57    /**
58     * Application layer protocol.
59     * The format is like this Workerman\\Protocols\\Http.
60     *
61     * @var ?class-string
62     */
63    public ?string $protocol = null;
64
65    /**
66     * Emitted when data is received.
67     *
68     * @var ?callable
69     */
70    public $onMessage = null;
71
72    /**
73     * Emitted when the other end of the socket sends a FIN packet.
74     *
75     * @var ?callable
76     */
77    public $onClose = null;
78
79    /**
80     * Emitted when an error occurs with connection.
81     *
82     * @var ?callable
83     */
84    public $onError = null;
85
86    /**
87     * @var ?EventInterface
88     */
89    public ?EventInterface $eventLoop = null;
90
91    /**
92     * @var ?callable
93     */
94    public $errorHandler = null;
95
96    /**
97     * Sends data on the connection.
98     *
99     * @param mixed $sendBuffer
100     * @param bool $raw
101     * @return bool|null
102     */
103    abstract public function send(mixed $sendBuffer, bool $raw = false): bool|null;
104
105    /**
106     * Get remote IP.
107     *
108     * @return string
109     */
110    abstract public function getRemoteIp(): string;
111
112    /**
113     * Get remote port.
114     *
115     * @return int
116     */
117    abstract public function getRemotePort(): int;
118
119    /**
120     * Get remote address.
121     *
122     * @return string
123     */
124    abstract public function getRemoteAddress(): string;
125
126    /**
127     * Get local IP.
128     *
129     * @return string
130     */
131    abstract public function getLocalIp(): string;
132
133    /**
134     * Get local port.
135     *
136     * @return int
137     */
138    abstract public function getLocalPort(): int;
139
140    /**
141     * Get local address.
142     *
143     * @return string
144     */
145    abstract public function getLocalAddress(): string;
146
147    /**
148     * Close connection.
149     *
150     * @param mixed $data
151     * @param bool $raw
152     * @return void
153     */
154    abstract public function close(mixed $data = null, bool $raw = false): void;
155
156    /**
157     * Is ipv4.
158     *
159     * return bool.
160     */
161    abstract public function isIpV4(): bool;
162
163    /**
164     * Is ipv6.
165     *
166     * return bool.
167     */
168    abstract public function isIpV6(): bool;
169
170    /**
171     * @param Throwable $exception
172     * @return void
173     */
174    public function error(Throwable $exception): void
175    {
176        if (!$this->errorHandler) {
177            Worker::stopAll(250, $exception);
178            return;
179        }
180        try {
181            ($this->errorHandler)($exception);
182        } catch (Throwable $exception) {
183            Worker::stopAll(250, $exception);
184            return;
185        }
186    }
187}