ACE之旅的开始问题

                              ACE之旅的开始问题
     最近闲来,没什么事情做,就来研究ACE。刚开始,接触ACE遇到一些莫名其妙的问题。
以下是我碰到的问题,以及解决方法:
问题一:调用ACE::select(int,ACE_Handle_Set) 返回-1。
调 ACE_ERROR_RETURN((LM_DEBUG,"%p/n","ACE::select:"),-1);
输出:app version not supported by dll.
刚开始一看到这个输出,我一直都找不到问题的所在。一直在网上搜索问题的解决方法,发邮件给steve寻求解决方法。
他给我的回复:he second issue could be that you're using a version of Windows or Winsock that's too old or            your  ACE configuration is not correct.
       对于他的回复,我感觉一雾水,后来去看源代码。
       发现app version not supported by dll 的错误是 WSAEMFILE。
     而这个错误WSAEMFILE在msdn上的描述: Invalid argument.
    Some invalid argument was supplied (for example, specifying an invalid level to the setsockopt function).     In some instances, it also refers to the current state of the socket—for instance, calling accept on a        socket that is not listening 。
     后来我才发现,我再调 ACE::select之前,ACE_Handle_Set 对象里面没有句柄。
我提这个问题的一个提示:
在遇到莫名其妙的问题时候,先检查自己的代码是否有问题!
很多时候,我们一看到莫名其妙的问题时候,心理就发毛,一直寻找答案。
2:第二个问题:在ACE 提供的例子C++NPv1_Reactive_Logging_Server_Ex_vc8.vcproj中的代码:
virtual int handle_connections () { ACE_SOCK_Stream logging_peer; while (acceptor ().accept (logging_peer) != -1) { ACE_FILE_IO *log_file = new ACE_FILE_IO; // Use the client's hostname as the logfile name. make_log_file (*log_file, &logging_peer); // Add the new <logging_peer>'s handle to the map and // to the set of handles we <select> for input. log_map_.bind (logging_peer.get_handle (), log_file); master_handle_set_.set_bit (logging_peer.get_handle ()); } active_read_handles_.clr_bit (acceptor ().get_handle ()); return 0; } virtual int handle_data (ACE_SOCK_Stream *) { ACE_Handle_Set_Iterator peer_iterator (active_read_handles_); for (ACE_HANDLE handle; (handle = peer_iterator ()) != ACE_INVALID_HANDLE; ) { ACE_FILE_IO *log_file = 0; log_map_.find (handle, log_file); Logging_Handler logging_handler (*log_file, handle); if (logging_handler.log_record () == -1) { logging_handler.close (); master_handle_set_.clr_bit (handle); log_map_.unbind (handle); log_file->close (); delete log_file; } } return 0; }
程序运行到handle_data有时候会崩溃,原因如果当前handle 是监听的那个句柄。这时log_file 还是为0.
因此对log_file进行访问,会使程序崩溃。
解决方法在Logging_Handler logging_handler (*log_file, handle)之前加:
if(log_file == 0)
continue;
3:在该例子中的函数:
int Logging_Handler::write_log_record (ACE_Message_Block *mblk) { // Peer hostname is in the <mblk> and the log record data // is in its continuation. if (log_file_.send_n (mblk) == -1) return -1; if (ACE::debug ()) { // Build a CDR stream from the log record data. ACE_InputCDR cdr (mblk->cont ()); ACE_CDR::Boolean byte_order; ACE_CDR::ULong length; // Extract the byte-order and length, ending up at the start // of the log record itself. Use the byte order to properly // set the CDR stream for extracting the contents. cdr >> ACE_InputCDR::to_boolean (byte_order); cdr.reset_byte_order (byte_order); cdr >> length; ACE_Log_Record log_record; cdr >> log_record; // Finally extract the <ACE_log_record>. log_record.print (mblk->rd_ptr (), 1, cerr); } return mblk->total_length (); }
输出到文件的内容有乱码,一个简单的解析方法:
 int Logging_Handler::write_log_record(ACE_Message_Block* mblk) { std::ostringstream os; ACE_InputCDR cdr(mblk->cont()); ACE_CDR::Boolean byte_order; cdr >> ACE_InputCDR::to_boolean(byte_order); cdr.reset_byte_order(byte_order); ACE_CDR::ULong length; cdr >> length; ACE_Log_Record log_record; cdr >> log_record; log_record.print(mblk->rd_ptr(),1,os); os<<"/n"; _log_file.send(os.str().c_str(),os.str().length()); std::cout<<os.str()<<std::endl; return mblk->total_length(); }

4:调ACE_ERROR_RETURN((LM_DEBUG,"%s%p/n","The error is "),-1)有时程序会崩溃,有时不会。
如果在崩溃的时候,去掉%s,就不会崩溃。
但是调式,会在output.c的一个地方出现访问冲突。
在查看ACE_Log_Msg::log()方法介绍,其介绍const ACE_TCHAR* format_str跟printf类似。但没看到不能如例子所举的那样用法。

以上是我开始ACE之旅遇到的问题,及解决方法。
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值