netread.cc (1175B)
1 #include "netbuffer" 2 #include "SocketHandling/socket/socket" 3 4 unsigned Netbuffer::netread (Socket &s, unsigned timeout) { 5 PROFILE("Netbuffer::netread"); 6 7 // debugmsg("Reading from fd " << s.fd() << ", timeout " 8 // << timeout << "\n"); 9 if (timeout) { 10 Fdset set(timeout); 11 set.add(s); 12 set.wait_r(); 13 if (! set.readable(s)) { 14 msg("Fd "<< s.fd() << " failed to become readable within " << 15 timeout << " sec\n"); 16 return 0; 17 } 18 } 19 20 check_space(config.buffersize()); 21 22 // Read from the network. If this fails, don't throw an exception 23 // because it's quite common (too much logging otherwise). 24 ssize_t nread = read (s.fd(), buf_data + buf_sz, config.buffersize()); 25 if (nread < 0) { 26 msg("Read failed on fd " << s.fd() + ": " << strerror(errno) << '\n'); 27 return 0; 28 } 29 buf_sz += nread; 30 31 if (config.debug() && nread) { 32 ostringstream o; 33 o << "Got " << nread << " bytes from fd " << s.fd() 34 << ", timeout " << timeout << ": "; 35 for (unsigned i = 0; i < (unsigned)nread; i++) 36 o << printable(buf_data[i]); 37 o << "\n"; 38 debugmsg (o.str()); 39 } 40 41 return nread; 42 }