Difference between revisions of "Server.c"

From Organic Design wiki
(structure and notes)
(remove si struct)
Line 6: Line 6:
 
#define PAKSIZE 128  // keep packet-size small for non-multithreaded design
 
#define PAKSIZE 128  // keep packet-size small for non-multithreaded design
 
#define BUFSIZE 10000 // max message size
 
#define BUFSIZE 10000 // max message size
#define MAXCLIENTS 100
 
  
 
// - todo: don't exit on connect errors, keep trying every 10s
 
// - todo: don't exit on connect errors, keep trying every 10s
Line 23: Line 22:
  
 
int processMessage(char* msg);
 
int processMessage(char* msg);
 
// struct type to represent a currently connected stream
 
typedef struct streamstruct {
 
int fd;
 
char* in;
 
char* out;
 
} streamInfo;
 
  
 
// set up socket struct
 
// set up socket struct
Line 64: Line 56:
 
fd_set *fdset;
 
fd_set *fdset;
  
// NOTE: CHANGE THIS TO USE NEW LINKED-LIST FUNCTIONS
+
// Server is in nodal ROOT loop
// array of current client connections
 
// - this may be replaced by blocks pointed to by select()'s fd array
 
streamInfo streams[MAXCLIENTS],si;
 
 
 
 
 
 
 
// - server is in is in nodal ROOT loop
 
 
void network() {
 
void network() {
  

Revision as of 11:42, 17 July 2006

// good sock function ref at // http://www.opengroup.org/onlinepubs/009695399/idx/networking.html

// Set up socket and listening loop

  1. define LISTENPORT 2012
  2. define PAKSIZE 128 // keep packet-size small for non-multithreaded design
  3. define BUFSIZE 10000 // max message size

// - todo: don't exit on connect errors, keep trying every 10s // Includes for socket (trying to use one source cpp for osx,win32,*ux)

  1. ifdef WINDOWS
  2. include <winsock.h>
  3. else
  4. include <sys/socket.h>
  5. include <sys/select.h>
  6. include <netinet/in.h>
  7. include <fcntl.h> // needed for O_NONBLOCK option on server
  8. include <sys/time.h> // for select()

//#include <arpa/inet.h> //#include <netdb.h>

  1. endif

int processMessage(char* msg);

// set up socket struct struct sockaddr_in my_addr, client_addr; int sa_in_size = sizeof(struct sockaddr_in); memset((char *)&my_addr, 0, sa_in_size); // zero the struct my_addr.sin_family = PF_INET; my_addr.sin_port = htons(LISTENPORT); my_addr.sin_addr.s_addr = htonl(INADDR_ANY);

// get a socket int server; if ((server = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) logAdd("socket() failed!");

// make it reusable int sockopt_on = 1; if (setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &sockopt_on, sizeof(int)) < 0) logAdd("setsockopt() failed!");

// bind our socket to the port if (bind(server, (struct sockaddr *)&my_addr, sa_in_size) < 0) logAdd("bind() failed!";

// start listening for incoming connections if (listen(server, MAXCLIENTS) < 0) logAdd("listen() failed!";

// make the server non-blocking so accept() returns straight away // - if no incoming requests, returns EAGAIN or EWOULDBLOCK state // - we need this for non-multithreaded design // - this may need to be done for each new incomming stream...? // - some examples use fcntl(server, F_SETFL, FNDELAY) or fcntl(server, F_SETFL, O_NONBLOCK); fcntl(server, O_NONBLOCK);

// Setup stuff for select() struct timeval timeout; fd_set *fdset;

// Server is in nodal ROOT loop void network() {

// NOTE: no need for si struct, just use individual States in streamNode-assocs

int i; streamInfo *si = nodeGetState(this); timeout.tv_sec = 1; // later use 0 for min time timeout.tv_usec = 0;

// Check for new connection FD_ZERO(fdset); FD_SET(server,fdset); if (select(maxfd+1,fdset,NULL,NULL,&timeout)>0) { // new connection // - create new stream node and insert into loop // - create new si struct and SetState of new node }

// Data to receive? FD_ZERO(fdset); FD_SET(si.fd,fdset); if ((i=select(maxfd+1, streams, NULL, NULL, &timeout))>0) { // read a packet into si.in // hook a process into the loop if a msg complete } else if (i<0) { // close connection // - remove this stream node from loop // - free the si struct }


// Data to send? FD_ZERO(fdset); FD_SET(si.fd,fdset); if (select(maxfd+1, NULL, streams, NULL, &timeout)>0) { // write a packet from si.out }

}

// Parses a message content and responds to client int processMessage(char* msg) { // test if restart cmd first

// send response // - NOTE: this should put the msg in si.out if (send(stream, MSG, strlen(MSG)+1, 0) == -1) logAdd("send() failed!"); else logAdd("Sent message"); }