From cc57a74cefa69519f2145cebcadad4333afd57fb Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Sat, 22 Sep 2012 22:04:15 +0000 Subject: [PATCH] Introducing common code --- Makefile | 2 ++ src/Makefile | 7 +++++-- src/appclient.c | 12 ++---------- src/common.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/common.h | 25 +++++++++++++++++++++++++ src/project.conf | 6 +++--- 6 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 src/common.c create mode 100644 src/common.h diff --git a/Makefile b/Makefile index 4743e99..b4a05df 100644 --- a/Makefile +++ b/Makefile @@ -43,8 +43,10 @@ dist: $(PACKAGE)-$(VERSION)/src/appclient.c \ $(PACKAGE)-$(VERSION)/src/appinterface.c \ $(PACKAGE)-$(VERSION)/src/appserver.c \ + $(PACKAGE)-$(VERSION)/src/common.c \ $(PACKAGE)-$(VERSION)/src/Makefile \ $(PACKAGE)-$(VERSION)/src/appinterface.h \ + $(PACKAGE)-$(VERSION)/src/common.h \ $(PACKAGE)-$(VERSION)/src/project.conf \ $(PACKAGE)-$(VERSION)/tools/appbroker.c \ $(PACKAGE)-$(VERSION)/tools/appclient.c \ diff --git a/src/Makefile b/src/Makefile index fb847d6..83583c3 100644 --- a/src/Makefile +++ b/src/Makefile @@ -20,7 +20,7 @@ INSTALL ?= install all: $(TARGETS) -libApp_OBJS = appclient.o appinterface.o appserver.o +libApp_OBJS = appclient.o appinterface.o appserver.o common.o libApp_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS) libApp_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) `pkg-config --libs openssl` -lssl @@ -33,7 +33,7 @@ libApp.so.0.0 libApp.so.0 libApp.so: $(libApp_OBJS) $(LN) -s -- libApp.so.0.0 libApp.so.0 $(LN) -s -- libApp.so.0.0 libApp.so -appclient.o: appclient.c appinterface.h ../include/System/App/appclient.h +appclient.o: appclient.c appinterface.h ../include/System/App/appclient.h common.h $(CC) $(libApp_CFLAGS) -c appclient.c appinterface.o: appinterface.c ../include/System/App/appserver.h ../config.h @@ -42,6 +42,9 @@ appinterface.o: appinterface.c ../include/System/App/appserver.h ../config.h appserver.o: appserver.c appinterface.h ../include/System/App/appserver.h ../config.h $(CC) $(libApp_CFLAGS) -c appserver.c +common.o: common.c + $(CC) $(libApp_CFLAGS) -c common.c + clean: $(RM) -- $(libApp_OBJS) diff --git a/src/appclient.c b/src/appclient.c index 2a306d8..c35dcbf 100644 --- a/src/appclient.c +++ b/src/appclient.c @@ -39,6 +39,7 @@ #include #include "System/App/appclient.h" #include "appinterface.h" +#include "common.h" /* AppClient */ @@ -334,8 +335,6 @@ static int _new_connect(AppClient * appclient, char const * app) const char init[] = "Init"; struct sockaddr_in sa; int32_t port = -1; - int optval = 1; - int res; #ifdef DEBUG fprintf(stderr, "DEBUG: %s(%p, \"%s\")\n", __func__, (void *)appclient, @@ -347,14 +346,7 @@ static int _new_connect(AppClient * appclient, char const * app) sa.sin_port = htons(appinterface_get_port(appclient->interface)); if(_connect_addr(init, &sa.sin_addr.s_addr) != 0) return 1; -#ifdef TCP_NODELAY - res = setsockopt(appclient->fd, SOL_SOCKET, TCP_NODELAY, &optval, - sizeof(optval)); -# ifdef DEBUG - if(res != 0) - fprintf(stderr, "%s%s%s", init, ": ", strerror(errno)); -# endif -#endif + common_socket_set_nodelay(appclient->fd, 1); if(connect(appclient->fd, (struct sockaddr *)&sa, sizeof(sa)) != 0) return error_set_code(1, "%s%s%s", init, ": ", strerror(errno)); #ifdef DEBUG diff --git a/src/common.c b/src/common.c new file mode 100644 index 0000000..2f5ce17 --- /dev/null +++ b/src/common.c @@ -0,0 +1,42 @@ +/* $Id$ */ +/* Copyright (c) 2012 Pierre Pronchery */ +/* This file is part of DeforaOS System libApp */ +/* 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, version 3 of the License. + * + * 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 . */ + + + +#include +#include +#include +#include + + +/* common_socket_set_nodelay */ +int common_socket_set_nodelay(int fd, int nodelay) +{ + int ret = -1; +#ifdef TCP_NODELAY + int optval = nodelay ? 1 : 0; + + ret = setsockopt(fd, SOL_SOCKET, TCP_NODELAY, &optval, sizeof(optval)); +# ifdef DEBUG + if(ret != 0) + fprintf(stderr, "%s%s", "libApp: TCP_NODELAY: ", + strerror(errno)); +# endif + return (ret == 0) ? 0 : -1; +#else + errno = ENOSYS; + return ret; +#endif +} diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..66ed100 --- /dev/null +++ b/src/common.h @@ -0,0 +1,25 @@ +/* $Id$ */ +/* Copyright (c) 2012 Pierre Pronchery */ +/* This file is part of DeforaOS System libApp */ +/* 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, version 3 of the License. + * + * 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 . */ + + + +#ifndef LIBAPP_COMMON_H +# define LIBAPP_COMMON_H + + +/* functions */ +int common_socket_set_nodelay(int fd, int nodelay); + +#endif /* !LIBAPP_COMMON_H */ diff --git a/src/project.conf b/src/project.conf index 2485565..6af4d9b 100644 --- a/src/project.conf +++ b/src/project.conf @@ -6,17 +6,17 @@ cflags_force=-W -fPIC `pkg-config --cflags libSystem` cflags=-Wall -g -O2 -pedantic ldflags_force=`pkg-config --libs libSystem` ldflags=-lssl -dist=Makefile,appinterface.h +dist=Makefile,appinterface.h,common.h [libApp] type=library -sources=appclient.c,appinterface.c,appserver.c +sources=appclient.c,appinterface.c,appserver.c,common.c ldflags=`pkg-config --libs openssl` -lssl -lsocket -lws2_32 #ldflags=-lsocket -lws2_32 install=$(LIBDIR) [appclient.c] -depends=appinterface.h,../include/System/App/appclient.h +depends=appinterface.h,../include/System/App/appclient.h,common.h [appinterface.c] depends=../include/System/App/appserver.h,../config.h