forked from santiago739/php-beanstalk
/
beanstalk.c
581 lines (495 loc) · 14.9 KB
/
beanstalk.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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2008 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: Alexey Romanenko <santiago739@gmail.com> |
+----------------------------------------------------------------------+
*/
/* $Id */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_beanstalk.h"
#include <zend_exceptions.h>
/* If you declare any globals in php_beanstalk.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(beanstalk)
*/
static int le_beanstalk_client;
static zend_class_entry *beanstalk_ce;
static zend_class_entry *beanstalk_exception_ce;
static zend_class_entry *spl_ce_RuntimeException = NULL;
const zend_function_entry beanstalk_functions[] = {
PHP_ME(Beanstalk, __construct, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Beanstalk, connect, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Beanstalk, close, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Beanstalk, put, NULL, ZEND_ACC_PUBLIC)
//PHP_ME(Beanstalk, useTube, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
/* {{{ beanstalk_module_entry
*/
zend_module_entry beanstalk_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"beanstalk",
beanstalk_functions,
PHP_MINIT(beanstalk),
PHP_MSHUTDOWN(beanstalk),
PHP_RINIT(beanstalk), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(beanstalk), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(beanstalk),
#if ZEND_MODULE_API_NO >= 20010901
PHP_BEANSTALK_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_BEANSTALK
ZEND_GET_MODULE(beanstalk)
#endif
static zend_class_entry *beanstalk_get_exception_base(int root TSRMLS_DC) /* {{{ */
{
#if HAVE_SPL
if (!root) {
if (!spl_ce_RuntimeException) {
zend_class_entry **pce;
if (zend_hash_find(CG(class_table), "runtimeexception",
sizeof("RuntimeException"), (void **) &pce) == SUCCESS) {
spl_ce_RuntimeException = *pce;
return *pce;
}
} else {
return spl_ce_RuntimeException;
}
}
#endif
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 2)
return zend_exception_get_default();
#else
return zend_exception_get_default(TSRMLS_C);
#endif
}
/* }}} */
static int beanstalk_client_disconnect(beanstalk_client *bs_client TSRMLS_DC) /* {{{ */
{
int res = 0;
if (bs_client->stream != NULL) {
//redis_sock_write(redis_sock, "QUIT", sizeof("QUIT") - 1);
bs_client->status = BEANSTALK_CLIENT_STATUS_DISCONNECTED;
php_stream_close(bs_client->stream);
bs_client->stream = NULL;
res = 1;
}
return res;
}
/* }}} */
static void beanstalk_client_free(beanstalk_client *bs_client) /* {{{ */
{
efree(bs_client->host);
efree(bs_client);
}
/* }}} */
static void beanstalk_client_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
{
beanstalk_client *bs_client = (beanstalk_client *) rsrc->ptr;
beanstalk_client_disconnect(bs_client TSRMLS_CC);
beanstalk_client_free(bs_client);
}
/* }}} */
static int beanstalk_client_get(zval *id, beanstalk_client **bs_client TSRMLS_DC) /* {{{ */
{
zval **conn;
int resource_type;
if (Z_TYPE_P(id) != IS_OBJECT || zend_hash_find(Z_OBJPROP_P(id), "connection",
sizeof("connection"), (void **) &conn) == FAILURE) {
return -1;
}
*bs_client = (beanstalk_client *) zend_list_find(Z_LVAL_PP(conn), &resource_type);
if (!*bs_client || resource_type != le_beanstalk_client) {
return -1;
}
return Z_LVAL_PP(conn);
}
/* }}} */
static beanstalk_client* beanstalk_client_create(char *host, int host_len, unsigned short port, long timeout TSRMLS_DC) /* {{{ */
{
beanstalk_client *bs_client;
bs_client = emalloc(sizeof(beanstalk_client));
bs_client->host = emalloc(host_len + 1);
bs_client->stream = NULL;
bs_client->status = BEANSTALK_CLIENT_STATUS_DISCONNECTED;
memcpy(bs_client->host, host, host_len);
bs_client->host[host_len] = '\0';
bs_client->port = port;
bs_client->timeout = timeout;
return bs_client;
}
/* }}} */
static int beanstalk_client_connect(beanstalk_client *bs_client TSRMLS_DC) /* {{{ */
{
struct timeval tv, *tv_ptr = NULL;
char *host = NULL, *hash_key = NULL, *errstr = NULL;
int host_len, err = 0;
if (bs_client->stream != NULL) {
beanstalk_client_disconnect(bs_client TSRMLS_CC);
}
tv.tv_sec = bs_client->timeout;
tv.tv_usec = 0;
host_len = spprintf(&host, 0, "%s:%d", bs_client->host, bs_client->port);
if(tv.tv_sec != 0) {
tv_ptr = &tv;
}
bs_client->stream = php_stream_xport_create(
host, host_len, ENFORCE_SAFE_MODE, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT,
hash_key, tv_ptr, NULL, &errstr, &err
);
efree(host);
if (!bs_client->stream) {
efree(errstr);
return -1;
}
php_stream_auto_cleanup(bs_client->stream);
if(tv.tv_sec != 0) {
php_stream_set_option(bs_client->stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &tv);
}
php_stream_set_option(bs_client->stream, PHP_STREAM_OPTION_WRITE_BUFFER,
PHP_STREAM_BUFFER_NONE, NULL);
bs_client->status = BEANSTALK_CLIENT_STATUS_CONNECTED;
return 0;
}
/* }}} */
static int beanstalk_client_open(beanstalk_client *bs_client, int force_connect TSRMLS_DC) /* {{{ */
{
int res = -1;
switch (bs_client->status) {
case BEANSTALK_CLIENT_STATUS_DISCONNECTED:
return beanstalk_client_connect(bs_client TSRMLS_CC);
case BEANSTALK_CLIENT_STATUS_CONNECTED:
res = 0;
break;
case BEANSTALK_CLIENT_STATUS_UNKNOWN:
if (force_connect > 0 && beanstalk_client_connect(bs_client TSRMLS_CC) < 0) {
res = -1;
} else {
bs_client->status = BEANSTALK_CLIENT_STATUS_CONNECTED;
res = 0;
}
break;
}
return res;
}
/* }}} */
static void beanstalk_check_eof(beanstalk_client *bs_client TSRMLS_DC) /* {{{ */
{
int eof = php_stream_eof(bs_client->stream);
while(eof) {
bs_client->stream = NULL;
beanstalk_client_connect(bs_client TSRMLS_CC);
eof = php_stream_eof(bs_client->stream);
}
}
/* }}} */
static int beanstalk_client_write(beanstalk_client *bs_client, char *cmd, size_t sz TSRMLS_DC) /* {{{ */
{
beanstalk_check_eof(bs_client TSRMLS_CC);
return php_stream_write(bs_client->stream, cmd, sz);
}
/* }}} */
static char *beanstalk_client_read(beanstalk_client *bs_client, int *buf_len TSRMLS_DC) /* {{{ */
{
char inbuf[1024];
char *resp = NULL;
beanstalk_check_eof(bs_client TSRMLS_CC);
php_stream_gets(bs_client->stream, inbuf, 1024);
*buf_len = strlen(inbuf) - 2;
if(*buf_len >= 2) {
resp = emalloc(1 + *buf_len);
memcpy(resp, inbuf, *buf_len);
resp[*buf_len] = 0;
return resp;
} else {
printf("protocol error \n");
return NULL;
}
}
/* }}} */
//~
//~ static int beanstalk_str_left(char *haystack, int haystack_len, char *needle, int needle_len) /* {{{ */
//~ {
//~ char *found;
//~
//~ found = php_memnstr(haystack, needle, needle_len, haystack + haystack_len);
//~ if ((found - haystack) == 0) {
//~ return 1;
//~ }
//~ return 0;
//~ }
//~ /* }}} */
//~
//~ static int beanstalk_parse_response_error(char *response, int response_len TSRMLS_DC) /* {{{ */
//~ {
//~ if(beanstalk_str_left(response, response_len, "OUT_OF_MEMORY", sizeof("OUT_OF_MEMORY") - 1)) {
//~ return 0;
//~ }
//~ if(beanstalk_str_left(response, response_len, "INTERNAL_ERROR", sizeof("INTERNAL_ERROR") - 1)) {
//~ return 0;
//~ }
//~ if(beanstalk_str_left(response, response_len, "DRAINING", sizeof("DRAINING") - 1)) {
//~ return 0;
//~ }
//~ if(beanstalk_str_left(response, response_len, "BAD_FORMAT", sizeof("BAD_FORMAT") - 1)) {
//~ return 0;
//~ }
//~ if(beanstalk_str_left(response, response_len, "UNKNOWN_COMMAND", sizeof("UNKNOWN_COMMAND") - 1)) {
//~ return 0;
//~ }
//~ return 1;
//~ }
//~
//~ static int beanstalk_parse_response_put(char *response, int response_len TSRMLS_DC) /* {{{ */
//~ {
//~ if(beanstalk_str_left(response, response_len, "INSERTED", sizeof("INSERTED") - 1)) {
//~ return 1;
//~ }
//~ if(beanstalk_str_left(response, response_len, "BURIED", sizeof("BURIED") - 1)) {
//~ return 1;
//~ }
//~ if(beanstalk_str_left(response, response_len, "EXPECTED_CRLF", sizeof("EXPECTED_CRLF") - 1)) {
//~ return 0;
//~ }
//~ if(beanstalk_str_left(response, response_len, "JOB_TOO_BIG", sizeof("JOB_TOO_BIG") - 1)) {
//~ return 0;
//~ }
//~ return 0;
//~ }
/* {{{ proto void Beanstalk::__construct() */
PHP_METHOD(Beanstalk, __construct)
{
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto boolean Beanstalk::connect(string host, int port [, int timeout])
*/
PHP_METHOD(Beanstalk, connect)
{
zval *object;
int host_len, id;
char *host = NULL;
long port;
struct timeval timeout = {0L, 0L};
beanstalk_client *bs_client = NULL;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osl|l",
&object, beanstalk_ce, &host, &host_len, &port, &timeout.tv_sec) == FAILURE) {
RETURN_FALSE;
}
if (timeout.tv_sec < 0L || timeout.tv_sec > INT_MAX) {
zend_throw_exception(beanstalk_exception_ce, "Invalid timeout", 0 TSRMLS_CC);
RETURN_FALSE;
}
bs_client = beanstalk_client_create(host, host_len, port, timeout.tv_sec TSRMLS_CC);
if (beanstalk_client_open(bs_client, 1 TSRMLS_CC) < 0) {
beanstalk_client_free(bs_client);
zend_throw_exception_ex(
beanstalk_exception_ce, 0 TSRMLS_CC, "Can't connect to %s:%d", host, port
);
RETURN_FALSE;
}
id = zend_list_insert(bs_client, le_beanstalk_client);
add_property_resource(object, "connection", id);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto boolean Beanstalk::close()
*/
PHP_METHOD(Beanstalk, close)
{
zval *object;
beanstalk_client *bs_client = NULL;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
&object, beanstalk_ce) == FAILURE) {
RETURN_FALSE;
}
if (beanstalk_client_get(object, &bs_client TSRMLS_CC) < 0) {
RETURN_FALSE;
}
if (beanstalk_client_disconnect(bs_client TSRMLS_CC)) {
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto boolean Beanstalk::put(string key, string value)
*/
PHP_METHOD(Beanstalk, put)
{
zval *object;
beanstalk_client *bs_client = NULL;
char *value = NULL, *command;
int value_len, command_len;
long priority, delay, ttr;
char *response;
int response_len;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ollls",
&object, beanstalk_ce, &priority, &delay, &ttr, &value, &value_len) == FAILURE) {
RETURN_FALSE;
}
if (beanstalk_client_get(object, &bs_client TSRMLS_CC) < 0) {
RETURN_FALSE;
}
command_len = spprintf(&command, 0, "put %d %d %d %d\r\n%s\r\n",
priority, delay, ttr, value_len, value);
if (beanstalk_client_write(bs_client, command, command_len TSRMLS_CC) < 0) {
efree(command);
RETURN_FALSE;
}
efree(command);
//beanstalk_boolean_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, bs_client);
if ((response = beanstalk_client_read(bs_client, &response_len TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
printf("response: %s \n", response);
//~ if(beanstalk_parse_response_put(response, response_len TSRMLS_CC)) {
//~ efree(response);
//~ RETURN_TRUE;
//~ }
efree(response);
RETURN_FALSE;
}
/* }}} */
//~ /* {{{ proto boolean Beanstalk::useTube(string tube)
//~ */
//~ PHP_METHOD(Beanstalk, useTube)
//~ {
//~ zval *object;
//~ beanstalk_client *bs_client = NULL;
//~ char *tube = NULL, *command;
//~ int tube_len, command_len;
//~ char *response;
//~ int response_len;
//~
//~ if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
//~ &object, beanstalk_ce, &tube, &tube_len) == FAILURE) {
//~ RETURN_FALSE;
//~ }
//~
//~ if (_beanstalk_client_get(object, &bs_client TSRMLS_CC) < 0) {
//~ RETURN_FALSE;
//~ }
//~
//~ command_len = spprintf(&command, 0, "use %s\r\n", tube);
//~
//~ if (beanstalk_client_write(bs_client, command, command_len TSRMLS_CC) < 0) {
//~ efree(command);
//~ RETURN_FALSE;
//~ }
//~ efree(command);
//~
//~ if ((response = beanstalk_client_read(bs_client, &response_len TSRMLS_CC)) == NULL) {
//~ RETURN_FALSE;
//~ }
//~ printf("response: %s \n", response);
//~ efree(response);
//~
//~ RETURN_TRUE;
//~ }
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("beanstalk.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_beanstalk_globals, beanstalk_globals)
STD_PHP_INI_ENTRY("beanstalk.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_beanstalk_globals, beanstalk_globals)
PHP_INI_END()
*/
/* }}} */
/* {{{ php_beanstalk_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_beanstalk_init_globals(zend_beanstalk_globals *beanstalk_globals)
{
beanstalk_globals->global_value = 0;
beanstalk_globals->global_string = NULL;
}
*/
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(beanstalk)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "Beanstalk", beanstalk_functions);
beanstalk_ce = zend_register_internal_class(&ce TSRMLS_CC);
zend_class_entry exception_ce;
INIT_CLASS_ENTRY(exception_ce, "BeanstalkException", NULL);
beanstalk_exception_ce = zend_register_internal_class_ex(
&exception_ce,
beanstalk_get_exception_base(0 TSRMLS_CC),
NULL TSRMLS_CC
);
le_beanstalk_client = zend_register_list_destructors_ex(
beanstalk_client_dtor, NULL, "Beanstalk client", module_number
);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(beanstalk)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(beanstalk)
{
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(beanstalk)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(beanstalk)
{
php_info_print_table_start();
php_info_print_table_header(2, "beanstalk support", "enabled");
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/