Ejemplo n.º 1
0
void
Sender::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
{
  this->trace_read_completion (result);

  {
    ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->mutex ());

    this->ref_cnt_r_--;

    ACE_Message_Block & mb = result.message_block ();
    mb.release ();

    if (result.error () != 0)
      {
        this->cancel_i ();
      }
    else if (result.bytes_transferred () == 0)
      {
        this->shutdown_i ();
      }
    else if (this->initiate_write_stream () != 0 ||
             this->initiate_read_stream() != 0)
      {
        this->cancel_i ();
      }

    if (!this->is_safe_to_delete())
      return;
  }
  this->manager()->destroy_session(this);
}
Ejemplo n.º 2
0
void
PConnection::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
{
    this->trace_read_completion (result);

    {
        ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->mutex ());

        this->update_last_time();

        this->ref_cnt_r_--;

        ACE_Message_Block & mb = result.message_block ();
        int              error = result.error();
        size_t      xfer_bytes = result.bytes_transferred ();

        this->total_rcv_ += xfer_bytes;

        if (error == 0 && this->protocol_ != 0)
        {
            this->protocol_->on_read_finished (mb, xfer_bytes, error);
        }
        else
        {
            this->free_msg (&mb);
            this->cancel ();
        }

        if (!this->is_safe_to_delete ())
            return;
    }
    this->manager()->destroy_connection (this);
}
Ejemplo n.º 3
0
void
Peer_Handler::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
{
  if (result.bytes_transferred () > 0 &&
      this->mb_.length () > 0)
    {
      this->mb_.rd_ptr ()[result.bytes_transferred ()] = '\0';
      // Print out the message received from the server.
      ACE_DEBUG ((LM_DEBUG, "%s", this->mb_.rd_ptr ()));
    }
  else
    {
      // If a read failed, we will assume it's because the remote peer
      // went away.  We will end the event loop.  Since we're in the
      // main thread, we don't need to do a notify.
      ACE_Reactor::end_event_loop();
      return;
    }

  // Reset pointers
  this->mb_.wr_ptr (this->mb_.wr_ptr () - result.bytes_transferred ());

  // Start off another read
  if (this->rd_stream_.read (this->mb_,
                             this->mb_.size ()) == -1)
    ACE_ERROR ((LM_ERROR, "%p Read initiate.\n", "Peer_Handler"));
}
Ejemplo n.º 4
0
void HttpdPeer::handle_read_stream( const ACE_Asynch_Read_Stream::Result &result )
{

	if (!result.success() || result.bytes_transferred() == 0)
	{
		ACE_ERROR ((LM_ERROR,
			"%p ",
			"HttpdPeer::Read"));
		ACE_OS::printf("%d\n",ACE_OS::last_error());
		delete this;
		
	}
	else
	{
		//write response
		if (connect_succeed_)
		{
			init_read();
			return;
		}
		ACE_Message_Block *lpMb_ = NULL;
		ACE_NEW_NORETURN(lpMb_,ACE_Message_Block(HTTP_RESPONSE,ACE_OS::strlen(HTTP_RESPONSE)));
		lpMb_->wr_ptr(ACE_OS::strlen(HTTP_RESPONSE));
		putQ(lpMb_ );
		init_write();

		init_read();
		connect_succeed_ = true;
		bIsIniting_ =true;
		sentinel_ =0;
	}
}
Ejemplo n.º 5
0
void AIO_Output_Handler::handle_read_stream
       (const ACE_Asynch_Read_Stream::Result &result) {
  result.message_block ().release ();
  writer_.cancel ();
  ACE_OS::closesocket (result.handle ());
  handle (ACE_INVALID_HANDLE);
  can_write_ = 0;
  CLD_CONNECTOR::instance ()->reconnect ();
}
Ejemplo n.º 6
0
void
PConnection::trace_read_completion (const ACE_Asynch_Read_Stream::Result &result)
{
    int loglevel = this->config().loglevel ();

    size_t xfer_bytes = result.bytes_transferred();

    if (loglevel == 0)
    {
        LogLocker log_lock;

        ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("(%t) **** %s=%d handle_read_stream() ****\n"),
            this->get_name(),
            this->index()));

        ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("bytes_to_read = %d\n"),
            result.bytes_to_read ()));

        ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("handle = %d\n"),
            result.handle ()));

        ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("bytes_transfered = %d\n"),
            xfer_bytes));

        ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("error = %d\n"),
            result.error ()));

        trace_buffers (result.get_buffer_info (), xfer_bytes, true);

    }
    else if (result.error () != 0 )
    {
        LogLocker log_lock;

        ACE_Log_Msg::instance ()->errnum (result.error ());
        ACE_OS::last_error (result.error ());
        ACE_Log_Msg::instance ()->log (LM_ERROR,
            ACE_TEXT ("(%t) %s=%d READ %p\n"),
            this->get_name (),
            this->index (),
            ACE_TEXT ("ERROR"));
    }
    else if (loglevel == 1)
    {
        ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("(%t) %s=%d read_bytes=%d OK\n"),
            this->get_name (),
            this->index (),
            xfer_bytes));
    }

    return;

}
Ejemplo n.º 7
0
void HD_CCU_Request_Handler::handle_read_stream(
    const ACE_Asynch_Read_Stream::Result &result)
{
    ACE_Message_Block & mb = result.message_block();
    if(!result.success() || result.bytes_transferred() == 0)
    {
        // 认为接收数据失败
        ACE_DEBUG((LM_ERROR,"读取 CCU 数据失败!!"));
        free_handler();
        return;
    }
    else
    {
        ACE_DEBUG((LM_DEBUG,"开始处理数据..."));
        // 读取数据完成
        ACE_InputCDR cdr(&mb);
        ACE_CDR::UShort data_len;
        cdr >> data_len;
        if(mb.length() - MSG_BUF_LEN_HEADER >= data_len )
        {
            // 读取完成,处理业务
            if( process_request(&mb)<= 0 )
            {
                // 处理失败或者不需要应答
                free_handler();
            }
            return;
        }
        // 认为数据有问题
        // 继续读取
        if(mb.length() >= 65535)
        {
            ACE_DEBUG((LM_ERROR,"数据包长度不合法!!!!"));
            free_handler();
            return;
        }
        ACE_DEBUG((LM_DEBUG,"继续读取数据..."));
        mb.wr_ptr(mb.length());
        if( _reader.read(mb
                         ,result.bytes_to_read() - result.bytes_transferred()) != 0)
        {
            ACE_DEBUG((LM_ERROR,"读取 CCU 数据失败!!"));
            free_handler();
            return;
        }
    }
}
Ejemplo n.º 8
0
void CProConnectClient::handle_read_stream(const ACE_Asynch_Read_Stream::Result &result)
{
	ACE_Message_Block& mb = result.message_block();
	uint32 u4PacketLen = (uint32)result.bytes_transferred();

	//OUR_DEBUG((LM_DEBUG,"[CProConnectClient::handle_read_stream] m_nServerID=%d, bytes_transferred=%d, this=0x%08x.\n", 
	//	m_nServerID, 
	//	u4PacketLen,
	//	this));
	
	if(!result.success() || u4PacketLen == 0)
	{
		mb.release();
		if(NULL != m_pClientMessage)
		{
			_ClientIPInfo objServerIPInfo;
			sprintf_safe(objServerIPInfo.m_szClientIP, MAX_BUFF_20, "%s", m_AddrRemote.get_host_addr());
			objServerIPInfo.m_nPort = m_AddrRemote.get_port_number();
			
			//这里只处理远端服务器断开连接的消息,回调ConnectError
			//服务器主动关闭不在回调ConnectError
			if(S2S_NEED_CALLBACK == m_ems2s)
			{
				m_pClientMessage->ConnectError((int)ACE_OS::last_error(), objServerIPInfo);
			}
		}
		//OUR_DEBUG((LM_INFO, "[CProConnectClient::handle_read_stream]m_ems2s=%d.\n", m_ems2s));
		Close();
		return;
	}
	else 
	{
		//处理接收数据(这里不区分是不是完整包,交给上层逻辑自己去判定)
		if(NULL != m_pClientMessage)
		{
			_ClientIPInfo objServerIPInfo;
			sprintf_safe(objServerIPInfo.m_szClientIP, MAX_BUFF_20, "%s", m_AddrRemote.get_host_addr());
			objServerIPInfo.m_nPort = m_AddrRemote.get_port_number();
			m_pClientMessage->RecvData(&mb, objServerIPInfo);
		}
		mb.release();

		//接受下一个数据包
		RecvData(App_MainConfig::instance()->GetConnectServerRecvBuffer());
	}
}
Ejemplo n.º 9
0
void ProactorService::handle_read_stream( const ACE_Asynch_Read_Stream::Result& result )
{
	ACE_Message_Block& block = result.message_block();
	if (!result.success() || result.bytes_transferred() == 0)
	{
		block.release();
		ReserveClose();
	}
	else
	{
		if (false == ISession::OnReceive(block.rd_ptr(), (unsigned short)block.length(), m_sessionDesc))
		{
			block.release();
			ReserveClose();
			return;
		}

		PostRecv();
	}
}
Ejemplo n.º 10
0
void
ACE_SSL_Asynch_Stream::handle_read_stream (
  const ACE_Asynch_Read_Stream::Result &result)
{
  ACE_MT (ACE_GUARD (ACE_SYNCH_MUTEX, ace_mon, this->mutex_));

  this->bio_inp_flag_ &= ~BF_AIO;

  size_t bytes_trn = result.bytes_transferred ();
  u_long errval    = result.error ();

  if (errval != 0)                     // error ?
     this->bio_inp_errno_ = errval;    // save err code
  else if (bytes_trn == 0)             // end of stream ?
     this->bio_inp_flag_ |= BF_EOS;    // set flag EOS

  this->do_SSL_state_machine ();

  return;
}
Ejemplo n.º 11
0
void ProactorService::handle_read_stream( const ACE_Asynch_Read_Stream::Result& Result )
{
	ACE_Message_Block& Block = Result.message_block();
	if(!Result.success() || Result.bytes_transferred() == 0)
	{
		Block.release();
		ReserveClose();
	}
	else
	{
		if(false == ISession::OnReceive(Block.rd_ptr(), Block.length()))
		{
			Block.release();
			ReserveClose();
			return;
		}

		PostRecv();
	}
}
Ejemplo n.º 12
0
void
Server_Handler::handle_read_stream
  (const ACE_Asynch_Read_Stream::Result &result)
{
  if (!result.success ())
    {
      errno = result.error ();
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("(%t) Server handle %d: %p\n"),
                  this->stream_.handle (),
                  ACE_TEXT ("read")));
      delete this;
      return;
    }
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Server %@ handle %d recv %B of %B bytes\n"),
              this, this->stream_.handle (),
              result.bytes_transferred (), result.bytes_to_read ()));
  if (result.bytes_transferred () == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%t) Server handle %d closed by peer\n"),
                  this->stream_.handle ()));
      delete this;
      return;
    }

  // Scan through the received data for the expected string. There may be
  // multiples and/or partials. Count up how many arrive before the connection
  // is closed.
  // Remember that the client side sends the terminating nul; in case the
  // whole thing didn't arrive, we add a nul to the end of the receive
  // block so we don't run off the end. When the recv into this buffer was
  // initiated, we left the last byte empty to facilitate this.
  ACE_Message_Block &b = result.message_block ();
  *(b.wr_ptr ()) = '\0';
  size_t test_string_len = ACE_OS::strlen (test_string);
  while (b.length () >= test_string_len)
    {
      if (0 != ACE_OS::strncmp (b.rd_ptr (), test_string, test_string_len))
        ACE_ERROR_BREAK ((LM_ERROR,
                          ACE_TEXT ("(%t) Read string: %C; expected: %C\n"),
                          b.rd_ptr (),
                          test_string));
      b.rd_ptr (test_string_len);
      // That ran up over the string; can we also consume the nul?
      if (b.length () > 0)
        b.rd_ptr (1);
      ++this->msgs_rcvd_;
    }
  b.crunch ();
  if (this->stream_.read (b, b.space () - 1) == -1)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("(%t) Server_Handler: %p\n"),
                  ACE_TEXT ("read")));
      delete this;
    }
  return;
}
Ejemplo n.º 13
0
    virtual void handle_read_stream(const ACE_Asynch_Read_Stream::Result &result)
    {		
        trace_msg("handle input.");

		ace::auto_protected<ace::SocketStreamAsync::buffers_type> ap(_peer.buffers);		

        if (!result.success() || result.bytes_transferred() == 0)
        {
            // free resource.
            result.message_block().release();

            trace_msg("failed to read data from handle.");
            _owner->emit(kSignalClosed);
            return;
        }

        // set new buffer size.
        _peer.buffer->set_length(result.bytes_transferred());

		// add new buffer into array.
        _peer.buffers.push(_peer.buffer);

        // need post signal.
		if (_peer.wait == false)
        {            
            //trace_msg("post bytes available signal.");
            _peer.emit(kSignalBytesAvailable, eventobj_t::Data(&_peer));
        }

# ifdef NNT_DEBUG
		trace_msg("read stream: " + core::string(_peer.buffer->c_str(), _peer.buffer->length()));
# endif

        // free resource.
        result.message_block().release();

		// unlock.
		_peer.buffers.unlock();
    }
Ejemplo n.º 14
0
void
JAWS_EC_AH_Adapter
::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
{
  JAWS_Event_Result io_result;

  io_result = this->make_io_result ( result
                                   , JAWS_Event_Result::JE_RECV_OK
                                   , JAWS_Event_Result::JE_RECV_FAIL
                                   );
  // More useful diagnostics not implemented yet.

  void *act = const_cast<void *> (result.act ());

  this->completer_->input_complete (io_result, act);
  delete this;
}
Ejemplo n.º 15
0
void AIO_Input_Handler::handle_read_stream
    (const ACE_Asynch_Read_Stream::Result &result) {
  if (!result.success () || result.bytes_transferred () == 0)
    delete this;
  else if (result.bytes_transferred () < result.bytes_to_read ())
    reader_.read (*mblk_, result.bytes_to_read () -
                  result.bytes_transferred ());
  else if (mblk_->length () == LOG_HEADER_SIZE) {
    ACE_InputCDR cdr (mblk_);

    ACE_CDR::Boolean byte_order;
    cdr >> ACE_InputCDR::to_boolean (byte_order);
    cdr.reset_byte_order (byte_order);

    ACE_CDR::ULong length;
    cdr >> length;

    mblk_->size (length + LOG_HEADER_SIZE);
    reader_.read (*mblk_, length);
  }
Ejemplo n.º 16
0
void
Svc_Handler::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
{
  if (result.success () && result.bytes_transferred () > 0)
    {
      result.message_block ().rd_ptr ()[result.message_block ().length ()] = '\0';

      // Print out the message received from the server.
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%t) message size %d.\n"),
                  result.message_block ().length ()));
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%C"),
                  result.message_block ().rd_ptr ()));
      // Reset the message block here to make sure multiple writes to
      // the pipe don't keep appending to the message_block!
      this->mb_.reset ();

      this->ar_.read (this->mb_, this->mb_.size ());
    }
  else
    ACE_Proactor::end_event_loop ();
}
Ejemplo n.º 17
0
void
Receiver::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
{
  ACE_DEBUG ((LM_DEBUG,
              "handle_read_stream called\n"));

  // Reset pointers.
  result.message_block ().rd_ptr ()[result.bytes_transferred ()] = '\0';

  ACE_DEBUG ((LM_DEBUG, "********************\n"));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "bytes_to_read", result.bytes_to_read ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "handle", result.handle ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "bytes_transfered", result.bytes_transferred ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "act", (uintptr_t) result.act ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "success", result.success ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "completion_key", (uintptr_t) result.completion_key ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "error", result.error ()));
  ACE_DEBUG ((LM_DEBUG, "********************\n"));
#if 0
  // This can overrun the ACE_Log_Msg buffer and do bad things.
  // Re-enable it at your risk.
  ACE_DEBUG ((LM_DEBUG, "%s = %s\n", "message_block", result.message_block ().rd_ptr ()));
#endif /* 0 */

  if (result.success () && result.bytes_transferred () != 0)
    {
      // Successful read: write the data to the file asynchronously.
      // Note how we reuse the <ACE_Message_Block> for the writing.
      // Therefore, we do not delete this buffer because it is handled
      // in <handle_write_stream>.
      if (this->wf_.write (result.message_block (),
                           result.bytes_transferred (),
                           this->file_offset_) == -1)
        {
          ACE_ERROR ((LM_ERROR,
                      "%p\n",
                      "ACE_Asynch_Write_File::write"));
          return;
        }

      // Initiate new read from the stream.
      if (this->initiate_read_stream () == -1)
        return;
    }
  else
    {
      ACE_DEBUG ((LM_DEBUG,
                  "Receiver completed\n"));

      // No need for this message block anymore.
      result.message_block ().release ();

      // Note that we are done with the test.
      done = 1;

      // We are done: commit suicide.
      delete this;
    }
}
Ejemplo n.º 18
0
void CProConnectClient::handle_read_stream(const ACE_Asynch_Read_Stream::Result &result)
{
	ACE_Message_Block& mb = result.message_block();
	uint32 u4PacketLen = (uint32)result.bytes_transferred();

	//OUR_DEBUG((LM_DEBUG,"[CProConnectClient::handle_read_stream] m_nServerID=%d, bytes_transferred=%d, this=0x%08x.\n", 
	//	m_nServerID, 
	//	u4PacketLen,
	//	this));
	
	if(!result.success() || u4PacketLen == 0)
	{
		mb.release();
		if(NULL != m_pClientMessage)
		{
			_ClientIPInfo objServerIPInfo;
			sprintf_safe(objServerIPInfo.m_szClientIP, MAX_BUFF_20, "%s", m_AddrRemote.get_host_addr());
			objServerIPInfo.m_nPort = m_AddrRemote.get_port_number();
			
			//这里只处理远端服务器断开连接的消息,回调ConnectError
			//服务器主动关闭不在回调ConnectError
			if(S2S_NEED_CALLBACK == m_ems2s)
			{
				m_pClientMessage->ConnectError((int)ACE_OS::last_error(), objServerIPInfo);
			}
		}
		//OUR_DEBUG((LM_INFO, "[CProConnectClient::handle_read_stream]m_ems2s=%d.\n", m_ems2s));
		Close();
		return;
	}
	else 
	{
		//处理接收数据(这里不区分是不是完整包,交给上层逻辑自己去判定)
		if(NULL != m_pClientMessage)
		{
			_ClientIPInfo objServerIPInfo;
			sprintf_safe(objServerIPInfo.m_szClientIP, MAX_BUFF_20, "%s", m_AddrRemote.get_host_addr());
			objServerIPInfo.m_nPort = m_AddrRemote.get_port_number();
			//m_pClientMessage->RecvData(&mb, objServerIPInfo);

			//这里处理一下是不是完整包
			uint16 u2CommandID             = 0;
			ACE_Message_Block* pRecvFinish = NULL;

			m_atvRecv     = ACE_OS::gettimeofday();
			m_emRecvState = SERVER_RECV_BEGIN;

			while(true)
			{
				bool blRet = m_pClientMessage->Recv_Format_data(&mb, App_MessageBlockManager::instance(), u2CommandID, pRecvFinish);
				if(true == blRet)
				{
					if(App_MainConfig::instance()->GetConnectServerRunType() == 0)
					{
						//调用数据包处理
						m_pClientMessage->RecvData(u2CommandID, pRecvFinish, objServerIPInfo);
						//回收处理包
						App_MessageBlockManager::instance()->Close(pRecvFinish);
					}
					else
					{
						//异步消息处理
						_Server_Message_Info* pServer_Message_Info = new _Server_Message_Info();
						pServer_Message_Info->m_pClientMessage  = m_pClientMessage;
						pServer_Message_Info->m_objServerIPInfo = objServerIPInfo;
						pServer_Message_Info->m_pRecvFinish     = pRecvFinish;
						pServer_Message_Info->m_u2CommandID     = u2CommandID;
						App_ServerMessageTask::instance()->PutMessage(pServer_Message_Info);
					}
				}
				else
				{
					break;
				}
			}

		}
		mb.release();
		m_emRecvState = SERVER_RECV_END;


		//接受下一个数据包
		RecvData(App_MainConfig::instance()->GetConnectServerRecvBuffer());
	}
}
void
Receiver::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
{
  ACE_DEBUG ((LM_DEBUG,
              "handle_read_stream called\n"));

  // Reset pointers.
  result.message_block ().rd_ptr ()[result.bytes_transferred ()] =
    '\0';

  ACE_DEBUG ((LM_DEBUG, "********************\n"));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "bytes_to_read", result.bytes_to_read
              ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "handle", result.handle ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "bytes_transfered",
              result.bytes_transferred ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "act", (u_long) result.act ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "success", result.success ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "completion_key", (u_long)
              result.completion_key ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "error", result.error ()));
  ACE_DEBUG ((LM_DEBUG, "********************\n"));
  ACE_DEBUG ((LM_DEBUG, "%s = %s\n", "message_block",
              result.message_block ().rd_ptr ()));

  if ( result.success () && result.bytes_transferred () != 0)
    {
      // Successful read: write the data to the file asynchronously.
      // Note how we reuse the <ACE_Message_Block> for the writing.
      // Therefore, we do not delete this buffer because it is handled
      // in <handle_write_stream>.

      if(this->initiate_write_stream (result.message_block (),

                                      result.bytes_transferred () ) == 0 )
        {
          if ( duplex != 0 )
            {
              // Initiate new read from the stream.
              this->initiate_read_stream () ;
            }
        }
    }
  else
    {
      result.message_block ().release ();
      ACE_DEBUG ((LM_DEBUG,  "Receiver completed\n"));
    }

  {
    ACE_Guard<MyMutex> locker (m_Mtx) ;
    nIOCount-- ;
  }
  check_destroy () ;
}
void
Sender::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
{
  ACE_DEBUG ((LM_DEBUG,
              "handle_read_stream called\n"));

  // Reset pointers.
  result.message_block ().rd_ptr ()[result.bytes_transferred ()] =
    '\0';

  ACE_DEBUG ((LM_DEBUG, "********************\n"));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "bytes_to_read", result.bytes_to_read
              ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "handle", result.handle ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "bytes_transfered",
              result.bytes_transferred ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "act", (u_long) result.act ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "success", result.success ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "completion_key", (u_long)
              result.completion_key ()));
  ACE_DEBUG ((LM_DEBUG, "%s = %d\n", "error", result.error ()));
  ACE_DEBUG ((LM_DEBUG, "********************\n"));
  ACE_DEBUG ((LM_DEBUG, "%s = %s\n", "message_block",
              result.message_block ().rd_ptr ()));

  result.message_block().release ();

  if ( result.success () && result.bytes_transferred () != 0)
    {
      // Successful read: write the data to the file asynchronously.
      // Note how we reuse the <ACE_Message_Block> for the writing.
      // Therefore, we do not delete this buffer because it is handled
      // in <handle_write_stream>.

      if ( duplex != 0 )  // full duplex, continue read
        {
          initiate_read_stream () ;
        }
      else  // half-duplex  writey, after write we will start read
        {
          initiate_write_stream () ;
        }
    }

  {
    ACE_Guard<MyMutex> locker (m_Mtx) ;
    nIOCount-- ;
  }
}
void CProConsoleHandle::handle_read_stream(const ACE_Asynch_Read_Stream::Result &result)
{
	ACE_Message_Block& mb = result.message_block();
	uint32 u4PacketLen = (uint32)result.bytes_transferred();
	int nTran = (int)result.bytes_transferred();

	if(!result.success() || result.bytes_transferred() == 0)
	{
		//链接断开
		if(m_pPacketParse->GetMessageHead() != NULL)
		{
			App_MessageBlockManager::instance()->Close(m_pPacketParse->GetMessageHead());
		}

		if(m_pPacketParse->GetMessageBody() != NULL)
		{
			App_MessageBlockManager::instance()->Close(m_pPacketParse->GetMessageBody());
		}

		if(&mb != m_pPacketParse->GetMessageHead() && &mb != m_pPacketParse->GetMessageBody())
		{
			//OUR_DEBUG((LM_DEBUG,"[CProConsoleHandle::handle_read_stream] Message_block release.\n"));
			App_MessageBlockManager::instance()->Close(&mb);
		}
		SAFE_DELETE(m_pPacketParse);

		OUR_DEBUG((LM_DEBUG,"[%tCConnectHandler::handle_read_stream]Connectid=[%d] error(%d)...\n", GetConnectID(), errno));
		//AppLogManager::instance()->WriteLog(LOG_SYSTEM_CONNECT, "Close Connection from [%s:%d] RecvSize = %d, RecvCount = %d, SendSize = %d, SendCount = %d.",m_addrRemote.get_host_addr(), m_addrRemote.get_port_number(), m_u4AllRecvSize, m_u4AllRecvCount, m_u4AllSendSize, m_u4AllSendCount);
		//因为是要关闭连接,所以要多关闭一次IO,对应Open设置的1的初始值

		Close(2);

		return;
	}

	m_atvInput = ACE_OS::gettimeofday();

	if(result.bytes_transferred() < result.bytes_to_read())
	{
		//短读,继续读
		int nRead = (int)result.bytes_to_read() - (int)result.bytes_transferred();
		if(-1 == m_Reader.read(mb, nRead))
		{
			if(m_pPacketParse->GetMessageHead() != NULL)
			{
				App_MessageBlockManager::instance()->Close(m_pPacketParse->GetMessageHead());
			}

			if(m_pPacketParse->GetMessageBody() != NULL)
			{
				App_MessageBlockManager::instance()->Close(m_pPacketParse->GetMessageBody());
			}

			if(&mb != m_pPacketParse->GetMessageHead() && &mb != m_pPacketParse->GetMessageBody())
			{
				App_MessageBlockManager::instance()->Close(&mb);
			}
			SAFE_DELETE(m_pPacketParse);

			OUR_DEBUG((LM_ERROR, "[CConnectHandler::handle_read_stream]Read Shoter error(%d).", errno));
			//AppLogManager::instance()->WriteLog(LOG_SYSTEM_CONNECT, "Close Connection from [%s:%d] RecvSize = %d, RecvCount = %d, SendSize = %d, SendCount = %d.",m_addrRemote.get_host_addr(), m_addrRemote.get_port_number(), m_u4AllRecvSize, m_u4AllRecvCount, m_u4AllSendSize, m_u4AllSendCount);
			//因为是要关闭连接,所以要多关闭一次IO,对应Open设置的1的初始值

			Close(2);
			return;
		}

	}
	else if(mb.length() == m_pPacketParse->GetPacketHeadLen() && m_pPacketParse->GetIsHead() == false)
	{
		//判断头的合法性
		m_pPacketParse->SetPacketHead(GetConnectID(), &mb, App_MessageBlockManager::instance());
		uint32 u4PacketBodyLen = m_pPacketParse->GetPacketBodyLen();

		//如果超过了最大包长度,为非法数据
		if(u4PacketBodyLen >= MAX_MSG_PACKETLENGTH || u4PacketBodyLen <= 0)
		{
			OUR_DEBUG((LM_ERROR, "[CConnectHandler::handle_read_stream]u4PacketHeadLen(%d) more than MAX_MSG_PACKETLENGTH.", u4PacketBodyLen));

			if(m_pPacketParse->GetMessageHead() != NULL)
			{
				App_MessageBlockManager::instance()->Close(m_pPacketParse->GetMessageHead());
			}

			if(m_pPacketParse->GetMessageBody() != NULL)
			{
				App_MessageBlockManager::instance()->Close(m_pPacketParse->GetMessageBody());
			}

			if(&mb != m_pPacketParse->GetMessageHead() && &mb != m_pPacketParse->GetMessageBody())
			{
				App_MessageBlockManager::instance()->Close(&mb);
			}
			SAFE_DELETE(m_pPacketParse);

			Close(2);
			return;
		}
		else
		{
			Close();
			RecvClinetPacket(u4PacketBodyLen);
		}
	}
	else
	{
		//接受完整数据完成,开始分析完整数据包
		m_pPacketParse->SetPacketBody(GetConnectID(), &mb, App_MessageBlockManager::instance());

		CheckMessage();

		m_pPacketParse = new CConsolePacketParse();

		Close();

		//接受下一个数据包
		RecvClinetPacket(m_pPacketParse->GetPacketHeadLen());
	}

	return;
}
Ejemplo n.º 22
0
void
PSession::trace_read_completion (const ACE_Asynch_Read_Stream::Result &result)
{
    {
        ACE_GUARD (ACE_SYNCH_MUTEX, monitor, this->mutex ());

        this->update_last_time();

        if (result.success())
            this->total_rcv_ += result.bytes_transferred ();
    }

    int loglevel = this->config().loglevel ();

    ACE_Message_Block & mb = result.message_block ();

    size_t xfer_bytes = result.bytes_transferred();
    char * last  = mb.wr_ptr();
    char * first = last - xfer_bytes; 

    if (loglevel == 0)
    {
        LogLocker log_lock;

        ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("(%t) **** %s=%d handle_read_stream() ****\n"),
            this->get_name(),
            this->index()));

        ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("bytes_to_read = %d\n"),
            result.bytes_to_read ()));

        ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("handle = %d\n"),
            result.handle ()));

        ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("bytes_transfered = %d\n"),
            xfer_bytes));

        ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("error = %d\n"),
            result.error ()));

        ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("message_block:\n")));

        ACE_HEX_DUMP ((LM_DEBUG, first, xfer_bytes));


        ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("**** end of message ****************\n")));
    }
    else if (result.error () != 0 )
    {
        LogLocker log_lock;

        ACE_Log_Msg::instance ()->errnum (result.error ());
        ACE_OS::last_error (result.error ());
        ACE_Log_Msg::instance ()->log (LM_ERROR,
            ACE_TEXT ("(%t) %s=%d READ %p\n"),
            this->get_name (),
            this->index (),
            ACE_TEXT ("ERROR"));
    }
    else if (loglevel == 1)
    {
        ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("(%t) %s=%d read_bytes=%d OK\n"),
            this->get_name (),
            this->index (),
            xfer_bytes));
    }

    return;
}
Ejemplo n.º 23
0
void Reciever::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
{  
    ACE_Message_Block &mb = result.message_block ();

    //if the connection is failed£¬release the connection resource to the client
    if (!result.success () || result.bytes_transferred () == 0)  
    {  
        mb.release ();  
        delete this;  
        return;  
    }

	ACE_OS::printf("In read data\n");

	dispatcher = new MessageDispatcher();
	dispatcher->dispatchMessage(mb);
	handler = new MessageHandler();
	ACE_UINT32 cmd = dispatcher->getCmd();
	ACE_Message_Block* smb;

	//case CONNECT_SERVER
	//------------------------------------------------------------------------//
	if(cmd == COM::CONNECT_SERVER)
	{
		ACE_OS::printf("In CONNECT_SERVER\n");

		pair<ACE_UINT16,ACE_UINT16> randomPos = handler->handleConnectionSever(dispatcher->getIP(),dispatcher->getPort());
		if(randomPos.first != 0 && randomPos.second != 0)
		{
			MessageConstructor::getInstance()->setAddress(this->local_address);
			smb = MessageConstructor::getInstance()->createConnectionAck(randomPos.first,randomPos.second,DEFAULT_AOI_RADIUS);
		}
	}

	//case GET_SCENE_DATA
	//------------------------------------------------------------------------//
	else if(cmd == COM::GET_SCENE_DATA)
	{

	}

	//case CONNECT_FAIL_REPORT
	//------------------------------------------------------------------------//

	//case CLIENT_MOVE
	//------------------------------------------------------------------------//

	//case CLIENT_QUIT
	//------------------------------------------------------------------------//

	//case MORE_COMMAND
	//------------------------------------------------------------------------//

	dispatcher->setMB_NULL();
	mb.release();
	ACE_OS::printf("\nMMB data:%s\n",smb->rd_ptr());
	if (this->writer_.write(*smb,smb->length()) != 0)  
	{  
		ACE_OS::printf("Write Failed!");
		//MessageConstructor::getInstance()->setMB_NULL();
		smb->release();
		delete this; 
		return;
	}

	MessageConstructor::getInstance()->setMB_NULL();

	if(handler != NULL)
		delete handler;
	if(dispatcher != NULL)
		delete dispatcher;

	smb->release();
}
Ejemplo n.º 24
0
void
Receiver::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
{
  ACE_Message_Block *mb = &result.message_block ();

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Receiver::handle_read_stream - (%s) read %d\n"),
              this->odd_ ? ACE_TEXT ("ODD ") : ACE_TEXT ("EVEN"),
              result.bytes_transferred ()));

  // Transfer only complete chunks to the writer.
  // Save last partial chunk for the next call.
  // On disconnect (error or 0 transferred), transfer whatever we have.

  // at this stage there should not be anything there
  ACE_TEST_ASSERT (!this->partial_chunk_);

  // first, remove the empty chunks
  remove_empty_chunks (mb);

  if (mb && Receiver::writer_)
    { // there's something to write, and who to write to

      // write everything or only complete chunks?

      // write everything - when no new bytes were transferred
      int write_everything = 0;
      if (!result.bytes_transferred ())
        write_everything = 1;
      if (write_everything)
        Receiver::writer_->handle_read_chunks_chain (mb,
                                                     this->odd_ ? ODD : EVEN);
      else
        { // filter out the partial chunk at the end (if present)
          // and save it for later before writing the full chunks

          // have this->partial_chunk_ point to the last chunk in the chain
          size_t last_index = last_chunk (mb, this->partial_chunk_);
          if (this->partial_chunk_ &&
              this->partial_chunk_->length () < chunk_size)
            { // found partial chunk at end of chain
              // detach it from the chain
              if (last_index > 1) // chain bigger than 1
                {
                  ACE_Message_Block *pre_last = mb;
                  for (size_t index = 1; index < last_index - 1; ++index)
                    pre_last = pre_last->cont ();

                  // detach partial chunk from chain
                  pre_last->cont (0);
                }
              else
                // chain in length of 1 - so we need to zero mb
                mb = 0;
            }
          else // last is a full chunk, so hand it over with the rest
            this->partial_chunk_ = 0;

          // transfer (if there's anything left)
          if (mb && mb->total_length ())
            Receiver::writer_->handle_read_chunks_chain (
                                                         mb,
                                                         this->odd_ ? ODD : EVEN);

          // initiate more reads only if no error
          if (!result.error ())
            this->initiate_read_stream ();
          else
            ACE_TEST_ASSERT (0);
        }
    }
  else if (mb && !Receiver::writer_)
    // no one to write to
    free_chunks_chain (mb);

  --this->io_count_;

  this->check_destroy ();
}
Ejemplo n.º 25
0
void
JAWS_Asynch_IO::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
{
  // This callback is for this->receive_file()
  if (result.act () != 0)
    {
      int code = 0;
      if (result.success () && result.bytes_transferred () != 0)
        {
          if (result.message_block ().length () == result.message_block ().size ())
            code = ACE_Filecache_Handle::ACE_SUCCESS;
          else
            {
              ACE_Asynch_Read_Stream ar;
              if (ar.open (*this, this->handle_) == -1
                  || ar.read (result.message_block (),
                              result.message_block ().size () - result.message_block ().length (),
                              result.act ()) == -1)
                code = -1;
              else
                return;
            }
        }
      else
        code = -1;

      if (code == ACE_Filecache_Handle::ACE_SUCCESS)
        this->handler_->receive_file_complete ();
      else
        this->handler_->receive_file_error (code);

      delete &result.message_block ();
      delete (ACE_Filecache_Handle *) result.act ();
    }
  else
    {
      // This callback is for this->read()
      if (result.success ()
          && result.bytes_transferred () != 0)
        this->handler_->read_complete (result.message_block ());
      else
        this->handler_->read_error ();
    }
}
Ejemplo n.º 26
0
void
Client_Service_Handler::handle_read_stream (
  const ACE_Asynch_Read_Stream::Result &result)
{
  ACE_GUARD (ACE_SYNCH_RECURSIVE_MUTEX, guard, this->mtx_);

  this->pending_reads_--;

  if (!result.success () || 0 == result.bytes_transferred ())
  {
    // Error or remote disconnect

    result.message_block ().release ();

    if (!this->closing_)
    {
      // No error message when shutting down

      if (!result.success ())
      {
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT (
          "Client_Service_Handler::handle_read_stream: error: %d\n"),
          result.error ()));
      }
      else
      {
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT (
          "Client_Service_Handler::handle_read_stream: remote disconnect\n")));
      }
    }

    this->read_completed_.signal ();

    this->cancel_and_close ();
  }
  else if (result.bytes_transferred () < result.bytes_to_read ())
  {
    // More to read...

    if (this->read (result.message_block(),
      result.bytes_to_read () - result.bytes_transferred ()) < 0)
    {
      result.message_block ().release ();

      this->cancel_and_close ();
    }
  }
  else
  {
    // Read it all

    this->completed_reads_++;

    result.message_block ().release ();

    // We now have sent and received data in the proactor thread.  Signal the
    // main thread to try sending data in the main thread.
    if (this->completed_reads_ == 1)
    {
      this->ready_for_external_write_.signal ();
    }
    else
    {
      // The main thread wrote data that was echoed back to us on our
      // second read.  If we get here, the test was successful in that
      // the main thread successfully sent data to the server.
      this->read_successful_ = 1;
      this->read_completed_.signal ();
    }

    // Next read
    if (this->read_data () < 0)
    {
      this->cancel_and_close ();
    }
  }
}
Ejemplo n.º 27
0
void
Server_Service_Handler::handle_read_stream(
  const ACE_Asynch_Read_Stream::Result &result)
{
  ACE_GUARD (ACE_SYNCH_RECURSIVE_MUTEX, guard, this->mtx_);

  this->pending_reads_--;

  if (!result.success () || 0 == result.bytes_transferred ())
  {
    // Error or remote disconnect

    result.message_block ().release ();

    if (!this->closing_)
    {
      // No error message when shutting down

      if (!result.success ())
      {
        ACE_DEBUG ((LM_DEBUG,
          ACE_TEXT ("Server_Service_Handler::handle_read_stream: error: %d\n"),
          result.error ()));
      }
      else
      {
        ACE_DEBUG ((LM_DEBUG,
          ACE_TEXT ("Server_Service_Handler::handle_read_stream: remote disconnect\n")));
      }
    }

    this->cancel_and_close ();
  }
  else if (result.bytes_transferred () < result.bytes_to_read ())
  {
    // More to read...

    if (this->read (result.message_block (),
      result.bytes_to_read () - result.bytes_transferred ()) < 0)
    {
      result.message_block ().release ();

      this->cancel_and_close ();
    }
  }
  else
  {
    // Read it all

    result.message_block ().release ();

    // Send data back

    if (this->write_data () < 0)
    {
      this->cancel_and_close ();
    }

    // Next read

    else if (this->read_data () < 0)
    {
      this->cancel_and_close ();
    }
  }
}