forked from EvilFreelancer/routeros-api-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPIConnector.php
69 lines (62 loc) · 1.62 KB
/
APIConnector.php
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
<?php
namespace RouterOS;
use RouterOS\Interfaces\StreamInterface;
/**
* Class APIConnector
*
* Implement middle level dialog with router by masking word dialog implementation to client class
*
* @package RouterOS
* @since 0.9
*/
class APIConnector
{
/**
* @var StreamInterface $stream The stream used to communicate with the router
*/
protected $stream;
/**
* APIConnector constructor.
*
* @param \RouterOS\Interfaces\StreamInterface $stream
*/
public function __construct(StreamInterface $stream)
{
$this->stream = $stream;
}
/**
* Close stream connection
*
* @return void
*/
public function close(): void
{
$this->stream->close();
}
/**
* Reads a WORD from the stream
*
* WORDs are part of SENTENCE. Each WORD has to be encoded in certain way - length of the WORD followed by WORD content.
* Length of the WORD should be given as count of bytes that are going to be sent
*
* @return string The word content, en empty string for end of SENTENCE
*/
public function readWord(): string
{
// Get length of next word
$length = APILengthCoDec::decodeLength($this->stream);
return ($length > 0) ? $this->stream->read($length) : '';
}
/**
* Write word to stream
*
* @param string $word
*
* @return int return number of written bytes
*/
public function writeWord(string $word): int
{
$encodedLength = APILengthCoDec::encodeLength(strlen($word));
return $this->stream->write($encodedLength . $word);
}
}