Code Duplication    Length = 51-51 lines in 2 locations

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

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

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

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