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
|
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2007 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Alexandre Kalendarev akalend@mail.ru Copyright (c) 2009-2010 |
| Lead: |
| - Pieter de Zwart |
| Maintainers: |
| - Brad Rodriguez |
| - Jonathan Tansavatdi |
+----------------------------------------------------------------------+
*/
#include "amqp_basic_properties.h"
#include "amqp_methods_handling.h"
/* taken from rabbbitmq-c */
static int amqp_id_in_reply_list(amqp_method_number_t expected, amqp_method_number_t *list)
{
while (*list != 0) {
if (*list == expected) {
return 1;
}
list++;
}
return 0;
}
/* taken from rabbbitmq-c */
int amqp_simple_wait_method_list_noblock(
amqp_connection_state_t state,
amqp_channel_t expected_channel,
amqp_method_number_t *expected_methods,
amqp_method_t *output,
struct timeval *timeout
)
{
amqp_frame_t frame;
int res = amqp_simple_wait_frame_noblock(state, &frame, timeout);
if (AMQP_STATUS_OK != res) {
return res;
}
if (AMQP_FRAME_METHOD != frame.frame_type || expected_channel != frame.channel ||
!amqp_id_in_reply_list(frame.payload.method.id, expected_methods)) {
if (AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id ||
AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id) {
*output = frame.payload.method;
return AMQP_RESPONSE_SERVER_EXCEPTION;
}
return AMQP_STATUS_WRONG_METHOD;
}
*output = frame.payload.method;
return AMQP_STATUS_OK;
}
/* taken from rabbbitmq-c */
int amqp_simple_wait_method_noblock(
amqp_connection_state_t state,
amqp_channel_t expected_channel,
amqp_method_number_t expected_method,
amqp_method_t *output,
struct timeval *timeout
)
{
amqp_method_number_t expected_methods[] = {0, 0};
expected_methods[0] = expected_method;
return amqp_simple_wait_method_list_noblock(state, expected_channel, expected_methods, output, timeout);
}
int php_amqp_handle_basic_return(char **message, amqp_channel_object *channel, amqp_method_t *method)
{
amqp_rpc_reply_t ret;
amqp_message_t msg;
int status = PHP_AMQP_RESOURCE_RESPONSE_OK;
assert(AMQP_BASIC_RETURN_METHOD == method->id);
amqp_basic_return_t *m = (amqp_basic_return_t *) method->decoded;
ret = amqp_read_message(
channel->channel_resource->connection_resource->connection_state,
channel->channel_resource->channel_id,
&msg,
0
);
if (AMQP_RESPONSE_NORMAL != ret.reply_type) {
return php_amqp_connection_resource_error(
ret,
message,
channel->channel_resource->connection_resource,
channel->channel_resource->channel_id
);
}
if (channel->callbacks.basic_return.fci.size > 0) {
status = php_amqp_call_basic_return_callback(m, &msg, &channel->callbacks.basic_return);
} else {
zend_error(
E_NOTICE,
"Unhandled basic.return method from server received. Use AMQPChannel::setReturnCallback() to process it."
);
status = PHP_AMQP_RESOURCE_RESPONSE_BREAK;
}
amqp_destroy_message(&msg);
return status;
}
int php_amqp_call_basic_return_callback(amqp_basic_return_t *m, amqp_message_t *msg, amqp_callback_bucket *cb)
{
zval params;
zval basic_properties;
int status = PHP_AMQP_RESOURCE_RESPONSE_OK;
ZVAL_UNDEF(¶ms);
array_init(¶ms);
ZVAL_UNDEF(&basic_properties);
/* callback(int $reply_code, string $reply_text, string $exchange, string $routing_key, AMQPBasicProperties $properties, string $body); */
add_next_index_long(¶ms, (zend_long) m->reply_code);
add_next_index_stringl(¶ms, m->reply_text.bytes, m->reply_text.len);
add_next_index_stringl(¶ms, m->exchange.bytes, m->exchange.len);
add_next_index_stringl(¶ms, m->routing_key.bytes, m->routing_key.len);
php_amqp_basic_properties_to_zval(&msg->properties, &basic_properties);
add_next_index_zval(¶ms, &basic_properties);
Z_ADDREF_P(&basic_properties);
add_next_index_stringl(¶ms, msg->body.bytes, msg->body.len);
status = php_amqp_call_callback_with_params(params, cb);
zval_ptr_dtor(&basic_properties);
return status;
}
int php_amqp_handle_basic_ack(char **message, amqp_channel_object *channel, amqp_method_t *method)
{
assert(AMQP_BASIC_ACK_METHOD == method->id);
amqp_basic_ack_t *m = (amqp_basic_ack_t *) method->decoded;
if (channel->callbacks.basic_ack.fci.size > 0) {
return php_amqp_call_basic_ack_callback(m, &channel->callbacks.basic_ack);
}
zend_error(
E_NOTICE,
"Unhandled basic.ack method from server received. Use AMQPChannel::setConfirmCallback() to process it."
);
return PHP_AMQP_RESOURCE_RESPONSE_BREAK;
}
int php_amqp_call_basic_ack_callback(amqp_basic_ack_t *m, amqp_callback_bucket *cb)
{
zval params;
ZVAL_UNDEF(¶ms);
array_init(¶ms);
/* callback(int $delivery_tag, bool $multiple); */
add_next_index_long(¶ms, (zend_long) m->delivery_tag);
add_next_index_bool(¶ms, m->multiple);
return php_amqp_call_callback_with_params(params, cb);
}
int php_amqp_handle_basic_nack(char **message, amqp_channel_object *channel, amqp_method_t *method)
{
int status = PHP_AMQP_RESOURCE_RESPONSE_OK;
assert(AMQP_BASIC_NACK_METHOD == method->id);
amqp_basic_nack_t *m = (amqp_basic_nack_t *) method->decoded;
if (channel->callbacks.basic_nack.fci.size > 0) {
status = php_amqp_call_basic_nack_callback(m, &channel->callbacks.basic_nack);
} else {
zend_error(
E_NOTICE,
"Unhandled basic.nack method from server received. Use AMQPChannel::setConfirmCallback() to process it."
);
status = PHP_AMQP_RESOURCE_RESPONSE_BREAK;
}
return status;
}
int php_amqp_call_basic_nack_callback(amqp_basic_nack_t *m, amqp_callback_bucket *cb)
{
zval params;
ZVAL_UNDEF(¶ms);
array_init(¶ms);
/* callback(int $delivery_tag, bool $multiple, bool $requeue); */
add_next_index_long(¶ms, (zend_long) m->delivery_tag);
add_next_index_bool(¶ms, m->multiple);
add_next_index_bool(¶ms, m->requeue);
return php_amqp_call_callback_with_params(params, cb);
}
int php_amqp_call_callback_with_params(zval params, amqp_callback_bucket *cb)
{
zval retval;
zval *retval_ptr = &retval;
int status = PHP_AMQP_RESOURCE_RESPONSE_OK;
ZVAL_NULL(&retval);
/* Convert everything to be callable */
zend_fcall_info_args(&cb->fci, ¶ms);
/* Initialize the return value pointer */
cb->fci.retval = retval_ptr;
zend_call_function(&cb->fci, &cb->fcc);
/* Check if user land function wants to bail */
if (EG(exception) || Z_TYPE_P(retval_ptr) == IS_FALSE) {
status = PHP_AMQP_RESOURCE_RESPONSE_BREAK;
}
/* Clean up our mess */
zend_fcall_info_args_clear(&cb->fci, 1);
zval_ptr_dtor(¶ms);
zval_ptr_dtor(retval_ptr);
return status;
}
|