Changeset - 1a829a8ad7e6
[Not reviewed]
default
0 4 0
Nathan Brink (binki) - 16 years ago 2009-11-28 00:58:58
ohnobinki@ohnopublishing.net
reorganization of structs, partial implementation of distrend_accept()
4 files changed with 77 insertions and 15 deletions:
0 comments (0 inline, 0 general)
src/server/distrend.c
Show inline comments
 
@@ -93,13 +93,6 @@ int distrend_do()
 
  return 0;
 
}
 
/**
 
   Accepts a client's connection
 
 */
 
void distrend_accept()
 
{
 

	
 
}
 
/**
 
   Frees the action
 
*/
 
void distrend_action_free()
 
@@ -1182,10 +1175,10 @@ int main(int argc, char *argv[])
 
  /* This is called the "main loop" */
 
  while(cont)
 
    {
 
      struct distren_action *action;
 
      struct distrend_action *action;
 
      int clientsays = 0; /*< temporary example variable, will be replaced when we can handle messages */
 

	
 
      distrend_accept(&action);
 
      distrend_accept(config, clients, &action);
 
      cont = distrend_do(action);
 

	
 
      /* Make the following code more event-driven */
src/server/distrend.h
Show inline comments
 
@@ -35,5 +35,9 @@ struct distrend_config
 
  char *datadir;
 
};
 

	
 
struct distrend_action
 
{
 
  struct distrend_client *client;
 
};
 

	
 
#endif
src/server/listen.c
Show inline comments
 
@@ -23,20 +23,23 @@
 
#include <malloc.h>
 
#include <netinet/in.h>
 
#include <stdio.h>
 
#include <string.h>
 
#include <sys/types.h>
 
#include <sys/select.h>
 
#include <sys/socket.h>
 

	
 
/* local */
 

	
 
struct distrend_client
 
{
 
  int sock;
 
  int state;
 
};
 

	
 
struct distrend_clientset
 
{
 
  LIST *clients;
 

	
 
  /*
 
    for select()
 
   */
 
  fd_set readfds;
 
  fd_set writefds;
 
  int nfds;
 
};
 

	
 

	
 
@@ -71,6 +74,51 @@ int distrend_listen(struct distrend_conf
 

	
 
  tmp = listen(config->listens->sock, 1);
 

	
 
  FD_ZERO(&(*clients)->readfds);
 
  FD_ZERO(&(*clients)->writefds);
 

	
 
  return 0;
 
}
 

	
 
int distrend_accept_find_client(fd_set *fdset, struct distrend_client *client)
 
{
 
  if(FD_ISSET(client->sock, fdset))
 
    return FALSE;
 
  return TRUE;
 
}
 

	
 
int distrend_accept(struct distrend_config *config, struct distrend_clientset *clients, struct distrend_action **action)
 
{
 
  int tmp;
 
  fd_set readfds;
 
  fd_set writefds;
 

	
 
  struct distrend_client *cli;
 

	
 
  memcpy(&readfds, &clients->readfds, sizeof(fd_set));
 
  memcpy(&writefds, &clients->writefds, sizeof(fd_set));
 

	
 
  tmp = select(clients->nfds, &readfds, &writefds, NULL, (struct timeval *)NULL);
 
  if(tmp == -1)
 
    {
 
      perror("select");
 

	
 
      return 1;
 
    }
 

	
 
  /**
 
     check if our traversal function found a bit that was set
 
   */
 
  if(list_traverse(clients->clients, &readfds, &distrend_accept_find_client, LIST_FRNT | LIST_ALTR) == LIST_OK)
 
    {
 
      cli = (struct distrend_client *)list_curr(clients->clients);
 
      fprintf(stderr, "%s:%d: My traversal says that sock %d has data waiting, which is %s\n",
 
	     __FILE__, __LINE__,
 
	     cli->sock, FD_ISSET(cli->sock, &readfds) ? "true" : "false");
 
      fprintf(stderr, "stub\n");
 
      return 1;
 
    }
 

	
 
  return 0;
 
}
 

	
src/server/listen.h
Show inline comments
 
@@ -19,6 +19,7 @@
 

	
 
struct distrend_clientset;
 
struct distrend_listen;
 
struct distrend_client;
 

	
 
#ifndef _DISTREN_LISTEN_H
 
#define _DISTREN_LISTEN_H
 
@@ -31,6 +32,13 @@ struct distrend_listen
 
  int sock;
 
};
 

	
 
struct distrend_client
 
{
 
  int sock;
 
  int state;
 
};
 

	
 

	
 

	
 
/**
 
   initializes the listens and clientset
 
@@ -40,6 +48,15 @@ struct distrend_listen
 
int distrend_listen(struct distrend_config *config, struct distrend_clientset **clients);
 

	
 
/**
 
   checks states of the sockets I'm managing. If there are any new connections,
 
   or activity on any sockets, I'll call the appropriate function.
 
   I will block until there is some sort of activity, including
 
   signals. If you want to cleanly shut down, it's best to register
 
   signal handlers somewhere
 
 */
 
int distrend_accept(struct distrend_config *config, struct distrend_clientset *clients, struct distrend_action **action);
 

	
 
/**
 
   cleans listening socket. Unnecessary for a working server, currently a stub.
 
 */
 
int distrend_unlisten(struct distrend_listen *listens, struct distrend_clientset *clients);
0 comments (0 inline, 0 general)