⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 io.cpp

📁 ACE编程的一本经典BIBLE的源代码,喜欢网络编程的别错过
💻 CPP
📖 第 1 页 / 共 2 页
字号:
      if (mb == 0)
        {
          errno = ENOMEM;
          result = -1;
        }
      else
        {
          ACE_Asynch_Read_Stream ar;

          if (ar.open (*this, this->handle_) == -1
              || ar.read (*mb, mb->size () - mb->length (), handle) == -1)
            result = -1;
        }
    }

  if (result != ACE_Filecache_Handle::ACE_SUCCESS)
    {
      this->handler_->receive_file_error (result);
      delete mb;
      delete handle;
    }
}

void
JAWS_Asynch_IO::transmit_file (const char *filename,
                               const char *header,
                               int header_size,
                               const char *trailer,
                               int trailer_size)
{
  ACE_Asynch_Transmit_File::Header_And_Trailer *header_and_trailer = 0;
  ACE_Filecache_Handle *handle = new ACE_Filecache_Handle (filename, ACE_NOMAP);

  int result = handle->error ();

  if (result == ACE_Filecache_Handle::ACE_SUCCESS)
    {
      ACE_Message_Block header_mb (header, header_size);
      ACE_Message_Block trailer_mb (trailer, trailer_size);

      header_and_trailer = new ACE_Asynch_Transmit_File::Header_And_Trailer
        (&header_mb, header_size, &trailer_mb, trailer_size);

      ACE_Asynch_Transmit_File tf;

      if (tf.open (*this, this->handle_) == -1
          || tf.transmit_file (handle->handle (), // file handle
                               header_and_trailer, // header and trailer data
                               0,  // bytes_to_write
                               0,  // offset
                               0,  // offset_high
                               0,  // bytes_per_send
                               0,  // flags
                               handle // act
                               ) == -1)
        result = -1;
    }

  if (result != ACE_Filecache_Handle::ACE_SUCCESS)
    {
      this->handler_->transmit_file_error (result);
      delete header_and_trailer;
      delete handle;
    }
}


// This method will be called when an asynchronous transmit file completes.
void
JAWS_Asynch_IO::handle_transmit_file (const ACE_Asynch_Transmit_File::Result &result)
{
  if (result.success ())
    this->handler_->transmit_file_complete ();
  else
    this->handler_->transmit_file_error (-1);

  delete result.header_and_trailer ();
  delete (ACE_Filecache_Handle *) result.act ();
}

void
JAWS_Asynch_IO::send_confirmation_message (const char *buffer,
                                           int length)
{
  this->send_message (buffer, length, CONFORMATION);
}

void
JAWS_Asynch_IO::send_error_message (const char *buffer,
                                    int length)
{
  this->send_message (buffer, length, ERROR_MESSAGE);
}

void
JAWS_Asynch_IO::send_message (const char *buffer,
                              int length,
                              int act)
{
  ACE_Message_Block *mb;
  ACE_NEW (mb, ACE_Message_Block (buffer, length));

  if (mb == 0)
    {
      this->handler_->error_message_complete ();
      return;
    }

  ACE_Asynch_Write_Stream aw;
  if (aw.open (*this, this->handle_) == -1
      || aw.write (*mb, length, (void *) act) == -1)
    {
      mb->release ();

      if (act == CONFORMATION)
        this->handler_->confirmation_message_complete ();
      else
        this->handler_->error_message_complete ();
    }
}

void
JAWS_Asynch_IO::handle_write_stream (const ACE_Asynch_Write_Stream::Result &result)
{
  result.message_block ().release ();

  if (result.act () == (void *) CONFORMATION)
    this->handler_->confirmation_message_complete ();
  else
    this->handler_->error_message_complete ();
}

#endif /* ACE_WIN32 */

//-------------------Adding SYNCH IO no Caching

JAWS_Synch_IO_No_Cache::JAWS_Synch_IO_No_Cache (void)
  : handle_ (ACE_INVALID_HANDLE)
{
}

JAWS_Synch_IO_No_Cache::~JAWS_Synch_IO_No_Cache (void)
{
  ACE_OS::closesocket (this->handle_);
}

