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

📄 protocol.h

📁 ACE编程的一本经典BIBLE的源代码,喜欢网络编程的别错过
💻 H
📖 第 1 页 / 共 2 页
字号:
// file      : ace/RMCast/Protocol.h
// author    : Boris Kolpackov <boris@kolpackov.net>
// cvs-id    : Protocol.h,v 1.23 2006/03/01 23:48:42 shuston Exp

#ifndef ACE_RMCAST_PROTOCOL_H
#define ACE_RMCAST_PROTOCOL_H

#include "ace/Refcounted_Auto_Ptr.h"

#include "ace/Vector_T.h"
#include "ace/Hash_Map_Manager.h"

#include "ace/CDR_Stream.h"
#include "ace/CDR_Size.h"

#include "ace/INET_Addr.h"
#include "ace/Null_Mutex.h"

#include "ace/OS_NS_string.h"
#include "ace/OS_NS_stdlib.h"

#include "Bits.h"

/*
#include <iostream>
*/

namespace ACE_RMCast
{
  // Basic types.
  //
  typedef ACE_CDR::UShort u16;
  typedef ACE_CDR::ULong u32;
  typedef ACE_CDR::ULongLong u64;

  // Protocol parameters
  //
  //
  unsigned short const max_service_size = 60;  // service profiles (Part, SN,
                                               // etc), sizes plus message size.

  //
  //
  //
  typedef ACE_INET_Addr Address;

  struct AddressHasher
  {
    unsigned long
    operator() (Address const& a) const
    {
      unsigned long port (a.get_port_number ());
      unsigned long ip (a.get_ip_address ());

      port <<= sizeof (unsigned long) - sizeof (unsigned short);

      return port ^ ip;
    }
  };

  //@@ Provide stream<< (Address const&)
  //

  typedef ACE_OutputCDR ostream;
  typedef ACE_SizeCDR sstream;
  typedef ACE_InputCDR istream;

  struct Profile;

  typedef
  ACE_Refcounted_Auto_Ptr<Profile, Mutex>
  Profile_ptr;

  struct Profile
  {
  public:
    class Header
    {
    public:
      Header (u16 id, u16 size)
          : id_ (id), size_ (size)
      {
      }

      Header (istream& is)
      {
        (void) (is >> id_ >> size_);
      }

    public:
      u16
      id () const
      {
        return id_;
      }

      u16
      size () const
      {
        return size_;
      }

    protected:
      void
      size (u16 s)
      {
        size_ = s;
      }

      friend struct Profile;

    private:
      u16 id_;
      u16 size_;
    };

  public:
    virtual
    ~Profile ()
    {
    }

    Profile_ptr
    clone ()
    {
      return clone_ ();
    }

  protected:
    Profile (u16 id)
        : header_ (id, 0)
    {
    }

    Profile (Header const& h)
        : header_ (h)
    {
    }

    virtual Profile_ptr
    clone_ () = 0;

  private:
    Profile&
    operator= (Profile const&);

  public:
    u16
    id () const
    {
      return header_.id ();
    }

    u16
    size () const
    {
      return header_.size ();
    }

  protected:
    void
    size (u16 s)
    {
      header_.size (s);
    }

    u16
    calculate_size ()
    {
      sstream ss;

      serialize_body (ss);

      return static_cast<u16> (ss.total_length ());
    }

  public:
    virtual void
    serialize_body (ostream&) const = 0;

    virtual void
    serialize_body (sstream&) const = 0;

    friend
    ostream&
    operator<< (ostream& os, Profile const& p);

    friend
    sstream&
    operator<< (sstream& ss, Profile const& p);

  private:
    Header header_;
  };

  inline
  ostream&
  operator<< (ostream& os, Profile::Header const& hdr)
  {
    os << hdr.id ();
    os << hdr.size ();

    return os;
  }

  inline
  sstream&
  operator<< (sstream& ss, Profile::Header const& hdr)
  {
    ss << hdr.id ();
    ss << hdr.size ();

    return ss;
  }

  inline
  ostream&
  operator<< (ostream& os, Profile const& p)
  {
    os << p.header_;
    p.serialize_body (os);

    return os;
  }

  inline
  sstream&
  operator<< (sstream& ss, Profile const& p)
  {
    ss << p.header_;
    p.serialize_body (ss);

    return ss;
  }


  //
  //
  //
  class Message;

  typedef
  ACE_Refcounted_Auto_Ptr<Message, Mutex>
  Message_ptr;

  class Message
  {
    typedef
    ACE_Hash_Map_Manager<u16, Profile_ptr, ACE_Null_Mutex>
    Profiles;

  public:
    Message ()
        : profiles_ (4)
    {
    }

    Message_ptr
    clone ()
    {
      return new Message (*this);
    }

  protected:
    Message (Message const& m)
        : profiles_ (4)
    {
      for (Profiles::const_iterator i (m.profiles_); !i.done (); i.advance ())
      {
        // Shallow copy of profiles. This implies that profiles are not
        // modified as they go up/down the stack.
        //
        profiles_.bind ((*i).ext_id_, (*i).int_id_);
      }
    }

  private:
    Message&
    operator= (Message const&);

  public:
    bool
    add (Profile_ptr p)
    {
      u16 id (p->id ());

      if (profiles_.find (id) == 0)
      {
        return false;
      }

      profiles_.bind (id, p);

      return true;
    }

    void
    replace (Profile_ptr p)
    {
      profiles_.rebind (p->id (), p);
    }

    void
    remove (u16 id)
    {
      profiles_.unbind (id);
    }

