From 2f562a175af17a7477749e2759294faba5c5f552 Mon Sep 17 00:00:00 2001 From: sears Date: Tue, 26 Jan 2010 20:19:36 +0000 Subject: [PATCH] remove more duplicated code git-svn-id: svn+ssh://svn.corp.yahoo.com/yahoo/yrl/labs/pnuts/code/logstore@528 8dad8b1f-cf64-0410-95b6-bcf113ffbcfe --- logserver.cpp | 28 ++++------------------------ logserver.h | 23 +++++++++++++++++++++++ test/check_tcpclient.cpp | 28 ++++------------------------ 3 files changed, 31 insertions(+), 48 deletions(-) diff --git a/logserver.cpp b/logserver.cpp index 3f9eb54..4bd7e2e 100644 --- a/logserver.cpp +++ b/logserver.cpp @@ -212,7 +212,6 @@ void logserver::eventLoop() int maxfd; - struct timeval Timeout; struct timespec ts; while(true) @@ -406,25 +405,6 @@ void *serverLoop(void *args) } -inline void readfromsocket(int sockd, byte *buf, int count) -{ - - int n = 0; - while( n < count ) - { - n += read( sockd, buf + n, count - n); - } - -} - -inline void writetosocket(int sockd, byte *buf, int count) -{ - int n = 0; - while( n < count ) - { - n += write( sockd, buf + n, count - n); - } -} @@ -504,12 +484,12 @@ void * thread_work_fn( void * args) //read the key tuple.key = (byte*) malloc(*tuple.keylen); - readfromsocket(*(item->data->workitem), (byte*) tuple.key, *tuple.keylen); + logserver::readfromsocket(*(item->data->workitem), (byte*) tuple.key, *tuple.keylen); //read the data if(!tuple.isDelete() && opcode != logserver::OP_FIND) { tuple.data = (byte*) malloc(*tuple.datalen); - readfromsocket(*(item->data->workitem), (byte*) tuple.data, *tuple.datalen); + logserver::readfromsocket(*(item->data->workitem), (byte*) tuple.data, *tuple.datalen); } else tuple.data = 0; @@ -570,7 +550,7 @@ void * thread_work_fn( void * args) assert(n == sizeof(uint8_t)); //send the tuple - writetosocket(*(item->data->workitem), (byte*) dt->keylen, dt->byte_length()); + logserver::writetosocket(*(item->data->workitem), (byte*) dt->keylen, dt->byte_length()); //free datatuple free(dt->keylen); @@ -643,7 +623,7 @@ void * thread_work_fn( void * args) } pthread_mutex_unlock(item->data->th_mut); - + return NULL; } diff --git a/logserver.h b/logserver.h index dd9888a..c3212a0 100644 --- a/logserver.h +++ b/logserver.h @@ -142,6 +142,29 @@ public: public: + // XXX utility methods, pull out into some other class. + + static inline void readfromsocket(int sockd, byte *buf, int count) + { + + int n = 0; + while( n < count ) + { + n += read( sockd, buf + n, count - n); + } + + } + + static inline void writetosocket(int sockd, byte *buf, int count) + { + int n = 0; + while( n < count ) + { + n += write( sockd, buf + n, count - n); + } + } + + private: //main loop of server diff --git a/test/check_tcpclient.cpp b/test/check_tcpclient.cpp index cc9754b..9cbd113 100644 --- a/test/check_tcpclient.cpp +++ b/test/check_tcpclient.cpp @@ -19,26 +19,6 @@ #undef begin #undef end -inline void readfromsocket(int sockd, byte *buf, int count) -{ - - int n = 0; - while( n < count ) - { - n += read( sockd, buf + n, count - n); - } - -} - -inline void writetosocket(int sockd, byte *buf, int count) -{ - int n = 0; - while( n < count ) - { - n += write( sockd, buf + n, count - n); - } -} - datatuple * sendTuple(std::string & servername, int serverport, uint8_t opcode, datatuple &tuple) { struct sockaddr_in serveraddr; @@ -84,9 +64,9 @@ datatuple * sendTuple(std::string & servername, int serverport, uint8_t opcode, n = write(sockfd, (byte*) tuple.datalen, sizeof(uint32_t)); assert( n == sizeof(uint32_t)); - writetosocket(sockfd, (byte*) tuple.key, *tuple.keylen); + logserver::writetosocket(sockfd, (byte*) tuple.key, *tuple.keylen); if(!tuple.isDelete() && *tuple.datalen != 0) - writetosocket(sockfd, (byte*) tuple.data, *tuple.datalen); + logserver::writetosocket(sockfd, (byte*) tuple.data, *tuple.datalen); //read the reply code uint8_t rcode; @@ -105,12 +85,12 @@ datatuple * sendTuple(std::string & servername, int serverport, uint8_t opcode, assert(n == sizeof(uint32_t)); //read key rcvdtuple->key = (byte*) malloc(*rcvdtuple->keylen); - readfromsocket(sockfd, (byte*) rcvdtuple->key, *rcvdtuple->keylen); + logserver::readfromsocket(sockfd, (byte*) rcvdtuple->key, *rcvdtuple->keylen); if(!rcvdtuple->isDelete()) { //read key rcvdtuple->data = (byte*) malloc(*rcvdtuple->datalen); - readfromsocket(sockfd, (byte*) rcvdtuple->data, *rcvdtuple->datalen); + logserver::readfromsocket(sockfd, (byte*) rcvdtuple->data, *rcvdtuple->datalen); } close(sockfd);