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
This commit is contained in:
sears 2010-01-26 20:19:36 +00:00
parent cead29f0e9
commit 2f562a175a
3 changed files with 31 additions and 48 deletions

View file

@ -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;
}

View file

@ -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

View file

@ -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);