File: netread.cc

package info (click to toggle)
crossroads 2.65-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,664 kB
  • ctags: 355
  • sloc: cpp: 4,212; perl: 1,658; xml: 269; makefile: 186; sh: 46
file content (39 lines) | stat: -rw-r--r-- 1,020 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "netbuffer"

unsigned Netbuffer::netread (int fd, int timeout) {    
    PROFILE("Netbuffer::netread");

    if (timeout) {
	Fdset set(timeout);
	set.add(fd);
	set.wait_r();
	if (! set.readable(fd)) {
	    msg(Mstr("Fd ") + Mstr(fd) +
		Mstr(" failed to become readable within ") + Mstr(timeout) +
		Mstr(" sec"));
	    return 0;
	}
    }
    
    check_space(config.buffersize());

    // Read from the network. If this fails, don't throw an exception
    // because it's quite common (too much logging otherwise).
    ssize_t nread = read (fd, buf_data + buf_sz, config.buffersize());
    if (nread < 0) {
	msg(Mstr("Read failed on fd ") + fd + ": " + strerror(errno));
	return 0;
    }
    buf_sz += nread;

    if (config.debug() && nread) {
        ostringstream o;
        o << "Got " << nread << " bytes from fd " << fd << ": ";
        for (unsigned i = 0; i < (unsigned)nread; i++)
            o << printable(buf_data[i]);
        o << "\n";
        _debugmsg (o.str());
    }
    
    return nread;
}