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\MessageInterface as Message; |
15
|
|
|
use Gelf\Encoder\JsonEncoder as DefaultEncoder; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* TcpTransport allows the transfer of GELF-messages (with SSL/TLS support) |
19
|
|
|
* to a compatible GELF-TCP-backend as described in |
20
|
|
|
* https://github.com/Graylog2/graylog2-docs/wiki/GELF |
21
|
|
|
* |
22
|
|
|
* It can also act as a direct publisher |
23
|
|
|
* |
24
|
|
|
* @author Benjamin Zikarsky <[email protected]> |
25
|
|
|
* @author Ahmed Trabelsi <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class TcpTransport extends AbstractTransport |
28
|
|
|
{ |
29
|
|
|
const DEFAULT_HOST = "127.0.0.1"; |
|
|
|
|
30
|
|
|
const DEFAULT_PORT = 12201; |
31
|
|
|
|
32
|
|
|
const AUTO_SSL_PORT = 12202; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $host; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
protected $port; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var StreamSocketClient |
46
|
|
|
*/ |
47
|
|
|
protected $socketClient; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var SslOptions|null |
51
|
|
|
*/ |
52
|
|
|
protected $sslOptions = null; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Class constructor |
56
|
|
|
* |
57
|
|
|
* @param string|null $host when NULL or empty default-host is used |
58
|
|
|
* @param int|null $port when NULL or empty default-port is used |
59
|
|
|
* @param SslOptions|null $sslOptions when null not SSL is used |
60
|
|
|
*/ |
61
|
6 |
|
public function __construct( |
62
|
|
|
$host = self::DEFAULT_HOST, |
63
|
|
|
$port = self::DEFAULT_PORT, |
64
|
|
|
SslOptions $sslOptions = null |
65
|
|
|
) { |
66
|
6 |
|
$this->host = $host; |
67
|
6 |
|
$this->port = $port; |
68
|
|
|
|
69
|
6 |
|
if ($port == self::AUTO_SSL_PORT && $sslOptions == null) { |
70
|
1 |
|
$sslOptions = new SslOptions(); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
6 |
|
$this->sslOptions = $sslOptions; |
74
|
|
|
|
75
|
6 |
|
$this->messageEncoder = new DefaultEncoder(); |
76
|
6 |
|
$this->socketClient = new StreamSocketClient( |
|
|
|
|
77
|
6 |
|
$this->getScheme(), |
78
|
6 |
|
$this->host, |
79
|
6 |
|
$this->port, |
80
|
6 |
|
$this->getContext() |
81
|
6 |
|
); |
82
|
6 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sends a Message over this transport |
86
|
|
|
* |
87
|
|
|
* @param Message $message |
88
|
|
|
* |
89
|
|
|
* @return int the number of TCP packets sent |
90
|
|
|
*/ |
91
|
1 |
|
public function send(Message $message) |
92
|
|
|
{ |
93
|
1 |
|
$rawMessage = $this->getMessageEncoder()->encode($message) . "\0"; |
94
|
|
|
|
95
|
|
|
// send message in one packet |
96
|
1 |
|
$this->socketClient->write($rawMessage); |
97
|
|
|
|
98
|
1 |
|
return 1; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
6 |
|
private function getScheme() |
105
|
|
|
{ |
106
|
6 |
|
return null === $this->sslOptions ? 'tcp' : 'ssl'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
6 |
|
private function getContext() |
113
|
|
|
{ |
114
|
6 |
|
if (null === $this->sslOptions) { |
115
|
6 |
|
return array(); |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
return $this->sslOptions->toStreamContext($this->host); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Sets the connect-timeout |
123
|
|
|
* |
124
|
|
|
* @param int $timeout |
125
|
|
|
*/ |
126
|
1 |
|
public function setConnectTimeout($timeout) |
127
|
|
|
{ |
128
|
1 |
|
$this->socketClient->setConnectTimeout($timeout); |
129
|
1 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the connect-timeout |
133
|
|
|
* |
134
|
|
|
* @return int |
135
|
|
|
*/ |
136
|
1 |
|
public function getConnectTimeout() |
137
|
|
|
{ |
138
|
1 |
|
return $this->socketClient->getConnectTimeout(); |
139
|
|
|
} |
140
|
|
|
} |
|
|
|
|
141
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.