[ACE系列] ACE_Proactor简介

来源:互联网 发布:淘宝外宣怎么宣传 编辑:程序博客网 时间:2024/05/22 01:31

ACE_Proactor

OS 平台支持异步操作时,一种高效而方便的实现高性能 Web 服务器的方法是使用前摄式事件分派。使用前摄式事件分派模型设计的 Web 服务器通过一或多个线程控制来处理异步操作的完成。这样, 通过集成完成事件多路分离(completion event demultiplexing)和事件处理器分派,前摄器模式简化了异步的 Web 服务器。

当应用调用异步操作时,OS 代表应用执行此操作。这使得应用可以让多个操作同时运行,而又不需要应用拥有相应数目的线程。因此,通过使用更少的线程和有效利用 OS 对异步操作的支持,前摄器模式简化了并发编程,并改善了性能。

AC_Proactor中,所有的数据传输都采用Block_Messagemb是异步读写接口的固定参数,并且被封装在回调接口的Result固定参数中。

ACE_Asynch_Read_Streamreader_; 异步读  接口read()

Int read (ACE_Message_Block &message_block,size_t num_bytes_to_read, const void *act=0, int priority=0, intsignal_number=ACE_SIGRTMIN)


This starts off anasynchronous read. Upto <bytes_to_read> will beread and stored in the <message_block>. <message_block>'s <wr_ptr> will be updated to reflect the addedbytes if the read operation is successful completed. Priority of the operationis specified by <priority>. On POSIX4-Unix, this is supported. Works like<nice> in Unix. Negative values are not allowed. 0 means priority of theoperation same as the process priority. 1 means priority of the operation isone less than process. And so forth. On Win32, <priority> is a no-op.<signal_number> is the POSIX4 real-time signal number to be used for theoperation. <signal_number> ranges from ACE_SIGRTMIN to ACE_SIGRTMAX. Thisargument is a no-op on non-POSIX4 systems.


注意:read的数据不可能超过bytes_to_read,读取的数据,将从mbwr_ptr开始写到mb中,同时wr_ptr将会被更新。mb仅仅是普通指针的封装,实际也是new空间,如果想要复用mb,只需正确的改变wr_ptrrd_ptr即可。Read本身并不提供阻塞读功能,在ProactorReactor中使用阻塞读写是不好的,会极大降低性能。非阻塞读,需要程序员自己读写过程中的逻辑。Read一次后,就会返回,回调read完成接口。

mb提供很多接口,常用的有:space() (剩余可用空间),length()(可用数据,wr_ptr - rd_ptr, rd_ptr(), wr_ptr(). 如果要设置rd_ptrwr_ptr,可采用使用mb.wr_ptr(mb.base())(设置内存起始地址);mb.wr_ptr(1024*194)(指针要偏移的字节数);的办法

Write异步写:提供多少数据就一定会一次写多少数据才回调write_stream bytes_to_write仅仅是写的最大数据量,未必每次都写这么多,除非提供的数据足够。数据可能会分成多次进行网络传输,但只有一次回调。对于提供的数据量,是指存放在mb中的数据,write将从rd_ptr开始,最多写length()的数据(也就是到wr_ptr为止)要写的数据。如果设定bytes_to_write 大于length()那么write将在写完数据之后返回;如果设定bytes_to_write小于length(),那么write将在写完bytes_to_write数据之后返回

ACE_Asynch_Write_Streamwriter_; 异步写  接口write()。类似read

 

原创粉丝点击