Code Duplication    Length = 51-51 lines in 2 locations

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

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

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

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