Introducing common code

This commit is contained in:
Pierre Pronchery 2012-09-22 22:04:15 +00:00
parent 68121f52f3
commit cc57a74cef
6 changed files with 79 additions and 15 deletions

View File

@ -43,8 +43,10 @@ dist:
$(PACKAGE)-$(VERSION)/src/appclient.c \ $(PACKAGE)-$(VERSION)/src/appclient.c \
$(PACKAGE)-$(VERSION)/src/appinterface.c \ $(PACKAGE)-$(VERSION)/src/appinterface.c \
$(PACKAGE)-$(VERSION)/src/appserver.c \ $(PACKAGE)-$(VERSION)/src/appserver.c \
$(PACKAGE)-$(VERSION)/src/common.c \
$(PACKAGE)-$(VERSION)/src/Makefile \ $(PACKAGE)-$(VERSION)/src/Makefile \
$(PACKAGE)-$(VERSION)/src/appinterface.h \ $(PACKAGE)-$(VERSION)/src/appinterface.h \
$(PACKAGE)-$(VERSION)/src/common.h \
$(PACKAGE)-$(VERSION)/src/project.conf \ $(PACKAGE)-$(VERSION)/src/project.conf \
$(PACKAGE)-$(VERSION)/tools/appbroker.c \ $(PACKAGE)-$(VERSION)/tools/appbroker.c \
$(PACKAGE)-$(VERSION)/tools/appclient.c \ $(PACKAGE)-$(VERSION)/tools/appclient.c \

View File

@ -20,7 +20,7 @@ INSTALL ?= install
all: $(TARGETS) 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_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
libApp_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) `pkg-config --libs openssl` -lssl 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.0
$(LN) -s -- libApp.so.0.0 libApp.so $(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 $(CC) $(libApp_CFLAGS) -c appclient.c
appinterface.o: appinterface.c ../include/System/App/appserver.h ../config.h 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 appserver.o: appserver.c appinterface.h ../include/System/App/appserver.h ../config.h
$(CC) $(libApp_CFLAGS) -c appserver.c $(CC) $(libApp_CFLAGS) -c appserver.c
common.o: common.c
$(CC) $(libApp_CFLAGS) -c common.c
clean: clean:
$(RM) -- $(libApp_OBJS) $(RM) -- $(libApp_OBJS)

View File

@ -39,6 +39,7 @@
#include <System.h> #include <System.h>
#include "System/App/appclient.h" #include "System/App/appclient.h"
#include "appinterface.h" #include "appinterface.h"
#include "common.h"
/* AppClient */ /* AppClient */
@ -334,8 +335,6 @@ static int _new_connect(AppClient * appclient, char const * app)
const char init[] = "Init"; const char init[] = "Init";
struct sockaddr_in sa; struct sockaddr_in sa;
int32_t port = -1; int32_t port = -1;
int optval = 1;
int res;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "DEBUG: %s(%p, \"%s\")\n", __func__, (void *)appclient, 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)); sa.sin_port = htons(appinterface_get_port(appclient->interface));
if(_connect_addr(init, &sa.sin_addr.s_addr) != 0) if(_connect_addr(init, &sa.sin_addr.s_addr) != 0)
return 1; return 1;
#ifdef TCP_NODELAY common_socket_set_nodelay(appclient->fd, 1);
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
if(connect(appclient->fd, (struct sockaddr *)&sa, sizeof(sa)) != 0) if(connect(appclient->fd, (struct sockaddr *)&sa, sizeof(sa)) != 0)
return error_set_code(1, "%s%s%s", init, ": ", strerror(errno)); return error_set_code(1, "%s%s%s", init, ": ", strerror(errno));
#ifdef DEBUG #ifdef DEBUG

42
src/common.c Normal file
View File

@ -0,0 +1,42 @@
/* $Id$ */
/* Copyright (c) 2012 Pierre Pronchery <khorben@defora.org> */
/* 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 <http://www.gnu.org/licenses/>. */
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
/* 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
}

25
src/common.h Normal file
View File

@ -0,0 +1,25 @@
/* $Id$ */
/* Copyright (c) 2012 Pierre Pronchery <khorben@defora.org> */
/* 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 <http://www.gnu.org/licenses/>. */
#ifndef LIBAPP_COMMON_H
# define LIBAPP_COMMON_H
/* functions */
int common_socket_set_nodelay(int fd, int nodelay);
#endif /* !LIBAPP_COMMON_H */

View File

@ -6,17 +6,17 @@ cflags_force=-W -fPIC `pkg-config --cflags libSystem`
cflags=-Wall -g -O2 -pedantic cflags=-Wall -g -O2 -pedantic
ldflags_force=`pkg-config --libs libSystem` ldflags_force=`pkg-config --libs libSystem`
ldflags=-lssl ldflags=-lssl
dist=Makefile,appinterface.h dist=Makefile,appinterface.h,common.h
[libApp] [libApp]
type=library 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=`pkg-config --libs openssl` -lssl -lsocket -lws2_32
#ldflags=-lsocket -lws2_32 #ldflags=-lsocket -lws2_32
install=$(LIBDIR) install=$(LIBDIR)
[appclient.c] [appclient.c]
depends=appinterface.h,../include/System/App/appclient.h depends=appinterface.h,../include/System/App/appclient.h,common.h
[appinterface.c] [appinterface.c]
depends=../include/System/App/appserver.h,../config.h depends=../include/System/App/appserver.h,../config.h