int
Supplier::svc (void)
{
  ACE_Message_Block *mb = 0;

  // Send one message for each letter of the alphabet, then send an empty
  // message to mark the end.
  for (const char *c = ACE_ALPHABET; *c != '\0'; c++)
    {
      // Allocate a new message.
      char d[2];
      d[0] = *c;
      d[1] = '\0';

      ACE_NEW_RETURN (mb,
                      ACE_Message_Block (2),
                      -1);
      ACE_OS::strcpy (mb->wr_ptr (), d);

      mb->wr_ptr (2);

      if (this->put_next (mb) == -1)
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"),
                    ACE_TEXT ("put_next")));
    }

  ACE_NEW_RETURN(mb, ACE_Message_Block, -1);
  if (this->put_next (mb) == -1)
    ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), ACE_TEXT ("put_next")));

  return 0;
}
Beispiel #2
0
int
Sender::initiate_write (void)
{
  if ( this->msg_queue ()->message_count () < 20) // flow control
    {
      size_t nbytes = ACE_OS::strlen (send_buf_);

      ACE_Message_Block *mb = 0;
      ACE_NEW_RETURN (mb,
                      ACE_Message_Block (nbytes+8),
                      -1);

      mb->init (send_buf_, nbytes);
      mb->rd_ptr (mb->base ());
      mb->wr_ptr (mb->base ());
      mb->wr_ptr (nbytes);

      ACE_Time_Value tv = ACE_Time_Value::zero;

      int qcount =this->putq (mb, & tv);

      if (qcount <= 0)
        {
          ACE_Message_Block::release (mb);
          return -1;
        }
    }

  return initiate_io (ACE_Event_Handler::WRITE_MASK);
}
Beispiel #3
0
 static ACE_Message_Block * put( const T& t, unsigned long msg_type = 0  ) {
     ACE_Message_Block * mb = new ACE_Message_Block( sizeof(T) );
     *reinterpret_cast<T*>( mb->wr_ptr() ) = t;
     mb->wr_ptr( sizeof(T) );
     if ( msg_type >= ACE_Message_Block::MB_USER )
         mb->msg_type( msg_type );
     return mb;
 }
