[IOMultiplexerV2] Added simple Server example
authorpk910 <philipp@zoelle1.de>
Tue, 27 Jan 2015 23:53:25 +0000 (00:53 +0100)
committerpk910 <philipp@zoelle1.de>
Tue, 27 Jan 2015 23:53:25 +0000 (00:53 +0100)
configure.ac
src/IOHandler_test/Makefile.am
src/IOHandler_test/server/.gitignore [new file with mode: 0644]
src/IOHandler_test/server/Makefile.am [new file with mode: 0644]
src/IOHandler_test/server/iotest.c [new file with mode: 0644]

index 4275959d2caf16b73054236bb343d5bfcd31e588..f424ed782bc7525b88d0205cf44495fa79e71225 100644 (file)
@@ -35,7 +35,15 @@ AC_FUNC_MALLOC
 AC_CHECK_FUNCS([usleep select socket inet_pton inet_ntop])
 AC_CHECK_HEADERS([fcntl.h sys/socket.h sys/select.h sys/time.h sys/types.h unistd.h windows.h winsock2.h errno.h sys/epoll.h sys/event.h])
 
-AC_CHECK_LIB(ws2_32, main, [ LIBS="$LIBS -lws2_32" ], [])
+
+
+AC_CHECK_LIB(ws2_32, main, [ 
+  LIBS="$LIBS -lws2_32" 
+  is_win32="yes"
+], [
+  is_win32="no"
+])
+
 have_gnutls="no"
 AC_CHECK_LIB(gnutls, gnutls_init, [
   AC_CHECK_HEADERS(gnutls/gnutls.h, [
@@ -44,14 +52,16 @@ AC_CHECK_LIB(gnutls, gnutls_init, [
   ])
 ])
 if test x"$have_gnutls" = xno; then
-  AC_CHECK_LIB(ssl, SSL_read, [
-    AC_CHECK_LIB(crypto, X509_new, [
-      AC_CHECK_HEADERS(openssl/ssl.h openssl/err.h openssl/rand.h, [
-        LIBS="$LIBS -lssl -lcrypto"
-      ])
-    ])
-  ])
+  if test x$is_win32 = xyes ; then
+    openssl_deps="-lcrypto -lgdi32"
+  else
+    openssl_deps="-lcrypto"
+  fi
+  AC_CHECK_LIB([ssl],[SSL_library_init], [
+    LIBS="$LIBS -lssl $openssl_deps"
+  ], [AC_MSG_ERROR([OpenSSL libraries required])], $openssl_deps)
 fi
+
 AC_CHECK_LIB(pthread, pthread_create, [
   AC_CHECK_HEADERS(pthread.h, [
     LIBS="$LIBS -lpthread"
@@ -63,6 +73,7 @@ AC_CHECK_LIB(cares, ares_init, [
   ])
 ])
 
+
 AC_CONFIG_FILES([
   Makefile
   src/Makefile
@@ -72,6 +83,7 @@ AC_CONFIG_FILES([
   src/IOHandler_test/client/Makefile
   src/IOHandler_test/client++/Makefile
   src/IOHandler_test/client_ssl/Makefile
+  src/IOHandler_test/server/Makefile
   src/IOHandler_test/server_ssl/Makefile
   src/IOHandler_test/timer/Makefile
   src/IOHandler_test/timer++/Makefile
index d8ca43d946728000a7e4407c5feff67bee86ba21..ef3454a9664036ef07d1e60eda08a90ccfe19a5d 100644 (file)
@@ -1,3 +1,3 @@
 ##Process this file with automake to create Makefile.in
 ACLOCAL_AMFLAGS = -I m4
-SUBDIRS = client client++ client_ssl server_ssl timer timer++ resolv
+SUBDIRS = client client++ client_ssl server server_ssl timer timer++ resolv
diff --git a/src/IOHandler_test/server/.gitignore b/src/IOHandler_test/server/.gitignore
new file mode 100644 (file)
index 0000000..7af2f69
--- /dev/null
@@ -0,0 +1,7 @@
+.deps
+.libs
+*.o
+*.exe
+iotest
+Makefile
+Makefile.in
diff --git a/src/IOHandler_test/server/Makefile.am b/src/IOHandler_test/server/Makefile.am
new file mode 100644 (file)
index 0000000..feda450
--- /dev/null
@@ -0,0 +1,8 @@
+##Process this file with automake to create Makefile.in
+ACLOCAL_AMFLAGS = -I m4
+
+noinst_PROGRAMS = iotest
+iotest_LDADD = ../../IOHandler/libiohandler.la
+
+iotest_SOURCES = iotest.c
+
diff --git a/src/IOHandler_test/server/iotest.c b/src/IOHandler_test/server/iotest.c
new file mode 100644 (file)
index 0000000..8894cf7
--- /dev/null
@@ -0,0 +1,81 @@
+/* main.c - IOMultiplexer
+ * Copyright (C) 2012  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include "../../IOHandler/IOHandler.h"
+#include "../../IOHandler/IOSockets.h"
+#include "../../IOHandler/IOLog.h"
+
+static IOSOCKET_CALLBACK(io_callback);
+static IOLOG_CALLBACK(io_log);
+
+static struct IOSocket *irc_iofd = NULL;
+
+int main(int argc, char *argv[]) {
+       iohandler_init();
+       
+    iolog_register_callback(io_log);
+    
+    irc_iofd = iosocket_listen("0.0.0.0", 12345, io_callback);
+       
+       iohandler_run();
+       
+       return 0;
+}
+
+static IOSOCKET_CALLBACK(io_callback) {
+    switch(event->type) {
+        case IOSOCKETEVENT_ACCEPT:
+            printf("[client accepted]\n");
+                       struct IOSocket *client = event->data.accept_socket;
+                       client->callback = io_callback;
+                       
+                       char *html = "<html><head><title>Test Page</title></head><body><h1>IOHandler SSL Test</h1></body></html>";
+            iosocket_printf(client, "HTTP/1.1 200 OK\r\n");
+                       iosocket_printf(client, "Server: Apache\r\n");
+                       iosocket_printf(client, "Content-Length: %d\r\n", strlen(html));
+                       iosocket_printf(client, "Content-Type: text/html\r\n");
+                       iosocket_printf(client, "\r\n");
+                       iosocket_printf(client, "%s", html);
+                       
+            break;
+        case IOSOCKETEVENT_CLOSED:
+                       if(event->socket->listening)
+                               printf("[server closed]\n");
+                       else
+                               printf("[client disconnect]\n");
+            break;
+        case IOSOCKETEVENT_RECV:
+                       {
+                               struct IOSocketBuffer *recv_buf = event->data.recv_buf;
+                               int i;
+                               for(i = 0; i < recv_buf->bufpos; i++)
+                                       putchar(recv_buf->buffer[i]);
+                               recv_buf->bufpos = 0;
+                               printf("\n");
+            }
+            break;
+        
+        default:
+            break;
+    }
+}
+
+static IOLOG_CALLBACK(io_log) {
+       printf("%s", message);
+}