| @@ 113-160 (lines=48) @@ | ||
| 110 | * @callback $cb ( ) |
|
| 111 | * @return boolean Success. |
|
| 112 | */ |
|
| 113 | public function sendFrame($data, $type = null, $cb = null) |
|
| 114 | { |
|
| 115 | if (!$this->handshaked) { |
|
| 116 | return false; |
|
| 117 | } |
|
| 118 | ||
| 119 | if ($this->finished && $type !== 'CONNCLOSE') { |
|
| 120 | return false; |
|
| 121 | } |
|
| 122 | if ($type === 'CONNCLOSE') { |
|
| 123 | if ($cb !== null) { |
|
| 124 | $cb($this); |
|
| 125 | return true; |
|
| 126 | } |
|
| 127 | } |
|
| 128 | ||
| 129 | $type = $this->getFrameType($type); |
|
| 130 | // Binary |
|
| 131 | if (($type & self::BINARY) === self::BINARY) { |
|
| 132 | $n = mb_orig_strlen($data); |
|
| 133 | $len = ''; |
|
| 134 | $pos = 0; |
|
| 135 | ||
| 136 | char: |
|
| 137 | ||
| 138 | ++$pos; |
|
| 139 | $c = $n >> 0 & 0x7F; |
|
| 140 | $n >>= 7; |
|
| 141 | ||
| 142 | if ($pos !== 1) { |
|
| 143 | $c += 0x80; |
|
| 144 | } |
|
| 145 | ||
| 146 | if ($c !== 0x80) { |
|
| 147 | $len = chr($c) . $len; |
|
| 148 | goto char; |
|
| 149 | }; |
|
| 150 | ||
| 151 | $this->write(chr(self::BINARY) . $len . $data); |
|
| 152 | } // String |
|
| 153 | else { |
|
| 154 | $this->write(chr(self::STRING) . $data . "\xFF"); |
|
| 155 | } |
|
| 156 | if ($cb !== null) { |
|
| 157 | $this->onWriteOnce($cb); |
|
| 158 | } |
|
| 159 | return true; |
|
| 160 | } |
|
| 161 | ||
| 162 | /** |
|
| 163 | * Called when new data received |
|
| @@ 82-130 (lines=49) @@ | ||
| 79 | * @callback $cb ( ) |
|
| 80 | * @return boolean Success. |
|
| 81 | */ |
|
| 82 | public function sendFrame($data, $type = null, $cb = null) |
|
| 83 | { |
|
| 84 | if (!$this->handshaked) { |
|
| 85 | return false; |
|
| 86 | } |
|
| 87 | ||
| 88 | if ($this->finished && $type !== 'CONNCLOSE') { |
|
| 89 | return false; |
|
| 90 | } |
|
| 91 | ||
| 92 | if ($type === 'CONNCLOSE') { |
|
| 93 | if ($cb !== null) { |
|
| 94 | $cb($this); |
|
| 95 | return true; |
|
| 96 | } |
|
| 97 | } |
|
| 98 | ||
| 99 | // Binary |
|
| 100 | $type = $this->getFrameType($type); |
|
| 101 | if (($type & self::BINARY) === self::BINARY) { |
|
| 102 | $n = mb_orig_strlen($data); |
|
| 103 | $len = ''; |
|
| 104 | $pos = 0; |
|
| 105 | ||
| 106 | char: |
|
| 107 | ||
| 108 | ++$pos; |
|
| 109 | $c = $n >> 0 & 0x7F; |
|
| 110 | $n >>= 7; |
|
| 111 | ||
| 112 | if ($pos !== 1) { |
|
| 113 | $c += 0x80; |
|
| 114 | } |
|
| 115 | ||
| 116 | if ($c !== 0x80) { |
|
| 117 | $len = chr($c) . $len; |
|
| 118 | goto char; |
|
| 119 | }; |
|
| 120 | ||
| 121 | $this->write(chr(self::BINARY) . $len . $data); |
|
| 122 | } // String |
|
| 123 | else { |
|
| 124 | $this->write(chr(self::STRING) . $data . "\xFF"); |
|
| 125 | } |
|
| 126 | if ($cb !== null) { |
|
| 127 | $this->onWriteOnce($cb); |
|
| 128 | } |
|
| 129 | return true; |
|
| 130 | } |
|
| 131 | ||
| 132 | /** |
|
| 133 | * Called when new data received |
|