Beispiel #4
0
int ScadaClientMgr::sendData(char* data,int length)
{
	ACE_Message_Block * mb = new ACE_Message_Block(length);
	ACE_OS::memcpy(mb->wr_ptr(),data,length);
	mb->wr_ptr(length);

	return m_tcpClient.send(mb);
}
Beispiel #5
0
int
JAWS_IO_Reactive_Transmit::handle_output_source (ACE_HANDLE handle)
{
  ACE_Message_Block *mb = this->source_buf_;

  // Try to read data into the mb if data is still available.
  if (mb->space () && this->source_ != ACE_INVALID_HANDLE)
    {
      ssize_t count;
      count = ACE_OS::read (this->source_, mb->wr_ptr (), mb->space ());

      if (count < 0)
        {
          this->source_ = ACE_INVALID_HANDLE;
          this->source_buf_ = 0;

          if (this->bytes_ == 0)
            {
              JAWS_Event_Result io_result ( 0
                                          , JAWS_Event_Result::JE_ERROR
                                          , JAWS_Event_Result::JE_TRANSMIT_FAIL
                                          );
              this->io_result_ = io_result;
            }
          else if (this->bytes_ > 0)
            {
              JAWS_Event_Result io_result ( this->bytes_
                                          , JAWS_Event_Result::JE_ERROR
                                          , JAWS_Event_Result::JE_TRANSMIT_SHORT
                                          );
              this->io_result_ = io_result;
            }

          return -1;
        }
      else if (count == 0)
        this->source_ = ACE_INVALID_HANDLE;
      else
        mb->wr_ptr (count);
    }

  int result = 0;

  if (mb->length () > 0)
    result = this->handle_output_mb (handle, mb);

  if (result < 0)
    {
      this->source_ = ACE_INVALID_HANDLE;
      this->source_buf_ = 0;
    }
  else if (mb == 0 && this->source_ == ACE_INVALID_HANDLE)
    this->source_buf_ = 0;
  else
    this->source_buf_->crunch ();

  return result;
}
Beispiel #6
0
static int
issue_aio_calls (void)
{
  // Setup AIOCB.
  aiocb1.aio_fildes = file_handle;
  aiocb1.aio_offset = 0;
  aiocb1.aio_buf = mb1.wr_ptr ();
  aiocb1.aio_nbytes = BUFSIZ;
  aiocb1.aio_reqprio = 0;
  aiocb1.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
  aiocb1.aio_sigevent.sigev_signo = SIGRTMIN;
  aiocb1.aio_sigevent.sigev_value.sival_ptr = (void *) &aiocb1;

  // Fire off the aio read.
  if (aio_read (&aiocb1) == -1)
    // Queueing failed.
    ACE_ERROR_RETURN ((LM_ERROR, "Error: %p\n",
                       "Asynch_Read_Stream: aio_read queueing failed"),
                      -1);

  // Setup AIOCB.
  aiocb2.aio_fildes = file_handle;
  aiocb2.aio_offset = BUFSIZ + 1;
  aiocb2.aio_buf = mb2.wr_ptr ();
  aiocb2.aio_nbytes = BUFSIZ;
  aiocb2.aio_reqprio = 0;
  aiocb2.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
  aiocb2.aio_sigevent.sigev_signo = SIGRTMIN;
  aiocb2.aio_sigevent.sigev_value.sival_ptr = (void *) &aiocb2;

  // Fire off the aio read.
  if (aio_read (&aiocb2) == -1)
    // Queueing failed.
    ACE_ERROR_RETURN ((LM_ERROR, "Error: %p\n",
                       "Asynch_Read_Stream: aio_read queueing failed"),
                      -1);

  // Setup sigval.
  aiocb3.aio_fildes = ACE_INVALID_HANDLE;
  aiocb3.aio_offset = 0;
  aiocb3.aio_buf = 0;
  aiocb3.aio_nbytes = 0;
  aiocb3.aio_reqprio = 0;
  aiocb3.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
  aiocb3.aio_sigevent.sigev_signo = SIGRTMIN;
  aiocb3.aio_sigevent.sigev_value.sival_ptr = (void *) &aiocb3;
  sigval value;
  value.sival_ptr = reinterpret_cast<void *> (&aiocb3);
  // Queue this one for completion right now.
  if (sigqueue (ACE_OS::getpid (), SIGRTMIN, value) == -1)
    // Queueing failed.
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error: %p\n", "sigqueue"),
                      -1);

  return 0;
}
Beispiel #7
0
//get file and store it into ACE message block.
bool ZIP_Wrapper::get_file (char* archive_path, char* filename,
                            ACE_Message_Block &file)
{
  bool return_code = true;
  unzFile uf=0;
  uf = unzOpen(archive_path);
  /* locate the desired file in the zip file and set it as current file*/
  int j=unzLocateFile(uf, filename, 0);
  if (j==UNZ_END_OF_LIST_OF_FILE)
    {
      DANCE_ERROR (DANCE_LOG_ERROR,
                   (LM_DEBUG, ACE_TEXT("File not found in zip archive")));
      return false;
    }
  else if (j==UNZ_OK)
    {
      int k=unzOpenCurrentFile(uf);
      if (k!=UNZ_OK)
        {
          DANCE_ERROR (DANCE_LOG_ERROR,
                       (LM_DEBUG, ACE_TEXT("Error in opening the current")
                        ACE_TEXT(" file using unzOpenCurrentFile")));
          return false;
        }
      else
        {
          int num_read = 0;
          ACE_Message_Block* head = &file;

          //read the file into the ACE_Message_Block
          do
            {
              if (head->space () == 0)
                {
                  ACE_Message_Block* next = 0;
                  ACE_NEW_RETURN (next, ACE_Message_Block (BUFSIZ), false);
                  head = head->cont ();
                }
              num_read = unzReadCurrentFile(archive_path, head->wr_ptr(),
                                                  head->space());
              if (num_read > 0)
                head->wr_ptr (num_read);
            } while (num_read > 0);
          if (num_read < 0)
            return_code = false;
          unzCloseCurrentFile(uf);
          unzClose(uf);
          return return_code;
        }
    }
  return return_code;
}
Beispiel #8
0
  void produce_item (int item)
  {
    psema_.acquire ();
    ACE_Message_Block *mb
      = new ACE_Message_Block (sizeof (int),
                               ACE_Message_Block::MB_DATA);
    ACE_OS::memcpy (mb->wr_ptr (), &item, sizeof item);
    mb->wr_ptr (sizeof (int));
    this->consumer_.putq (mb);

    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Produced %d\n"), item));
    csema_.release();
  }
