Code Duplication    Length = 51-51 lines in 2 locations

PHPDaemon/Servers/WebSocket/Protocols/VE.php 1 location

@@ 138-188 (lines=51) @@
135
     */
136
    public function onRead()
137
    {
138
        while (($buflen = $this->getInputLength()) >= 2) {
139
            $hdr = $this->look(10);
140
            $frametype = ord(mb_orig_substr($hdr, 0, 1));
141
            if (($frametype & 0x80) === 0x80) {
142
                $len = 0;
143
                $i = 0;
144
                do {
145
                    if ($buflen < $i + 1) {
146
                        return;
147
                    }
148
                    $b = ord(mb_orig_substr($hdr, ++$i, 1));
149
                    $n = $b & 0x7F;
150
                    $len *= 0x80;
151
                    $len += $n;
152
                } while ($b > 0x80);
153
154
                if ($this->pool->maxAllowedPacket <= $len) {
155
                    // Too big packet
156
                    $this->finish();
157
                    return;
158
                }
159
160
                if ($buflen < $len + $i + 1) {
161
                    // not enough data yet
162
                    return;
163
                }
164
165
                $this->drain($i + 1);
166
                $this->onFrame($this->read($len), $frametype);
167
            } else {
168
                if (($p = $this->search("\xFF")) !== false) {
169
                    if ($this->pool->maxAllowedPacket <= $p - 1) {
170
                        // Too big packet
171
                        $this->finish();
172
                        return;
173
                    }
174
                    $this->drain(1);
175
                    $data = $this->read($p);
176
                    $this->drain(1);
177
                    $this->onFrame($data, 'STRING');
178
                } else {
179
                    if ($this->pool->maxAllowedPacket < $buflen - 1) {
180
                        // Too big packet
181
                        $this->finish();
182
                        return;
183
                    }
184
                }
185
            }
186
        }
187
    }
188
}
189

PHPDaemon/Servers/WebSocket/Protocols/V0.php 1 location

@@ 176-226 (lines=51) @@
173
            $this->handshake();
174
        }
175
        if ($this->state === self::STATE_HANDSHAKED) {
176
            while (($buflen = $this->getInputLength()) >= 2) {
177
                $hdr = $this->look(10);
178
                $frametype = ord(mb_orig_substr($hdr, 0, 1));
179
                if (($frametype & 0x80) === 0x80) {
180
                    $len = 0;
181
                    $i = 0;
182
                    do {
183
                        if ($buflen < $i + 1) {
184
                            // not enough data yet
185
                            return;
186
                        }
187
                        $b = ord(mb_orig_substr($hdr, ++$i, 1));
188
                        $n = $b & 0x7F;
189
                        $len *= 0x80;
190
                        $len += $n;
191
                    } while ($b > 0x80);
192
193
                    if ($this->pool->maxAllowedPacket <= $len) {
194
                        // Too big packet
195
                        $this->finish();
196
                        return;
197
                    }
198
199
                    if ($buflen < $len + $i + 1) {
200
                        // not enough data yet
201
                        return;
202
                    }
203
                    $this->drain($i + 1);
204
                    $this->onFrame($this->read($len), 'BINARY');
205
                } else {
206
                    if (($p = $this->search("\xFF")) !== false) {
207
                        if ($this->pool->maxAllowedPacket <= $p - 1) {
208
                            // Too big packet
209
                            $this->finish();
210
                            return;
211
                        }
212
                        $this->drain(1);
213
                        $data = $this->read($p);
214
                        $this->drain(1);
215
                        $this->onFrame($data, 'STRING');
216
                    } else {
217
                        if ($this->pool->maxAllowedPacket < $buflen - 1) {
218
                            // Too big packet
219
                            $this->finish();
220
                            return;
221
                        }
222
                        // not enough data yet
223
                        return;
224
                    }
225
                }
226
            }
227
        }
228
    }
229
}