sciop_acceptor.cpp

来自「这是广泛使用的通信开源项目,对于大容量,高并发的通讯要求完全能够胜任,他广泛可用」· C++ 代码 · 共 1,132 行 · 第 1/3 页

CPP
1,132
字号
#include "SCIOP_Acceptor.h"
#include "SCIOP_Profile.h"

#if TAO_HAS_SCIOP == 1

#include "tao/MProfile.h"
#include "tao/debug.h"
#include "tao/Protocols_Hooks.h"
#include "tao/Codeset_Manager.h"
#include "tao/Transport.h"

#if !defined(__ACE_INLINE__)
#include "SCIOP_Acceptor.i"
#endif /* __ACE_INLINE__ */

ACE_RCSID(tao,
          SCIOP_Acceptor,
          "SCIOP_Acceptor.cpp,v 1.5 2003/10/31 19:54:45 gthaker Exp")

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)

template class ACE_Auto_Basic_Array_Ptr<ACE_INET_Addr>;
template class ACE_Acceptor<TAO_SCIOP_Connection_Handler, ACE_SOCK_SEQPACK_ACCEPTOR>;
template class ACE_Strategy_Acceptor<TAO_SCIOP_Connection_Handler, ACE_SOCK_SEQPACK_ACCEPTOR>;
template class ACE_Accept_Strategy<TAO_SCIOP_Connection_Handler, ACE_SOCK_SEQPACK_ACCEPTOR>;
template class ACE_Creation_Strategy<TAO_SCIOP_Connection_Handler>;
template class ACE_Concurrency_Strategy<TAO_SCIOP_Connection_Handler>;
template class ACE_Scheduling_Strategy<TAO_SCIOP_Connection_Handler>;
template class TAO_Creation_Strategy<TAO_SCIOP_Connection_Handler>;
template class TAO_Concurrency_Strategy<TAO_SCIOP_Connection_Handler>;
template class TAO_Accept_Strategy<TAO_SCIOP_Connection_Handler, ACE_SOCK_SEQPACK_ACCEPTOR>;

#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)

#pragma instantiate ACE_Auto_Basic_Array_Ptr<ACE_INET_Addr>
#pragma instantiate ACE_Acceptor<TAO_SCIOP_Connection_Handler, ACE_SOCK_SEQPACK_ACCEPTOR>
#pragma instantiate ACE_Strategy_Acceptor<TAO_SCIOP_Connection_Handler, ACE_SOCK_SEQPACK_ACCEPTOR>
#pragma instantiate ACE_Accept_Strategy<TAO_SCIOP_Connection_Handler, ACE_SOCK_SEQPACK_ACCEPTOR>
#pragma instantiate ACE_Creation_Strategy<TAO_SCIOP_Connection_Handler>
#pragma instantiate ACE_Concurrency_Strategy<TAO_SCIOP_Connection_Handler>
#pragma instantiate ACE_Scheduling_Strategy<TAO_SCIOP_Connection_Handler>
#pragma instantiate TAO_Creation_Strategy<TAO_SCIOP_Connection_Handler>
#pragma instantiate TAO_Concurrency_Strategy<TAO_SCIOP_Connection_Handler>
#pragma instantiate TAO_Accept_Strategy<TAO_SCIOP_Connection_Handler, ACE_SOCK_SEQPACK_ACCEPTOR>

#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */

TAO_SCIOP_Acceptor::TAO_SCIOP_Acceptor (CORBA::Boolean flag)
  : TAO_Acceptor (TAO_TAG_SCIOP_PROFILE),
    addrs_ (0),
    port_span_ (1),
    hosts_ (0),
    endpoint_count_ (0),
    hostname_in_ior_ (0),
    version_ (TAO_DEF_SCIOP_MAJOR, TAO_DEF_SCIOP_MINOR),
    orb_core_ (0),
    lite_flag_ (flag),
    base_acceptor_ (),
    creation_strategy_ (0),
    concurrency_strategy_ (0),
    accept_strategy_ (0)
{
}

TAO_SCIOP_Acceptor::~TAO_SCIOP_Acceptor (void)
{
  // Make sure we are closed before we start destroying the
  // strategies.
  this->close ();

  delete this->creation_strategy_;
  delete this->concurrency_strategy_;
  delete this->accept_strategy_;

  delete [] this->addrs_;

  for (CORBA::ULong i = 0; i < this->endpoint_count_; ++i)
    CORBA::string_free (this->hosts_[i]);

  delete [] this->hosts_;
}