Beispiel #9
0
int
JAWS_Parse_Headers::parse_header_name (JAWS_Header_Info *info,
                                       ACE_Message_Block &mb)
{
  char *p = mb.rd_ptr ();
  char *q;

  q = this->skipset (":\n", p, mb.wr_ptr ());
  if (q == mb.wr_ptr ())
    {
      // no more progress can be made until we find a ':'
      return 1;
    }
  if (*q != '\n' && q == p)
    {
      // Ignore empty header type names
      info->finish_last_header_value ();
      info->create_next_header_value (0);
      info->end_of_line (0);
      mb.rd_ptr (q+1);
      return 0;
    }
  if (*q == '\n')
    {
      // ignore this line
      mb.rd_ptr (q+1);
      if (q == p || ((q-1) == p && q[-1] == '\r'))
        {
          // blank line means end of headers
          info->finish_last_header_value ();
          info->create_next_header_value (0);
          info->end_of_headers (1);
          if (mb.rd_ptr () == mb.wr_ptr ())
            mb.crunch ();
          return 1;
        }

      // not a blank line, but no ':', so ignore it
      info->finish_last_header_value ();
      info->create_next_header_value (0);
      return 0;
    }

  // otherwise, we have a header type name!
  *q = '\0';
  info->create_next_header_value (p);
  info->end_of_line (0);

  mb.rd_ptr (q+1);
  return 0;
}
Beispiel #10
0
int Make_Common_Dispose_Client_WorkTread_Message(uint16 u2CommandID, uint32 u4ServerID, ACE_Message_Block* pmblk, ACE_INET_Addr& AddrRemote)
{
    //组织数据
    CMessage* pMessage = App_MessageServiceGroup::instance()->CreateMessage(u4ServerID, CONNECT_IO_TCP);

    if (NULL == pMessage)
    {
        //放入消息框架失败
        OUR_DEBUG((LM_ERROR, "[CConnectClient::SendMessageGroup] ConnectID = %d CreateMessage fail.\n", u4ServerID));
        App_MessageBlockManager::instance()->Close(pmblk);
        return -1;
    }
    else
    {
        ACE_Message_Block* pMBBHead = App_MessageBlockManager::instance()->Create(sizeof(uint32));

        if (NULL == pMBBHead)
        {
            OUR_DEBUG((LM_ERROR, "[CConnectClient::SendMessageGroup] ConnectID = %d pMBBHead fail.\n", u4ServerID));
            App_MessageBlockManager::instance()->Close(pmblk);
            return -1;
        }

        //添加消息包头
        uint32 u4PacketLen = (uint32)pmblk->length();
        memcpy_safe((char*)&u4PacketLen, sizeof(uint32), pMBBHead->wr_ptr(), sizeof(uint32));
        pMBBHead->wr_ptr(sizeof(uint32));

        sprintf_safe(pMessage->GetMessageBase()->m_szListenIP, MAX_BUFF_20, "%s", AddrRemote.get_host_addr());
        sprintf_safe(pMessage->GetMessageBase()->m_szIP, MAX_BUFF_20, "127.0.0.1");
        pMessage->GetMessageBase()->m_u2Cmd = u2CommandID;
        pMessage->GetMessageBase()->m_u4ConnectID = u4ServerID;
        pMessage->GetMessageBase()->m_u4ListenPort = (uint32)AddrRemote.get_port_number();
        pMessage->GetMessageBase()->m_tvRecvTime = ACE_OS::gettimeofday();
        pMessage->GetMessageBase()->m_u1ResouceType = RESOUCE_FROM_SERVER;
        pMessage->GetMessageBase()->m_u4HeadSrcSize = sizeof(uint32);
        pMessage->GetMessageBase()->m_u4BodySrcSize = u4PacketLen;
        pMessage->SetPacketHead(pMBBHead);
        pMessage->SetPacketBody(pmblk);

        //将要处理的消息放入消息处理线程
        if (false == App_MessageServiceGroup::instance()->PutMessage(pMessage))
        {
            OUR_DEBUG((LM_ERROR, "[CConnectClient::SendMessageGroup] App_MessageServiceGroup::instance()->PutMessage Error.\n"));
            App_MessageServiceGroup::instance()->DeleteMessage(u4ServerID, pMessage);
            return -1;
        }
    }

    return 0;
}
 int GadgetInstrumentationStreamController::put_config(const char* config)
 {
   size_t l = std::strlen(config);
   ACE_Message_Block* mb = new ACE_Message_Block(l+1);
   memcpy(mb->wr_ptr(),config,l+1);
   mb->wr_ptr(l+1);
   mb->set_flags(Gadget::GADGET_MESSAGE_CONFIG);
   if (stream_.put(mb) == -1) {
     GERROR("Failed to put configuration on stream, too long wait, %d\n",  ACE_OS::last_error () ==  EWOULDBLOCK);
     mb->release();
     return GADGET_FAIL;
   }
   return GADGET_OK;
 }
