crossroads

Git mirror of https://crossroads.e-tunity.com/
git clone git://git.finwo.net/app/crossroads
Log | Files | Refs | LICENSE

wait.cc (2127B)


      1 #include "fdset"
      2 
      3 double Fdset::wait(bool wait_read, bool wait_write) {
      4     PROFILE("Fdset::wait");
      5 
      6     Timestamp start;
      7     struct timeval tv, *tvp;
      8 
      9     // No fd's? Nothing to wait for.
     10     if (set.size() < 1)
     11 	throw Error("Internal jam in Fdset::wait(): no fd's to wait for");
     12 
     13     // Prepare select sets.
     14     FD_ZERO (&readset);
     15     FD_ZERO (&writeset);
     16     FD_ZERO (&exceptset);
     17     for (unsigned i = 0; i < set.size(); i++) {
     18 	FD_SET (set[i], &readset);
     19 	FD_SET (set[i], &writeset);
     20 	FD_SET (set[i], &exceptset);
     21 	debugmsg("About to wait for fd "<< set[i] << '\n');
     22     }
     23 
     24     // Prepare timout specifier.
     25     if (tsec) {
     26 	tv.tv_sec = tsec;
     27 	tv.tv_usec = 0;
     28 	tvp = &tv;
     29 	debugmsg("Waiting limitation: " << tsec << '\n');
     30     } else {
     31 	tvp = 0;
     32 	debugmsg("No waiting limitation\n");
     33     }
     34 
     35     // Run the select.
     36     if (select (FD_SETSIZE,
     37 		wait_read ? &readset : 0,
     38 		wait_write ? &writeset : 0,
     39 		&exceptset, tvp) < 0) {
     40 	if (errno != EINTR)
     41 	    throw Error(string("Select failure: failed to wait: ") +
     42 			strerror(errno));
     43 	FD_ZERO(&readset);
     44 	FD_ZERO(&writeset);
     45 	FD_ZERO(&exceptset);
     46 	return start.elapsed();
     47     }
     48 
     49     // Check for exceptions.
     50     for (unsigned i = 0; i < set.size(); i++)
     51 	if (FD_ISSET (set[i], &exceptset)) {
     52 	    ostringstream o;
     53 	    o << "Exception on fd/socket " << int(set[i]);
     54 	    throw Error(o.str());
     55 	}
     56 
     57     // More debugging: What has become readable, what has become
     58     // writeable, also state if no change was seen
     59     if (config.debug()) {
     60 	bool statechanged = false;
     61 	for (unsigned int i = 0; i < FD_SETSIZE; i++) {
     62 	    if (FD_ISSET(i, &readset)) {
     63 		debugmsg("Fd " << i << " is readable\n");
     64 		statechanged = true;
     65 	    }
     66 	    if (FD_ISSET(i, &writeset)) {
     67 		debugmsg("Fd " << i << " is writeable\n");
     68 		statechanged = true;
     69 	    }
     70 	}
     71 	if (!statechanged) {
     72 	    ostringstream o;
     73 	    o << "Select timeout: neither of the fd's ";
     74 	    for (unsigned int i = 0; i < set.size(); i++)
     75 		o << set[i] << ' ';
     76 	    o << "has shown activity in " << tsec << " sec\n";
     77 	    debugmsg(o.str());
     78 	}	    
     79     }
     80 
     81     // All done. Return microsecs since start.
     82     return start.elapsed();
     83 }