// TODO =
//    2) For V1.[1,2] there are tagged components
int
TAO_SCIOP_Acceptor::create_profile (const TAO::ObjectKey &object_key,
                                   TAO_MProfile &mprofile,
                                   CORBA::Short priority)
{
  // Sanity check.
  if (this->endpoint_count_ == 0)
    return -1;

  // Check if multiple endpoints should be put in one profile or
  // if they should be spread across multiple profiles.
  /*
  if (priority == TAO_INVALID_PRIORITY)
    return this->create_new_profile (object_key,
                                     mprofile,
                                     priority);
  else
  */
    return this->create_shared_profile (object_key,
                                        mprofile,
                                        priority);
}

int
TAO_SCIOP_Acceptor::create_new_profile (const TAO::ObjectKey &object_key,
                                       TAO_MProfile &mprofile,
                                       CORBA::Short priority)
{
  // Adding this->endpoint_count_ to the TAO_MProfile.
  int count = mprofile.profile_count ();
  if ((mprofile.size () - count) < this->endpoint_count_
      && mprofile.grow (count + this->endpoint_count_) == -1)
    return -1;

  // Create a profile for each acceptor endpoint.
  for (CORBA::ULong i = 0; i < this->endpoint_count_; ++i)
    {
      TAO_SCIOP_Profile *pfile = 0;
      ACE_NEW_RETURN (pfile,
                      TAO_SCIOP_Profile (this->hosts_[i],
                                        this->addrs_[i].get_port_number (),
                                        object_key,
                                        this->addrs_[i],
                                        this->version_,
                                        this->orb_core_),
                      -1);
      pfile->endpoint ()->priority (priority);

      if (mprofile.give_profile (pfile) == -1)
        {
          pfile->_decr_refcnt ();
          pfile = 0;
          return -1;
        }

      pfile->tagged_components ().set_orb_type (TAO_ORB_TYPE);

      this->orb_core_->codeset_manager()->
        set_codeset(pfile->tagged_components());
    }

  return 0;
}

int
TAO_SCIOP_Acceptor::create_shared_profile (const TAO::ObjectKey &object_key,
                                          TAO_MProfile &mprofile,
                                          CORBA::Short priority)
{
  CORBA::ULong index = 0;
  TAO_SCIOP_Profile *sciop_profile = 0;

  // Do not check <mprofile> for the presence of an existing
  // SCIOP_Profile.  With SCIOP, there is a one-to-one relationship
  // between Acceptors and Profiles.
    {
      ACE_NEW_RETURN (sciop_profile,
                      TAO_SCIOP_Profile (this->hosts_[0],
                                        this->addrs_[0].get_port_number (),
                                        object_key,
                                        this->addrs_[0],
                                        this->version_,
                                        this->orb_core_),
                      -1);
      sciop_profile->endpoint ()->priority (priority);

      if (mprofile.give_profile (sciop_profile) == -1)
        {
          sciop_profile->_decr_refcnt ();
          sciop_profile = 0;
          return -1;
        }

      sciop_profile->tagged_components ().set_orb_type (TAO_ORB_TYPE);

      this->orb_core_->codeset_manager()->
        set_codeset(sciop_profile->tagged_components());

      index = 1;
    }

  // Add any remaining acceptor endpoints to the SCIOP_Profile.
  for (;
       index < this->endpoint_count_;
       ++index)
    {
      TAO_SCIOP_Endpoint *endpoint = 0;
      ACE_NEW_RETURN (endpoint,
                      TAO_SCIOP_Endpoint (this->hosts_[index],
                                         this->addrs_[index].get_port_number (),
                                         this->addrs_[index]),
                      -1);
      endpoint->priority (priority);
      sciop_profile->add_endpoint (endpoint);
    }

  return 0;
}

int
TAO_SCIOP_Acceptor::is_collocated (const TAO_Endpoint *endpoint)
{
  const TAO_SCIOP_Endpoint *endp =
    ACE_dynamic_cast (const TAO_SCIOP_Endpoint *, endpoint);

  // Make sure the dynamically cast pointer is valid.
  if (endp == 0)
    return 0;

  for (CORBA::ULong i = 0; i < this->endpoint_count_; ++i)
    {
      // compare the port and host name.  Please do *NOT* optimize
      // this code by comparing the IP address instead.  That would
      // trigger the following bug:
      //
      // http://deuce.doc.wustl.edu/bugzilla/show_bug.cgi?id=1220
      //
      if (endp->port() == this->addrs_[i].get_port_number()
          && ACE_OS::strcmp(endp->host(), this->hosts_[i]) == 0)
        return 1;
    }

  return 0;
}

