Beispiel #1
0
ssize_t 
NetStream::read_from_net(
   void   *buf, 
   size_t  nbytes
   ) const
{
   char  *tmpbuf  = (char*) buf;
   int    numread = 0;
   double stime   = 0;

   while (nbytes) {
#ifdef WIN32
      int readb = read_win32(_fd, tmpbuf, nbytes);
#else
      int readb = ::read(_fd, tmpbuf, nbytes);
#endif

      if (errno == EAGAIN) { // if nothing's left to read, then
         if (Config::get_var_bool("PRINT_ERRS",false,true)) 
            cerr << "  bytes read from network (EAGAIN) = " << numread << endl;
         return numread + (readb == -1 ? 0:readb);  //just return what we have
     }

      if (readb < 0) {
         perror("NetStream::read_from_net : Warning - ");
         return -1;
      }

      if (readb == 0 && nbytes > 0) 
      {
#ifdef WIN32
         //XXX - errno not set on WIN32 when there's not enough
         //to read on a non-blocking fd... should prolly
         //set some state in read_win32 to reflect this, but
         //for now just assume this is the reason and
         //return without error...
         return numread + (readb == -1 ? 0:readb);
#else
         if (stime == 0)
            stime = the_time();

         if (the_time() - stime > 1 || errno != EAGAIN) {
            if (port_ > 0) {
               // Only print out a message if reading from the net
               cerr << "NetStream::read_from_net - read error: peer reset"
                    << endl;
            }
            return -1;
         }
#endif
      }

      nbytes -= readb;
      tmpbuf += readb;
      numread+= readb;
   }
   if (Config::get_var_bool("PRINT_ERRS",false,true)) 
      cerr << "  bytes read from network = " << numread << endl;
   return numread;
}
Beispiel #2
0
static void
vehicle_wince_io(struct vehicle_priv *priv)
{
	int size, rc = 0;
	char *str, *tok;

	dbg(1, "vehicle_file_io : enter\n");

	size = read_win32(priv, priv->buffer + priv->buffer_pos, buffer_size - priv->buffer_pos - 1);
	
	if (size <= 0) {
		switch (priv->on_eof) {
		case 0:
			vehicle_wince_close(priv);
			vehicle_wince_open(priv);
			break;
		case 1:
			vehicle_wince_disable_watch(priv);
			break;
		case 2:
			exit(0);
			break;
		}
		return;
	}
	priv->buffer_pos += size;
	priv->buffer[priv->buffer_pos] = '\0';
	dbg(1, "size=%d pos=%d buffer='%s'\n", size,
		priv->buffer_pos, priv->buffer);
	str = priv->buffer;
	while ((tok = strchr(str, '\n'))) {
		*tok++ = '\0';
		dbg(1, "line='%s'\n", str);
		rc +=vehicle_wince_parse(priv, str);
		str = tok;
		if (priv->file_type == file_type_file && rc)
			break;
	}

	if (str != priv->buffer) {
		size = priv->buffer + priv->buffer_pos - str;
		memmove(priv->buffer, str, size + 1);
		priv->buffer_pos = size;
		dbg(2, "now pos=%d buffer='%s'\n",
			priv->buffer_pos, priv->buffer);
	} else if (priv->buffer_pos == buffer_size - 1) {
		dbg(0,
			"Overflow. Most likely wrong baud rate or no nmea protocol\n");
		priv->buffer_pos = 0;
	}
	if (rc)
		callback_list_call_attr_0(priv->cbl, attr_position_coord_geo);
}