improve socket lib

This commit is contained in:
云风
2013-07-24 12:16:31 +08:00
parent 4fe5f3738b
commit 9ec4267965

View File

@@ -55,6 +55,13 @@ reply(struct skynet_context * ctx, uint32_t source, int session, char * cmd, int
skynet_send(ctx, 0, source, PTYPE_RESPONSE, session, cmd, sz); skynet_send(ctx, 0, source, PTYPE_RESPONSE, session, cmd, sz);
} }
static void
_reply_bind(struct skynet_context * ctx, int sock, int session, uint32_t source) {
char ret[10];
int sz = sprintf(ret,"%d",sock);
reply(ctx, source, session, ret, sz);
}
static int static int
_set_nonblocking(int fd) _set_nonblocking(int fd)
{ {
@@ -116,6 +123,9 @@ cmd_bind(struct skynet_context * ctx, struct socket_pool *p, int sock, int sessi
reply(ctx, source, session, NULL , 0); reply(ctx, source, session, NULL , 0);
return; return;
} }
if (!connecting) {
_reply_bind(ctx, id, session, source);
}
if (p->count == 1) { if (p->count == 1) {
skynet_command(ctx, "TIMEOUT", "0"); skynet_command(ctx, "TIMEOUT", "0");
} }
@@ -215,13 +225,6 @@ cmd_close(struct skynet_context * ctx, struct socket_pool *p, int id, int sessio
} }
} }
static void
_reply_bind(struct skynet_context * ctx, int sock, int session, uint32_t source) {
char ret[10];
int sz = sprintf(ret,"%d",sock);
reply(ctx, source, session, ret, sz);
}
static void static void
_ctrl(struct skynet_context * ctx, struct socket_pool *p, char * command, int id, char * arg, int session, uint32_t source) { _ctrl(struct skynet_context * ctx, struct socket_pool *p, char * command, int id, char * arg, int session, uint32_t source) {
if (strcmp(command, "open")==0) { if (strcmp(command, "open")==0) {
@@ -230,7 +233,6 @@ _ctrl(struct skynet_context * ctx, struct socket_pool *p, char * command, int id
cmd_close(ctx, p, id, session, source); cmd_close(ctx, p, id, session, source);
} else if (strcmp(command, "bind")==0) { } else if (strcmp(command, "bind")==0) {
cmd_bind(ctx, p, id, session, source, false); cmd_bind(ctx, p, id, session, source, false);
_reply_bind(ctx, id, session, source);
} else { } else {
skynet_error(ctx, "Unknown command %s", command); skynet_error(ctx, "Unknown command %s", command);
reply(ctx, source, session, NULL, 0); reply(ctx, source, session, NULL, 0);
@@ -403,7 +405,7 @@ _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t
if (p->count == 0) if (p->count == 0)
return 0; return 0;
assert(type == PTYPE_RESPONSE); assert(type == PTYPE_RESPONSE);
int n = event_wait(p->fd, p->ev, 100); // timeout : 100ms int n = event_wait(p->fd, p->ev, 1); // timeout : 1ms
int i; int i;
for (i=0;i<n;i++) { for (i=0;i<n;i++) {