proactor和mfc的问题
在mfc下就连接不上,很郁闷啊。哪有mfc+proactor的东东
回复 #1 psycheqiqi 的帖子
在mfc的cpp里UINT ThreadReactor(LPVOID param)
{
ACE_INET_Addr listen_addr(3000);
ACE_Asynch_Acceptor<HA_Proactive_Service>aio_acceptor;
if (0 != aio_acceptor.open (listen_addr, 0, 0, 5, 1, 0, 0))
ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("acceptor open")), 1);
int result = ACE_Proactor::instance ()->proactor_run_event_loop ();
ACE_DEBUG ((LM_DEBUG, ACE_TEXT("Thread ended\n")));
return result;
}
void CProactorServerDlg::OnStart()
{
// TODO: Add your control notification handler code
AfxBeginThread(ThreadReactor,NULL);
}
HA_Proactor_Service.h里
class HA_Proactive_Service : public ACE_Service_Handler
{
public:
~HA_Proactive_Service ()
{
if (this->handle () != ACE_INVALID_HANDLE)
ACE_OS::closesocket (this->handle ());
}
virtual void open (ACE_HANDLE h, ACE_Message_Block&)
{
this->handle (h);
if (this->reader_.open (*this) != 0 )
{
ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
ACE_TEXT ("HA_Proactive_Service open")));
delete this;
return;
}
ACE_Message_Block *mb = new ACE_Message_Block(buffer,1024);
if (this->reader_.read (*mb, mb->space ()) != 0)
{
ACE_OS::printf("Begin read fail\n");
delete this;
return;
}
return;
}
//异步读完成后会调用此函数
virtual void handle_read_stream
(const ACE_Asynch_Read_Stream::Result &result)
{
ACE_Message_Block &mb = result.message_block ();
if (!result.success () || result.bytes_transferred () == 0)
{
mb.release ();
delete this;
return;
}
mb.copy(""); //为字符串添加结束标记'\0'
ACE_OS::printf("rev:\t%s\n",mb.rd_ptr());
mb.release();
ACE_Message_Block *nmb = new ACE_Message_Block(buffer,1024);
if (this->reader_.read (*nmb, nmb->space ()) != 0)
return;
}
private:
ACE_Asynch_Read_Stream reader_;
char buffer;
};
问题是在onstart里执行线程后,无法跳转到HA_Proactor_Service的open方法。
但在控制台下的运行是正确的。
回复 #2 psycheqiqi 的帖子
找到原因了,又忘记加 ACE::init();了唉!第二次了。 如何在mfc的cpp文件里,onsend函数中发送消息?(proactor框架)
程序和APG第八章的相似 问问题,要描述清楚。我们看贴的,都不知道你讲什么,怎么帮你解决呢?
回复 #5 winston 的帖子
定义的头文件class HA_Proactive_Service : public ACE_Service_Handler
{
public:
~HA_Proactive_Service ()
{
if (this->handle () != ACE_INVALID_HANDLE)
ACE_OS::closesocket (this->handle ());
}
virtual void open (ACE_HANDLE h, ACE_Message_Block&)
{ }
virtual void handle_read_stream(const ACE_Asynch_Read_Stream::Result &result)
{ }
virtual void handle_write_dgram(const ACE_Asynch_Write_Stream::Result &result)
{ }
private:
ACE_Asynch_Write_Stream writer_;
ACE_Asynch_Read_Stream reader_;
};
发送消息时可以在open中初始化message_block,
但是我现在用MFC做了个对话框
我想在DLG.cpp中的onsend函数中发送消息,不知道该如何发送
回复 #6 psycheqiqi 的帖子
怎么获得open函数中的handle? virtual ACE_HANDLEget_handle (void) constGet the underlying handle associated with the <peer_>.
virtual voidset_handle (ACE_HANDLE)
Set the underlying handle associated with the <peer_>.
ACE_PEER_STREAM &peer (void) const
回复 #8 winston 的帖子
我想在onsend里发送消息,可不可以不通过获取handle我直接定义
public:
ACE_Asynch_Write_Stream writer_;
ACE_Asynch_Read_Stream reader_;
然后在onsend()里
{
HA_Proactive_Service *ps;
ps->writer_.write(*mb,mb->length()) ;
}
但为什么会出现运行错误?这种思路不对吗?还是....
回复 #9 psycheqiqi 的帖子
知道是哪里出的问题了
页:
[1]