1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the php-gelf package. |
5
|
|
|
* |
6
|
|
|
* (c) Benjamin Zikarsky <http://benjamin-zikarsky.de> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Gelf\Transport; |
13
|
|
|
|
14
|
|
|
use Gelf\Encoder\EncoderInterface; |
15
|
|
|
use Gelf\Encoder\NoNullByteEncoderInterface; |
16
|
|
|
use Gelf\MessageInterface as Message; |
17
|
|
|
use Gelf\Encoder\JsonEncoder as DefaultEncoder; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* TcpTransport allows the transfer of GELF-messages (with SSL/TLS support) |
22
|
|
|
* to a compatible GELF-TCP-backend as described in |
23
|
|
|
* https://github.com/Graylog2/graylog2-docs/wiki/GELF |
24
|
|
|
* |
25
|
|
|
* It can also act as a direct publisher |
26
|
|
|
* |
27
|
|
|
* @author Benjamin Zikarsky <[email protected]> |
28
|
|
|
* @author Ahmed Trabelsi <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class TcpTransport extends AbstractTransport |
31
|
|
|
{ |
32
|
|
|
const DEFAULT_HOST = "127.0.0.1"; |
33
|
|
|
const DEFAULT_PORT = 12201; |
34
|
|
|
|
35
|
|
|
const AUTO_SSL_PORT = 12202; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $host; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
protected $port; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var StreamSocketClient |
49
|
|
|
*/ |
50
|
|
|
protected $socketClient; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var SslOptions|null |
54
|
|
|
*/ |
55
|
|
|
protected $sslOptions = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Class constructor |
59
|
|
|
* |
60
|
|
|
* @param string|null $host when NULL or empty default-host is used |
61
|
|
|
* @param int|null $port when NULL or empty default-port is used |
62
|
|
|
* @param SslOptions|null $sslOptions when null not SSL is used |
63
|
|
|
*/ |
64
|
8 |
|
public function __construct( |
65
|
|
|
$host = self::DEFAULT_HOST, |
66
|
|
|
$port = self::DEFAULT_PORT, |
67
|
|
|
SslOptions $sslOptions = null |
68
|
|
|
) { |
69
|
8 |
|
$this->host = $host; |
70
|
8 |
|
$this->port = $port; |
71
|
|
|
|
72
|
8 |
|
if ($port == self::AUTO_SSL_PORT && $sslOptions == null) { |
73
|
1 |
|
$sslOptions = new SslOptions(); |
74
|
|
|
} |
75
|
|
|
|
76
|
8 |
|
$this->sslOptions = $sslOptions; |
77
|
|
|
|
78
|
8 |
|
$this->setMessageEncoder(new DefaultEncoder()); |
79
|
8 |
|
$this->socketClient = new StreamSocketClient( |
80
|
8 |
|
$this->getScheme(), |
81
|
8 |
|
$this->host, |
82
|
8 |
|
$this->port, |
83
|
8 |
|
$this->getContext() |
84
|
|
|
); |
85
|
8 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Sends a Message over this transport |
89
|
|
|
* |
90
|
|
|
* @param Message $message |
91
|
|
|
* |
92
|
|
|
* @return int the number of TCP packets sent |
93
|
|
|
*/ |
94
|
1 |
|
public function send(Message $message) |
95
|
|
|
{ |
96
|
1 |
|
$rawMessage = $this->getMessageEncoder()->encode($message) . "\0"; |
97
|
|
|
|
98
|
|
|
// send message in one packet |
99
|
1 |
|
$this->socketClient->write($rawMessage); |
100
|
|
|
|
101
|
1 |
|
return 1; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
8 |
|
private function getScheme() |
108
|
|
|
{ |
109
|
8 |
|
return null === $this->sslOptions ? 'tcp' : 'ssl'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
8 |
|
private function getContext() |
116
|
|
|
{ |
117
|
8 |
|
if (null === $this->sslOptions) { |
118
|
8 |
|
return array(); |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
return $this->sslOptions->toStreamContext($this->host); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Sets the connect-timeout |
126
|
|
|
* |
127
|
|
|
* @param int $timeout |
128
|
|
|
*/ |
129
|
1 |
|
public function setConnectTimeout($timeout) |
130
|
|
|
{ |
131
|
1 |
|
$this->socketClient->setConnectTimeout($timeout); |
132
|
1 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the connect-timeout |
136
|
|
|
* |
137
|
|
|
* @return int |
138
|
|
|
*/ |
139
|
1 |
|
public function getConnectTimeout() |
140
|
|
|
{ |
141
|
1 |
|
return $this->socketClient->getConnectTimeout(); |
142
|
|
|
} |
143
|
|
|
|
144
|
8 |
|
public function setMessageEncoder(EncoderInterface $encoder) |
145
|
|
|
{ |
146
|
8 |
|
if (!$encoder instanceof NoNullByteEncoderInterface) { |
147
|
1 |
|
throw new InvalidArgumentException( |
148
|
1 |
|
"TcpTransport only works with NoNullByteEncoderInterface encoders" |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
8 |
|
return parent::setMessageEncoder($encoder); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|