summaryrefslogtreecommitdiff
path: root/vendor/jonahgeorge/jaeger-client-php/src/Jaeger/ThriftUdpTransport.php
blob: babc689dcc67bc4838ef9637a805de019ad8cfdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php

namespace Jaeger;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Thrift\Transport\TTransport;

class ThriftUdpTransport extends TTransport
{
    private $socket;

    /**
     * @var string
     */
    private $host;

    /**
     * @var int
     */
    private $port;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var Config
     */
    private $config;

    /**
     * ThriftUdpTransport constructor.
     * @param string $host
     * @param int $port
     * @param LoggerInterface $logger
     */
    public function __construct(string $host, int $port, LoggerInterface $logger = null, Config $config = null)
    {
        $this->setLogger($logger);

        $this->config = $config;

        $ipProtocol = $this->ipProtocolVersion();
        $this->socket = $this->createSocket($ipProtocol);

        $this->host = $host;
        $this->port = $port;
    }

    protected function setLogger($logger)
    {
        $this->logger = $logger ?? new NullLogger();
    }

    protected function createSocket(string $ipProtocol)
    {
        $socketDomain = AF_INET;
        if ($ipProtocol === Config::IPV6) {
            $socketDomain = AF_INET6;
        }

        $socket = @socket_create($socketDomain, SOCK_DGRAM, SOL_UDP);
        if ($socket === false) {
            $this->handleSocketError("socket_create failed");
        }
        return $socket;
    }

    protected function ipProtocolVersion()
    {
        if (!empty($this->config)) {
            return $this->config->ipProtocolVersion();
        }
        return "";
    }

    /**
     * Whether this transport is open.
     *
     * @return boolean true if open
     */
    public function isOpen()
    {
        return $this->socket !== null;
    }

    /**
     * Open the transport for reading/writing
     */
    public function open()
    {
        $ok = @socket_connect($this->socket, $this->host, $this->port);
        if ($ok === false) {
            $this->handleSocketError('socket_connect failed');
        }
    }

    /**
     * Close the transport.
     */
    public function close()
    {
        if (is_null($this->socket)) {
            $this->logger->warning("can't close empty socket");
            return ;
        }

        @socket_close($this->socket);
        $this->socket = null;
    }

    /**
     * Read some data into the array.
     *
     * @todo
     *
     * @param int $len How much to read
     * @return string The data that has been read
     */
    public function read($len)
    {
    }

    /**
     * Writes the given data out.
     *
     * @param string $buf The data to write
     */
    public function write($buf)
    {
        if (!$this->isOpen()) {
            $this->logger->warning('transport is closed');
            return ;
        }

        $ok = @socket_write($this->socket, $buf);
        if ($ok === false) {
            $this->handleSocketError("socket_write failed");
        }
    }

    public function handleSocketError($msg)
    {
        $errorCode = socket_last_error($this->socket);
        $errorMsg = socket_strerror($errorCode);

        $this->logger->warning(sprintf('%s: [code - %d] %s', $msg, $errorCode, $errorMsg));
    }
}