Connection::requestInfo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
nop 1
1
<?php
2
namespace PHPDaemon\Clients\Valve;
3
4
use PHPDaemon\Network\ClientConnection;
5
use PHPDaemon\Utils\Binary;
6
use PHPDaemon\Utils\Encoding;
7
8
/**
9
 * @package    NetworkClients
10
 * @subpackage HLClient
11
 * @author     Vasily Zorin <[email protected]>
12
 */
13
class Connection extends ClientConnection
14
{
15
    /**
16
     * @var integer Timeout
17
     */
18
    public $timeout = 1;
19
20
    /**
21
     * Sends a request of type 'players'
22
     * @param  callable $cb Callback
23
     * @callback $cb ( )
24
     * @return void
25
     */
26 View Code Duplication
    public function requestPlayers($cb)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28
        $this->request('challenge', null, function ($conn, $result) use ($cb) {
29
            if (is_array($result)) {
30
                $cb($conn, $result);
31
                return;
32
            }
33
            $conn->request('players', $result, $cb);
34
        });
35
    }
36
37
    /**
38
     * Sends a request of type 'info'
39
     * @param  callable $cb Callback
40
     * @callback $cb ( )
41
     * @return void
42
     */
43
    public function requestInfo($cb)
44
    {
45
        $this->request('info', null, $cb);
46
    }
47
48
    /**
49
     * Sends a request
50
     * @param  string $type Type of request
51
     * @param  string $data Data
0 ignored issues
show
Documentation introduced by
Should the type for parameter $data not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
52
     * @param  callable $cb Callback
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cb not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
53
     * @callback $cb ( )
54
     * @return void
55
     */
56
    public function request($type, $data = null, $cb = null)
57
    {
58
        $packet = "\xFF\xFF\xFF\xFF";
59
        if ($type === 'ping') {
60
            $packet .= Pool::A2A_PING;
61
        } elseif ($type === 'challenge') {
62
            //$packet .= ValveClient::A2S_SERVERQUERY_GETCHALLENGE;
63
            $packet .= Pool::A2S_PLAYER . "\xFF\xFF\xFF\xFF";
64
        } elseif ($type === 'info') {
65
            $packet .= Pool::A2S_INFO . "Source Engine Query\x00";
66
            //"\xFF\xFF\xFF\xFFdetails\x00"
67
        } elseif ($type === 'players') {
68
            if ($data === null) {
69
                $data = "\xFF\xFF\xFF\xFF";
70
            }
71
            $packet .= Pool::A2S_PLAYER . $data;
72
        } else {
73
            return null;
74
        }
75
        $this->onResponse->push($cb);
0 ignored issues
show
Bug introduced by
It seems like $cb defined by parameter $cb on line 56 can also be of type null; however, PHPDaemon\Structures\StackCallbacks::push() does only seem to accept callable, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
76
        $this->setFree(false);
77
        //Daemon::log('packet: '.Debug::exportBytes($packet, true));
78
        $this->write($packet);
79
    }
80
81
    /**
82
     * Called when new data received
83
     * @return void
84
     */
85
    protected function onRead()
86
    {
87
        start:
88
        if ($this->getInputLength() < 5) {
89
            return;
90
        }
91
        /* @TODO: refactoring Binary::* to support direct buffer calls */
92
        $pct = $this->read(4096);
93
        $h = Binary::getDWord($pct);
94
        if ($h !== 0xFFFFFFFF) {
95
            $this->finish();
96
            return;
97
        }
98
        $type = Binary::getChar($pct);
99
        if (($type === Pool::S2A_INFO) || ($type === Pool::S2A_INFO_SOURCE)) {
100
            $result = self::parseInfo($pct, $type);
101
        } elseif ($type === Pool::S2A_PLAYER) {
102
            $result = self::parsePlayers($pct);
103
        } elseif ($type === Pool::S2A_SERVERQUERY_GETCHALLENGE) {
104
            $result = mb_orig_substr($pct, 0, 4);
105
            $pct = mb_orig_substr($pct, 5);
0 ignored issues
show
Unused Code introduced by
$pct is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
106
        } elseif ($type === Pool::S2A_PONG) {
107
            $result = true;
108
        } else {
109
            $result = null;
110
        }
111
        $this->onResponse->executeOne($this, $result);
112
        $this->checkFree();
113
        goto start;
114
    }
