diff options
author | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2002-08-19 22:46:46 +0800 |
---|---|---|
committer | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2002-08-19 22:46:46 +0800 |
commit | ad652344be2baad5b929a1c53dfff0e28b3ee34f (patch) | |
tree | 1c0578067f1263ca9c5ec5158fe7cdd493016083 /util | |
parent | 30ae3eb353c54ba06efab9b776efdec11983cb6b (diff) | |
download | pttbbs-ad652344be2baad5b929a1c53dfff0e28b3ee34f.tar pttbbs-ad652344be2baad5b929a1c53dfff0e28b3ee34f.tar.gz pttbbs-ad652344be2baad5b929a1c53dfff0e28b3ee34f.tar.bz2 pttbbs-ad652344be2baad5b929a1c53dfff0e28b3ee34f.tar.lz pttbbs-ad652344be2baad5b929a1c53dfff0e28b3ee34f.tar.xz pttbbs-ad652344be2baad5b929a1c53dfff0e28b3ee34f.tar.zst pttbbs-ad652344be2baad5b929a1c53dfff0e28b3ee34f.zip |
cacheserver.c
git-svn-id: http://opensvn.csie.org/pttbbs/pttbbs/trunk/pttbbs@476 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'util')
-rw-r--r-- | util/cacheserver.c | 73 |
1 files changed, 46 insertions, 27 deletions
diff --git a/util/cacheserver.c b/util/cacheserver.c index 14fe5909..cc0e36e3 100644 --- a/util/cacheserver.c +++ b/util/cacheserver.c @@ -2,9 +2,22 @@ #include <err.h> #include <avltree.h> -int acceptone(int port); +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) { @@ -16,7 +29,8 @@ int main(int argc, char **argv) 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, OC_KEYLEN); + avl_create_index(&ix, AVL_COUNT_DUPS, 5); while( (ch = getopt(argc, argv, "p:")) != -1 ) switch( ch ){ @@ -27,8 +41,9 @@ int main(int argc, char **argv) if( port == -1 ) usage(); - sfd = acceptone(port); - while( Read(sfd, &len, sizeof(len)) > 0 ){ + 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); @@ -58,8 +73,8 @@ int main(int argc, char **argv) store = (OCstore_t *)pe.recptr; len = store->data.length + OC_HEADERLEN; - write(sfd, &len, sizeof(len)); - write(sfd, &(store->data), len); + 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"); @@ -67,34 +82,38 @@ int main(int argc, char **argv) else puts("error"); } + close(sfd); } return 0; } -int acceptone(int port) +int toaccept(int port) { - int sockfd, val, cfd, len; + 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); + } - if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) - err(1, NULL); - setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (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); - - if( (cfd = accept(sockfd, (struct sockaddr *)&clientaddr, &len)) < 0 ) - err(1, NULL); + while( (cfd = accept(sockfd, (struct sockaddr *)&clientaddr, &len)) <= 0 ) + ; - close(sockfd); return cfd; } @@ -109,7 +128,7 @@ void usage(void) ssize_t Read(int fd, void *BUF, size_t nbytes) { char *buf = (char *)BUF; - size_t thisgot, totalgot = nbytes; + ssize_t thisgot, totalgot = nbytes; while( nbytes > 0 ){ if( (thisgot = read(fd, buf, nbytes)) <= 0 ) err(1, "read from socket: "); |