libsent/src/net/rdwt.c

Go to the documentation of this file.
00001 
00018 /*
00019  * Copyright (c) 1991-2007 Kawahara Lab., Kyoto University
00020  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00021  * Copyright (c) 2005-2007 Julius project team, Nagoya Institute of Technology
00022  * All rights reserved
00023  */
00024 
00025 #include <sent/stddefs.h>
00026 #include <sent/tcpip.h>
00027 
00028 #define         BUFSZ   4096    
00029 
00030 
00043 int
00044 rd(int fd, char *data, int *len, int maxlen)
00045 {
00046   int count=0;
00047   int tmpbytes, tmplen;
00048   
00049   if ((tmpbytes=
00050 #ifdef WINSOCK
00051        recv(fd,(char *)len,sizeof(int),0)
00052 #else
00053        read(fd,(char *)len,sizeof(int))
00054 #endif
00055        ) != sizeof(int)) {
00056     /*jlog( "failed to read num\n");*/
00057     return(-1);
00058   }
00059   if (*len > maxlen) {
00060     jlog("Error: rdwt: transfer data length exceeded: %d (>%d)\n", len, maxlen);
00061     return(-1);
00062   }
00063   while (count<(*len)){
00064 
00065     tmplen = (*len) - count;
00066     if (tmplen > BUFSZ) tmplen = BUFSZ;
00067     if ((tmpbytes =
00068 #ifdef WINSOCK
00069          recv(fd,data+count,tmplen,0)
00070 #else
00071          read(fd,data+count,tmplen)
00072 #endif
00073          ) < 0) {
00074       jlog("Error: rdwt: failed to read data at %d / %d\n",count, len);
00075       return(count);
00076     }
00077     count += tmpbytes;
00078   }
00079   return(count);
00080 }
00081 
00091 int
00092 wt(int fd, char *data, int len)
00093 {
00094   int tmpbytes;
00095 
00096   /* len == 0 is used to tell end of segment ack */
00097   if ((tmpbytes=
00098 #ifdef WINSOCK
00099        send(fd,(char *)&len,sizeof(int),0)
00100 #else
00101        write(fd,(char *)&len,sizeof(int))
00102 #endif
00103        ) != sizeof(int)) {
00104     /*jlog( "failed to write num\n");*/
00105     return(-1);
00106   }
00107   if (len > 0) {
00108     if ((tmpbytes=
00109 #ifdef WINSOCK
00110          send(fd,data,len,0)
00111 #else
00112          write(fd,data,len)
00113 #endif
00114          ) < 0) {
00115       jlog("Error: rdwt: failed to write data (%d bytes)\n",len);
00116       return(-1);
00117     }
00118   } else {
00119     tmpbytes = 0;
00120   }
00121   return(tmpbytes);
00122 }

Generated on Tue Dec 18 15:59:55 2007 for Julius by  doxygen 1.5.4