int
TAO_SCIOP_Acceptor::close (void)
{
  return this->base_acceptor_.close ();
}

int
TAO_SCIOP_Acceptor::open (TAO_ORB_Core *orb_core,
                         ACE_Reactor *reactor,
                         int major,
                         int minor,
                         const char *address,
                         const char *options)
{

  this->orb_core_ = orb_core;

  if (this->init_tcp_properties () != 0)
    return -1;

  if (this->hosts_ != 0)
    {
      // The hostname cache has already been set!
      // This is bad mojo, i.e. an internal TAO error.
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("TAO (%P|%t) - ")
                         ACE_TEXT ("SCIOP_Acceptor::open, "),
                         ACE_TEXT ("hostname already set\n\n")),
                        -1);
    }

  if (address == 0)
    return -1;

  ACE_UNUSED_ARG (major);
  ACE_UNUSED_ARG (minor);

  // Parse options
  if (this->parse_options (options) == -1)
    return -1;

  ACE_Multihomed_INET_Addr addr;

  const char *port_separator_loc = ACE_OS::strchr (address, ':');
  ACE_Auto_Basic_Array_Ptr<char> tmp_host_auto;

  if (port_separator_loc == address)
    {
      // The address is a port number or port name.  No hostname was
      // specified.  The hostname for each network interface and the
      // fully qualified domain name must be obtained.

      // Check for multiple network interfaces.
      if (this->probe_interfaces (orb_core) == -1)
        return -1;

      // First convert the port into a usable form.
      ACE_INET_Addr temp_addr;
      if (temp_addr.set (address + sizeof (':')) != 0)
        return -1;

      // Now reset the port and set the host.
      if (addr.set (temp_addr.get_port_number (),
                    ACE_static_cast (ACE_UINT32, INADDR_ANY),
                    1) != 0)
        return -1;
      else
        return this->open_i (addr,
                             reactor);
    }

  // If we've reached this point, then the address consists of one or
  // more hostnames, followed perhaps by a port.

  u_short port_number = 0;
  char *tmp_host = 0;
  size_t hostname_length = 0;

  if (port_separator_loc != 0) {

    // Port separator was found.  Check that the next character is
    // not the terminator.
    const char *port_loc = port_separator_loc;
    ++port_loc;
    if (port_loc == 0) {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("TAO (%P|%t) ")
                         ACE_TEXT ("SCIOP_Acceptor::open - ")
                         ACE_TEXT ("no port number after the ")
                         ACE_TEXT ("colon in \"%s\"\n"),
                         address),
                        -1);
    }

    // Read the port number
    ACE_INET_Addr temp_addr;
    if (temp_addr.string_to_addr(port_loc) != 0)
      return -1;
    port_number = temp_addr.get_port_number();

    // Set the length of the hostname
    hostname_length = port_separator_loc - address;

  } else {

    // Port separator was not found.  We allow port_number to retain
    // the value of 0, which will cause the port to be chosen for us
    // in open_i.

    // Set the length of the hostname
    hostname_length = ACE_OS::strlen(address);
  }

  ACE_NEW_RETURN(tmp_host, char[hostname_length + 1], -1);
  tmp_host_auto.reset(tmp_host);
  ACE_OS::memcpy (tmp_host, address, hostname_length);
  tmp_host[hostname_length] = '\0';

  // There may be multiple hostnames.  Parse them.
  ACE_Array<ACE_CString> hostnames;
  if (parse_multiple_hostnames(tmp_host, hostnames) != 0)
    return -1;

  // Check that at least one hostname was obtained.
  if (hostnames.size() < 1) {
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("TAO (%P|%t) ")
                       ACE_TEXT ("SCIOP_Acceptor::open - ")
                       ACE_TEXT ("no hostnames in string \"%s\"\n"),
                       tmp_host),
                      -1);
  }

  // Obtain the primary ip address.
  ACE_UINT32 primary_ip_addr = 0;
  {
    // Obtain a char* for the primary hostname.
    ACE_CString & primary_hostname_obj = hostnames[0];
    ACE_Auto_Basic_Array_Ptr<char> primary_hostname_auto(primary_hostname_obj.rep());
    const char* primary_hostname = primary_hostname_auto.get();

    // Convert the primary hostname to ACE_UINT32
    ACE_INET_Addr temp_addr;
    if (temp_addr.set((u_short) 0, primary_hostname) != 0)
      return -1;

    primary_ip_addr = temp_addr.get_ip_address();
  }

⌨️ 快捷键说明

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