Example #1
0
request_t *get_request()
{
	request_t *request;
	request = (request_t *) malloc(sizeof(request_t));
	request->data = read_from_net();
	return(request)
}
Example #2
0
int
NetStream::read_stuff()
{

   const unsigned int BUFSIZE= 666666;
   char buff[BUFSIZE];
   int  num_read = 0;

   if (Config::get_var_bool("PRINT_ERRS",false,true)) cerr << "NetStream: ReadStuff called\n";

   // If we do not have a message size (msgSize_), read it from the network
   if (msgSize_ == -1) {

      char packbuf_space[sizeof(int) + 1];
      char *packbuf = packbuf_space;
      int nread = read_from_net(packbuf, sizeof(int));
      if (nread < 0)
         return nread;
      int count = 0;
      UGA_UNPACK_INTEGER(msgSize_, packbuf, count);
      if (Config::get_var_bool("PRINT_ERRS",false,true)) cerr << "NetStream: msgSize is " << msgSize_ << endl;
   }

   // After we know the message size, we read everything that is available on
   // the network, in blocks of BUFSIZE bytes
   do {
      int nread = read_from_net(buff, BUFSIZE);
      if (nread <= 0) 
           return nread;
      else num_read = nread;
      if (msgSize_ > (int)BUFSIZE) {
         //XXX - Better error checking... (I hope)
         if (num_read != (int)BUFSIZE) return num_read;
         _in_queue.put((UGAptr)buff, BUFSIZE);
         msgSize_ -= BUFSIZE;
         if (Config::get_var_bool("PRINT_ERRS",false,true)) cerr << "NetStream: Big message, storing first BUFSIZE bytes (msgSize = " << msgSize_ << endl;
         num_read  = 0;
      }
   } while (num_read == 0);

   // If we have read at least one full message...
   char *tbuf = buff; 
   if (Config::get_var_bool("PRINT_ERRS",false,true)) cerr << "NetStream: processing num_read " << num_read << endl;
   if (num_read >= msgSize_) {
      // For each full message...
      while (num_read && num_read >= msgSize_) {
        _in_queue.put((UGAptr)tbuf, msgSize_); // Stuff the message onto queue
        num_read -= msgSize_;                  // skip to end of this message
        tbuf     += msgSize_;

        if (Config::get_var_bool("PRINT_ERRS",false,true)) cerr << "NetStream: processing full_message " << msgSize_ << " (num_read = " << num_read << endl;

        if (interpret() != 0)                  // Let app decode message
           return 1;  // this flag terminates this NetStream connection

        // If we still have more data to process read the next message size
        if (num_read > 0) {
           int count = 0;
           UGA_UNPACK_INTEGER(msgSize_, tbuf, count); // tbuf is updated
           num_read -= count;
           if (Config::get_var_bool("PRINT_ERRS",false,true)) cerr << "NetStream: next message" << msgSize_ << " (num_read = " << num_read << endl;
        } else
           msgSize_ = -1; // Otherwise, clear the message size
      }
   }
   // Anything left over is less than a complete message, so we store it
   // away in _in_queue and decrease our msgSize_ request accordingly
   _in_queue.put(tbuf, num_read);
   msgSize_ -= num_read;
   if (Config::get_var_bool("PRINT_ERRS",false,true)) cerr << "NetStream: saved for next time " << num_read << " (msgSize = " << msgSize_ << endl;
   return 0;
}