diff options
author | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2002-08-20 10:42:36 +0800 |
---|---|---|
committer | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2002-08-20 10:42:36 +0800 |
commit | 70148db3434a3d889218972a490437d2f3363036 (patch) | |
tree | bd2f9f0695968b8a34524e6fdb84d025b03b928b | |
parent | 0b5909999dcc7f79f44e1fda88223d24dc612ccf (diff) | |
download | pttbbs-70148db3434a3d889218972a490437d2f3363036.tar pttbbs-70148db3434a3d889218972a490437d2f3363036.tar.gz pttbbs-70148db3434a3d889218972a490437d2f3363036.tar.bz2 pttbbs-70148db3434a3d889218972a490437d2f3363036.tar.lz pttbbs-70148db3434a3d889218972a490437d2f3363036.tar.xz pttbbs-70148db3434a3d889218972a490437d2f3363036.tar.zst pttbbs-70148db3434a3d889218972a490437d2f3363036.zip |
remove OUTTA_CACHE
git-svn-id: http://opensvn.csie.org/pttbbs/pttbbs/trunk/pttbbs@480 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r-- | include/bbs.h | 3 | ||||
-rw-r--r-- | include/outtacache.h | 28 | ||||
-rw-r--r-- | include/proto.h | 7 | ||||
-rw-r--r-- | mbbsd/board.c | 54 | ||||
-rw-r--r-- | mbbsd/cache.c | 164 | ||||
-rw-r--r-- | mbbsd/edit.c | 5 | ||||
-rw-r--r-- | mbbsd/more.c | 6 | ||||
-rw-r--r-- | mbbsd/voteboard.c | 5 | ||||
-rw-r--r-- | util/.cvsignore | 2 | ||||
-rw-r--r-- | util/Makefile | 14 | ||||
-rw-r--r-- | util/cacheserver.c | 139 | ||||
-rw-r--r-- | util/localserver.c | 116 |
12 files changed, 9 insertions, 534 deletions
diff --git a/include/bbs.h b/include/bbs.h index a56ee944..09ce1982 100644 --- a/include/bbs.h +++ b/include/bbs.h @@ -45,9 +45,6 @@ #ifndef INCLUDE_VAR_H #include "var.h" #endif -#ifdef OUTTA_CACHE - #include "outtacache.h" -#endif #ifdef FreeBSD #include <machine/limits.h> #else diff --git a/include/outtacache.h b/include/outtacache.h deleted file mode 100644 index 6865935b..00000000 --- a/include/outtacache.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef INCLUDE_OUTTACACHE_H -#define INCLUDE_OUTTACACHE_H - -#define CACHE_BUFSIZE (20000000) -#define OC_HEADERLEN (sizeof(OCkey_t) + sizeof(int)) -#define OC_KEYLEN (sizeof(OCkey_t)) -#define OC_pidadd 10000000 -#define OC_msto 5111 -#define OC_mtos 5112 - -typedef struct { - pid_t pid; - char cacheid; -} OCkey_t; - -typedef struct { - OCkey_t key; - int length; - char buf[CACHE_BUFSIZE]; -} OCbuf_t; - - -typedef struct { - time_t mtime; - OCbuf_t data; -} OCstore_t; - -#endif diff --git a/include/proto.h b/include/proto.h index 858f0a8b..fe8bad6c 100644 --- a/include/proto.h +++ b/include/proto.h @@ -1,4 +1,4 @@ -/* $Id: proto.h,v 1.26 2002/08/17 12:31:55 in2 Exp $ */ +/* $Id: proto.h,v 1.27 2002/08/20 02:42:36 in2 Exp $ */ #ifndef INCLUDE_PROTO_H #define INCLUDE_PROTO_H @@ -117,11 +117,6 @@ int get_fileheader_cache(int bid, char *direct, fileheader_t *headers, int recbase, int nlines); void *attach_shm(int shmkey, int shmsize); void attach_SHM(void); -#ifdef OUTTA_CACHE -void *outta_malloc(size_t size, char id); -void outta_swapout(void **inptr); -void *outta_swapin(void **inptr, char cacheid); -#endif /* cal */ int give_tax(int money); diff --git a/mbbsd/board.c b/mbbsd/board.c index 59dbd68a..350a482d 100644 --- a/mbbsd/board.c +++ b/mbbsd/board.c @@ -1,4 +1,4 @@ -/* $Id: board.c,v 1.52 2002/08/19 14:53:05 in2 Exp $ */ +/* $Id: board.c,v 1.53 2002/08/20 02:42:36 in2 Exp $ */ #include "bbs.h" #define BRC_STRLEN 15 /* Length of board name */ #define BRC_MAXSIZE 24576 @@ -235,18 +235,11 @@ init_brdbuf() /* MAXBOARDS ==> 至多看得見 32 個新板 */ n = numboards + 32; size = n * sizeof(int); -#ifdef OUTTA_CACHE - zapbuf = (int *)outta_malloc(size, 'z'); - favbuf = (int *)outta_malloc(size + sizeof(int), 'f'); -#else zapbuf = (int *)malloc(size); favbuf = (int *)malloc(size + sizeof(int)); -#endif -#if 0 favbuf[0] = 0x5c4d3e; /* for check memory */ ++favbuf; -#endif memset(favbuf, 0, size); @@ -266,11 +259,6 @@ init_brdbuf() favbuf[n] &= ~BRD_TAG; brc_expire_time = login_start_time - 365 * 86400; - -#ifdef OUTTA_CACHE - outta_swapout((void **)&favbuf); - outta_swapout((void **)&zapbuf); -#endif } void @@ -282,10 +270,7 @@ save_brdbuf() if ( reentrant ) return; reentrant = 1; -#ifdef OUTTA_CACHE - outta_swapin((void **)&favbuf, 'f'); - outta_swapin((void **)&zapbuf, 'z'); -#endif + if (!zapbuf) return; setuserfile(fname, STR_BBSRC); @@ -294,14 +279,12 @@ save_brdbuf() write(fd, zapbuf, size); close(fd); } -#if 0 if (favbuf[-1] != 0x5c4d3e) { FILE *fp = fopen(BBSHOME "/log/memorybad", "a"); fprintf(fp, "%s %s %d\n", cuser.userid, Cdatelite(&now), favbuf[-1]); fclose(fp); return; } -#endif setuserfile(fname, STR_FAV); if ((fd = open(fname, O_WRONLY | O_CREAT, 0600)) != -1) { size = numboards * sizeof(int); @@ -468,11 +451,7 @@ load_boards(char *key) brdnum = 0; if (class_bid <= 0) { nbrdlength = numboards * sizeof(boardstat_t); -#ifdef OUTTA_CACHE - nbrd = (boardstat_t *) outta_malloc(nbrdlength, 'b'); -#else nbrd = (boardstat_t *) malloc(nbrdlength); -#endif for (i = 0; i < numboards; i++) { if ((bptr = SHM->bsorted[type][i]) == NULL) continue; @@ -491,11 +470,7 @@ load_boards(char *key) } } else { nbrdlength = bptr->childcount * sizeof(boardstat_t); -#ifdef OUTTA_CACHE - nbrd = (boardstat_t *) outta_malloc(nbrdlength, 'b'); -#else nbrd = (boardstat_t *) malloc(nbrdlength); -#endif for (bptr = bptr->firstchild[type]; bptr != (boardheader_t *) ~ 0; bptr = bptr->next[type]) { n = (int)(bptr - bcache); @@ -738,14 +713,6 @@ choose_board(int newflag) char genbuf[200]; #endif -#ifdef OUTTA_CACHE - static char depth = 0; - ++depth; - if( favbuf == NULL ) - outta_swapin((void **)&favbuf, 'f'); - if( zapbuf == NULL ) - outta_swapin((void **)&zapbuf, 'z'); -#endif setutmpmode(newflag ? READNEW : READBRD); brdnum = 0; if (!cuser.userlevel) /* guest yank all boards */ @@ -1054,18 +1021,7 @@ choose_board(int newflag) board_visit_time = zapbuf[ptr->bid - 1]; if (!(ptr->myattr & BRD_ZAP)) zapbuf[ptr->bid - 1] = now; -#ifdef OUTTA_CACHE - outta_swapout((void **)&favbuf); - outta_swapout((void **)&zapbuf); - outta_swapout((void **)&nbrd); -#endif Read(); -#ifdef OUTTA_CACHE - outta_swapin((void **)&favbuf, 'f'); - outta_swapin((void **)&zapbuf, 'z'); - outta_swapin((void **)&nbrd, 'b'); - ptr = &nbrd[num]; -#endif check_newpost(ptr); head = -1; setutmpmode(newflag ? READNEW : READBRD); @@ -1112,12 +1068,6 @@ choose_board(int newflag) } } while (ch != 'q'); free(nbrd); -#ifdef OUTTA_CACHE - if( --depth == 0 ){ - outta_swapout((void **)&favbuf); - outta_swapout((void **)&zapbuf); - } -#endif } int diff --git a/mbbsd/cache.c b/mbbsd/cache.c index c824b4f6..7c4a1992 100644 --- a/mbbsd/cache.c +++ b/mbbsd/cache.c @@ -1,4 +1,4 @@ -/* $Id: cache.c,v 1.48 2002/08/19 14:47:41 in2 Exp $ */ +/* $Id: cache.c,v 1.49 2002/08/20 02:42:36 in2 Exp $ */ #include "bbs.h" #ifndef __FreeBSD__ @@ -1066,165 +1066,3 @@ hbflcheck(int bid, int uid) } return 1; } - -#ifdef MDCACHE -char * -cachepath(const char *fpath) -{ - static char cpath[128]; - char *ptr; - snprintf(cpath, sizeof(cpath), "cache/%s", fpath); - for (ptr = &cpath[6]; *ptr != 0; ++ptr) - if (*ptr == '/') - *ptr = '.'; - return cpath; -} - -int -updatemdcache(const char *CPATH, const char *fpath) -{ - /* - * save file to mdcache with *cpath and *fpath, return: -1 if error - * else the fd - */ - int len, sourcefd, targetfd; - char buf[1024], *cpath; - cpath = (CPATH == NULL) ? cachepath(fpath) : (char *)CPATH; - if ((sourcefd = open(fpath, O_RDONLY)) < 0) - return -1; - if ((targetfd = open(cpath, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) - /* md is full? */ - return -1; - while ((len = read(sourcefd, buf, sizeof(buf))) > 0) - if (write(targetfd, buf, len) < len) { - /* md is full? */ - close(targetfd); - unlink(cpath); - lseek(sourcefd, 0, SEEK_SET); - return sourcefd; - } - close(sourcefd); - lseek(targetfd, 0, SEEK_SET); - return targetfd; -} - -int -mdcacheopen(char *fpath) -{ - int fd; - char *cpath; - if (strncmp(fpath, "boards/", 7) && strncmp(fpath, "etc/", 4)) - return open(fpath, O_RDONLY); - -#ifdef MDCACHEHITRATE - ++GLOBE[0]; -#endif - if ((fd = open((cpath = cachepath(fpath)), O_RDONLY)) < 0) - return updatemdcache(cpath, fpath); -#ifdef MDCACHEHITRATE - else - ++GLOBE[1]; -#endif - - return fd; -} -#endif - -#ifdef OUTTA_CACHE -#include <err.h> -static ssize_t Read(int fd, void *BUF, size_t nbytes) -{ - char *buf = (char *)BUF; - ssize_t thisgot, totalgot = nbytes; - while( nbytes > 0 ){ - if( (thisgot = read(fd, buf, nbytes)) <= 0 ) - err(1, "read from socket: "); - nbytes -= thisgot; - buf += thisgot; - } - return totalgot; -} - -static ssize_t Write(int fd, void *BUF, size_t nbytes) -{ - char *buf = (char *)BUF; - ssize_t thisgot, totalgot = nbytes; - while( nbytes > 0 ){ - if( (thisgot = write(fd, buf, nbytes)) <= 0 ) - err(1, "read from socket: "); - nbytes -= thisgot; - buf += thisgot; - } - return totalgot; -} - -int connectserver(char *host, int port) -{ - struct sockaddr_in servaddr; - int fd; - - if( (fd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) - err(1, "socket"); - bzero(&servaddr, sizeof(servaddr)); - servaddr.sin_family = AF_INET; - inet_pton(AF_INET, host, &servaddr.sin_addr); - servaddr.sin_port = htons(port); - if( connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) - err(1, "connect"); - return fd; -} - -void *outta_malloc(size_t size, char id) -{ - OCbuf_t *ptr = (OCbuf_t *)malloc(OC_HEADERLEN + size); -#ifdef DEBUG - vmsg("outta_malloc(%d, %c)", size, id); -#endif - ptr->key.pid = getpid(); - ptr->key.cacheid = id; - ptr->length = size; - return ptr->buf; -} - -void outta_swapout(void **inptr) -{ - OCbuf_t *ptr = (OCbuf_t *)(*inptr - OC_HEADERLEN); - int fd, len; - fd = connectserver("10.2.1.2", 1477); - len = ptr->length + OC_HEADERLEN; -#ifdef DEBUG - vmsg("outta_swapout(%d)", len); -#endif - Write(fd, &len, sizeof(len)); - Write(fd, ptr, len); - close(fd); - free(ptr); - *inptr = NULL; -} - -void *outta_swapin(void **inptr, char cacheid) -{ - char buf[OC_HEADERLEN]; - OCbuf_t *ptr = (OCbuf_t *)buf; - int fd, len; - - fd = connectserver("10.2.1.2", 1477); - ptr->key.pid = getpid() + OC_pidadd; - ptr->key.cacheid = cacheid; - ptr->length = 0; - len = OC_HEADERLEN; - Write(fd, &len, sizeof(len)); - Write(fd, ptr, OC_HEADERLEN); - - Read(fd, &len, sizeof(len)); -#ifdef DEBUG - vmsg("outta_swapin(%d)", len); -#endif - ptr = (OCbuf_t *)malloc(len); - Read(fd, ptr, len); - close(fd); - - return (*inptr = ptr->buf); -} - -#endif diff --git a/mbbsd/edit.c b/mbbsd/edit.c index ad210b12..589be70d 100644 --- a/mbbsd/edit.c +++ b/mbbsd/edit.c @@ -1,4 +1,4 @@ -/* $Id: edit.c,v 1.15 2002/07/22 19:02:00 in2 Exp $ */ +/* $Id: edit.c,v 1.16 2002/08/20 02:42:36 in2 Exp $ */ #include "bbs.h" typedef struct textline_t { struct textline_t *prev; @@ -1025,9 +1025,6 @@ write_file(char *fpath, int saveheader, int *islocal) } fclose(fp); -#ifdef MDCACHE - close(updatemdcache(NULL, fpath)); -#endif if (local_article && (currstat == POSTING)) return 0; return 0; diff --git a/mbbsd/more.c b/mbbsd/more.c index d7508ce4..fc3434ba 100644 --- a/mbbsd/more.c +++ b/mbbsd/more.c @@ -1,4 +1,4 @@ -/* $Id: more.c,v 1.19 2002/07/27 10:55:30 kcwu Exp $ */ +/* $Id: more.c,v 1.20 2002/08/20 02:42:36 in2 Exp $ */ #include "bbs.h" #define MORE_BUFSIZE 4096 #define MORE_WINSIZE 4096 @@ -156,11 +156,7 @@ more(char *fpath, int promptend) search_char0 = *search_str; *search_str = 0; -#ifdef MDCACHE - fd = mdcacheopen(fpath); -#else fd = open(fpath, O_RDONLY, 0600); -#endif if (fd < 0) return -1; diff --git a/mbbsd/voteboard.c b/mbbsd/voteboard.c index 27ca0bd3..76bf0cbc 100644 --- a/mbbsd/voteboard.c +++ b/mbbsd/voteboard.c @@ -1,4 +1,4 @@ -/* $Id: voteboard.c,v 1.13 2002/07/22 19:02:01 in2 Exp $ */ +/* $Id: voteboard.c,v 1.14 2002/08/20 02:42:36 in2 Exp $ */ #include "bbs.h" #define VOTEBOARD "NewBoard" @@ -170,9 +170,6 @@ do_voteboardreply(fileheader_t * fhdr) fclose(fo); unlink(oldfpath); rename(fpath, oldfpath); -#ifdef MDCACHE - close(updatemdcache(NULL, oldfpath)); -#endif } int diff --git a/util/.cvsignore b/util/.cvsignore index c72037ba..d33e906f 100644 --- a/util/.cvsignore +++ b/util/.cvsignore @@ -44,5 +44,3 @@ shmctl mdclean splitpasswd indexuser -cacheserver -localserver diff --git a/util/Makefile b/util/Makefile index f704a564..81f466a7 100644 --- a/util/Makefile +++ b/util/Makefile @@ -1,4 +1,4 @@ -# $Id: Makefile,v 1.18 2002/08/17 12:31:56 in2 Exp $ +# $Id: Makefile,v 1.19 2002/08/20 02:42:36 in2 Exp $ BBSHOME?=$(HOME) OSTYPE!=uname @@ -30,8 +30,7 @@ CPROGS= bbsmail BM_money post account birth deluserfile expire mandex\ poststat showboard antispam countalldice webgrep bbsrf\ initbbs outmail xchatd userlist tunepasswd buildir reaper shmsweep\ merge_passwd merge_board inndBM buildAnnounce rmuid \ - toplazyBM jungo toplazyBBM shmctl mdclean splitpasswd indexuser \ - cacheserver localserver testoc + toplazyBM jungo toplazyBBM shmctl mdclean splitpasswd indexuser PROGS= $(CPROGS) BM_money.sh backpasswd.sh mailog.sh opendice.sh\ openticket.sh stock.sh topsong.sh weather.sh stock.perl weather.perl\ @@ -169,15 +168,6 @@ mdclean: mdclean.c splitpasswd: splitpasswd.c $(CC) $(CFLAGS) -o $@ $@.c -cacheserver: cacheserver.c - $(CC) $(CFLAGS) -I/usr/local/include -lavltree -L/usr/local/lib -o $@ $@.c - -localserver: localserver.c - $(CC) $(CFLAGS) -o $@ $@.c - -testoc: testoc.c - $(CC) $(CFLAGS) -o $@ $@.c - install: $(PROGS) install -d $(BBSHOME)/bin/ install -c -m 755 $(PROGS) $(BBSHOME)/bin/ diff --git a/util/cacheserver.c b/util/cacheserver.c deleted file mode 100644 index cc0e36e3..00000000 --- a/util/cacheserver.c +++ /dev/null @@ -1,139 +0,0 @@ -#include "bbs.h" -#include <err.h> -#include <avltree.h> - -int toaccept(int port); -void usage(void); -ssize_t Read(int fd, void *buf, size_t nbytes); -ssize_t Write(int fd, void *BUF, size_t nbytes) -{ - char *buf = (char *)BUF; - size_t thisgot, totalgot = nbytes; - while( nbytes > 0 ){ - if( (thisgot = write(fd, buf, nbytes)) <= 0 ) - err(1, "read from socket: "); - nbytes -= thisgot; - buf += thisgot; - } - return totalgot; -} - - -int main(int argc, char **argv) -{ - int sfd, len; - int port = -1; - char ch; - OCbuf_t OCbuf; - - AVL_IX_DESC ix; - AVL_IX_REC pe; - OCstore_t *store; - //avl_create_index(&ix, AVL_COUNT_DUPS, OC_KEYLEN); - avl_create_index(&ix, AVL_COUNT_DUPS, 5); - - while( (ch = getopt(argc, argv, "p:")) != -1 ) - switch( ch ){ - case 'p': - port = atoi(optarg); - break; - } - if( port == -1 ) - usage(); - - while( 1 ){ - sfd = toaccept(port); - Read(sfd, &len, sizeof(len)); - printf("reading %d bytes\n", len); - Read(sfd, &OCbuf, len); - printf("read! pid: %d\n", OCbuf.key.pid); - - memset(&pe, 0, sizeof(pe)); - if( OCbuf.key.pid <= 1 ){ - /* garbage collection */ - } - else if( OCbuf.key.pid < OC_pidadd ){ - /* store */ - puts("swapin"); - - len = OC_HEADERLEN + OCbuf.length; - store = (OCstore_t *)malloc(sizeof(time_t) + len); - store->mtime = time(NULL); - memcpy(&(store->data), &OCbuf, len); - - pe.recptr = (void *)store; - memcpy(pe.key, &OCbuf.key, OC_KEYLEN); - if( avl_add_key(&pe, &ix) != AVL_IX_OK ) - puts("add key error"); - } - else { - OCbuf.key.pid -= OC_pidadd; - memcpy(pe.key, &OCbuf.key, OC_KEYLEN); - if( avl_find_key(&pe, &ix) == AVL_IX_OK ){ - store = (OCstore_t *)pe.recptr; - - len = store->data.length + OC_HEADERLEN; - Write(sfd, &len, sizeof(len)); - Write(sfd, &(store->data), len); - free(store); - if( avl_delete_key(&pe, &ix) != AVL_IX_OK ) - puts("delete key error"); - } - else - puts("error"); - } - close(sfd); - } - return 0; -} - -int toaccept(int port) -{ - static int sockfd = 0, len; - int cfd, val; - struct sockaddr_in servaddr, clientaddr; - - if( sockfd == 0 ){ - if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) - err(1, NULL); - setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)); - setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (char *)&val, sizeof(val)); - - bzero(&servaddr, sizeof(servaddr)); - servaddr.sin_family = AF_INET; - servaddr.sin_addr.s_addr = htonl(INADDR_ANY); - servaddr.sin_port = htons(port); - if( bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) - err(1, NULL); - if( listen(sockfd, 5) < 0 ) - err(1, NULL); - - len = sizeof(struct sockaddr_in); - } - - while( (cfd = accept(sockfd, (struct sockaddr *)&clientaddr, &len)) <= 0 ) - ; - - return cfd; -} - -void usage(void) -{ - fprintf(stderr, - "usage:\tcacheserver [options]\n" - "-p port\tlistenport\n"); - exit(0); -} - -ssize_t Read(int fd, void *BUF, size_t nbytes) -{ - char *buf = (char *)BUF; - ssize_t thisgot, totalgot = nbytes; - while( nbytes > 0 ){ - if( (thisgot = read(fd, buf, nbytes)) <= 0 ) - err(1, "read from socket: "); - nbytes -= thisgot; - buf += thisgot; - } - return totalgot; -} diff --git a/util/localserver.c b/util/localserver.c deleted file mode 100644 index eb90bd6f..00000000 --- a/util/localserver.c +++ /dev/null @@ -1,116 +0,0 @@ -#include "bbs.h" -#include <err.h> - -void usage(void); -int connectserver(char *, int); -void listennetwork(int, int); -void listenmsgqueue(int, int); - -int main(int argc, char **argv) -{ - char *host = NULL; - int port = 0, ch, sfd, msto, mtos; - pid_t pid; - while( (ch = getopt(argc, argv, "s:p:")) != -1 ) - switch( ch ){ - case 's': - host = strdup(optarg); - break; - case 'p': - port = atoi(optarg); - break; - default: - usage(); - } - - if( host == NULL || port == 0 ) - usage(); - - printf("connecting to server %s:%d\n", host, port); - sfd = connectserver(host, port); - puts("connected"); - - puts("attaching message queue"); - if( (msto = msgget(OC_msto, 0600 | IPC_CREAT)) < 0 ) - err(1, "msgget OC_msto"); - if( (mtos = msgget(OC_mtos, 0600 | IPC_CREAT)) < 0 ) - err(1, "msgget OC_mtos"); - puts("attached"); - - if( (pid = fork()) < 0 ) - err(1, "fork()"); - else if( pid == 0 ) - listennetwork(sfd, msto); - listenmsgqueue(sfd, mtos); - - return 0; -} - -void usage(void) -{ - fprintf(stderr, "usage:\tlocalserver -s host -p port\n"); - exit(0); -} - -int connectserver(char *host, int port) -{ - struct sockaddr_in servaddr; - int fd; - - if( (fd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) - err(1, "socket"); - bzero(&servaddr, sizeof(servaddr)); - servaddr.sin_family = AF_INET; - inet_pton(AF_INET, host, &servaddr.sin_addr); - servaddr.sin_port = htons(port); - if( connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) - err(1, "connect"); - return fd; -} - -void listennetwork(int netfd, int msgfd) -{ - int len; - char buf[CACHE_BUFSIZE]; - printf("(%d)listening network %d => message queue %d\n", - getpid(), netfd, msgfd); - while( Read(netfd, &len, 4) > 0 ){ - if( Read(netfd, buf, len) < 0 ) - err(1, "(network)read()"); - if( msgsnd(msgfd, buf, len, 0) < 0 ) - err(1, "(network)msgsnd()"); - } - exit(0); -} - -void listenmsgqueue(int netfd, int msgfd) -{ - char buf[CACHE_BUFSIZE]; - OCbuf_t *ptr; - int len; - printf("(%d)listening message queue %d => network %d\n", - getpid(), msgfd, netfd); - while( msgrcv(msgfd, buf, CACHE_BUFSIZE, 0, 0) > 0 ){ - ptr = (OCbuf_t *)buf; - len = ptr->length + OC_HEADERLEN; - if( write(netfd, &len, sizeof(len)) < 0 || - write(netfd, ptr, ptr->length + OC_HEADERLEN) < 0 ) - err(1, "(msgqueue)write()"); - } - err(1, "(msgqueue)msgrcv()"); - exit(0); -} - -ssize_t Read(int fd, void *BUF, size_t nbytes) -{ - char *buf = (char *)BUF; - size_t thisgot, totalgot = nbytes; - while( nbytes > 0 ){ - if( (thisgot = read(fd, buf, nbytes)) <= 0 ) - err(1, "read from socket: "); - nbytes -= thisgot; - buf += thisgot; - } - return totalgot; -} - |