creating connections
authorChristian Thaeter <ct@pipapo.org>
Wed, 3 Oct 2007 02:41:10 +0000 (04:41 +0200)
committerChristian Thaeter <ct@pipapo.org>
Wed, 3 Oct 2007 02:41:10 +0000 (04:41 +0200)
rxpd.c
rxpd.h

diff --git a/rxpd.c b/rxpd.c
index ef78ff3..578099e 100644 (file)
--- a/rxpd.c
+++ b/rxpd.c
@@ -247,13 +247,16 @@ rxpd_socket_new_tcp4 (struct rxpd_base* base, const char* addr, unsigned short p
 }
 
 
-
 void
 rxpd_socket_delete (struct rxpd_socket* self)
 {
-  llist_unlink (&self->node);
-  event_del (&self->ev);
-  close (self->fd);
+  if (self)
+    {
+      event_del (&self->ev);
+      llist_unlink (&self->node);
+      close (self->fd);
+    }
+  free (self);
 }
 
 struct rxpd_socket*
@@ -272,34 +275,90 @@ rxpd_socket_suspend (struct rxpd_socket* self)
 {
   if (self)
     {
-      llist_insert_tail (&self->base->sockets_pending, &self->node);
       event_del (&self->ev);
+      llist_insert_tail (&self->base->sockets_pending, &self->node);
     }
   return self;
 }
 
 void
-rxpd_socket_accept (int sock, short event, void* ptr)
+rxpd_socket_accept (int fd, short event, void* ptr)
 {
   printf ("incoming connection\n");
 
-  //struct rxpd_connection* conn;
+  struct rxpd_socket* sock = ptr;
+
+  struct rxpd_connection* conn =
+    rxpd_connection_new (sock->base, fd);
+
+  rxpd_connection_activate (conn);
 }
 
 ///
 
 struct rxpd_connection*
-rxpd_connection_new (struct rxpd_base* base)
+rxpd_connection_new (struct rxpd_base* base, int fd)
 {
-  return NULL;
+  struct rxpd_connection* self = malloc (sizeof (struct rxpd_connection));
+  if (!self)
+    abort();
+
+  llist_init (&self->node);
+
+  socklen_t addr_sz = sizeof (self->peer_addr); 
+  self->fd = accept (fd, (struct sockaddr*)&self->peer_addr, &addr_sz);
+  if (self->fd == -1)
+    abort ();
+
+  self->base = base;
+  self->file = NULL;
+
+  self->eol = self->eob = self->buffer;
+  
+  event_set (&self->ev, self->fd, EV_READ, rxpd_connection_parse_cmd, self);
+
+  llist_insert_tail (&base->connections_pending, &self->node);
+
+  return self;
 }
 
 void
 rxpd_connection_delete (struct rxpd_connection* self)
 {
+  if (self)
+    {
+      event_del (&self->ev);
+      llist_unlink (&self->node);
+      close (self->fd);
+    }
   free (self);
 }
 
+struct rxpd_connection*
+rxpd_connection_activate (struct rxpd_connection* self)
+{
+  if (self)
+    {
+      llist_insert_tail (&self->base->connections_active, &self->node);
+      event_add (&self->ev, NULL);
+    }
+  return self;
+}
+
+struct rxpd_connection*
+rxpd_connection_suspend (struct rxpd_connection* self)
+{
+  if (self)
+    {
+      event_del (&self->ev);
+      llist_insert_tail (&self->base->connections_pending, &self->node);
+    }
+  return self;
+}
+
+
+
+
 int
 rxpd_connection_readline (struct rxpd_connection* self)
 {
@@ -308,6 +367,8 @@ rxpd_connection_readline (struct rxpd_connection* self)
 void
 rxpd_connection_parse_cmd (int sock, short event, void* ptr)
 {
+  printf ("parse cmd\n");
+  // TODO policy check
 }
 
 void
diff --git a/rxpd.h b/rxpd.h
index 2d4fe85..565fd2f 100644 (file)
--- a/rxpd.h
+++ b/rxpd.h
@@ -150,9 +150,10 @@ struct rxpd_connection
   int fd;
   struct event ev;
   struct rxpd_base* base;
+  struct rxpd_file* file;
+
+  struct sockaddr peer_addr;
 
-  struct sockaddr_in peer_addr;
-  
   char* eol;
   char* eob;
   char buffer[4096];
@@ -160,11 +161,20 @@ struct rxpd_connection
 
 
 struct rxpd_connection*
-rxpd_connection_new (struct rxpd_base* base);
+rxpd_connection_new (struct rxpd_base* base, int accept_fd);
 
 void
 rxpd_connection_delete (struct rxpd_connection* self);
 
+
+struct rxpd_connection*
+rxpd_connection_activate (struct rxpd_connection* self);
+
+
+struct rxpd_connection*
+rxpd_connection_suspend (struct rxpd_connection* self);
+
+
 int
 rxpd_connection_readline (struct rxpd_connection* self);