void BulkDataSenderPerfImpl::paceData()
{
    //ACS_TRACE("BulkDataSenderPerfImpl::paceData");

    int size;
    CORBA::ULong flowNumber;

    size = 300000000;

    try
	{

	ACE_Message_Block *mb;
	mb = new ACE_Message_Block(size);

	for (CORBA::Long j = 0; j < (size-1); j++)
	    {
	    *mb->wr_ptr()='d';
	    mb->wr_ptr(sizeof(char));
	    }
	*mb->wr_ptr()='\0';
	mb->wr_ptr(sizeof(char));

	flowNumber = 1;
	getSender()->sendData(flowNumber, mb);

	//ACS_SHORT_LOG ((LM_INFO,"flow 1 length sent data = %d", mb->length()));

	mb->release();
	}

    catch (AVInvalidFlowNumberExImpl & ex)
	{   
	ACS_SHORT_LOG((LM_INFO,"BulkDataSenderPerfImpl::paceData AVInvalidFlowNumberExImpl exception catched !"));
	AVPaceDataErrorExImpl err = AVPaceDataErrorExImpl(ex,__FILE__,__LINE__,"BulkDataSenderPerfImpl::paceData");
	throw err.getAVPaceDataErrorEx();
	}
    catch (AVSendFrameErrorExImpl & ex)
	{
	ACS_SHORT_LOG((LM_INFO,"BulkDataSenderPerfImpl::paceData AVSendFrameErrorExImpl exception catched !"));
	AVPaceDataErrorExImpl err = AVPaceDataErrorExImpl(ex,__FILE__,__LINE__,"BulkDataSenderPerfImpl::paceData");
	throw err.getAVPaceDataErrorEx();
	}
    catch (...)
	{
	ACS_SHORT_LOG((LM_INFO,"BulkDataSenderPerfImpl::paceData UNKNOWN exception"));
	AVPaceDataErrorExImpl err = AVPaceDataErrorExImpl(__FILE__,__LINE__,"BulkDataSenderPerfImpl::paceData");
	throw err.getAVPaceDataErrorEx();
	}
}
Beispiel #13
0
static void *
producer (void *args)
{
  ACE_Message_Queue<ACE_MT_SYNCH> *msg_queue =
    reinterpret_cast<ACE_Message_Queue<ACE_MT_SYNCH> *> (args);

  ACE_Message_Block *mb = 0;

  for (const char *c = ACE_ALPHABET; *c != '\0'; c++)
    {
      ++message_count;

      // Allocate a new message

      ACE_NEW_RETURN (mb,
                      ACE_Message_Block (1),
                      0);
      *mb->wr_ptr () = *c;

      // Set the priority.
      mb->msg_priority (message_count);
      mb->wr_ptr (1);

      // Enqueue in priority order.
      if (msg_queue->enqueue_prio (mb) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("(%t) %p\n"),
                           ACE_TEXT ("put_next")),
                          0);
    }

  // Now send a 0-sized shutdown message to the other thread
  ACE_NEW_RETURN (mb,
                  ACE_Message_Block ((size_t) 0),
                  0);

  if (msg_queue->enqueue_tail (mb) == -1)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("(%t) %p\n"),
                ACE_TEXT ("put_next")));

  ++message_count;

  // Now read all the items out in priority order (i.e., ordered by
  // the size of the lines!).
  consumer (msg_queue);

  return 0;
}
Beispiel #14
0
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);
    }
}
Beispiel #15
0
int
Sender::on_data_sent(ACE_Message_Block & mb,
                     const ACE_INET_Addr & remote)
{
  int nbytes = mb.length ();

  if (nbytes == 0)
    {
      mb.release();
      return 0;
    }
  
  if (this->io_count_r_ == 0)
    this->initiate_read();

  
  if (this->io_count_w_ == 0)
    {
      mb.rd_ptr(mb.base());
      mb.wr_ptr(mb.base() + nbytes);

      this->initiate_write(mb, remote);
    }
  
  return 0;
}
Beispiel #16
0
void TCSoapRunnable::run()
{
    struct soap soap;
    soap_init(&soap);
    soap_set_imode(&soap, SOAP_C_UTFSTRING);
    soap_set_omode(&soap, SOAP_C_UTFSTRING);

    // check every 3 seconds if world ended
    soap.accept_timeout = 3;
    soap.recv_timeout = 5;
    soap.send_timeout = 5;
    if (!soap_valid_socket(soap_bind(&soap, _host.c_str(), _port, 100)))
    {
        TC_LOG_ERROR("network.soap", "Couldn't bind to %s:%d", _host.c_str(), _port);
        exit(-1);
    }

    TC_LOG_INFO("network.soap", "Bound to http://%s:%d", _host.c_str(), _port);

    while (!World::IsStopped())
    {
        if (!soap_valid_socket(soap_accept(&soap)))
            continue;   // ran into an accept timeout

        TC_LOG_DEBUG("network.soap", "Accepted connection from IP=%d.%d.%d.%d", (int)(soap.ip>>24)&0xFF, (int)(soap.ip>>16)&0xFF, (int)(soap.ip>>8)&0xFF, (int)soap.ip&0xFF);
        struct soap* thread_soap = soap_copy(&soap);// make a safe copy

        ACE_Message_Block* mb = new ACE_Message_Block(sizeof(struct soap*));
        ACE_OS::memcpy(mb->wr_ptr(), &thread_soap, sizeof(struct soap*));
        process_message(mb);
    }

    soap_done(&soap);
}
Beispiel #17
0
void
JAWS_Asynch_Handler::open (ACE_HANDLE h,
                           ACE_Message_Block &mb)
{
  JAWS_TRACE ("JAWS_Asynch_Handler::open");

  // This currently does nothing, but just in case.
  ACE_Service_Handler::open (h, mb);

  // ioh_ set from the ACT hopefully
  //this->dispatch_handler ();

#if !defined (ACE_WIN32)
  // Assume at this point there is no data.
  mb.rd_ptr (mb.wr_ptr ());
  mb.crunch ();
#else
  // AcceptEx reads some initial data from the socket.
  this->handler ()->message_block ()->copy (mb.rd_ptr (), mb.length ());
#endif

  ACE_Asynch_Accept_Result_Impl *fake_result
    = ACE_Proactor::instance ()->create_asynch_accept_result
      (this->proxy (), JAWS_IO_Asynch_Acceptor_Singleton::instance ()->get_handle (),
       h, mb, JAWS_Data_Block::JAWS_DATA_BLOCK_SIZE,
       this->ioh_, ACE_INVALID_HANDLE, 0);

  this->handler ()->handler_ = this;

  fake_result->complete (0, 1, 0);
}
Beispiel #18
0
    int construct_message(void)  
	{
        
// A very fast message creation algorithm
// would lead to the need for queuing messages..
// here. These messages are created and then sent
// using the SLOW send_message() routine which is
// running in a different thread so that the message
//construction thread isn't blocked.
            ACE_DEBUG((LM_DEBUG, "(%t)Constructing message::>> "));
        
// Create a new message to send
            ACE_Message_Block * mb;
        const char *data = "Hello Connector";
        ACE_NEW_RETURN(mb, ACE_Message_Block(16,       //Message 16 bytes long
                                              ACE_Message_Block::MB_DATA,       //Set header to data
                                              0,        //No continuations.
                                              data      //The data we want to send
                        ), 0);
        mb->wr_ptr(16);        //Set the write pointer.
// Enqueue the message into the message queue
// we COULD have done a timed wait for enqueuing in case
// someone else holds the lock to the queue so it doesn't block
//forever..
        ACE_ASSERT(this->putq(mb) != -1);
        ACE_DEBUG((LM_DEBUG, "Enqueued msg successfully\n"));
    }
