/usr/share/php/PhpAmqpLib/Channel/AbstractChannel.php is in php-amqplib 2.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | <?php
namespace PhpAmqpLib\Channel;
use PhpAmqpLib\Connection\AbstractConnection;
use PhpAmqpLib\Exception\AMQPOutOfBoundsException;
use PhpAmqpLib\Exception\AMQPRuntimeException;
use PhpAmqpLib\Helper\MiscHelper;
use PhpAmqpLib\Helper\Protocol\MethodMap080;
use PhpAmqpLib\Helper\Protocol\MethodMap091;
use PhpAmqpLib\Helper\Protocol\Protocol080;
use PhpAmqpLib\Helper\Protocol\Protocol091;
use PhpAmqpLib\Helper\Protocol\Wait080;
use PhpAmqpLib\Helper\Protocol\Wait091;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPReader;
class AbstractChannel
{
public static $PROTOCOL_CONSTANTS_CLASS;
/**
* @var array
*/
protected $frame_queue;
/**
* @var array
*/
protected $method_queue;
/**
* @var bool
*/
protected $auto_decode;
/**
* @var string
*/
protected $amqp_protocol_header;
/**
* @var bool
*/
protected $debug;
/**
* @var AbstractConnection
*/
protected $connection;
/**
* @var string
*/
protected $protocolVersion;
/**
* @var \PhpAmqpLib\Helper\Protocol\Protocol091|\PhpAmqpLib\Helper\Protocol\Protocol080
*/
protected $protocolWriter;
/**
* @var \PhpAmqpLib\Helper\Protocol\Wait091|\PhpAmqpLib\Helper\Protocol\Wait080
*/
protected $waitHelper;
/**
* @var \PhpAmqpLib\Helper\Protocol\MethodMap091|\PhpAmqpLib\Helper\Protocol\MethodMap080
*/
protected $methodMap;
/**
* @var string
*/
protected $channel_id;
/**
* @var \PhpAmqpLib\Wire\AMQPReader
*/
protected $msg_property_reader = null;
/**
* @var \PhpAmqpLib\Wire\AMQPReader
*/
protected $wait_content_reader = null;
/**
* @var \PhpAmqpLib\Wire\AMQPReader
*/
protected $dispatch_reader = null;
/**
* @param \PhpAmqpLib\Connection\AbstractConnection $connection
* @param string $channel_id
*/
public function __construct(AbstractConnection $connection, $channel_id)
{
$this->connection = $connection;
$this->channel_id = $channel_id;
$connection->channels[$channel_id] = $this;
$this->frame_queue = array(); // Lower level queue for frames
$this->method_queue = array(); // Higher level queue for methods
$this->auto_decode = false;
$this->debug = defined('AMQP_DEBUG') ? AMQP_DEBUG : false;
$this->msg_property_reader = new AMQPReader(null);
$this->wait_content_reader = new AMQPReader(null);
$this->dispatch_reader = new AMQPReader(null);
$this->protocolVersion = defined('AMQP_PROTOCOL') ? AMQP_PROTOCOL : '0.9.1';
switch ($this->protocolVersion) {
case '0.9.1':
self::$PROTOCOL_CONSTANTS_CLASS = 'PhpAmqpLib\Wire\Constants091';
$c = self::$PROTOCOL_CONSTANTS_CLASS;
$this->amqp_protocol_header = $c::$AMQP_PROTOCOL_HEADER;
$this->protocolWriter = new Protocol091();
$this->waitHelper = new Wait091();
$this->methodMap = new MethodMap091();
break;
case '0.8':
self::$PROTOCOL_CONSTANTS_CLASS = 'PhpAmqpLib\Wire\Constants080';
$c = self::$PROTOCOL_CONSTANTS_CLASS;
$this->amqp_protocol_header = $c::$AMQP_PROTOCOL_HEADER;
$this->protocolWriter = new Protocol080();
$this->waitHelper = new Wait080();
$this->methodMap = new MethodMap080();
break;
default:
throw new AMQPRuntimeException('Protocol: ' . $this->protocolVersion . ' not implemented.');
}
}
public function getChannelId()
{
return $this->channel_id;
}
/**
* @param string $method_sig
* @param string $args
* @param $content
* @return mixed
*/
public function dispatch($method_sig, $args, $content)
{
if (!$this->methodMap->valid_method($method_sig)) {
throw new AMQPRuntimeException("Unknown AMQP method $method_sig");
}
$amqp_method = $this->methodMap->get_method($method_sig);
if (!method_exists($this, $amqp_method)) {
throw new AMQPRuntimeException("Method: $amqp_method not implemented by class: " . get_class($this));
}
$this->dispatch_reader->reuse($args);
if ($content == null) {
return call_user_func(array($this, $amqp_method), $this->dispatch_reader);
}
return call_user_func(array($this, $amqp_method), $this->dispatch_reader, $content);
}
public function next_frame($timeout = 0)
{
if ($this->debug) {
MiscHelper::debug_msg("waiting for a new frame");
}
if (!empty($this->frame_queue)) {
return array_shift($this->frame_queue);
}
return $this->connection->wait_channel($this->channel_id, $timeout);
}
protected function send_method_frame($method_sig, $args = "")
{
$this->connection->send_channel_method_frame($this->channel_id, $method_sig, $args);
}
/**
* This is here for performance reasons to batch calls to fwrite from basic.publish
*/
protected function prepare_method_frame($method_sig, $args = "", $pkt = null)
{
return $this->connection->prepare_channel_method_frame($this->channel_id, $method_sig, $args, $pkt);
}
public function wait_content()
{
$frm = $this->next_frame();
$frame_type = $frm[0];
$payload = $frm[1];
if ($frame_type != 2) {
throw new AMQPRuntimeException("Expecting Content header");
}
$this->wait_content_reader->reuse(mb_substr($payload, 0, 12, 'ASCII'));
// $payload_reader = new AMQPReader(substr($payload,0,12));
$class_id = $this->wait_content_reader->read_short();
$weight = $this->wait_content_reader->read_short();
$body_size = $this->wait_content_reader->read_longlong();
//hack to avoid creating new instances of AMQPReader;
$this->msg_property_reader->reuse(mb_substr($payload, 12, mb_strlen($payload, 'ASCII') - 12, 'ASCII'));
$msg = new AMQPMessage();
$msg->load_properties($this->msg_property_reader);
$body_parts = array();
$body_received = 0;
while (bccomp($body_size, $body_received) == 1) {
$frm = $this->next_frame();
$frame_type = $frm[0];
$payload = $frm[1];
if ($frame_type != 3) {
$PROTOCOL_CONSTANTS_CLASS = self::$PROTOCOL_CONSTANTS_CLASS;
throw new AMQPRuntimeException("Expecting Content body, received frame type $frame_type ("
. $PROTOCOL_CONSTANTS_CLASS::$FRAME_TYPES[$frame_type] . ")");
}
$body_parts[] = $payload;
$body_received = bcadd($body_received, mb_strlen($payload, 'ASCII'));
}
$msg->body = implode("", $body_parts);
if ($this->auto_decode && isset($msg->content_encoding)) {
try {
$msg->body = $msg->body->decode($msg->content_encoding);
} catch (\Exception $e) {
if ($this->debug) {
MiscHelper::debug_msg("Ignoring body decoding exception: " . $e->getMessage());
}
}
}
return $msg;
}
/**
* Wait for some expected AMQP methods and dispatch to them.
* Unexpected methods are queued up for later calls to this PHP
* method.
*
* @param array $allowed_methods
* @param bool $non_blocking
* @param int $timeout
* @return mixed
*/
public function wait($allowed_methods = null, $non_blocking = false, $timeout = 0)
{
$PROTOCOL_CONSTANTS_CLASS = self::$PROTOCOL_CONSTANTS_CLASS;
if ($allowed_methods && $this->debug) {
MiscHelper::debug_msg("waiting for " . implode(", ", $allowed_methods));
} elseif ($this->debug) {
MiscHelper::debug_msg("waiting for any method");
}
//Process deferred methods
foreach ($this->method_queue as $qk => $queued_method) {
if ($this->debug) {
MiscHelper::debug_msg("checking queue method " . $qk);
}
$method_sig = $queued_method[0];
if ($allowed_methods == null || in_array($method_sig, $allowed_methods)) {
unset($this->method_queue[$qk]);
if ($this->debug) {
MiscHelper::debug_msg("Executing queued method: $method_sig: " .
$PROTOCOL_CONSTANTS_CLASS::$GLOBAL_METHOD_NAMES[MiscHelper::methodSig($method_sig)]);
}
return $this->dispatch($queued_method[0], $queued_method[1], $queued_method[2]);
}
}
// No deferred methods? wait for new ones
while (true) {
$frm = $this->next_frame($timeout);
$frame_type = $frm[0];
$payload = $frm[1];
if ($frame_type != 1) {
throw new AMQPRuntimeException("Expecting AMQP method, received frame type: $frame_type ("
. $PROTOCOL_CONSTANTS_CLASS::$FRAME_TYPES[$frame_type] . ")");
}
if (mb_strlen($payload, 'ASCII') < 4) {
throw new AMQPOutOfBoundsException("Method frame too short");
}
$method_sig_array = unpack("n2", mb_substr($payload, 0, 4, 'ASCII'));
$method_sig = "" . $method_sig_array[1] . "," . $method_sig_array[2];
$args = mb_substr($payload, 4, mb_strlen($payload, 'ASCII') - 4, 'ASCII');
if ($this->debug) {
MiscHelper::debug_msg("> $method_sig: "
. $PROTOCOL_CONSTANTS_CLASS::$GLOBAL_METHOD_NAMES[MiscHelper::methodSig($method_sig)]);
}
if (in_array($method_sig, $PROTOCOL_CONSTANTS_CLASS::$CONTENT_METHODS)) {
$content = $this->wait_content();
} else {
$content = null;
}
if ($allowed_methods == null ||
in_array($method_sig, $allowed_methods) ||
in_array($method_sig, $PROTOCOL_CONSTANTS_CLASS::$CLOSE_METHODS)
) {
return $this->dispatch($method_sig, $args, $content);
}
// Wasn't what we were looking for? save it for later
if ($this->debug) {
MiscHelper::debug_msg("Queueing for later: $method_sig: "
. $PROTOCOL_CONSTANTS_CLASS::$GLOBAL_METHOD_NAMES[MiscHelper::methodSig($method_sig)]);
}
$this->method_queue[] = array($method_sig, $args, $content);
if ($non_blocking) {
break;
}
}
}
protected function dispatch_to_handler($handler, array $arguments)
{
if (is_callable($handler)) {
call_user_func_array($handler, $arguments);
}
}
}
|