diff --git a/Makefile b/Makefile index e1d751a..0202448 100644 --- a/Makefile +++ b/Makefile @@ -17,8 +17,8 @@ #Objects OBJS = socket.o amf.o rtmp.o -LIBRARIES = -lpthread -CFLAGS = -Wall -O2 +LIBRARIES = -lpthread +CFLAGS = -ggdb -Wall -O2 -D_POSIX_PTHREAD_SEMANTICS # Link The Binary $(BINARY) : $(OBJS) diff --git a/amf.c b/amf.c index b57a474..4e9dcfe 100644 --- a/amf.c +++ b/amf.c @@ -19,7 +19,7 @@ recLen = sReadSocket(fd,&data1,1536); for (i = 0;i < 1536;i++) { - data2[i] = (char)i; + data2[i] = 'a'; } if (send(fd,&cmd,1,MSG_NOSIGNAL) != 1) { printf("Error Sending Header\n"); diff --git a/rtmp.c b/rtmp.c index ca63f4b..7f3f6fb 100644 --- a/rtmp.c +++ b/rtmp.c @@ -23,13 +23,17 @@ int main(void) { int newFD = 0x0; //New Socket; - //socklen_t sin_size; - //struct sockaddr_in remoteAddr; // connector's address information + fd_set readset; AMFS = (amfHeader_real *)malloc(sizeof(amfHeader_real) * 128); sStartListener(); + while (1) { + if (sGetConnections(&readset) != 0) + perror("Problem Building readset"); + } + while(1) { // main accept() loop while (1); /* diff --git a/rtmp.h b/rtmp.h index 3a2cd2a..7b9d790 100644 --- a/rtmp.h +++ b/rtmp.h @@ -2,6 +2,9 @@ #define BACKLOG 10 // how many pending connections queue will hold +#define N 1000 +#define MEGEXTRA 1000000 + typedef struct { char unknown[3]; char amfSize[3]; @@ -28,6 +31,7 @@ int amfDoAccept(int); int amfDoHandshake(int); int sStartListener(); -void sListenerThread(int); +void *sListenerThread(void *); int sAddConnection(int); ssize_t sReadSocket(int socketFD,void *buffer,size_t length); +int sGetConnections(fd_set *); diff --git a/socket.c b/socket.c index 01cd66f..6a017cc 100644 --- a/socket.c +++ b/socket.c @@ -20,12 +20,14 @@ pthread_t listenerThread; pthread_mutex_t sConnectionsMutex; +pthread_attr_t attr; myConnections_t *connections = 0x0; int sStartListener() { int optVal = 0x1; int listenerFD = 0x0; + size_t stacksize; struct sockaddr_in myAddr; // my address information @@ -49,14 +51,18 @@ exit(1); } - if (listen(listenerFD, BACKLOG) == -1) { perror("listen"); exit(1); } pthread_mutex_init(&sConnectionsMutex,NULL); - pthread_create(&listenerThread,NULL,(void *)sListenerThread,(void *)listenerFD); + pthread_attr_getstacksize(&attr,&stacksize); + printf("SS: [0x%X]\n",stacksize); + pthread_attr_setstacksize(&attr,sizeof(double)*N*N+MEGEXTRA); + printf("SS: [0x%X]\n",stacksize); + + pthread_create(&listenerThread,&attr,sListenerThread,(void *)listenerFD); return(0x0); } @@ -86,28 +92,30 @@ return(recLen); } -void sListenerThread(int listenerFD) { +void *sListenerThread(void *lFD) { + int listenerFD = (int)lFD; int newFD = 0x0; //New Socket; socklen_t sin_size; struct sockaddr_in remoteAddr; // connector's address information + sin_size = sizeof(struct sockaddr_in); while (1) { - sin_size = sizeof(struct sockaddr_in); - + printf("Waiting On Connection\n"); if ((newFD = accept(listenerFD, (struct sockaddr *)&remoteAddr, &sin_size)) == -1) { perror("accept"); - continue; } - fcntl(newFD, F_SETFL, fcntl(newFD, F_GETFL, 0) | O_NONBLOCK); + else { + fcntl(newFD, F_SETFL, fcntl(newFD, F_GETFL, 0) | O_NONBLOCK); - printf("server: got connection from %s\n",inet_ntoa(remoteAddr.sin_addr)); + printf("server: got connection from %s\n",inet_ntoa(remoteAddr.sin_addr)); - if (amfDoHandshake(newFD) == -1) { - printf("Error: Bad Handshake\n"); - close(newFD); + if (amfDoHandshake(newFD) == -1) { + printf("Error: Bad Handshake\n"); + close(newFD); + } + printf("A"); + sAddConnection(newFD); } - printf("A"); - sAddConnection(newFD); } } @@ -137,3 +145,22 @@ return(0x0); } + +int sGetConnections(fd_set *readset) { + int retVal = 0; + myConnections_t *tmpConnection = 0x0; + + FD_ZERO(readset); + + pthread_mutex_lock(&sConnectionsMutex); + + if (connections != 0x0) { + for (tmpConnection = connections;tmpConnection != 0x0;tmpConnection = tmpConnection->next) { + FD_SET(tmpConnection->socketFD,readset); + } + retVal = 1; + } + + pthread_mutex_unlock(&sConnectionsMutex); + return(retVal); + }