Julius 4.2
libsent/src/net/rdwt.c
説明を見る。
00001 
00018 /*
00019  * Copyright (c) 1991-2011 Kawahara Lab., Kyoto University
00020  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00021  * Copyright (c) 2005-2011 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    ///< Buffer size
00029 
00030 
00043 int
00044 rd(int fd, char *data, int *len, int maxlen)
00045 {
00046   int count=0;
00047   int tmpbytes, tmplen;
00048   int toread = sizeof(int), ret, off = 0;
00049   
00050   while (toread > 0) {
00051     ret = 
00052 #ifdef WINSOCK
00053       recv(fd,((char *)len) + off, toread, 0);
00054 #else
00055       read(fd,((char *)len) + off, toread);
00056 #endif
00057     if (ret <= 0) {  
00058       if (ret < 0) jlog("Error: rdwt: failed to read data at %d / %d\n", count, len);
00059       return(-1);
00060     }
00061     toread -= ret;
00062     off += ret;
00063   }
00064   if (*len > maxlen) {
00065     jlog("Error: rdwt: transfer data length exceeded: %d (>%d)\n", len, maxlen);
00066     return(-1);
00067   }
00068   while (count<(*len)){
00069 
00070     tmplen = (*len) - count;
00071     if (tmplen > BUFSZ) tmplen = BUFSZ;
00072     if ((tmpbytes =
00073 #ifdef WINSOCK
00074          recv(fd,data+count,tmplen,0)
00075 #else
00076          read(fd,data+count,tmplen)
00077 #endif
00078          ) < 0) {
00079       jlog("Error: rdwt: failed to read data at %d / %d\n",count, len);
00080       return(count);
00081     }
00082     count += tmpbytes;
00083   }
00084   return(count);
00085 }
00086 
00096 int
00097 wt(int fd, char *data, int len)
00098 {
00099   int tmpbytes;
00100 
00101   /* len == 0 is used to tell end of segment ack */
00102   if ((tmpbytes=
00103 #ifdef WINSOCK
00104        send(fd,(char *)&len,sizeof(int),0)
00105 #else
00106        write(fd,(char *)&len,sizeof(int))
00107 #endif
00108        ) != sizeof(int)) {
00109     /*jlog( "failed to write num\n");*/
00110     return(-1);
00111   }
00112   if (len > 0) {
00113     if ((tmpbytes=
00114 #ifdef WINSOCK
00115          send(fd,data,len,0)
00116 #else
00117          write(fd,data,len)
00118 #endif
00119          ) < 0) {
00120       jlog("Error: rdwt: failed to write data (%d bytes)\n",len);
00121       return(-1);
00122     }
00123   } else {
00124     tmpbytes = 0;
00125   }
00126   return(tmpbytes);
00127 }