report new connection from gate

This commit is contained in:
云风
2012-08-09 22:06:04 +08:00
parent 24790ca5e4
commit c958a99b35
3 changed files with 16 additions and 5 deletions

View File

@@ -212,24 +212,26 @@ _alloc_socket(struct mread_pool * self) {
return s;
}
static void
static struct socket *
_add_client(struct mread_pool * self, int fd) {
struct socket * s = _alloc_socket(self);
if (s == NULL) {
close(fd);
return;
return NULL;
}
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.ptr = s;
if (epoll_ctl(self->epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
close(fd);
return;
return NULL;
}
s->fd = fd;
s->node = NULL;
s->status = SOCKET_SUSPEND;
return s;
}
static int
@@ -275,7 +277,11 @@ mread_poll(struct mread_pool * self , int timeout) {
int client_fd = accept(self->listen_fd , (struct sockaddr *)&remote_addr , &len);
if (client_fd >= 0) {
// printf("MREAD connect %s:%u (fd=%d)\n",inet_ntoa(remote_addr.sin_addr),ntohs(remote_addr.sin_port), client_fd);
_add_client(self, client_fd);
struct socket * s = _add_client(self, client_fd);
if (s) {
self->active = -1;
return s - self->sockets;
}
}
} else {
int index = s - self->sockets;

View File

@@ -1,4 +1,5 @@
local skynet = require "skynet"
local client = ...
skynet.dispatch(function(msg, sz , session, address)
local message = skynet.tostring(msg,sz)
@@ -6,3 +7,7 @@ skynet.dispatch(function(msg, sz , session, address)
local result = skynet.call("SIMPLEDB",message)
skynet.send(address, result)
end)
skynet.start(function()
skynet.send(client,"Welcome to skynet")
end)

View File

@@ -10,8 +10,8 @@ function command:open(parm)
local fd,addr = string.match(parm,"(%d+) ([^%s]+)")
fd = tonumber(fd)
print("agent open",self,string.format("%d %d %s",self,fd,addr))
local agent = skynet.launch("snlua","agent")
local client = skynet.launch("client",fd)
local agent = skynet.launch("snlua","agent",client)
print("watchdog launch agent client:",agent,client)
if agent then
agent_all[self] = { agent , client }