    Profile const*
    find (u16 id) const
    {
      Profiles::ENTRY* e = 0;

      if (profiles_.find (id, e) == -1) return 0;

      return e->int_id_.get ();
    }

    typedef
    Profiles::const_iterator
    ProfileIterator;

    ProfileIterator
    begin () const
    {
      return ProfileIterator (profiles_);
    }

  public:
    size_t
    size () const
    {
      sstream ss;

      u32 s (0);

      ss << s;

      for (Profiles::const_iterator i (profiles_); !i.done (); i.advance ())
      {
        ss << *((*i).int_id_);
      }

      return ss.total_length ();
    }

    friend
    ostream&
    operator<< (ostream& os, Message const& m)
    {
      u32 s (m.size ());

      os << s;

      for (Profiles::const_iterator i (m.profiles_); !i.done (); i.advance ())
      {
        os << *((*i).int_id_);
      }

      return os;
    }

  private:
    Profiles profiles_;
  };

  typedef ACE_Vector<Message_ptr, ACE_VECTOR_DEFAULT_SIZE> Messages;

  //
  //
  //
  struct From;

  typedef
  ACE_Refcounted_Auto_Ptr<From, Mutex>
  From_ptr;

  struct From : Profile
  {
    static u16 const id;

  public:
    From (Header const& h, istream& is)
        : Profile (h)
    {
      u32 addr;
      u16 port;

      is >> addr;
      is >> port;

      address_ = Address (port, addr);
    }

    From (Address const& addr)
        : Profile (id), address_ (addr)
    {
      size (calculate_size ());
    }

    From_ptr
    clone ()
    {
      return From_ptr (static_cast<From*> (clone_ ().release ()));
    }

  protected:
    virtual Profile_ptr
    clone_ ()
    {
      return new From (*this);
    }

    From (From const& from)
        : Profile (from),
          address_ (from.address_)
    {
    }

  public:
    Address const&
    address () const
    {
      return address_;
    }

  public:
    virtual void
    serialize_body (ostream& os) const
    {
      u32 addr (address_.get_ip_address ());
      u16 port (address_.get_port_number ());

      os << addr;
      os << port;
    }

    virtual void
    serialize_body (sstream& ss) const
    {
      u32 addr (0);
      u16 port (0);

      ss << addr;
      ss << port;
    }

  private:
    Address address_;
  };


  //
  //
  //
  struct To;

  typedef
  ACE_Refcounted_Auto_Ptr<To, Mutex>
  To_ptr;

  struct To : Profile
  {
    static u16 const id;

  public:
    To (Header const& h, istream& is)
        : Profile (h)
    {
      u32 addr;
      u16 port;

      is >> addr;
      is >> port;

      address_ = Address (port, addr);
    }

    To (Address const& addr)
        : Profile (id), address_ (addr)
    {
      size (calculate_size ());
    }

    To_ptr
    clone ()
    {
      return To_ptr (static_cast<To*> (clone_ ().release ()));
    }

  protected:
    virtual Profile_ptr
    clone_ ()
    {
      return new To (*this);
    }

    To (To const& to)
        : Profile (to),
          address_ (to.address_)
    {
    }

  public:
    Address const&
    address () const
    {
      return address_;
    }

  public:
    virtual void
    serialize_body (ostream& os) const
    {
      u32 addr (address_.get_ip_address ());
      u16 port (address_.get_port_number ());

      os << addr;
      os << port;
    }

    virtual void
    serialize_body (sstream& ss) const
    {
      u32 addr (0);
      u16 port (0);

      ss << addr;
      ss << port;
    }

  private:
    Address address_;
  };


  //
  //
  //
  struct Data;

  typedef
  ACE_Refcounted_Auto_Ptr<Data, Mutex>
  Data_ptr;

  struct Data : Profile
  {
    static u16 const id;

  public:
    virtual
    ~Data ()
    {
      if (buf_)
        operator delete (buf_);
    }

    Data (Header const& h, istream& is)
        : Profile (h),
          buf_ (0),
          size_ (h.size ()),
          capacity_ (size_)
    {
      if (size_)
      {
        buf_ = reinterpret_cast<char*> (operator new (capacity_));
        is.read_char_array (buf_, size_);
      }
    }

    Data (void const* buf, size_t s, size_t capacity = 0)
        : Profile (id),
          buf_ (0),
          size_ (s),
          capacity_ (capacity < size_ ? size_ : capacity)
    {
      if (size_)
      {
        buf_ = reinterpret_cast<char*> (operator new (capacity_));
        ACE_OS::memcpy (buf_, buf, size_);
      }

      Profile::size (calculate_size ());
    }

    Data_ptr
    clone ()
    {
      return Data_ptr (static_cast<Data*> (clone_ ().release ()));
    }

  protected:
    virtual Profile_ptr
    clone_ ()
    {
      return new Data (*this);
    }

    Data (Data const& d)
        : Profile (d),
          buf_ (0),
          size_ (d.size_),
          capacity_ (d.capacity_)
    {
      if (size_)
      {
        buf_ = reinterpret_cast<char*> (operator new (capacity_));
        ACE_OS::memcpy (buf_, d.buf_, size_);
      }

      Profile::size (calculate_size ());
    }

  public:
    char const*
    buf () const
    {
      return buf_;
    }

    char*
    buf ()
    {
      return buf_;
    }

    size_t
    size () const
    {
      return size_;
    }

    void
    size (size_t s)
    {
      if (s > capacity_)
        ACE_OS::abort ();

⌨️ 快捷键说明

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