Skip to content

Commit a5b6a36

Browse files
author
none
committed
sockets handling corrections
1 parent 86611b5 commit a5b6a36

File tree

3 files changed

+17
-27
lines changed

3 files changed

+17
-27
lines changed

src/tarantool.c

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
#include "utils.h"
1616

17-
int __tarantool_authenticate(tarantool_connection *obj);
17+
static int __tarantool_authenticate(tarantool_connection *obj);
18+
static void tarantool_stream_close(tarantool_connection *obj);
1819

1920
double
2021
now_gettimeofday(void)
@@ -128,11 +129,12 @@ PHP_INI_END()
128129
ZEND_GET_MODULE(tarantool)
129130
#endif
130131

131-
static int
132+
inline static int
132133
tarantool_stream_send(tarantool_connection *obj TSRMLS_DC) {
133134
int rv = tntll_stream_send(obj->stream, SSTR_BEG(obj->value),
134135
SSTR_LEN(obj->value));
135136
if (rv < 0) {
137+
tarantool_stream_close(obj);
136138
tarantool_throw_ioexception("Failed to send message");
137139
return FAILURE;
138140
}
@@ -218,10 +220,11 @@ static zend_string *pid_pzsgen(const char *host, int port, const char *login,
218220
* Legacy rtsisyk code, php_stream_read made right
219221
* See https://bugs.launchpad.net/tarantool/+bug/1182474
220222
*/
221-
static size_t
223+
inline static int
222224
tarantool_stream_read(tarantool_connection *obj, char *buf, size_t size) {
223225
size_t got = tntll_stream_read2(obj->stream, buf, size);
224226
if (got != size) {
227+
tarantool_stream_close(obj);
225228
tarantool_throw_ioexception("Failed to read %ld bytes", size);
226229
return FAILURE;
227230
}
@@ -240,7 +243,7 @@ tarantool_stream_close(tarantool_connection *obj) {
240243
}
241244
}
242245

243-
int __tarantool_connect(tarantool_object *t_obj) {
246+
static int __tarantool_connect(tarantool_object *t_obj) {
244247
TSRMLS_FETCH();
245248
tarantool_connection *obj = t_obj->obj;
246249
int status = SUCCESS;
@@ -284,7 +287,7 @@ int __tarantool_connect(tarantool_object *t_obj) {
284287
continue;
285288
}
286289
if (tntll_stream_read2(obj->stream, obj->greeting,
287-
GREETING_SIZE) == -1) {
290+
GREETING_SIZE) != GREETING_SIZE) {
288291
continue;
289292
}
290293
if (php_tp_verify_greetings(obj->greeting) == 0) {
@@ -306,7 +309,7 @@ int __tarantool_connect(tarantool_object *t_obj) {
306309
return status;
307310
}
308311

309-
int __tarantool_reconnect(tarantool_object *t_obj) {
312+
inline static int __tarantool_reconnect(tarantool_object *t_obj) {
310313
tarantool_stream_close(t_obj->obj);
311314
return __tarantool_connect(t_obj);
312315
}
@@ -391,54 +394,42 @@ static zend_object *tarantool_create(zend_class_entry *entry) {
391394
return &obj->zo;
392395
}
393396

394-
static int64_t tarantool_step_recv(
397+
static int tarantool_step_recv(
395398
tarantool_connection *obj,
396399
unsigned long sync,
397400
zval *header,
398401
zval *body) {
399402
char pack_len[5] = {0, 0, 0, 0, 0};
400403
if (tarantool_stream_read(obj, pack_len, 5) == FAILURE) {
401-
header = NULL;
402-
body = NULL;
403-
goto error_con;
404+
goto error;
404405
}
405406
if (php_mp_check(pack_len, 5)) {
406-
header = NULL;
407-
body = NULL;
408407
tarantool_throw_parsingexception("package length");
409408
goto error_con;
410409
}
411410
size_t body_size = php_mp_unpack_package_size(pack_len);
412411
smart_string_ensure(obj->value, body_size);
413412
if (tarantool_stream_read(obj, SSTR_POS(obj->value),
414413
body_size) == FAILURE) {
415-
header = NULL;
416-
body = NULL;
417414
goto error;
418415
}
419416
SSTR_LEN(obj->value) += body_size;
420417

421418
char *pos = SSTR_BEG(obj->value);
422419
if (php_mp_check(pos, body_size)) {
423-
header = NULL;
424-
body = NULL;
425420
tarantool_throw_parsingexception("package header");
426-
goto error;
421+
goto error_con;
427422
}
428423
if (php_mp_unpack(header, &pos) == FAILURE ||
429424
Z_TYPE_P(header) != IS_ARRAY) {
430-
header = NULL;
431-
body = NULL;
432-
goto error;
425+
goto error_con;
433426
}
434427
if (php_mp_check(pos, body_size)) {
435-
body = NULL;
436428
tarantool_throw_parsingexception("package body");
437429
goto error_con;
438430
}
439431
if (php_mp_unpack(body, &pos) == FAILURE) {
440-
body = NULL;
441-
goto error;
432+
goto error_con;
442433
}
443434

444435
HashTable *hash = HASH_OF(header);
@@ -488,7 +479,6 @@ static int64_t tarantool_step_recv(
488479
tarantool_throw_exception("Failed to retrieve answer code");
489480
error_con:
490481
tarantool_stream_close(obj);
491-
obj->stream = NULL;
492482
error:
493483
if (header) zval_ptr_dtor(header);
494484
if (body) zval_ptr_dtor(body);
@@ -1174,7 +1164,7 @@ PHP_METHOD(Tarantool, reconnect) {
11741164
RETURN_TRUE;
11751165
}
11761166

1177-
int __tarantool_authenticate(tarantool_connection *obj) {
1167+
static int __tarantool_authenticate(tarantool_connection *obj) {
11781168
TSRMLS_FETCH();
11791169

11801170
tarantool_schema_flush(obj->schema);

src/tarantool_network.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ int tntll_stream_open(const char *host, int port, zend_string *pid,
119119
* Legacy rtsisyk code, php_stream_read made right
120120
* See https://bugs.launchpad.net/tarantool/+bug/1182474
121121
*/
122-
int tntll_stream_read2(php_stream *stream, char *buf, size_t size) {
122+
size_t tntll_stream_read2(php_stream *stream, char *buf, size_t size) {
123123
TSRMLS_FETCH();
124124
size_t total_size = 0;
125125
size_t read_size = 0;

src/tarantool_network.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int tntll_stream_read (php_stream *stream, char *buf, size_t size);
2727
/*
2828
* Read size bytes exactly (if not error)
2929
*/
30-
int tntll_stream_read2(php_stream *stream, char *buf, size_t size);
30+
size_t tntll_stream_read2(php_stream *stream, char *buf, size_t size);
3131
int tntll_stream_send (php_stream *stream, char *buf, size_t size);
3232

3333
#endif /* PHP_TNT_NETWORK_H */

0 commit comments

Comments
 (0)