Beispiel #19
0
int
HTTP_SSL_Server_Session::on_read_complete(ACE_Message_Block& mb, const TRB_Asynch_Read_Stream::Result& result)
{
	//ACE_OS::write_n(ACE_STDOUT, mb.rd_ptr(), mb.length()); //@

	HTTP::Responser http;

	if ( mb.space() == 0 || (mb.length() > 4 && ACE_OS::strncmp(mb.wr_ptr()-4, "\r\n\r\n", 4) == 0) )
	{
		//ACE_Time_Value tv(0, 1 * 1000);
		//timespec_t t  = (timespec_t) tv;
		//ACE_OS::nanosleep(&t);

		int n_err = 0;
		if ( mb.space() == 0 ) n_err = HTTP::Response::Request_Entity_Too_Large;
		
		ACE_Message_Block* out = new (std::nothrow) ACE_Message_Block(BUFSIZE);
		int n_res = http.parse_header(&mb, out, n_err);

		write(*out);
	}
	else
	{
		read(mb);
		return 1;
	}

	return 0;
}
Beispiel #20
0
int ACE_TMAIN (int, ACE_TCHAR *[])
{
  Manager tp;
  tp.activate ();

  // Wait for a moment every time you send a message.
  ACE_Time_Value tv;
  tv.msec (100);

  ACE_Message_Block *mb = 0;
  for (int i = 0; i < 30; i++)
    {
      ACE_NEW_RETURN
        (mb, ACE_Message_Block(sizeof(int)), -1);

      ACE_OS::memcpy (mb->wr_ptr (), &i, sizeof(int));

      ACE_OS::sleep (tv);

      // Add a new work item.
      tp.putq (mb);
    }

  ACE_Thread_Manager::instance ()->wait ();
  return 0;
}
int ACE_TMAIN (int, ACE_TCHAR *[])
{
  LF_ThreadPool tp;
  tp.activate (THR_NEW_LWP| THR_JOINABLE, 5);

  // Wait for a few seconds...
  ACE_OS::sleep (2);
  ACE_Time_Value tv (1L);

  ACE_Message_Block *mb;
  for (int i = 0; i < 30; i++)
    {
      ACE_NEW_RETURN (mb, ACE_Message_Block (sizeof(int)), -1);
      ACE_OS::memcpy (mb->wr_ptr (), &i, sizeof(int));
      ACE_OS::sleep (tv);

      // Add a new work item.
      tp.putq (mb);
    }

  ACE_Thread_Manager::instance ()->wait ();

  ACE_OS::sleep (10);

  return 0;
}
Beispiel #22
0
int
Producer::svc (void)
{
    // Keep reading stdin, until we reach EOF.

    for (int n; ; )
    {
        // Allocate a new message (add one to avoid nasty boundary
        // conditions).

        ACE_Message_Block *mb = 0;

        ACE_NEW_RETURN (mb,
                        ACE_Message_Block (BUFSIZ + 1),
                        -1);

        n = ACE_OS::read (ACE_STDIN, mb->wr_ptr (), BUFSIZ);

        if (n <= 0)
        {
            // Send a shutdown message to the other thread and exit.
            mb->length (0);

            if (this->put_next (mb) == -1)
                ACE_ERROR ((LM_ERROR,
                            ACE_TEXT ("(%t) %p\n"),
                            ACE_TEXT ("put_next")));
            break;
        }

        // Send the message to the other thread.
        else
        {
            mb->wr_ptr (n);
            // NUL-terminate the string (since we use strlen() on it
            // later).
            mb->rd_ptr ()[n] = '\0';

            if (this->put_next (mb) == -1)
                ACE_ERROR ((LM_ERROR,
                            ACE_TEXT ("(%t) %p\n"),
                            ACE_TEXT ("put_next")));
        }
    }

    return 0;
}
bool CProactorUDPHandler::CheckMessage(ACE_Message_Block* pMbData, uint32 u4Len)
{
	if(NULL == m_pPacketParse || NULL == pMbData)
	{
		return false;
	}

	if(u4Len <= m_pPacketParse->GetPacketHeadLen())
	{
		return false;
	}

	//将完整的数据包转换为PacketParse对象
	ACE_Message_Block* pMBHead = App_MessageBlockManager::instance()->Create(m_pPacketParse->GetPacketHeadLen());
	ACE_OS::memcpy(pMBHead->wr_ptr(), (const void*)pMbData->rd_ptr(), m_pPacketParse->GetPacketHeadLen());
	pMBHead->wr_ptr(m_pPacketParse->GetPacketHeadLen());

	m_pPacketParse->SetPacketHead(pMBHead->rd_ptr(), (uint32)pMBHead->length());
	if(u4Len != m_pPacketParse->GetPacketHeadLen() + m_pPacketParse->GetPacketDataLen())
	{
		return false;
	}

	m_pPacketParse->SetMessageHead(pMBHead);
	pMbData->rd_ptr(m_pPacketParse->GetPacketHeadLen());


	ACE_Message_Block* pMBBody = App_MessageBlockManager::instance()->Create(m_pPacketParse->GetPacketDataLen());
	ACE_OS::memcpy(pMBBody->wr_ptr(), (const void*)pMbData->rd_ptr(), m_pPacketParse->GetPacketDataLen());
	pMBBody->wr_ptr(m_pPacketParse->GetPacketDataLen());
	m_pPacketParse->SetPacketData(pMBBody->rd_ptr(), (uint32)pMBBody->length());
	m_pPacketParse->SetMessageBody(pMBBody);

	//UDP因为不是面向链接的
	if(false == App_MakePacket::instance()->PutUDPMessageBlock(m_addrRemote, PACKET_PARSE, m_pPacketParse))
	{
		App_PacketParsePool::instance()->Delete(m_pPacketParse);
		OUR_DEBUG((LM_ERROR, "[CProactorUDPHandler::SendMessage]PutMessageBlock is error.\n"));
		return false;
	}

	m_atvInput = ACE_OS::gettimeofday();
	m_u4RecvSize += u4Len;
	m_u4RecvPacketCount++;

	return true;
}
Beispiel #24
0
static void *
peer2 (void *)
{
  ACE_UPIPE_Acceptor acc (addr);
  ACE_UPIPE_Stream s_stream;

  // Spawn a peer1 thread.
  if (ACE_Thread_Manager::instance ()->spawn (ACE_THR_FUNC (peer1),
                                              (void *) 0,
                                              THR_NEW_LWP | THR_DETACHED) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "spawn"),
                      0);

  ACE_DEBUG ((LM_DEBUG,
              "(%t) peer2 starting accept\n"));

  if (acc.accept (s_stream) == -1)
    ACE_ERROR ((LM_ERROR,
                "(%t) ACE_UPIPE_Acceptor.accept failed\n"));

  ACE_Message_Block *mb = 0;

  if (s_stream.recv (mb) == -1)
    ACE_ERROR ((LM_ERROR,
                "(%t) peer2 recv failed\n"));

  ACE_DEBUG ((LM_DEBUG, "(%t) peer2 recv is \"%s\"\n",
              mb->rd_ptr ()));

  mb->wr_ptr (mb->rd_ptr ());
  mb->copy ("thanks", 7);

  if (s_stream.send (mb) == -1)
    ACE_ERROR ((LM_ERROR,
                "(%t) peer2 send failed\n"));

  char s_buf[42];
  ACE_DEBUG ((LM_DEBUG,
              "(%t) peer2 sleeping on recv\n"));

  if (s_stream.recv (s_buf, sizeof s_buf) == -1)
    ACE_ERROR ((LM_ERROR,
                "(%t) peer2 recv failed\n"));
  else
    ACE_DEBUG ((LM_DEBUG,
                "(%t) peer2 received buffer with \"%s\"\n",
                s_buf));

  ACE_OS::strcpy (s_buf,
                  "this is the peer2 response!");

  if (s_stream.send (s_buf, 30) == -1)
    ACE_ERROR ((LM_ERROR,
                "(%t) peer2 send failed\n"));
  s_stream.close ();
  return 0;
}
Beispiel #25
0
// Listing 4 code/ch07
int Client::handle_timeout(const ACE_Time_Value &, const void *)
{
  if (++this->iterations_ >= ITERATIONS)
    {
      this->peer ().close_writer ();
      return 0;
    }

  ACE_Message_Block *mb = 0;
  ACE_NEW_RETURN (mb, ACE_Message_Block (128), -1);
  int nbytes = ACE_OS::sprintf
    (mb->wr_ptr (), "Iteration %d\n", this->iterations_);
  ACE_ASSERT (nbytes > 0);
  mb->wr_ptr (static_cast<size_t> (nbytes));
  this->putq (mb);
  return 0;
}
Beispiel #26
0
void
Messenger_i::abort (CORBA::Short delay_secs)
{
  ACE_Message_Block *mb = 0;
  ACE_NEW(mb, ACE_Message_Block(2));
  ACE_OS::sprintf(mb->wr_ptr (), "%d", delay_secs);
  terminator_.putq(mb);
}
Beispiel #27
0
bool CClientReConnectManager::SendData(int nServerID, char*& pData, int nSize, bool blIsDelete)
{
    ACE_Guard<ACE_Recursive_Thread_Mutex> guard(m_ThreadWritrLock);
    //查找已有连接
    char szServerID[10] = {'\0'};
    sprintf_safe(szServerID, 10, "%d", nServerID);
    CReactorClientInfo* pClientInfo = m_objClientTCPList.Get_Hash_Box_Data(szServerID);

    if (NULL == pClientInfo)
    {
        //如果这个链接已经存在,则不创建新的链接
        OUR_DEBUG((LM_ERROR, "[CProConnectManager::SendData]nServerID =(%d) is not exist.\n", nServerID));

        if (true == blIsDelete)
        {
            SAFE_DELETE_ARRAY(pData);
        }

        return false;
    }

    ACE_Message_Block* pmblk = App_MessageBlockManager::instance()->Create(nSize);

    if (NULL == pmblk)
    {
        OUR_DEBUG((LM_ERROR, "[CClientReConnectManager::SendData]nServerID =(%d) pmblk is NULL.\n", nServerID));

        if (true == blIsDelete)
        {
            SAFE_DELETE_ARRAY(pData);
        }

        return false;
    }

    memcpy_safe((char* )pData, (uint32)nSize, pmblk->wr_ptr(), (uint32)nSize);
    pmblk->wr_ptr(nSize);

    if (true == blIsDelete)
    {
        SAFE_DELETE_ARRAY(pData);
    }

    //发送数据
    return pClientInfo->SendData(pmblk);
}
Beispiel #28
0
void
PConnection::trace_buffers(const Buffer_Info& buf_info,
                           size_t xfer_bytes,
                           bool   flg_read)
{
  int   iovcnt = buf_info.get_iov_count ();
  if (iovcnt < 0)   // ACE_Message_Block
    {
      ACE_Message_Block * mb = buf_info.get_message_block_ptr();

      for (int i=0; 
            xfer_bytes != 0 && mb != 0 ;
            mb = mb->cont (), ++i)
      {
        char * ptr = flg_read ? mb->wr_ptr () : mb->rd_ptr ();
        size_t len = flg_read ? mb->length () : (ptr - mb->base ());
        if (len > xfer_bytes)
            len = xfer_bytes;

        ptr -= len;
        xfer_bytes -=len;

        ACE_DEBUG ((LM_DEBUG,
          ACE_TEXT ("message_block [%d] length=%d:\n"), i, (int)len));

        ACE_HEX_DUMP ((LM_DEBUG, ptr, len));
      }
    }
  else if (iovcnt > 0) // iovec 
    {
      iovec *iov = buf_info.get_iov ();
      for (int i=0; xfer_bytes != 0 && i < iovcnt; ++i)
      {
        char * ptr = (char*) iov[i].iov_base;
        size_t len = iov[i].iov_len;
        if (len > xfer_bytes)
            len = xfer_bytes;

        ptr -= len;
        xfer_bytes -=len;

        ACE_DEBUG ((LM_DEBUG,
          ACE_TEXT ("iov[%d] length=%d:\n"), i, (int)len));

        ACE_HEX_DUMP ((LM_DEBUG, ptr, len));
      }
    }
  else // simple  buffer
    {
      char *ptr = buf_info.get_buffer ();
      ACE_DEBUG ((LM_DEBUG,
          ACE_TEXT ("buffer length=%d:\n"), (int)xfer_bytes));

      ACE_HEX_DUMP ((LM_DEBUG, ptr, xfer_bytes));
    }
  ACE_DEBUG ((LM_DEBUG,
    ACE_TEXT ("**** end of buffers ****************\n")));
}
Beispiel #29
0
int Logging_Handler::recv_log_record (ACE_Message_Block *&mblk)
{
  // Put <logging_peer>'s hostname in new message block.
  ACE_INET_Addr peer_addr;
  logging_peer_.get_remote_addr (peer_addr);
  mblk = new ACE_Message_Block (MAXHOSTNAMELEN + 1);
  peer_addr.get_host_name (mblk->wr_ptr (), MAXHOSTNAMELEN);
  mblk->wr_ptr (ACE_OS::strlen (mblk->wr_ptr ()) + 1); // Go past name

  // Allocate a message block for the payload; initially at least
  // large enough to hold the header, but needs some room for
  // alignment.
  ACE_Message_Block *payload =
    new ACE_Message_Block (ACE_DEFAULT_CDR_BUFSIZE);
  // Align the Message Block for a CDR stream
  ACE_CDR::mb_align (payload);
  if (logging_peer_.recv_n (payload->wr_ptr (), 8) == 8) {
    payload->wr_ptr (8);               // Reflect addition of 8 bytes

    // Create a CDR stream to parse the 8-byte header.
    ACE_InputCDR cdr (payload);

    // Extract the byte-order and use helper methods to
    // disambiguate octet, booleans, and chars.
    ACE_CDR::Boolean byte_order;
    cdr >> ACE_InputCDR::to_boolean (byte_order);

    // Set the byte-order on the stream...
    cdr.reset_byte_order (byte_order);

    // Extract the length
    ACE_CDR::ULong length;
    cdr >> length;

    // Ensure there's sufficient room for log record payload.
    ACE_CDR::grow (payload, 8 + ACE_CDR::MAX_ALIGNMENT + length);

    // Use <recv_n> to obtain the contents.
    if (logging_peer_.recv_n (payload->wr_ptr (), length) > 0) {
      payload->wr_ptr (length);   // Reflect additional bytes
      // Chain the payload to mblk via the contination field.
      mblk->cont (payload);
      return length;
    }
  }
Beispiel #30
0
int 
Sender::initiate_write_stream (void)
{
  ACE_Guard<ACE_Recursive_Thread_Mutex> locker (mutex_);   

  welcome_message_.rd_ptr(welcome_message_.base ());
  welcome_message_.wr_ptr(welcome_message_.base ());
  welcome_message_.wr_ptr (ACE_OS::strlen (data));
	
  if (this->ws_.write (welcome_message_,
                       welcome_message_.length ()) == -1)
    ACE_ERROR_RETURN((LM_ERROR,
                      "%p\n",
                      "ACE_Asynch_Write_Stream::write"),
                     -1);
  io_count_++;
  return 0;
}