1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* soluble-japha / PHPJavaBridge driver client. |
4
|
|
|
* |
5
|
|
|
* Refactored version of phpjababridge's Java.inc file compatible |
6
|
|
|
* with php java bridge 6.2 |
7
|
|
|
* |
8
|
|
|
* |
9
|
|
|
* @credits http://php-java-bridge.sourceforge.net/pjb/ |
10
|
|
|
* |
11
|
|
|
* @see http://github.com/belgattitude/soluble-japha |
12
|
|
|
* |
13
|
|
|
* @author Jost Boekemeier |
14
|
|
|
* @author Vanvelthem Sébastien (refactoring and fixes from original implementation) |
15
|
|
|
* @license MIT |
16
|
|
|
* |
17
|
|
|
* The MIT License (MIT) |
18
|
|
|
* Copyright (c) 2014-2017 Jost Boekemeier |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
20
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
21
|
|
|
* in the Software without restriction, including without limitation the rights |
22
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
23
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
24
|
|
|
* furnished to do so, subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in |
27
|
|
|
* all copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
31
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
32
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
33
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
34
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
35
|
|
|
* THE SOFTWARE. |
36
|
|
|
*/ |
37
|
|
|
|
38
|
|
|
namespace Soluble\Japha\Bridge\Driver\Pjb62; |
39
|
|
|
|
40
|
|
|
use Soluble\Japha\Bridge\Exception\AuthenticationException; |
41
|
|
|
use Soluble\Japha\Bridge\Exception\BrokenConnectionException; |
42
|
|
|
|
43
|
|
|
class SocketHandler |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @var Protocol |
47
|
|
|
*/ |
48
|
|
|
public $protocol; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var EmptyChannel|SocketChannel |
52
|
|
|
*/ |
53
|
|
|
public $channel; |
54
|
|
|
|
55
|
9 |
|
public function __construct(Protocol $protocol, EmptyChannel $channel) |
56
|
|
|
{ |
57
|
9 |
|
$this->protocol = $protocol; |
58
|
9 |
|
$this->channel = $channel; |
59
|
9 |
|
} |
60
|
|
|
|
61
|
117 |
|
public function write(string $data): ?int |
62
|
|
|
{ |
63
|
117 |
|
return $this->channel->fwrite($data); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function fwrite(string $data): ?int |
67
|
|
|
{ |
68
|
|
|
return $this->write($data); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @throws BrokenConnectionException |
73
|
|
|
*/ |
74
|
113 |
|
public function read(int $size): string |
75
|
|
|
{ |
76
|
113 |
|
$str = $this->channel->fread($size); |
77
|
113 |
|
if ($str === null) { |
78
|
|
|
$this->shutdownBrokenConnection('Cannot read from socket'); |
|
|
|
|
79
|
|
|
} else { |
80
|
113 |
|
return $str; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function fread(int $size): ?string |
85
|
|
|
{ |
86
|
|
|
return $this->read($size); |
87
|
|
|
} |
88
|
|
|
|
89
|
112 |
|
public function redirect(): void |
90
|
|
|
{ |
91
|
112 |
|
} |
92
|
|
|
|
93
|
23 |
|
public function getKeepAlive(): string |
94
|
|
|
{ |
95
|
23 |
|
return $this->channel->getKeepAlive(); |
96
|
|
|
} |
97
|
|
|
|
98
|
23 |
|
public function keepAlive(): void |
99
|
|
|
{ |
100
|
23 |
|
$this->channel->keepAlive(); |
101
|
8 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @throws BrokenConnectionException|AuthenticationException |
105
|
|
|
* |
106
|
|
|
* @return never |
107
|
|
|
*/ |
108
|
|
|
public function shutdownBrokenConnection(string $msg = '', int $code = null): void |
109
|
|
|
{ |
110
|
|
|
$msg = $msg ?? 'Broken connection: Unkown error, please see back end log for detail'; |
111
|
|
|
|
112
|
|
|
// Log error |
113
|
|
|
$client = $this->protocol->getClient(); |
114
|
|
|
$client->getLogger()->critical("[soluble-japha] $msg\" (".__METHOD__.')'); |
115
|
|
|
|
116
|
|
|
$this->channel->shutdownBrokenConnection(); |
117
|
|
|
PjbProxyClient::unregisterAndThrowBrokenConnectionException($msg, $code); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: