////////////////////////////////////////////////////////////////////////
  // Now the svc() method where everything interesting happens.
  //
  int 
  LaserTask::svc()
  {
    MIRO_DBG_OSTR(SICK, LL_DEBUG, "("<<(void *) this <<"|"<<ACE_Thread::self ()<<") Task 0x%x starts in thread %u\n");

    // Where we getq() the message
    ACE_Message_Block *message;
    LaserMessage * data;

    while (true) {
      // Get the message...
      if (getq (message) == -1) {
	throw Miro::Exception("LaserTask::svc: could not getq from message queue");
      }

      // Is it a shutdown request?
      if (message->msg_type () == ACE_Message_Block::MB_HANGUP) {
	break;
      }

      // Get the LaserMessage pointer out of the ACE message block.
      data = (LaserMessage*)message->rd_ptr ();
      if (!data) 
	throw Miro::Exception("LaserTask::svc: got empty message block");
      
      doPkt( data );

      // throw away message block
      message->release ();
    }

    MIRO_LOG(LL_NOTICE, "left service.");

    return (0);
  }
Example #2
0
int
Terminator::svc()
{
  while (1)
    {
      ACE_Message_Block* mb = 0;
      if (this->getq(mb) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%P|%t|%T) ERROR: Terminator::svc() could not get "
                             "message block from queue"), -1);
        }

      if (mb->msg_type () == ACE_Message_Block::MB_HANGUP)
        {
          mb->release ();
          break;
        }
      int delay_secs = ACE_OS::atoi(mb->rd_ptr());
      ACE_DEBUG ((LM_DEBUG,
                  "(%P|%t|%T) Terminator::svc() Sleeping %d seconds before aborting\n", delay_secs));
      ACE_OS::sleep(delay_secs);
      ACE_OS::abort();
    }
  return 0;
}
Example #3
0
static void *
consumer (ACE_Message_Queue<ACE_MT_SYNCH> *msg_queue)
{
  // Keep looping, reading a message out of the queue, until we
  // timeout or get a message with a length == 0, which signals us to
  // quit.

  for (;;)
    {
      ACE_Message_Block *mb = 0;

      if (msg_queue->dequeue_head (mb) == -1)
        break;

      int length = ACE_Utils::truncate_cast<int> (mb->length ());

      if (length > 0)
        ACE_OS::puts (mb->rd_ptr ());

      // Free up the buffer memory and the Message_Block.
      ACE_Allocator::instance ()->free (mb->rd_ptr ());
      mb->release ();

      if (length == 0)
        break;
    }

  return 0;
}
Example #4
0
  virtual int svc (void)
  {
    ACE_Thread_ID id;
    thread_id_ = id;
    while (1)
      {
        ACE_Message_Block *mb = 0;
        if (this->getq (mb) == -1)
          ACE_ERROR_BREAK
            ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("getq")));
        if (mb->msg_type () == ACE_Message_Block::MB_HANGUP)
          {
            ACE_DEBUG ((LM_INFO,
                        ACE_TEXT ("(%t) Shutting down\n")));
            mb->release ();
            break;
          }
        // Process the message.
        process_message (mb);
        // Return to work.
        this->manager_->return_to_work (this);
      }

    return 0;
  }
Example #5
0
void
JAWS_Asynch_IO::send_message (JAWS_IO_Handler *ioh,
                              const char *buffer,
                              unsigned int length,
                              long act)
{
  ioh->idle ();

  JAWS_Asynch_IO_Handler *aioh =
    dynamic_cast<JAWS_Asynch_IO_Handler *> (ioh);

  ACE_Message_Block *mb = 0;
  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 (*(aioh->handler ()), aioh->handle ()) == -1
      || aw.write (*mb, length, (void *) static_cast<intptr_t> (act)) == -1)
    {
      mb->release ();

      if (act == CONFIRMATION)
        ioh->confirmation_message_complete ();
      else
        ioh->error_message_complete ();
    }
}
Example #6
0
void release_cfentries(ACE_Message_Block& mb_hdr)
{
  ACE_Message_Block* cf = mb_hdr.cont();
  mb_hdr.cont(mb_hdr.cont()->cont());
  cf->cont(0);
  cf->release();
}
int SiteWorkTask::svc()
{
	ImageCommRequestArgs *pImageCommRequestArgs = NULL;

	SetupDefaultComments();

	while(true)
	{
		try
		{
			ACE_Message_Block *mb = NULL;
			if( getq(mb) == -1 )
			{
				break;
			}

			if( mb->msg_type() == ACE_Message_Block::MB_STOP )
			{
				mb->release();
				break;
			}

			if (NULL == mb)
			{
				continue;
			}

			memcpy(&pImageCommRequestArgs, mb->rd_ptr(), sizeof(pImageCommRequestArgs));
			onCommandRequest(pImageCommRequestArgs);

			delete pImageCommRequestArgs;
			mb->release();

			if (_pSiteRenderTask)
			{
				_pSiteRenderTask->Process();
			}
		}
		catch(...)
		{
			//throw "work task crashed";
			LOG_ERROR("catch exception.");
		}
	}

	return 0;
}
Example #8
0
template <class BARRIER> int
Worker_Task<BARRIER>::svc (void)
{
  // Note that the <ACE_Task::svc_run> method automatically adds us to
  // the Thread_Manager when the thread begins.

  // Keep looping, reading a message out of the queue, until we get a
  // message with a length == 0, which signals us to quit.

  for (int iter = 1; ;iter++)
    {
      ACE_Message_Block *mb = 0;

      int result = this->getq (mb);

      if (result == -1)
        {
          ACE_ERROR ((LM_ERROR,
                      "(%t) in iteration %d\n",
                      "error waiting for message in iteration",
                      iter));
          break;
        }

      size_t length = mb->length ();
      this->service (mb,iter);

      if (length == 0)
        {
          ACE_DEBUG ((LM_DEBUG,
                      "(%t) in iteration %d got quit, exit!\n",
                      iter));
                      mb->release ();
          break;
        }

      this->barrier_.wait ();
      this->output (mb);

      mb->release ();
    }

  // Note that the <ACE_Task::svc_run> method automatically removes us
  // from the Thread_Manager when the thread exits.

  return 0;
}
Example #9
0
static void *
connector (void *)
{
  ACE_UPIPE_Stream c_stream;

  ACE_OS::sleep (5);

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) connector starting connect\n")));
  ACE_UPIPE_Connector con;

  if (con.connect (c_stream, addr) == -1)
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) connector ACE_UPIPE_Connector failed\n")));

  ACE_Message_Block *mb = 0;

  ACE_NEW_RETURN (mb, ACE_Message_Block (sizeof ("hello thanks") * sizeof (char)), 0);

  mb->copy ("hello");

  if (c_stream.send (mb) == -1)
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) error connector send\n")));

  if (c_stream.recv (mb) == -1)
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) error connector recv\n")));

  ACE_TEST_ASSERT (ACE_OS::strcmp (mb->rd_ptr (), "thanks") == 0);

  // Free up the memory block.
  mb->release ();

  // Now try the send()/recv() interface.
  char mytext[] = "This string is sent by connector as a buffer";

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) connector sending text\n")));
  if (c_stream.send (mytext, sizeof (mytext)) == -1)
    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("(%t) buffer send from connector failed\n")));

  char conbuf[BUFSIZ];  // Buffer to receive response.

  int i = 0;

  for (char c = ' '; c != '!'; i++)
    {
      if (c_stream.recv (&c, 1) == -1)
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("(%t) buffer recv from connector failed\n")));
      else
        conbuf[i] = c;
    }

  conbuf[i] = '\0';
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) conbuf = %s\n"), conbuf));
  ACE_TEST_ASSERT (ACE_OS::strcmp (conbuf, "this is the acceptor response!") == 0);

  c_stream.close ();
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) exiting thread\n")));
  return 0;
}
Example #10
0
File: Peer.cpp Project: manut/ACE
int
Peer_Handler::handle_output (ACE_HANDLE)
{
  ACE_Message_Block *mb = 0;

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("in handle_output\n")));

  if (this->msg_queue ()->dequeue_head
      (mb,
       (ACE_Time_Value *) &ACE_Time_Value::zero) != -1)
    {
      switch (this->nonblk_put (mb))
        {
        case 0:           // Partial send.
          ACE_ASSERT (errno == EWOULDBLOCK);
          // Didn't write everything this time, come back later...
          break;
          /* NOTREACHED */
        case -1:
          // Caller is responsible for freeing a ACE_Message_Block if
          // failures occur.
          mb->release ();
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%p\n"),
                      ACE_TEXT ("transmission failure in handle_output")));
          /* FALLTHROUGH */
        default: // Sent the whole thing.
          // If we succeed in writing the entire event (or we did not
          // fail due to EWOULDBLOCK) then check if there are more
          // events on the <ACE_Message_Queue>.  If there aren't, tell
          // the <ACE_Reactor> not to notify us anymore (at least
          // until there are new events queued up).

          if (this->msg_queue ()->is_empty ())
            {
              ACE_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("queue now empty on handle %d to connection id %d\n"),
                          this->get_handle (),
                          this->connection_id_));

              if (ACE_Reactor::instance ()->cancel_wakeup
                  (this, ACE_Event_Handler::WRITE_MASK) == -1)
                ACE_ERROR ((LM_ERROR,
                            ACE_TEXT ("%p\n"),
                            ACE_TEXT ("cancel_wakeup")));
            }
        }
      return 0;
    }
  else
    // If the list is empty there's a bug!
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("%p\n"),
                       ACE_TEXT ("dequeue_head")),
                      0);
}
Example #11
0
// Called when input is available from the client
//
// We can handle requests until the peer closes on, potentially, many connections simultaneously.
// We do this asynchronously, acoiding blocking I/O operations which are bad because they block all connection processing. 
//
int ClientService::handle_input(ACE_HANDLE fileDescriptor)
{
	const size_t inputSize = 4096;
	char		 buffer[inputSize];
	ssize_t		 receivedBytes;
	ssize_t		 sentBytes;

	if ((receivedBytes = m_socket.recv(buffer, sizeof(buffer))) <= 0)
	{
		ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%P|%t) Connection closed\n")));
		return -1;
	}

	sentBytes = m_socket.send(buffer, static_cast<size_t>(receivedBytes));

	if (sentBytes == receivedBytes)
	{
		return 0;
	}

	if (sentBytes == -1 && ACE_OS::last_error() != EWOULDBLOCK)
	{
		ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("(%P|%t) %p\n"), ACE_TEXT("send")), 0);
	}

	if (sentBytes == -1)
	{
		sentBytes = 0;
	}

	//
	ACE_Message_Block* message;

	size_t remaining = static_cast<size_t>(receivedBytes - sentBytes);

	ACE_NEW_RETURN(message, ACE_Message_Block(remaining), -1);

	message->copy(&buffer[sentBytes], remaining);

	bool isEmpty = m_queue.is_empty();

	ACE_Time_Value nowait(ACE_OS::gettimeofday());

	if (m_queue.enqueue_tail(message, &nowait) == -1)
	{
		ACE_ERROR((LM_ERROR, ACE_TEXT("(%P|%t) %p; discarding data\n"), ACE_TEXT("enqueue failed")));
		message->release();
		return 0;
	}

	if (isEmpty)
	{
		return this->reactor()->register_handler(this, ACE_Event_Handler::WRITE_MASK);		
	}

	return 0;
}
Example #12
0
int
Thread_Pool::svc (void)
{
  // Keep looping, reading a message out of the queue, until we get a
  // message with a length == 0, which signals us to quit.

  for (int count = 1; ; count++)
    {
      ACE_Message_Block *mb = 0;

      int result = this->getq (mb);

      ACE_TEST_ASSERT (result != -1 || errno == ESHUTDOWN);

      if (result == -1 && errno == ESHUTDOWN)
        {
          // The queue has been deactivated, so let's bail out.
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%t) in iteration %d, queue len = %d, ")
                      ACE_TEXT ("queue deactivated, exiting\n"),
                      count,
                      this->msg_queue ()->message_count ()));

          break;
        }

      size_t length = mb->length ();

      if (length > 0)
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("(%t) in iteration %d, queue len = %d, ")
                    ACE_TEXT ("length = %d, text = \"%*s\"\n"),
                    count,
                    this->msg_queue ()->message_count (),
                    length,
                    length - 1,
                    mb->rd_ptr ()));

      // We're responsible for deallocating this.
      mb->release ();

      if (length == 0)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%t) in iteration %d, queue len = %d, ")
                      ACE_TEXT ("got \"empty\" message, exiting\n"),
                      count,
                      this->msg_queue ()->message_count ()));
          break;
        }
    }

  // Note that the <ACE_Task::svc_run> method automatically removes us
  // from the <ACE_Thread_Manager> when the thread exits.
  return 0;
}
void SerialportTask::clear()
{
    while (!message_queue.is_empty())
    {
        ACE_Message_Block *b = 0;
        message_queue.dequeue(b);

        b->release();
    }
}
Example #14
0
int RealmSocket::handle_output(ACE_HANDLE)
{
    if (closing_)
        return -1;

    ACE_Message_Block* mb = 0;

    if (msg_queue()->is_empty())
    {
        reactor()->cancel_wakeup(this, ACE_Event_Handler::WRITE_MASK);
        return 0;
    }

    if (msg_queue()->dequeue_head(mb, (ACE_Time_Value *)(&ACE_Time_Value::zero)) == -1)
        return -1;

    ssize_t n = noblk_send(*mb);

    if (n < 0)
    {
        mb->release();
        return -1;
    }
    else if (size_t(n) == mb->length())
    {
        mb->release();
        return 1;
    }
    else
    {
        mb->rd_ptr(n);

        if (msg_queue()->enqueue_head(mb, (ACE_Time_Value *) &ACE_Time_Value::zero) == -1)
        {
            mb->release();
            return -1;
        }

        return 0;
    }

    ACE_NOTREACHED(return -1);
}
Example #15
0
  virtual int svc (void)
  {
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) starting up %C\n"),
                name_));

    ACE_OS::sleep (2);
    ACE_Message_Block *mb = 0;
    while (this->getq (mb) != -1)
      {
        if (mb->msg_type () == ACE_Message_Block::MB_BREAK)
          {
            mb->release ();
            break;
          }
        process_message (mb);
        mb->release ();
      }
    return 0;
  }
Example #16
0
Task::~Task (void)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) Task dtor 0x%x\n",
              (void *) this));

  ACE_Message_Block *message;
  this->getq (message);
  message->release ();
}
void SiteWorkRenderTask::ProcessQueue(ACE_Message_Queue<ACE_MT_SYNCH> *pQueue)
{
	if (pQueue == NULL)
		return;

	ACE_Time_Value cur;;
	ACE_Time_Value secds(0);
	ACE_Message_Block *mb = NULL;
	const IStudyImage *pIStudyImage = NULL;
	ImageRenderTaskArgs *pImageRenderTaskArgs = NULL;

	while (pQueue->is_empty() == false)
	{
		cur = ACE_OS::gettimeofday();
		cur += secds;

		int res = pQueue->dequeue_head(mb, &cur);
		if (res >= 0 && mb)
		{
			memcpy(&pImageRenderTaskArgs, mb->rd_ptr(), sizeof(pImageRenderTaskArgs));

			pIStudyImage = pImageRenderTaskArgs->pImageOwner;
			
			if (pIStudyImage)
			{
				if ((pImageRenderTaskArgs->taskFlags) & OPTYPE_RENDER)
				{
					pIStudyImage->SyncRenderImageOut();
				}
				
				if ((pImageRenderTaskArgs->taskFlags) & OPTYPE_REPORT_POSTRENDER_MSG)
				{
					IStudyImage *pModStudyImage = const_cast<IStudyImage *>(pIStudyImage);
					if (pModStudyImage)
					{
						ImageStatusMsgNotifier *pMsgNotifier = dynamic_cast<ImageStatusMsgNotifier *>(pModStudyImage);
						if (pMsgNotifier)
						{
							pMsgNotifier->SendPostRenderStatusMsgs();
						}
					}
					
				}
			}
			
			DEL_PTR(pImageRenderTaskArgs);

			mb->release();
		}
		else
		{
			break;
		}
	}
}
Example #18
0
int
DSession::initiate_write (ACE_Message_Block &mb,
                          const ACE_INET_Addr & addr)
{

  size_t nbytes = mb.length();

  if (cfg.loglevel() <= 1)
      {
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("(%t) %s=%d Initiate WRITE %u bytes\n"),
                    this->get_name (),
                    this->index (),
                    nbytes));
      }

  if (nbytes == 0)
    {
      mb.release ();
      this->stream_.cancel_and_close();
      return -1;
    }

  if (this->stream_.send(mb,
                         nbytes,
                         0, 
                         addr) == -1)
    {
      mb.release ();
      this->stream_.cancel_and_close();

      ACE_ERROR_RETURN((LM_ERROR,
                        ACE_TEXT ("(%t) %s=%d attempt WRITE failed\n"),
                        this->get_name(),
                        this->index()),
                        -1);
    }

  this->io_count_w_++;
  this->total_w_++;
  return 0;
}
Example #19
0
void ProactorService::PostRecv()
{
	ACE_Message_Block* pBlock;

	ACE_NEW_NORETURN(pBlock, ACE_Message_Block (2048));
	if(this->m_AsyncReader.read(*pBlock, pBlock->space()) != 0)
	{
		pBlock->release();
		ReserveClose();
	}
}
int AC_Input_Handler::handle_input (ACE_HANDLE handle) {
  ACE_Message_Block *mblk = 0;
  Logging_Handler logging_handler (handle);

  if (logging_handler.recv_log_record (mblk) != -1)
    {
      if (output_handler_->put (mblk->cont ()) != -1)
        {
          mblk->cont (0);
          mblk->release ();
          return 0; // Success return.
        }
      else
        {
          mblk->release ();
        }
    }
    
  return -1; // Error return.
}
Example #21
0
int RASocket::process_command (const std::string& command)
{
    if (command.length() == 0)
        return 0;

    sLog->outRemote("Got command: %s", command.c_str());

    // handle quit, exit and logout commands to terminate connection
    if (command == "quit" || command == "exit" || command == "logout")
    {
        (void) send("Bye\r\n");
        return -1;
    }

    CliCommandHolder* cmd = new CliCommandHolder(this, command.c_str(), &RASocket::zprint, &RASocket::commandFinished);
    sWorld->QueueCliCommand(cmd);

    // wait for result
    ACE_Message_Block* mb;
    for (;;)
    {
        if (getq(mb) == -1)
            return -1;

        if (mb->msg_type() == ACE_Message_Block::MB_BREAK)
        {
            mb->release();
            break;
        }

        if (size_t(peer().send(mb->rd_ptr(), mb->length())) != mb->length())
        {
            mb->release();
            return -1;
        }

        mb->release();
    }

    return 0;
}
Example #22
0
int ACE_TMAIN (int, ACE_TCHAR **)
{
#if 0
// Just for the book...

// Listing 1 code/ch12
  ACE_Message_Block *mb;
  ACE_NEW_RETURN (mb, ACE_Message_Block (128), -1);

  const char *deviceAddr= "Dev#12";
  mb->copy (deviceAddr, ACE_OS::strlen (deviceAddr)+1);
// Listing 1
#endif /* 0 */
// Listing 2 code/ch12
  ACE_Message_Block *mb;
  ACE_NEW_RETURN (mb, ACE_Message_Block (128), -1);

  const char *commandSeq= "CommandSeq#14";
  ACE_OS::sprintf (mb->wr_ptr (), commandSeq);
  // Move the wr_ptr() forward in the buffer by the
  // amount of data we just put in.
  mb->wr_ptr (ACE_OS::strlen (commandSeq) +1);
// Listing 2
// Listing 3 code/ch12
  ACE_DEBUG((LM_DEBUG,
             ACE_TEXT ("Command Sequence --> %C\n"),
             mb->rd_ptr ()));
  mb->rd_ptr (ACE_OS::strlen (mb->rd_ptr ())+1);
  mb->release ();
// Listing 3
// Listing 4 code/ch12
  // Send a hangup notification to the receiver.
  ACE_NEW_RETURN
    (mb, ACE_Message_Block (128, ACE_Message_Block::MB_HANGUP), -1);
  // Send an error notification to the receiver.
  mb->msg_type (ACE_Message_Block::MB_ERROR);
// Listing 4
  mb->release ();

  return 0;
}
Example #23
0
/*virtual*/ int BufferedSocket::handle_output(ACE_HANDLE /*= ACE_INVALID_HANDLE*/)
{
    ACE_Message_Block* mb = 0;

    if (this->msg_queue()->is_empty())
    {
        // if no more data to send, then cancel notification
        this->reactor()->cancel_wakeup(this, ACE_Event_Handler::WRITE_MASK);
        return 0;
    }

    if (this->msg_queue()->dequeue_head(mb, (ACE_Time_Value*) &ACE_Time_Value::zero) == -1)
        return -1;

    ssize_t n = this->noblk_send(*mb);

    if (n < 0)
    {
        mb->release();
        return -1;
    }
    else if (n == mb->length())
    {
        mb->release();
        return 1;
    }
    else
    {
        mb->rd_ptr(n);

        if (this->msg_queue()->enqueue_head(mb, (ACE_Time_Value*) &ACE_Time_Value::zero) == -1)
        {
            mb->release();
            return -1;
        }

        return 0;
    }

    ACE_NOTREACHED(return -1);
}
Example #24
0
int RASocket::process_command(const std::string& command)
{
    if (command.length() == 0)
        return 0;

    TC_LOG_INFO(LOG_FILTER_REMOTECOMMAND, "Received command: %s", command.c_str());

    // handle quit, exit and logout commands to terminate connection
    if (command == "quit" || command == "exit" || command == "logout") {
        (void) send("Bye\r\n");
        return -1;
    }

    _commandExecuting = true;
    CliCommandHolder* cmd = new CliCommandHolder(this, command.c_str(), &RASocket::zprint, &RASocket::commandFinished);
    sWorld->QueueCliCommand(cmd);

    // wait for result
    ACE_Message_Block* mb;
    for (;;)
    {
        if (getq(mb) == -1)
            return -1;

        if (mb->msg_type() == ACE_Message_Block::MB_BREAK)
        {
            mb->release();
            break;
        }

        if (send(std::string(mb->rd_ptr(), mb->length())) == -1)
        {
            mb->release();
            return -1;
        }

        mb->release();
    }

    return 0;
}
Example #25
0
/* When an object wants to do work in the pool, it should call the
   enqueue() method.  We introduce the ACE_Message_Block here but,
   unfortunately, we seriously misuse it.  */
