From: Ronnie sahlberg Date: Wed, 11 Apr 2007 23:09:27 +0000 (+1000) Subject: add an example on how to read a message from the domain socket X-Git-Tag: tevent-0.9.20~348^2~2933^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=51c660d1a144a5d9d4951df8ede961e493f7550e;p=thirdparty%2Fsamba.git add an example on how to read a message from the domain socket (This used to be ctdb commit 9723828b1562eb6a386eb26e63db3b6617ebb454) --- diff --git a/ctdb/direct/ctdbd_test.c b/ctdb/direct/ctdbd_test.c index 17c8d8f9721..18cf7d3f84c 100644 --- a/ctdb/direct/ctdbd_test.c +++ b/ctdb/direct/ctdbd_test.c @@ -130,10 +130,40 @@ int send_a_message(int fd, int ourvnn, int vnn, int pid, TDB_DATA data) cnt=write(fd, &r, offsetof(struct ctdb_req_message, data)); /* write data */ if(data.dsize){ - cnt=write(fd, data.dptr, data.dsize); + cnt+=write(fd, data.dptr, data.dsize); } } +void wait_for_a_message(int fd) +{ + int cnt, tot; + uint32_t len; + struct ctdb_req_message *msg; + + /* read the 4 bytes of length for the pdu */ + cnt=0; + tot=4; + while(cnt!=tot){ + int numread; + numread=read(fd, ((char *)&len)+cnt, tot-cnt); + if(numread>0){ + cnt+=numread; + } + } + msg=malloc(len); + msg->hdr.length=len; + /* read the rest of the pdu */ + tot=msg->hdr.length; + while(cnt!=tot){ + int numread; + numread=read(fd, (char *)msg+cnt, tot-cnt); + if(numread>0){ + cnt+=numread; + } + } + printf("got a message : %s\n",&msg->data[0]); +} + int main(int argc, const char *argv[]) { int fd, pid, vnn, dstvnn, dstpid; @@ -172,8 +202,10 @@ int main(int argc, const char *argv[]) send_a_message(fd, vnn, dstvnn, dstpid, message); - /* wait for the message to come back */ - + /* wait for the message to come back. + i.e. the one we just sent to ourself + */ + wait_for_a_message(fd); return 0; }