⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpp-inserver-fancy.cpp

📁 这是广泛使用的通信开源项目,对于大容量,高并发的通讯要求完全能够胜任,他广泛可用于网络游戏医学图像网关的高qos要求.更详细的内容可阅读相应的材料
💻 CPP
📖 第 1 页 / 共 2 页
字号:
 // CPP-inserver-fancy.cpp,v 4.24 2003/11/09 04:12:08 dhinton Exp

// This example tests the features of the <ACE_SOCK_Acceptor>,
// <ACE_SOCK_Stream>, and <ACE_Svc_Handler> classes.  If the platform
// supports threads it uses a thread-per-connection concurrency model.
// Otherwise, it uses a single-threaded iterative server model.

#include "ace/OS_main.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/Svc_Handler.h"
#include "ace/Singleton.h"
#include "ace/Profile_Timer.h"
#include "ace/Get_Opt.h"
#include "ace/OS_NS_sys_select.h"

#include "CPP-inserver-fancy.h"

ACE_RCSID(SOCK_SAP, CPP_inserver_fancy, "CPP-inserver-fancy.cpp,v 4.24 2003/11/09 04:12:08 dhinton Exp")

// Forward declaration.
class Handler;

class Handler_Factory
{
  // = TITLE
  //   Creates the oneway or twoway handlers.
public:
  Handler_Factory (void);
  // Constructor.

  ~Handler_Factory (void);
  // Destructor.

  int handle_events (void);
  // Run the main event loop.

private:
  int init_acceptors (void);
  // Initialize the acceptors.

  int create_handler (ACE_SOCK_Acceptor &acceptor,
                      Handler *(*handler_factory) (ACE_HANDLE),
                      const char *handler_type);
  // Factory that creates the right kind of <Handler>.

  // = Factory functions.
  static Handler *make_twoway_handler (ACE_HANDLE);
  // Create a twoway handler.

  static Handler *make_oneway_handler (ACE_HANDLE);
  // Create a oneway handler.

  ACE_SOCK_Acceptor twoway_acceptor_;
  // Twoway acceptor factory.

  ACE_SOCK_Acceptor oneway_acceptor_;
  // Oneway acceptor factory.
};

class Handler : public ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH>
{
  // = TITLE
  //   Base class for the oneway and twoway handlers.

  friend class Handler_Factory;
  // The factory has special permission. (to access svc ()).

public:
  virtual int open (void * = 0);
  // Generic initialization method.

  virtual int close (u_long);
  // Close down and delete this.

protected:
  Handler (ACE_HANDLE handle);
  // Constructor.

  int parse_header_and_allocate_buffer (char *&buf,
                                        ACE_INT32 *len);
  // Implement the generic code that's called from any of the subclass
  // <run> methods to get the header and the buffer to read the data.
  // This method factors out common code.

  virtual int run (void) = 0;
  // Hook method called by the <svc> template method to do the actual
  // protocol.  Must be overridden by the subclass.

  virtual int svc (void);
  // Template method entry point into the handler task.

  virtual void print_results (void);
  // Print the results.

  size_t total_bytes_;
  // Total number of bytes received.

  size_t message_count_;
  // Number of messages received.

  ACE_Profile_Timer timer_;
  // Keeps track of how much time we're using.
};

class Twoway_Handler : public Handler
{
  // = TITLE
  //   Performs the twoway protocol.
public:
  Twoway_Handler (ACE_HANDLE handle);
  // Constructor.

private:
  virtual int run (void);
  // Template Method hook called by <svc>.
};

class Oneway_Handler : public Handler
{
  // = TITLE
public:
  Oneway_Handler (ACE_HANDLE handle);
  // Constructor.

private:
  virtual int run (void);
  // Template Method hook called by <svc>.

  virtual void print_results (void);
  // Print the results.
};

u_short
Options::port (void) const
{
  return this->port_;
}

int
Options::verbose (void) const
{
  return this->verbose_;
}

int
Options::reply_message_len (void) const
{
  return this->reply_message_len_;
}

Options::~Options (void)
{
}

Options::Options (void)
  : verbose_ (0),
    port_ (ACE_DEFAULT_SERVER_PORT),
    reply_message_len_ (24) // Default to the approximate size of an
                            // GIOP reply message.
{
}

int
Options::parse_args (int argc, ACE_TCHAR *argv[])
{
  ACE_Get_Opt getopt (argc, argv, ACE_TEXT("p:r:v"), 1);

  for (int c; (c = getopt ()) != -1; )
    switch (c)
      {
      case 'p':
        this->port_ = ACE_OS::atoi (getopt.opt_arg ());
        break;
      case 'r':
        this->reply_message_len_ = ACE_OS::atoi (getopt.opt_arg ());
        break;
      case 'v':
        this->verbose_ = 1;
        break;
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "(%P|%t) usage: %n [-p <port>] [-v]"),
                          -1);
      }

  return 0;
}

// Options Singleton.
typedef ACE_Singleton<Options, ACE_SYNCH_RECURSIVE_MUTEX> OPTIONS;

Handler::Handler (ACE_HANDLE handle)
  : total_bytes_ (0),
    message_count_ (0)
{
  this->peer ().set_handle (handle);
}

int
Handler::open (void *)
{
  ACE_INET_Addr cli_addr;

  // Make sure we're not in non-blocking mode.
  if (this->peer ().disable (ACE_NONBLOCK) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "disable"),
                       0);

  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) client %s connected from %d on handle %d\n",
              cli_addr.get_host_name (),
              cli_addr.get_port_number (),
              this->peer ().get_handle ()));
  return 0;
}

int
Handler::close (u_long)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) closing down %x\n",
              this));
  delete this;
  return 0;
}

int
Handler::svc (void)
{
  // Timer logic.
  this->timer_.start ();

  // Invoke the hook method to run the specific test.
  int result = this->run ();

  this->timer_.stop ();

  this->print_results ();

  return result;
}

int
Handler::parse_header_and_allocate_buffer (char *&request,
                                           ACE_INT32 *len)
{
  ssize_t result = this->peer ().recv_n ((void *) len,
                                         sizeof (ACE_INT32));
  if (result == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "(%P|%t) connected closed\n"));
      return -1;
    }
  else if (result == -1 || result != sizeof (ACE_INT32))
    ACE_ERROR_RETURN ((LM_ERROR,
                       "(%P|%t) %p\n",
                       "recv_n failed"),
                      -1);
  else
    {
      *len = ntohl (*len);
      ACE_NEW_RETURN (request,
                      char[*len],
                      -1);
    }

  return 0;
}

void
Handler::print_results (void)
{
}

Twoway_Handler::Twoway_Handler (ACE_HANDLE handle)
  : Handler (handle)
{
}

// Function entry point into the twoway server task.

int
Twoway_Handler::run (void)
{
  // Read data from client (terminate on error).

  char *request = 0;

  for (;;)
    {
      ACE_INT32 len = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -