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