socket read all data once

This commit is contained in:
云风
2013-06-28 19:02:50 +08:00
parent 67610a9614
commit 40c77e7739
2 changed files with 29 additions and 27 deletions

View File

@@ -215,9 +215,7 @@ _cb(struct skynet_context * ctx, void * ud, int type, int session, uint32_t sour
assert(type == PTYPE_RESPONSE); assert(type == PTYPE_RESPONSE);
struct mread_pool * m = g->pool; struct mread_pool * m = g->pool;
int connection_id = mread_poll(m,100); // timeout : 100ms int connection_id = mread_poll(m,100); // timeout : 100ms
if (connection_id < 0) { if (connection_id >= 0) {
skynet_command(ctx, "TIMEOUT", "1");
} else {
int id = g->map[connection_id].uid; int id = g->map[connection_id].uid;
if (id == 0) { if (id == 0) {
id = _gen_id(g, connection_id); id = _gen_id(g, connection_id);
@@ -254,9 +252,9 @@ _cb(struct skynet_context * ctx, void * ud, int type, int session, uint32_t sour
_forward(ctx, g, id, data, len); _forward(ctx, g, id, data, len);
mread_yield(m); mread_yield(m);
_break:
skynet_command(ctx, "TIMEOUT", "0");
} }
_break:
skynet_command(ctx, "TIMEOUT", "0");
return 0; return 0;
} }

View File

@@ -234,32 +234,36 @@ parser(const char * msg, int sz, char * buffer, int *id) {
static void static void
forward(struct skynet_context * context, struct socket *s, struct socket_pool *p) { forward(struct skynet_context * context, struct socket *s, struct socket_pool *p) {
int * buffer = malloc(READ_BUFFER + sizeof(int));
*buffer = s->id; // convert endian ?
int r = 0;
for (;;) { for (;;) {
r = read(s->fd, buffer+1, READ_BUFFER); int * buffer = malloc(READ_BUFFER + sizeof(int));
if (r == -1) { *buffer = s->id; // convert endian ?
switch(errno) { int r = 0;
case EWOULDBLOCK: for (;;) {
free(buffer); r = read(s->fd, buffer+1, READ_BUFFER);
return; if (r == -1) {
case EINTR: switch(errno) {
continue; case EWOULDBLOCK:
free(buffer);
return;
case EINTR:
continue;
}
r = 0;
break;
} }
r = 0;
break; break;
} }
break; if (r == 0) {
} force_close(s,p);
if (r == 0) { }
force_close(s,p);
}
if (s->status == STATUS_HALFCLOSE) { if (s->status == STATUS_HALFCLOSE) {
free(buffer); free(buffer);
} else { } else {
skynet_send(context, 0, s->source, PTYPE_CLIENT | PTYPE_TAG_DONTCOPY, 0, buffer, r + 4); skynet_send(context, 0, s->source, PTYPE_CLIENT | PTYPE_TAG_DONTCOPY, 0, buffer, r + 4);
}
if (r < READ_BUFFER)
return;
} }
} }
@@ -391,7 +395,7 @@ _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t
} }
} }
} }
skynet_command(context, "TIMEOUT", n == MAX_EVENT ? "0" : "1"); skynet_command(context, "TIMEOUT", "0");
return 0; return 0;
} }