ACE_HANDLE
JAWS_Synch_IO_No_Cache::handle (void) const
{
  return this->handle_;
}

void
JAWS_Synch_IO_No_Cache::handle (ACE_HANDLE handle)
{
  this->handle_ = handle;
}

void
JAWS_Synch_IO_No_Cache::read (ACE_Message_Block &mb,
                     int size)
{
  ACE_SOCK_Stream stream;
  stream.set_handle (this->handle_);
  int result = stream.recv (mb.wr_ptr (), size);

  if (result <= 0)
    this->handler_->read_error ();
  else
    {
      mb.wr_ptr (result);
      this->handler_->read_complete (mb);
    }
}

void
JAWS_Synch_IO_No_Cache::receive_file (const char *filename,
                                      void *initial_data,
                                      int initial_data_length,
                                      int entire_length)
{
  //ugly hack to send HTTP_Status_Code::STATUS_FORBIDDEN
  this->handler_->receive_file_error (5);

  //To get rid of warnings on some platforms
  //NOTE: this function is necessary because the base class
  //version of the function is pure virtual
  ACE_UNUSED_ARG (filename);
  ACE_UNUSED_ARG (initial_data);
  ACE_UNUSED_ARG (initial_data_length);
  ACE_UNUSED_ARG (entire_length);
}

void
JAWS_Synch_IO_No_Cache::transmit_file (const char *filename,
                     const char *header,
                     int header_size,
                     const char *trailer,
                     int trailer_size)
{
  int result = 0;

  // Can we access the file?
  if (ACE_OS::access (filename, R_OK) == -1)
  {
    //ugly hack to send in HTTP_Status_Code::STATUS_NOT_FOUND
    result = ACE_Filecache_Handle::ACE_ACCESS_FAILED;
    this->handler_->transmit_file_error (result);
    return;
  }

  ACE_stat stat;

  // Can we stat the file?
  if (ACE_OS::stat (filename, &stat) == -1)
  {
    //ugly hack to send HTTP_Status_Code::STATUS_FORBIDDEN
    result = ACE_Filecache_Handle::ACE_STAT_FAILED;
      this->handler_->transmit_file_error (result);
    return;
  }

  ssize_t size = stat.st_size;

  // Can we open the file?
  ACE_HANDLE handle = ACE_OS::open (filename, O_RDONLY);
  if (handle == ACE_INVALID_HANDLE)
  {
    //ugly hack to send HTTP_Status_Code::STATUS_FORBIDDEN
    result = ACE_Filecache_Handle::ACE_OPEN_FAILED;
      this->handler_->transmit_file_error (result);
    return;
  }

  char* f = new char[size];
  auto_ptr<char> file (f);

  ACE_OS::read_n (handle, f, size);

  ACE_SOCK_Stream stream;
  stream.set_handle (this->handle_);

  if ((stream.send_n (header, header_size) == header_size)
      && (stream.send_n (f, size) == size)
      && (stream.send_n (trailer, trailer_size) == trailer_size))
      this->handler_->transmit_file_complete ();
  else
  {
    //ugly hack to default to HTTP_Status_Code::STATUS_INTERNAL_SERVER_ERROR
    result = -1;
    this->handler_->transmit_file_error (result);
    return;
  }
}

void
JAWS_Synch_IO_No_Cache::send_confirmation_message (const char *buffer,
                           int length)
{
  this->send_message (buffer, length);
  this->handler_->confirmation_message_complete ();
}

void
JAWS_Synch_IO_No_Cache::send_error_message (const char *buffer,
                        int length)
{
  this->send_message (buffer, length);
  this->handler_->error_message_complete ();
}

void
JAWS_Synch_IO_No_Cache::send_message (const char *buffer,
                    int length)
{
  ACE_SOCK_Stream stream;
  stream.set_handle (this->handle_);
  stream.send_n (buffer, length);
}

//-------------------


// #if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
// template class ACE_Singleton<JAWS_VFS, ACE_SYNCH_MUTEX>;
// #elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
// #pragma instantiate ACE_Singleton<JAWS_VFS, ACE_SYNCH_MUTEX>
// #endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -