|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the PHP Generics package. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Generics |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Generics\Socket; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* This class provides a secure socket client |
|
11
|
|
|
* |
|
12
|
|
|
* @author Maik Greubel <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class SecureClientSocket extends SecureSocket |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Whether the socket is connected |
|
19
|
|
|
* |
|
20
|
|
|
* @var boolean |
|
21
|
|
|
*/ |
|
22
|
|
|
private $conntected; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Create a new client socket |
|
26
|
|
|
* |
|
27
|
|
|
* @param Endpoint $endpoint |
|
28
|
|
|
* The endpoint to use |
|
29
|
|
|
* @param resource $clientHandle |
|
30
|
|
|
* optional existing client handle |
|
31
|
|
|
*/ |
|
32
|
12 |
View Code Duplication |
public function __construct(Endpoint $endpoint, $clientHandle = null) |
|
33
|
|
|
{ |
|
34
|
12 |
|
$this->endpoint = $endpoint; |
|
35
|
12 |
|
$this->handle = $clientHandle; |
|
36
|
12 |
|
$this->conntected = false; |
|
37
|
|
|
|
|
38
|
12 |
|
if (! is_resource($clientHandle)) { |
|
39
|
12 |
|
parent::__construct($endpoint); |
|
40
|
|
|
} |
|
41
|
12 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Connect to remote endpoint |
|
45
|
|
|
* |
|
46
|
|
|
* @throws SocketException |
|
47
|
|
|
*/ |
|
48
|
11 |
|
public function connect() |
|
49
|
|
|
{ |
|
50
|
11 |
|
$this->conntected = true; |
|
51
|
11 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Disconnects the socket |
|
55
|
|
|
* |
|
56
|
|
|
* @throws SocketException |
|
57
|
|
|
*/ |
|
58
|
5 |
|
public function disconnect() |
|
59
|
|
|
{ |
|
60
|
5 |
|
if (! $this->conntected) { |
|
61
|
|
|
throw new SocketException("Socket is not connected"); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
5 |
|
$this->close(); |
|
65
|
5 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Whether the client is connected |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
11 |
|
public function isConnected(): bool |
|
73
|
|
|
{ |
|
74
|
11 |
|
return $this->conntected; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* |
|
79
|
|
|
* @see \Generics\Socket\ClientSocket::disconnect() |
|
80
|
|
|
*/ |
|
81
|
6 |
|
public function close() |
|
82
|
|
|
{ |
|
83
|
6 |
|
parent::close(); |
|
84
|
6 |
|
$this->conntected = false; |
|
85
|
6 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
* @see \Generics\Socket\Socket::isWriteable() |
|
91
|
|
|
*/ |
|
92
|
11 |
|
public function isWriteable(): bool |
|
93
|
|
|
{ |
|
94
|
11 |
|
if (! $this->isConnected()) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
11 |
|
return parent::isWriteable(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|