Skip to content
Browse files

clean code and fix package length 400 overflow bug

  • Loading branch information...
1 parent 7781245 commit bc0fbe4795874edd4cd2b3c858f2200acdbf771f @changchang changchang committed Apr 3, 2013
Showing with 15 additions and 17 deletions.
  1. +1 −1 src/map.c
  2. +10 −7 src/package.c
  3. +4 −9 src/pkg-handshake.c
View
2 src/map.c
@@ -161,7 +161,7 @@ void pc_map_clear(pc_map_t *map) {
int i;
for(i=0; i<map->capacity; i++) {
- ngx_queue_t *head = &map->buckets[i];
+ ngx_queue_t *head = &map->buckets[i];
ngx_queue_t *q;
pc__pair_t *pair;
View
17 src/package.c
@@ -77,19 +77,22 @@ int pc_pkg_parser_feed(pc_pkg_parser_t *parser, const char *data, size_t nread)
while(offset < nread) {
if(parser->state == PC_PKG_HEAD) {
offset = pc__pkg_head(parser, data, offset, nread);
+ if(offset == -1) {
+ return -1;
+ }
}
- if(offset == -1) {
- return -1;
- }
+
if(parser->state == PC_PKG_BODY) {
offset = pc__pkg_body(parser, data, offset, nread);
+ if(offset == -1) {
+ return -1;
+ }
}
- if(offset == -1) {
- return -1;
- }
+
if(parser->state == PC_PKG_CLOSED) {
return 0;
}
+
if(parser->state != PC_PKG_HEAD && parser->state != PC_PKG_BODY &&
parser->state != PC_PKG_CLOSED) {
fprintf(stderr, "Invalid package parser state: %d\n", parser->state);
@@ -164,7 +167,7 @@ static size_t pc__pkg_head(pc_pkg_parser_t *parser,
if(i > 1) {
pkg_len <<= 8;
}
- pkg_len += parser->head_buf[i];
+ pkg_len += parser->head_buf[i] & 0xff;
}
if(pkg_len > 0) {
View
13 src/pkg-handshake.c
@@ -85,20 +85,15 @@ int pc__handshake_resp(pc_client_t *client,
if(sys) {
// setup heartbeat
json_int_t hb = json_integer_value(json_object_get(sys, "heartbeat"));
- if(hb < 0) {
+ if(hb <= 0) {
// no need heartbeat
client->heartbeat = -1;
client->timeout = -1;
} else {
- if(hb > 0) {
client->heartbeat = hb * 1000;
- client->timeout = client->heartbeat * PC_HEARTBEAT_TIMEOUT_FACTOR;
- uv_timer_set_repeat(client->heartbeat_timer, client->heartbeat);
- uv_timer_set_repeat(client->timeout_timer, client->timeout);
- } else {
- client->heartbeat = -1;
- client->timeout = -1;
- }
+ client->timeout = client->heartbeat * PC_HEARTBEAT_TIMEOUT_FACTOR;
+ uv_timer_set_repeat(client->heartbeat_timer, client->heartbeat);
+ uv_timer_set_repeat(client->timeout_timer, client->timeout);
}
// setup route dictionary

0 comments on commit bc0fbe4

Please sign in to comment.
Something went wrong with that request. Please try again.