Skip to content

Commit

Permalink
clean code and fix package length 400 overflow bug
Browse files Browse the repository at this point in the history
  • Loading branch information
changchang committed Apr 3, 2013
1 parent 7781245 commit bc0fbe4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 10 additions & 7 deletions src/package.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 4 additions & 9 deletions src/pkg-handshake.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bc0fbe4

Please sign in to comment.