forked from php-amqp/php-amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamqp_connection_resource.c
764 lines (620 loc) · 24.8 KB
/
amqp_connection_resource.c
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
/*
+----------------------------------------------------------------------+
| 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Alexandre Kalendarev [email protected] Copyright (c) 2009-2010 |
| Lead: |
| - Pieter de Zwart |
| Maintainers: |
| - Brad Rodriguez |
| - Jonathan Tansavatdi |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/datetime.h"
#include "zend_exceptions.h"
#ifdef PHP_WIN32
#if PHP_VERSION_ID >= 80000
#include <stdint.h>
#else
#include "win32/php_stdint.h"
#endif
#include "win32/signal.h"
#else
#include <signal.h>
#include <stdint.h>
#endif
#if HAVE_LIBRABBITMQ_NEW_LAYOUT
#include <rabbitmq-c/amqp.h>
#include <rabbitmq-c/framing.h>
#include <rabbitmq-c/tcp_socket.h>
#include <rabbitmq-c/ssl_socket.h>
#else
#include <amqp.h>
#include <amqp_framing.h>
#include <amqp_tcp_socket.h>
#include <amqp_ssl_socket.h>
#endif
#ifdef PHP_WIN32
#include "win32/unistd.h"
#else
#include <unistd.h>
#endif
#include "amqp_methods_handling.h"
#include "amqp_connection_resource.h"
#include "amqp_channel.h"
#include "php_amqp.h"
#ifndef E_DEPRECATED
#define E_DEPRECATED E_WARNING
#endif
int le_amqp_connection_resource;
int le_amqp_connection_resource_persistent;
static void connection_resource_destructor(amqp_connection_resource *resource, int persistent);
static void php_amqp_close_connection_from_server(
amqp_rpc_reply_t reply,
char **message,
amqp_connection_resource *resource
);
static void php_amqp_close_channel_from_server(
amqp_rpc_reply_t reply,
char **message,
amqp_connection_resource *resource,
amqp_channel_t channel_id
);
/* Figure out what's going on connection and handle protocol exceptions, if any */
int php_amqp_connection_resource_error(
amqp_rpc_reply_t reply,
char **message,
amqp_connection_resource *resource,
amqp_channel_t channel_id
)
{
assert(resource != NULL);
switch (reply.reply_type) {
case AMQP_RESPONSE_NORMAL:
return PHP_AMQP_RESOURCE_RESPONSE_OK;
case AMQP_RESPONSE_NONE:
spprintf(message, 0, "Missing RPC reply type.");
return PHP_AMQP_RESOURCE_RESPONSE_ERROR;
case AMQP_RESPONSE_LIBRARY_EXCEPTION:
spprintf(message, 0, "Library error: %s", amqp_error_string2(reply.library_error));
return PHP_AMQP_RESOURCE_RESPONSE_ERROR;
case AMQP_RESPONSE_SERVER_EXCEPTION:
switch (reply.reply.id) {
case AMQP_CONNECTION_CLOSE_METHOD: {
php_amqp_close_connection_from_server(reply, message, resource);
return PHP_AMQP_RESOURCE_RESPONSE_ERROR_CONNECTION_CLOSED;
}
case AMQP_CHANNEL_CLOSE_METHOD: {
php_amqp_close_channel_from_server(reply, message, resource, channel_id);
return PHP_AMQP_RESOURCE_RESPONSE_ERROR_CHANNEL_CLOSED;
}
}
/* Default for the above switch should be handled by the below default. */
default:
spprintf(message, 0, "Unknown server error, method id 0x%08X", reply.reply.id);
return PHP_AMQP_RESOURCE_RESPONSE_ERROR;
}
/* Should not never get here*/
}
static void php_amqp_close_connection_from_server(
amqp_rpc_reply_t reply,
char **message,
amqp_connection_resource *resource
)
{
amqp_connection_close_t *m = (amqp_connection_close_t *) reply.reply.decoded;
int result;
if (!reply.reply.id) {
PHP_AMQP_G(error_code) = -1;
spprintf(
message,
0,
"Server connection error: %ld, message: %s",
(long) PHP_AMQP_G(error_code),
"unexpected response"
);
} else {
PHP_AMQP_G(error_code) = m->reply_code;
spprintf(
message,
0,
"Server connection error: %d, message: %.*s",
m->reply_code,
(int) m->reply_text.len,
(char *) m->reply_text.bytes
);
}
/*
* - If r.reply.id == AMQP_CONNECTION_CLOSE_METHOD a connection exception
* occurred, cast r.reply.decoded to amqp_connection_close_t* to see
* details of the exception. The client amqp_send_method() a
* amqp_connection_close_ok_t and disconnect from the broker.
*/
amqp_connection_close_ok_t *decoded = (amqp_connection_close_ok_t *) NULL;
result = amqp_send_method(
resource->connection_state,
0, /* NOTE: 0-channel is reserved for things like this */
AMQP_CONNECTION_CLOSE_OK_METHOD,
&decoded
);
if (result != AMQP_STATUS_OK) {
zend_throw_exception(amqp_channel_exception_class_entry, "An error occurred while closing the connection.", 0);
}
/* Prevent finishing AMQP connection in connection resource destructor */
resource->is_connected = '\0';
}
static void php_amqp_close_channel_from_server(
amqp_rpc_reply_t reply,
char **message,
amqp_connection_resource *resource,
amqp_channel_t channel_id
)
{
assert(channel_id > 0 && channel_id <= resource->max_slots);
amqp_channel_close_t *m = (amqp_channel_close_t *) reply.reply.decoded;
if (!reply.reply.id) {
PHP_AMQP_G(error_code) = -1;
spprintf(
message,
0,
"Server channel error: %ld, message: %s",
(long) PHP_AMQP_G(error_code),
"unexpected response"
);
} else {
PHP_AMQP_G(error_code) = m->reply_code;
spprintf(
message,
0,
"Server channel error: %d, message: %.*s",
m->reply_code,
(int) m->reply_text.len,
(char *) m->reply_text.bytes
);
}
/*
* - If r.reply.id == AMQP_CHANNEL_CLOSE_METHOD a channel exception
* occurred, cast r.reply.decoded to amqp_channel_close_t* to see details
* of the exception. The client should amqp_send_method() a
* amqp_channel_close_ok_t. The channel must be re-opened before it
* can be used again. Any resources associated with the channel
* (auto-delete exchanges, auto-delete queues, consumers) are invalid
* and must be recreated before attempting to use them again.
*/
if (resource) {
int result;
amqp_channel_close_ok_t *decoded = (amqp_channel_close_ok_t *) NULL;
result = amqp_send_method(resource->connection_state, channel_id, AMQP_CHANNEL_CLOSE_OK_METHOD, &decoded);
if (result != AMQP_STATUS_OK) {
zend_throw_exception(amqp_channel_exception_class_entry, "An error occurred while closing channel.", 0);
}
}
}
int php_amqp_connection_resource_error_advanced(
amqp_rpc_reply_t reply,
char **message,
amqp_connection_resource *resource,
amqp_channel_t channel_id,
amqp_channel_object *channel
)
{
assert(resource != NULL);
amqp_frame_t frame;
assert(AMQP_RESPONSE_LIBRARY_EXCEPTION == reply.reply_type);
assert(AMQP_STATUS_UNEXPECTED_STATE == reply.library_error);
if (channel_id < 0 || AMQP_STATUS_OK != amqp_simple_wait_frame(resource->connection_state, &frame)) {
if (*message != NULL) {
efree(*message);
}
spprintf(message, 0, "Library error: %s", amqp_error_string2(reply.library_error));
return PHP_AMQP_RESOURCE_RESPONSE_ERROR;
}
if (channel_id != frame.channel) {
spprintf(message, 0, "Library error: channel mismatch");
return PHP_AMQP_RESOURCE_RESPONSE_ERROR;
}
if (AMQP_FRAME_METHOD == frame.frame_type) {
switch (frame.payload.method.id) {
case AMQP_CONNECTION_CLOSE_METHOD: {
php_amqp_close_connection_from_server(reply, message, resource);
return PHP_AMQP_RESOURCE_RESPONSE_ERROR_CONNECTION_CLOSED;
}
case AMQP_CHANNEL_CLOSE_METHOD: {
php_amqp_close_channel_from_server(reply, message, resource, channel_id);
return PHP_AMQP_RESOURCE_RESPONSE_ERROR_CHANNEL_CLOSED;
}
case AMQP_BASIC_ACK_METHOD:
/* if we've turned publisher confirms on, and we've published a message
* here is a message being confirmed
*/
return php_amqp_handle_basic_ack(message, resource, channel_id, channel, &frame.payload.method);
case AMQP_BASIC_NACK_METHOD:
/* if we've turned publisher confirms on, and we've published a message
* here is a message being confirmed
*/
return php_amqp_handle_basic_nack(message, resource, channel_id, channel, &frame.payload.method);
case AMQP_BASIC_RETURN_METHOD:
/* if a published message couldn't be routed and the mandatory flag was set
* this is what would be returned. The message then needs to be read.
*/
return php_amqp_handle_basic_return(message, resource, channel_id, channel, &frame.payload.method);
default:
if (*message != NULL) {
efree(*message);
}
spprintf(
message,
0,
"Library error: An unexpected method was received 0x%08X\n",
frame.payload.method.id
);
return PHP_AMQP_RESOURCE_RESPONSE_ERROR;
}
}
if (*message != NULL) {
efree(*message);
}
spprintf(message, 0, "Library error: %s", amqp_error_string2(reply.library_error));
return PHP_AMQP_RESOURCE_RESPONSE_ERROR;
}
/* Socket-related functions */
int php_amqp_set_resource_read_timeout(amqp_connection_resource *resource, double timeout)
{
assert(timeout >= 0.0);
#ifdef PHP_WIN32
DWORD read_timeout;
/*
In Windows, setsockopt with SO_RCVTIMEO sets actual timeout
to a value that's 500ms greater than specified value.
Also, it's not possible to set timeout to any value below 500ms.
Zero timeout works like it should, however.
*/
if (timeout == 0.) {
read_timeout = 0;
} else {
read_timeout = (int) (max(timeout * 1.e+3 - .5e+3, 1.));
}
#else
struct timeval read_timeout;
read_timeout.tv_sec = (int) floor(timeout);
read_timeout.tv_usec = (int) ((timeout - floor(timeout)) * 1.e+6);
#endif
if (0 != setsockopt(
amqp_get_sockfd(resource->connection_state),
SOL_SOCKET,
SO_RCVTIMEO,
(char *) &read_timeout,
sizeof(read_timeout)
)) {
zend_throw_exception(amqp_connection_exception_class_entry, "Socket error: cannot setsockopt SO_RCVTIMEO", 0);
return 0;
}
return 1;
}
int php_amqp_set_resource_rpc_timeout(amqp_connection_resource *resource, double timeout)
{
assert(timeout >= 0.0);
#if AMQP_VERSION_MAJOR * 100 + AMQP_VERSION_MINOR * 10 + AMQP_VERSION_PATCH >= 90
struct timeval rpc_timeout;
if (timeout == 0.)
return 1;
rpc_timeout.tv_sec = (int) floor(timeout);
rpc_timeout.tv_usec = (int) ((timeout - floor(timeout)) * 1.e+6);
if (AMQP_STATUS_OK != amqp_set_rpc_timeout(resource->connection_state, &rpc_timeout)) {
zend_throw_exception(amqp_connection_exception_class_entry, "Library error: cannot set rpc_timeout", 0);
return 0;
}
#endif
return 1;
}
int php_amqp_set_resource_write_timeout(amqp_connection_resource *resource, double timeout)
{
assert(timeout >= 0.0);
#ifdef PHP_WIN32
DWORD write_timeout;
if (timeout == 0.) {
write_timeout = 0;
} else {
write_timeout = (int) (max(timeout * 1.e+3 - .5e+3, 1.));
}
#else
struct timeval write_timeout;
write_timeout.tv_sec = (int) floor(timeout);
write_timeout.tv_usec = (int) ((timeout - floor(timeout)) * 1.e+6);
#endif
if (0 != setsockopt(
amqp_get_sockfd(resource->connection_state),
SOL_SOCKET,
SO_SNDTIMEO,
(char *) &write_timeout,
sizeof(write_timeout)
)) {
zend_throw_exception(amqp_connection_exception_class_entry, "Socket error: cannot setsockopt SO_SNDTIMEO", 0);
return 0;
}
return 1;
}
/* Channel-related functions */
amqp_channel_t php_amqp_connection_resource_get_available_channel_id(amqp_connection_resource *resource)
{
assert(resource != NULL);
assert(resource->slots != NULL);
/* Check if there are any open slots */
if (resource->used_slots >= resource->max_slots) {
return 0;
}
amqp_channel_t slot;
for (slot = resource->used_slots; slot < resource->max_slots; slot++) {
if (resource->slots[slot] == 0) {
return (amqp_channel_t) (slot + 1);
}
}
return 0;
}
int php_amqp_connection_resource_register_channel(
amqp_connection_resource *resource,
amqp_channel_resource *channel_resource,
amqp_channel_t channel_id
)
{
assert(resource != NULL);
assert(resource->slots != NULL);
assert(channel_id > 0 && channel_id <= resource->max_slots);
if (resource->slots[channel_id - 1] != 0) {
return FAILURE;
}
resource->slots[channel_id - 1] = channel_resource;
channel_resource->connection_resource = resource;
resource->used_slots++;
return SUCCESS;
}
int php_amqp_connection_resource_unregister_channel(amqp_connection_resource *resource, amqp_channel_t channel_id)
{
assert(resource != NULL);
assert(resource->slots != NULL);
assert(channel_id > 0 && channel_id <= resource->max_slots);
if (resource->slots[channel_id - 1] != 0) {
resource->slots[channel_id - 1]->connection_resource = NULL;
resource->slots[channel_id - 1] = 0;
resource->used_slots--;
}
return SUCCESS;
}
/* Creating and destroying resource */
amqp_connection_resource *connection_resource_constructor(amqp_connection_params *params, bool persistent)
{
struct timeval tv = {0};
struct timeval *tv_ptr = &tv;
char *std_datetime;
amqp_table_entry_t client_properties_entries[4];
amqp_table_t client_properties_table;
amqp_table_entry_t custom_properties_entries[2];
amqp_table_t custom_properties_table;
amqp_connection_resource *resource;
/* Allocate space for the connection resource */
resource = (amqp_connection_resource *) pecalloc(1, sizeof(amqp_connection_resource), persistent);
/* Create the connection */
resource->connection_state = amqp_new_connection();
/* Create socket object */
if (params->cacert) {
resource->socket = amqp_ssl_socket_new(resource->connection_state);
if (!resource->socket) {
zend_throw_exception(
amqp_connection_exception_class_entry,
"Socket error: could not create SSL socket.",
0
);
return NULL;
}
if (amqp_ssl_socket_set_cacert(resource->socket, params->cacert) != AMQP_STATUS_OK) {
zend_throw_exception(
amqp_connection_exception_class_entry,
"Socket error: could not set CA certificate.",
0
);
connection_resource_destructor(resource, persistent);
return NULL;
}
#if AMQP_VERSION_MAJOR * 100 + AMQP_VERSION_MINOR * 10 + AMQP_VERSION_PATCH >= 80
amqp_ssl_socket_set_verify_peer(resource->socket, params->verify);
amqp_ssl_socket_set_verify_hostname(resource->socket, params->verify);
#else
amqp_ssl_socket_set_verify(resource->socket, params->verify);
#endif
if (params->cert && params->key) {
int client_cert_result = amqp_ssl_socket_set_key(resource->socket, params->cert, params->key);
if (client_cert_result != AMQP_STATUS_OK) {
zend_throw_exception_ex(
amqp_connection_exception_class_entry,
0,
"Socket error: could not set client cert, %s",
amqp_error_string2(client_cert_result)
);
connection_resource_destructor(resource, persistent);
return NULL;
}
}
} else {
resource->socket = amqp_tcp_socket_new(resource->connection_state);
if (!resource->socket) {
zend_throw_exception(amqp_connection_exception_class_entry, "Socket error: could not create socket.", 0);
return NULL;
}
}
if (params->connect_timeout > 0) {
tv.tv_sec = (long int) params->connect_timeout;
tv.tv_usec = (long int) ((params->connect_timeout - tv.tv_sec) * 1000000);
} else {
tv_ptr = NULL;
}
/* Try to connect and verify that no error occurred */
int connection_result = amqp_socket_open_noblock(resource->socket, params->host, params->port, tv_ptr);
if (connection_result != AMQP_STATUS_OK) {
zend_throw_exception_ex(
amqp_connection_exception_class_entry,
0,
"Socket error: could not connect to host, %s",
amqp_error_string2(connection_result)
);
connection_resource_destructor(resource, persistent);
return NULL;
}
if (!php_amqp_set_resource_read_timeout(resource, params->read_timeout)) {
connection_resource_destructor(resource, persistent);
return NULL;
}
if (!php_amqp_set_resource_write_timeout(resource, params->write_timeout)) {
connection_resource_destructor(resource, persistent);
return NULL;
}
if (!php_amqp_set_resource_rpc_timeout(resource, params->rpc_timeout)) {
connection_resource_destructor(resource, persistent);
return NULL;
}
std_datetime = php_std_date(time(NULL));
client_properties_entries[0].key = amqp_cstring_bytes("type");
client_properties_entries[0].value.kind = AMQP_FIELD_KIND_UTF8;
client_properties_entries[0].value.value.bytes = amqp_cstring_bytes("php-amqp extension");
client_properties_entries[1].key = amqp_cstring_bytes("version");
client_properties_entries[1].value.kind = AMQP_FIELD_KIND_UTF8;
client_properties_entries[1].value.value.bytes = amqp_cstring_bytes(PHP_AMQP_VERSION);
client_properties_entries[2].key = amqp_cstring_bytes("connection type");
client_properties_entries[2].value.kind = AMQP_FIELD_KIND_UTF8;
client_properties_entries[2].value.value.bytes = amqp_cstring_bytes(persistent ? "persistent" : "transient");
client_properties_entries[3].key = amqp_cstring_bytes("connection started");
client_properties_entries[3].value.kind = AMQP_FIELD_KIND_UTF8;
client_properties_entries[3].value.value.bytes = amqp_cstring_bytes(std_datetime);
client_properties_table.entries = client_properties_entries;
client_properties_table.num_entries = sizeof(client_properties_entries) / sizeof(amqp_table_entry_t);
custom_properties_entries[0].key = amqp_cstring_bytes("client");
custom_properties_entries[0].value.kind = AMQP_FIELD_KIND_TABLE;
custom_properties_entries[0].value.value.table = client_properties_table;
if (params->connection_name) {
custom_properties_entries[1].key = amqp_cstring_bytes("connection_name");
custom_properties_entries[1].value.kind = AMQP_FIELD_KIND_UTF8;
custom_properties_entries[1].value.value.bytes = amqp_cstring_bytes(params->connection_name);
}
custom_properties_table.entries = custom_properties_entries;
custom_properties_table.num_entries = params->connection_name ? 2 : 1;
/* We can assume that connection established here but it is not true, real handshake goes during login */
assert(params->frame_max > 0);
amqp_rpc_reply_t res = amqp_login_with_properties(
resource->connection_state,
params->vhost,
params->channel_max,
params->frame_max,
params->heartbeat,
&custom_properties_table,
params->sasl_method,
params->login,
params->password
);
efree(std_datetime);
if (AMQP_RESPONSE_NORMAL != res.reply_type) {
char *message = NULL, *long_message = NULL;
php_amqp_connection_resource_error(res, &message, resource, 0);
spprintf(&long_message, 0, "%s - Potential login failure.", message);
zend_throw_exception(amqp_connection_exception_class_entry, long_message, PHP_AMQP_G(error_code));
efree(message);
efree(long_message);
/* https://www.rabbitmq.com/resources/specs/amqp0-9-1.pdf
*
* 2.2.4 The Connection Class:
* ... a peer that detects an error MUST close the socket without sending any further data.
*
* 4.10.2 Denial of Service Attacks:
* ... The general response to any exceptional condition in the connection negotiation is to pause that connection
* (presumably a thread) for a period of several seconds and then to close the network connection. This
* includes syntax errors, over-sized data, and failed attempts to authenticate.
*/
connection_resource_destructor(resource, persistent);
return NULL;
}
/* Allocate space for the channel slots in the ring buffer */
resource->max_slots = (amqp_channel_t) amqp_get_channel_max(resource->connection_state);
assert(resource->max_slots > 0);
resource->slots =
(amqp_channel_resource **) pecalloc(resource->max_slots + 1, sizeof(amqp_channel_object *), persistent);
resource->is_connected = '\1';
return resource;
}
ZEND_RSRC_DTOR_FUNC(amqp_connection_resource_dtor_persistent)
{
amqp_connection_resource *resource = (amqp_connection_resource *) res->ptr;
connection_resource_destructor(resource, 1);
}
ZEND_RSRC_DTOR_FUNC(amqp_connection_resource_dtor)
{
amqp_connection_resource *resource = (amqp_connection_resource *) res->ptr;
connection_resource_destructor(resource, 0);
}
static void connection_resource_destructor(amqp_connection_resource *resource, int persistent)
{
assert(resource != NULL);
#ifndef PHP_WIN32
void *old_handler;
/*
If we are trying to close the connection and the connection already closed, it will throw
SIGPIPE, which is fine, so ignore all SIGPIPES
*/
/* Start ignoring SIGPIPE */
old_handler = signal(SIGPIPE, SIG_IGN);
#endif
if (resource->parent) {
resource->parent->connection_resource = NULL;
}
if (resource->slots) {
php_amqp_prepare_for_disconnect(resource);
pefree(resource->slots, persistent);
resource->slots = NULL;
}
/* connection may be closed in case of previous failure */
if (resource->is_connected) {
amqp_connection_close(resource->connection_state, AMQP_REPLY_SUCCESS);
}
amqp_destroy_connection(resource->connection_state);
#ifndef PHP_WIN32
/* End ignoring of SIGPIPEs */
signal(SIGPIPE, old_handler);
#endif
pefree(resource, persistent);
}
void php_amqp_prepare_for_disconnect(amqp_connection_resource *resource)
{
if (resource == NULL) {
return;
}
if (resource->slots != NULL) {
/* NOTE: when we have persistent connection we do not move channels between php requests
* due to current php-amqp extension limitation in AMQPChannel where __construct == channel.open AMQP method call
* and __destruct = channel.close AMQP method call
*/
/* Clean up old memory allocations which are now invalid (new connection) */
amqp_channel_t slot;
for (slot = 0; slot < resource->max_slots; slot++) {
if (resource->slots[slot] != 0) {
php_amqp_close_channel(resource->slots[slot], 0);
}
}
}
/* If it's persistent connection do not destroy connection resource (this keep connection alive) */
if (resource->is_persistent) {
/* Cleanup buffers to reduce memory usage in idle mode */
amqp_maybe_release_buffers(resource->connection_state);
}
return;
}