1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-11 05:18:09 +03:00

r1515: move dublicate code to a function

metze
(This used to be commit a8ec53c81a)
This commit is contained in:
Stefan Metzmacher 2004-07-15 08:59:07 +00:00 committed by Gerald (Jerry) Carter
parent a1748ef743
commit b11e1a41d8
5 changed files with 85 additions and 175 deletions

View File

@ -39,11 +39,8 @@ static void single_accept_connection(struct event_context *ev, struct fd_event *
int accepted_fd;
struct sockaddr addr;
socklen_t in_addrlen = sizeof(addr);
struct fd_event fde;
struct timed_event idle;
struct server_socket *server_socket = srv_fde->private;
struct server_connection *conn;
TALLOC_CTX *mem_ctx;
/* accept an incoming connection. */
accepted_fd = accept(srv_fde->fd,&addr,&in_addrlen);
@ -53,62 +50,12 @@ static void single_accept_connection(struct event_context *ev, struct fd_event *
return;
}
mem_ctx = talloc_init("server_service_connection");
if (!mem_ctx) {
DEBUG(0,("talloc_init(server_service_connection) failed\n"));
return;
}
conn = talloc_p(mem_ctx, struct server_connection);
conn = server_setup_connection(ev, server_socket, accepted_fd, t);
if (!conn) {
DEBUG(0,("talloc_p(mem_ctx, struct server_service_connection) failed\n"));
talloc_destroy(mem_ctx);
DEBUG(0,("server_setup_connection(ev, server_socket, accepted_fd) failed\n"));
return;
}
ZERO_STRUCTP(conn);
conn->mem_ctx = mem_ctx;
fde.private = conn;
fde.fd = accepted_fd;
fde.flags = EVENT_FD_READ;
fde.handler = server_io_handler;
idle.private = conn;
idle.next_event = t + 300;
idle.handler = server_idle_handler;
conn->event.ctx = ev;
conn->event.fde = &fde;
conn->event.idle = &idle;
conn->event.idle_time = 300;
conn->server_socket = server_socket;
conn->service = server_socket->service;
/* TODO: we need a generic socket subsystem */
conn->socket = talloc_p(conn->mem_ctx, struct socket_context);
if (!conn->socket) {
DEBUG(0,("talloc_p(conn->mem_ctx, struct socket_context) failed\n"));
talloc_destroy(mem_ctx);
return;
}
conn->socket->private_data = NULL;
conn->socket->ops = NULL;
conn->socket->client_addr = NULL;
conn->socket->pkt_count = 0;
conn->socket->fde = conn->event.fde;
/* create a smb server context and add it to out event
handling */
server_socket->service->ops->accept_connection(conn);
/* accpect_connection() of the service may changed idle.next_event */
conn->event.fde = event_add_fd(ev,&fde);
conn->event.idle = event_add_timed(ev,&idle);
conn->socket->fde = conn->event.fde;
DLIST_ADD(server_socket->connection_list,conn);
/* return to event handling */
@ -122,9 +69,7 @@ static void single_terminate_connection(struct server_connection *conn, const ch
{
DEBUG(0,("single_terminate_connection: reason[%s]\n",reason));
conn->service->ops->close_connection(conn,reason);
close(conn->event.fde->fd);
event_remove_fd(conn->event.ctx, conn->event.fde);
event_remove_timed(conn->event.ctx, conn->event.idle);
server_destroy_connection(conn);
}
static int single_get_id(struct smbsrv_request *req)

View File

@ -40,11 +40,8 @@ static void standard_accept_connection(struct event_context *ev, struct fd_event
struct sockaddr addr;
socklen_t in_addrlen = sizeof(addr);
pid_t pid;
struct fd_event fde;
struct timed_event idle;
struct server_socket *server_socket = srv_fde->private;
struct server_connection *conn;
TALLOC_CTX *mem_ctx;
/* accept an incoming connection. */
accepted_fd = accept(srv_fde->fd,&addr,&in_addrlen);
@ -78,63 +75,12 @@ static void standard_accept_connection(struct event_context *ev, struct fd_event
set_need_random_reseed();
mem_ctx = talloc_init("server_service_connection");
if (!mem_ctx) {
DEBUG(0,("talloc_init(server_service_connection) failed\n"));
return;
}
conn = talloc_p(mem_ctx, struct server_connection);
conn = server_setup_connection(ev, server_socket, accepted_fd, t);
if (!conn) {
DEBUG(0,("talloc_p(mem_ctx, struct server_service_connection) failed\n"));
talloc_destroy(mem_ctx);
DEBUG(0,("server_setup_connection(ev, server_socket, accepted_fd) failed\n"));
return;
}
ZERO_STRUCTP(conn);
conn->mem_ctx = mem_ctx;
fde.private = conn;
fde.fd = accepted_fd;
fde.flags = EVENT_FD_READ;
fde.handler = server_io_handler;
idle.private = conn;
idle.next_event = t + 300;
idle.handler = server_idle_handler;
conn->event.ctx = ev;
conn->event.fde = &fde;
conn->event.idle = &idle;
conn->event.idle_time = 300;
conn->server_socket = server_socket;
conn->service = server_socket->service;
/* TODO: we need a generic socket subsystem */
conn->socket = talloc_p(conn->mem_ctx, struct socket_context);
if (!conn->socket) {
DEBUG(0,("talloc_p(conn->mem_ctx, struct socket_context) failed\n"));
talloc_destroy(mem_ctx);
return;
}
conn->socket->private_data = NULL;
conn->socket->ops = NULL;
conn->socket->client_addr = NULL;
conn->socket->pkt_count = 0;
conn->socket->fde = conn->event.fde;
/* create a smb server context and add it to out event
handling */
server_socket->service->ops->accept_connection(conn);
/* accpect_connection() of the service may changed idle.next_event */
conn->event.fde = event_add_fd(ev,&fde);
conn->event.idle = event_add_timed(ev,&idle);
conn->socket->fde = conn->event.fde;
DLIST_ADD(server_socket->connection_list,conn);
/* return to the event loop */
@ -146,9 +92,7 @@ static void standard_terminate_connection(struct server_connection *conn, const
{
DEBUG(0,("single_terminate_connection: reason[%s]\n",reason));
conn->service->ops->close_connection(conn,reason);
close(conn->event.fde->fd);
event_remove_fd(conn->event.ctx, conn->event.fde);
event_remove_timed(conn->event.ctx, conn->event.idle);
server_destroy_connection(conn);
/* terminate this process */
exit(0);
}

View File

@ -54,11 +54,8 @@ static void thread_accept_connection(struct event_context *ev, struct fd_event *
socklen_t in_addrlen = sizeof(addr);
pthread_t thread_id;
pthread_attr_t thread_attr;
struct fd_event fde;
struct timed_event idle;
struct server_socket *server_socket = srv_fde->private;
struct server_connection *conn;
TALLOC_CTX *mem_ctx;
/* accept an incoming connection. */
accepted_fd = accept(srv_fde->fd,&addr,&in_addrlen);
@ -82,62 +79,12 @@ static void thread_accept_connection(struct event_context *ev, struct fd_event *
return;
}
mem_ctx = talloc_init("server_service_connection");
if (!mem_ctx) {
DEBUG(0,("talloc_init(server_service_connection) failed\n"));
return;
}
conn = talloc_p(mem_ctx, struct server_connection);
conn = server_setup_connection(ev, server_socket, accepted_fd, t);
if (!conn) {
DEBUG(0,("talloc_p(mem_ctx, struct server_service_connection) failed\n"));
talloc_destroy(mem_ctx);
DEBUG(0,("server_setup_connection(ev, server_socket, accepted_fd) failed\n"));
return;
}
ZERO_STRUCTP(conn);
conn->mem_ctx = mem_ctx;
fde.private = conn;
fde.fd = accepted_fd;
fde.flags = EVENT_FD_READ;
fde.handler = server_io_handler;
idle.private = conn;
idle.next_event = t + 300;
idle.handler = server_idle_handler;
conn->event.ctx = ev;
conn->event.fde = &fde;
conn->event.idle = &idle;
conn->event.idle_time = 300;
conn->server_socket = server_socket;
conn->service = server_socket->service;
/* TODO: we need a generic socket subsystem */
conn->socket = talloc_p(conn->mem_ctx, struct socket_context);
if (!conn->socket) {
DEBUG(0,("talloc_p(conn->mem_ctx, struct socket_context) failed\n"));
talloc_destroy(mem_ctx);
return;
}
conn->socket->private_data = NULL;
conn->socket->ops = NULL;
conn->socket->client_addr = NULL;
conn->socket->pkt_count = 0;
conn->socket->fde = conn->event.fde;
/* create a smb server context and add it to out event
handling */
server_socket->service->ops->accept_connection(conn);
/* accpect_connection() of the service may changed idle.next_event */
conn->event.fde = event_add_fd(ev,&fde);
conn->event.idle = event_add_timed(ev,&idle);
conn->socket->fde = conn->event.fde;
/* TODO: is this MUTEX_LOCK in the right place here?
* --metze
*/
@ -162,9 +109,7 @@ static void thread_terminate_connection(struct server_connection *conn, const ch
{
DEBUG(0,("thread_terminate_connection: reason[%s]\n",reason));
conn->service->ops->close_connection(conn,reason);
close(conn->event.fde->fd);
event_remove_fd(conn->event.ctx, conn->event.fde);
event_remove_timed(conn->event.ctx, conn->event.idle);
server_destroy_connection(conn);
/* terminate this thread */
pthread_exit(NULL); /* thread cleanup routine will do actual cleanup */
}

View File

@ -37,4 +37,7 @@ struct server_context {
#define SERVER_TCP_LOW_PORT 1024
#define SERVER_TCP_HIGH_PORT 1300
/* the default idle time of a service */
#define SERVER_DEFAULT_IDLE_TIME 300
#endif /* _SERVER_H */

View File

@ -169,6 +169,72 @@ struct server_socket *service_setup_socket(struct server_service *service,
return sock;
}
struct server_connection *server_setup_connection(struct event_context *ev, struct server_socket *server_socket, int accepted_fd, time_t t)
{
struct fd_event fde;
struct timed_event idle;
struct server_connection *srv_conn;
TALLOC_CTX *mem_ctx;
mem_ctx = talloc_init("server_service_connection");
if (!mem_ctx) {
DEBUG(0,("talloc_init(server_service_connection) failed\n"));
return NULL;
}
srv_conn = talloc_p(mem_ctx, struct server_connection);
if (!srv_conn) {
DEBUG(0,("talloc_p(mem_ctx, struct server_service_connection) failed\n"));
talloc_destroy(mem_ctx);
return NULL;
}
ZERO_STRUCTP(srv_conn);
srv_conn->mem_ctx = mem_ctx;
fde.private = srv_conn;
fde.fd = accepted_fd;
fde.flags = EVENT_FD_READ;
fde.handler = server_io_handler;
idle.private = srv_conn;
idle.next_event = t + SERVER_DEFAULT_IDLE_TIME;
idle.handler = server_idle_handler;
srv_conn->event.ctx = ev;
srv_conn->event.fde = &fde;
srv_conn->event.idle = &idle;
srv_conn->event.idle_time = SERVER_DEFAULT_IDLE_TIME;
srv_conn->server_socket = server_socket;
srv_conn->service = server_socket->service;
/* TODO: we need a generic socket subsystem */
srv_conn->socket = talloc_p(srv_conn->mem_ctx, struct socket_context);
if (!srv_conn->socket) {
DEBUG(0,("talloc_p(srv_conn->mem_ctx, struct socket_context) failed\n"));
talloc_destroy(mem_ctx);
return NULL;
}
srv_conn->socket->private_data = NULL;
srv_conn->socket->ops = NULL;
srv_conn->socket->client_addr = NULL;
srv_conn->socket->pkt_count = 0;
srv_conn->socket->fde = srv_conn->event.fde;
/* create a smb server context and add it to out event
handling */
server_socket->service->ops->accept_connection(srv_conn);
/* accpect_connection() of the service may changed idle.next_event */
srv_conn->event.fde = event_add_fd(ev,&fde);
srv_conn->event.idle = event_add_timed(ev,&idle);
srv_conn->socket->fde = srv_conn->event.fde;
return srv_conn;
}
/*
close the socket and shutdown a server_context
*/
@ -178,6 +244,13 @@ void server_terminate_connection(struct server_connection *srv_conn, const char
srv_conn->service->model_ops->terminate_connection(srv_conn, reason);
}
void server_destroy_connection(struct server_connection *srv_conn)
{
close(srv_conn->event.fde->fd);
event_remove_fd(srv_conn->event.ctx, srv_conn->event.fde);
event_remove_timed(srv_conn->event.ctx, srv_conn->event.idle);
}
void server_io_handler(struct event_context *ev, struct fd_event *fde, time_t t, uint16_t flags)
{
struct server_connection *conn = fde->private;