115
116
    /**
117
     * Parses response to 'players' command into structure
118
     * @param  string &$st Data
119
     * @return array       Structure
120
     */
121
    public static function parsePlayers(&$st)
122
    {
123
        $playersn = Binary::getByte($st);
124
        $players = [];
125
        for ($i = 1; $i < $playersn; ++$i) {
126
            $n = Binary::getByte($st);
0 ignored issues
show
Unused Code introduced by
$n is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
127
            $name = Binary::getString($st);
128
            $score = Binary::getDWord($st, true);
129
            if (mb_orig_strlen($st) === 0) {
130
                break;
131
            }
132
            $u = unpack('f', mb_orig_substr($st, 0, 4));
133
            $st = mb_orig_substr($st, 4);
134
            $seconds = $u[1];
135
            if ($seconds === -1) {
136
                continue;
137
            }
138
            $players[] = [
139
                'name' => Encoding::toUTF8($name),
140
                'score' => $score,
141
                'seconds' => $seconds,
142
                'joinedts' => microtime(true) - $seconds,
143
                'spm' => $score / ($seconds / 60),
144
            ];
145
        }
146
        return $players;
147
    }
148
149
    /**
150
     * Parses response to 'info' command into structure
151
     * @param  string &$st Data
152
     * @param  string $type Type of request
153
     * @return array        Structure
154
     */
155
    public static function parseInfo(&$st, $type)
156
    {
157
        $info = [];
158
        if ($type === Pool::S2A_INFO) {
159
            $info['proto'] = Binary::getByte($st);
160
            $info['hostname'] = Binary::getString($st);
161
            $info['map'] = Binary::getString($st);
162
            $info['gamedir'] = Binary::getString($st);
163
            $info['gamedescr'] = Binary::getString($st);
164
            $info['steamid'] = Binary::getWord($st);
165
            $info['playersnum'] = Binary::getByte($st);
166
            $info['playersmax'] = Binary::getByte($st);
167
            $info['botcount'] = Binary::getByte($st);
168
            $info['servertype'] = Binary::getChar($st);
169
            $info['serveros'] = Binary::getChar($st);
170
            $info['passworded'] = Binary::getByte($st);
171
            $info['secure'] = Binary::getByte($st);
172
        } elseif ($type === Pool::S2A_INFO_SOURCE) {
173
            $info['srvaddress'] = Binary::getString($st);
174
            $info['hostname'] = Binary::getString($st);
175
            $info['map'] = Binary::getString($st);
176
            $info['gamedir'] = Binary::getString($st);
177
            $info['gamedescr'] = Binary::getString($st);
178
            $info['playersnum'] = Binary::getByte($st);
179
            $info['playersmax'] = Binary::getByte($st);
180
            $info['proto'] = Binary::getByte($st);
181
            $info['servertype'] = Binary::getChar($st);
182
            $info['serveros'] = Binary::getChar($st);
183
            $info['passworded'] = Binary::getByte($st);
184
            $info['modded'] = Binary::getByte($st);
185
            if ($info['modded']) {
186
                $info['mod_website'] = Binary::getString($st);
187
                $info['mod_downloadserver'] = Binary::getString($st);
188
                $info['mod_unused'] = Binary::getString($st);
189
                $info['mod_version'] = Binary::getDWord($st, true);
190
                $info['mod_size'] = Binary::getDWord($st);
191
                $info['mod_serverside'] = Binary::getByte($st);
192
                $info['mod_customdll'] = Binary::getByte($st);
193
            }
194
            $info['secure'] = Binary::getByte($st);
195
            $info['botsnum'] = Binary::getByte($st);
196
        }
197
        foreach ($info as &$val) {
198
            if (is_string($val)) {
199
                $val = Encoding::toUTF8($val);
200
            }
201
        }
202
        return $info;
203
    }
204
}
205