[Freeswitch-svn] [commit] r10895 - in freeswitch/trunk/libs/esl: . src src/include

FreeSWITCH SVN anthm at freeswitch.org
Sat Dec 20 07:40:33 PST 2008


Author: anthm
Date: Sat Dec 20 10:40:33 2008
New Revision: 10895

Log:
add more code for listener

Modified:
   freeswitch/trunk/libs/esl/Makefile
   freeswitch/trunk/libs/esl/src/esl.c
   freeswitch/trunk/libs/esl/src/include/esl.h

Modified: freeswitch/trunk/libs/esl/Makefile
==============================================================================
--- freeswitch/trunk/libs/esl/Makefile	(original)
+++ freeswitch/trunk/libs/esl/Makefile	Sat Dec 20 10:40:33 2008
@@ -3,24 +3,28 @@
 LIBEDIT_DIR=../../libs/libedit
 CFLAGS=$(INCS) -g -ggdb -I$(LIBEDIT_DIR)/src/
 MYLIB=libesl.a
-
+LIBS=-lesl -lncurses -lpthread
+LDFLAGS=-L.
 OBJS=src/esl.o src/esl_event.o src/esl_threadmutex.o src/esl_config.o
 
-all: $(MYLIB) fs_cli
+all: $(MYLIB) fs_cli testclient testserver
 
 $(MYLIB): $(OBJS) $(HEADERS)
 	ar rcs $(MYLIB) $(OBJS)
 	ranlib $(MYLIB)
 
-%.o: %.c
-	$(CC) $(CC_CFLAGS) $(CFLAGS) -c $< -o $@
+testserver: $(MYLIB) testserver.c
+	$(CC) $(CC_CFLAGS) $(CFLAGS) testserver.c -o testserver $(LDFLAGS) $(LIBS)
 
-test: $(MYLIB)
-	$(CC) $(CC_CFLAGS) $(CFLAGS) testclient.c -o testclient -L. -lesl
+testclient: $(MYLIB) testclient.c
+	$(CC) $(CC_CFLAGS) $(CFLAGS) testclient.c -o testclient $(LDFLAGS) $(LIBS)
 
 fs_cli: $(MYLIB) fs_cli.c
-	$(CC) $(CC_CFLAGS) $(CFLAGS) fs_cli.c -o fs_cli -L. -L$(LIBEDIT_DIR)/src/.libs -lesl -ledit -lncurses -lpthread
+	$(CC) $(CC_CFLAGS) $(CFLAGS) fs_cli.c -o fs_cli $(LDFLAGS) -L$(LIBEDIT_DIR)/src/.libs $(LIBS) -ledit
+
+%.o: %.c
+	$(CC) $(CC_CFLAGS) $(CFLAGS) -c $< -o $@
 
 clean:
-	rm -f *.o src/*.o testclient fs_cli libesl.a *~ src/*~ src/include/*~
+	rm -f *.o src/*.o testclient testserver fs_cli libesl.a *~ src/*~ src/include/*~
 

Modified: freeswitch/trunk/libs/esl/src/esl.c
==============================================================================
--- freeswitch/trunk/libs/esl/src/esl.c	(original)
+++ freeswitch/trunk/libs/esl/src/esl.c	Sat Dec 20 10:40:33 2008
@@ -323,6 +323,59 @@
 }
 
 
+esl_status_t esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback)
+{
+	esl_socket_t server_sock = ESL_SOCK_INVALID;
+	int reuse_addr = 1;
+	struct sockaddr_in addr;
+	esl_status_t status = ESL_SUCCESS;
+	
+	if ((server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
+		return ESL_FAIL;
+	}
+
+	setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr));
+			   
+	memset(&addr, 0, sizeof(addr));
+	addr.sin_family = AF_INET;
+    addr.sin_addr.s_addr = htonl(INADDR_ANY);
+    addr.sin_port = htons(port);
+	
+    if (bind(server_sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+		status = ESL_FAIL;
+		goto end;
+	}
+
+    if (listen(server_sock, 10000) < 0) {
+		status = ESL_FAIL;
+		goto end;
+	}
+
+	for (;;) {
+		int client_sock;                    
+		struct sockaddr_in echoClntAddr; 
+		unsigned int clntLen;            
+
+		clntLen = sizeof(echoClntAddr);
+    
+		if ((client_sock = accept(server_sock, (struct sockaddr *) &echoClntAddr, &clntLen)) == ESL_SOCK_INVALID) {
+			status = ESL_FAIL;
+			goto end;
+		}
+
+		callback(server_sock, client_sock, echoClntAddr);
+	}
+
+ end:
+
+	if (server_sock != ESL_SOCK_INVALID) {
+		close(server_sock);
+		server_sock = ESL_SOCK_INVALID;
+	}
+
+	return status;
+
+}
 
 esl_status_t esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *password)
 {

Modified: freeswitch/trunk/libs/esl/src/include/esl.h
==============================================================================
--- freeswitch/trunk/libs/esl/src/include/esl.h	(original)
+++ freeswitch/trunk/libs/esl/src/include/esl.h	Sat Dec 20 10:40:33 2008
@@ -256,6 +256,9 @@
 int esl_toupper(int c);
 int esl_tolower(int c);
 
+typedef void (*esl_listen_callback_t)(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in addr);
+
+esl_status_t esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback);
 
 esl_status_t esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *password);
 esl_status_t esl_disconnect(esl_handle_t *handle);



More information about the Freeswitch-svn mailing list