ACE 前摄式Acceptor

来源:互联网 发布:大学生网络购物论文 编辑:程序博客网 时间:2024/05/16 04:42


// --------------------------------------------------------------
// 
// Copyright (C) 2008 - All Rights Reserved
// 
// Author:  LiuYin
// File:  AsynchAcceptor
// Version:  1.0
// Date:   2008-4-9
// 
// Purpose: 
// 
// --------------------------------------------------------------

#ifndef AsynchAcceptor_H
#define AsynchAcceptor_H

//////////////////////////////////////////////////////////////////////////

#include <ACE/Atomic_Op.h>
#include <ACE/Asynch_Acceptor.h>

//////////////////////////////////////////////////////////////////////////

template <class Handler>
class CAsynchAcceptor : public ACE_Asynch_Acceptor<Handler>
{
 typedef ACE_Asynch_Acceptor<Handler> Super;

 enum {
  SleepSeconds = 0,
  SleepMicroseconds = 4000,
 };
public:
 CAsynchAcceptor()
  : initial_accepts_count_(0)
 {
 }
 ~CAsynchAcceptor()
 {
  if (this->get_handle() != ACE_INVALID_HANDLE) {
   ACE_OS::closesocket(this->get_handle());
   this->set_handle(ACE_INVALID_HANDLE);
  }

  while (initial_accepts_count_ > 0) {
   ACE_OS::sleep(ACE_Time_Value(SleepSeconds, SleepMicroseconds));
  }
 }

 int accept(size_t bytes_to_read = 0, const void *act = 0)
 {
  int ret = Super::accept(bytes_to_read, act);
  initial_accepts_count_ += (ret == 0) ? 1 : 0;
  return ret;
 }

protected:
 void handle_accept(const ACE_Asynch_Accept::Result &result)
 {
  Super::handle_accept(result);

  --initial_accepts_count_;
 }

private:
 ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long> initial_accepts_count_;
};

//////////////////////////////////////////////////////////////////////////

#endif

原创粉丝点击