int
Thread_Pool::enqueue (ACE_Event_Handler *handler)
{
    /* An ACE_Message_Block is a chunk of data.  You put them into an
      ACE_Message_Queue.  ACE_Task<> has an ACE_Message_Queue built in.
      In fact, the parameter to ACE_Task<> is passed directly to
      ACE_Message_Queue.  If you look back at our header file you'll see
      that we used ACE_MT_SYNCH as the parameter indicating that we want
      MultiThread Synch safety.  This allows us to safely put
      ACE_Message_Block objects into the message queue in one thread and
      take them out in another.  */

    /* An ACE_Message_Block wants to have char* data.  We don't have
      that.  We could cast our ACE_Event_Handler* directly to a char*
      but I wanted to be more explicit.  Since casting pointers around
      is a dangerous thing, I've gone out of my way here to be very
      clear about what we're doing.

      First: Cast the handler pointer to a void pointer.  You can't do
      any useful work on a void pointer, so this is a clear message that
      we're making the pointer unusable.

      Next: Cast the void pointer to a char pointer that the ACE_Message_Block will accept.  */
    void *v_data = (void *) handler;
    char *c_data = (char *) v_data;

    ACE_Message_Block *mb;

    /* Construct a new ACE_Message_Block.  For efficiency, you might
      want to preallocate a stack of these and reuse them.  For
      simplicity, I'll just create what I need as I need it.  */
    ACE_NEW_RETURN (mb,
                    ACE_Message_Block (c_data),
                    -1);

    /* Our putq() method is a wrapper around one of the enqueue methods
      of the ACE_Message_Queue that we own.  Like all good methods, it
      returns -1 if it fails for some reason.  */
    if (this->putq (mb) == -1)
    {
        /* Another trait of the ACE_Message_Block objects is that they
          are reference counted.  Since they're designed to be passed
          around between various objects in several threads we can't
          just delete them whenever we feel like it.  The release()
          method is similar to the destroy() method we've used
          elsewhere.  It watches the reference count and will delete the
          object when possible.  */
        mb->release ();
        return -1;
    }

    return 0;
}
Example #26
0
void PacketQueue::Reset()
{
    ACE_Time_Value tv;
    ACE_Message_Block* mb;
    while(dequeue(mb, &tv)>=0)
    {
        FieldPacket* p;
        memcpy(&p, mb->rd_ptr(), sizeof(p));
        mb->release();
        delete p;
    }
}
Example #27
0
void AudioThread::ProcessQueue(ACE_Time_Value* tm)
{
    TTASSERT(m_codec.codec != CODEC_NO_CODEC);
    TTASSERT(m_callback);
    ACE_Message_Block* mb;
    while(getq(mb, tm) >= 0)
    {
        media::AudioFrame* audframe = reinterpret_cast<media::AudioFrame*>(mb->rd_ptr());
        ProcessAudioFrame(*audframe);
        mb->release();
    }
}
Example #28
0
packet_ptr_t PacketQueue::GetNextPacket()
{
    FieldPacket* p = NULL;
    ACE_Message_Block* mb;
    ACE_Time_Value tv;
    if(this->dequeue(mb, &tv)>=0)
    {
        memcpy(&p, mb->rd_ptr(), sizeof(p));
        mb->release();
    }
    return packet_ptr_t(p);
}
int
Thread_Per_Request_Task::svc (void)
{
    ACE_Message_Block *mb;
    ACE_NEW_RETURN (mb, ACE_Message_Block (HTTP_Handler::MAX_REQUEST_SIZE + 1),
                    -1);
    Synch_HTTP_Handler_Factory factory;
    HTTP_Handler *handler = factory.create_http_handler ();
    handler->open (this->handle_, *mb);
    mb->release ();
    return 0;
}
Example #30
0
bool BufferedSocket::send(const char* buf, size_t len)
{
    if (buf == NULL || len == 0)
        return true;

    ACE_Data_Block db(
        len,
        ACE_Message_Block::MB_DATA,
        (const char*)buf,
        0,
        0,
        ACE_Message_Block::DONT_DELETE,
        0);

    ACE_Message_Block message_block(
        &db,
        ACE_Message_Block::DONT_DELETE,
        0);

    message_block.wr_ptr(len);

    if (this->msg_queue()->is_empty())
    {
        // Try to send it directly.
        ssize_t n = this->noblk_send(message_block);

        if (n < 0)
            return false;
        else if (n == len)
            return true;

        // adjust how much bytes we sent
        message_block.rd_ptr((size_t)n);

        // fall down
    }

    // enqueue the message, note: clone is needed cause we cant enqueue stuff on the stack
    ACE_Message_Block* mb = message_block.clone();

    if (this->msg_queue()->enqueue_tail(mb, (ACE_Time_Value*) &ACE_Time_Value::zero) == -1)
    {
        mb->release();
        return false;
    }

    // tell reactor to call handle_output() when we can send more data
    if (this->reactor()->schedule_wakeup(this, ACE_Event_Handler::WRITE_MASK) == -1)
        return false;

    return true;
}