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

📄 protocol.h

📁 ACE编程的一本经典BIBLE的源代码,喜欢网络编程的别错过
💻 H
📖 第 1 页 / 共 2 页
字号:
      size_ = s;

      Profile::size (calculate_size ());
    }

    size_t
    capacity () const
    {
      return capacity_;
    }

  public:
    virtual void
    serialize_body (ostream& os) const
    {
      os.write_char_array (buf_, size_);
    }

    virtual void
    serialize_body (sstream& ss) const
    {
      ss.write_char_array (buf_, size_);
    }

  private:
    char* buf_;
    size_t size_;
    size_t capacity_;
  };


  //
  //
  //
  struct SN;

  typedef
  ACE_Refcounted_Auto_Ptr<SN, Mutex>
  SN_ptr;

  struct SN : Profile
  {
    static u16 const id;

  public:
    SN (Header const& h, istream& is)
        : Profile (h)
    {
      is >> n_;
    }

    SN (u64 n)
        : Profile (id), n_ (n)
    {
      size (calculate_size ());
    }

    SN_ptr
    clone ()
    {
      return SN_ptr (static_cast<SN*> (clone_ ().release ()));
    }

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

    SN (SN const& sn)
        : Profile (sn),
          n_ (sn.n_)
    {
    }

  public:
    u64
    num () const
    {
      return n_;
    }

  public:
    virtual void
    serialize_body (ostream& os) const
    {
      os << n_;
    }

    virtual void
    serialize_body (sstream& ss) const
    {
      ss << n_;
    }

  private:
    u64 n_;
  };


  //
  //
  //
  class NAK;

  typedef
  ACE_Refcounted_Auto_Ptr<NAK, Mutex>
  NAK_ptr;

  class NAK : public Profile
  {
  public:

    static u16 const id;

    typedef ACE_Vector<u64, ACE_VECTOR_DEFAULT_SIZE> SerialNumbers;
    typedef SerialNumbers::Iterator iterator;

    NAK (Header const& h, istream& is)
        : Profile (h)
    {
      u64 sn (0);
      u32 addr (0);
      u16 port (0);

      sstream ss;

      ss << sn;
      size_t sn_size (ss.total_length ());

      ss.reset ();

      ss << addr;
      ss << port;

      size_t addr_size (ss.total_length ());


      is >> addr;
      is >> port;

      // num_of_sns = (size - addr_size) / sn_size
      //
      for (unsigned long i (0); i < ((h.size () - addr_size) / sn_size); ++i)
      {
        is >> sn;
        sns_.push_back (sn);
      }


      address_ = Address (port, addr);
    }

    NAK (Address const& src)
        : Profile (id), address_ (src)
    {
      size (calculate_size ());
    }

    NAK_ptr
    clone ()
    {
      return NAK_ptr (static_cast<NAK*> (clone_ ().release ()));
    }

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

    NAK (NAK const& nak)
        : Profile (nak),
          address_ (nak.address_),
          sns_ (nak.sns_)
    {
    }

  public:
    void
    add (u64 sn)
    {
      sns_.push_back (sn);
      size (calculate_size ());
    }

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


    iterator
    begin () /* const */
    {
      return iterator (sns_);
    }

    /*
    iterator
    end ()  const
    {
      return sns_.end ();
    }
    */

    size_t
    count () const
    {
      return sns_.size ();
    }

  public:
    // Count max number of elements that will fit into NAK profile
    // with size <= max_size.
    //
    static u32
    max_count (u32 max_size)
    {
      u32 n (0);

      sstream ss;

      Profile::Header hdr (0, 0);
      ss << hdr;

      u32 addr (0);
      u16 port (0);
      ss << addr;
      ss << port;

      while (true)
      {
        u64 sn (0);
        ss << sn;

        if (ss.total_length () <= max_size)
          ++n;

        if (ss.total_length () >= max_size)
          break;
      }

      return n;
    }

  public:
    virtual void
    serialize_body (ostream& os) const
    {
      NAK& this_ = const_cast<NAK&> (*this); // Don't put in ROM.

      u32 addr (address_.get_ip_address ());
      u16 port (address_.get_port_number ());

      os << addr;
      os << port;

      // Stone age iteration.
      //
      for (iterator i (this_.begin ()); !i.done (); i.advance ())
      {
        u64* psn;
        i.next (psn);
        os << *psn;
      }
    }

    virtual void
    serialize_body (sstream& ss) const
    {
      NAK& this_ = const_cast<NAK&> (*this); // Don't put in ROM.

      u32 addr (0);
      u16 port (0);

      ss << addr;
      ss << port;

      // Stone age iteration.
      //
      for (iterator i (this_.begin ()); !i.done (); i.advance ())
      {
        u64 sn (0);
        ss << sn;
      }
    }

  private:
    Address address_;
    SerialNumbers sns_;
  };

  //
  //
  //
  struct NRTM;

  typedef
  ACE_Refcounted_Auto_Ptr<NRTM, Mutex>
  NRTM_ptr;

  struct NRTM : Profile
  {
    static u16 const id;

  public:
    NRTM (Header const& h, istream& is)
        : Profile (h), map_ (10)
    {
      u32 addr (0);
      u16 port (0);
      u64 sn (0);

      sstream ss;

      ss << sn;
      ss << addr;
      ss << port;

      size_t block_size (ss.total_length ());


      // num_of_blocks = size / block_size
      //
      for (size_t i (0); i < (h.size () / block_size); ++i)
      {
        is >> sn;
        is >> addr;
        is >> port;

        map_.bind (Address (port, addr), sn);
      }
    }

    NRTM ()
        : Profile (id), map_ (10)
    {
      size (calculate_size ());
    }

    NRTM_ptr
    clone ()
    {
      return NRTM_ptr (static_cast<NRTM*> (clone_ ().release ()));
    }

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

    NRTM (NRTM const& nrtm)
        : Profile (nrtm)
    {
      for (Map::const_iterator i (nrtm.map_); !i.done (); i.advance ())
      {
        map_.bind ((*i).ext_id_, (*i).int_id_);
      }
    }

  public:
    void
    insert (Address const& addr, u64 sn)
    {
      map_.bind (addr, sn);

      size (calculate_size ());
    }

    u64
    find (Address const& addr) const
    {
      u64 sn;

      if (map_.find (addr, sn) == -1) return 0;

      return sn;
    }

    bool
    empty () const
    {
      return map_.current_size () == 0;
    }

  public:
    // Count max number of elements that will fit into NRTM profile
    // with size <= max_size.
    //
    static u32
    max_count (u32 max_size)
    {
      u32 n (0);

      sstream ss;

      Profile::Header hdr (0, 0);
      ss << hdr;

      while (true)
      {
        u32 addr (0);
        u16 port (0);
        u64 sn (0);

        ss << sn;
        ss << addr;
        ss << port;

        if (ss.total_length () <= max_size)
          ++n;

        if (ss.total_length () >= max_size)
          break;
      }

      return n;
    }

  public:
    virtual void
    serialize_body (ostream& os) const
    {
      for (Map::const_iterator i (map_), e (map_, 1); i != e; ++i)
      {
        u32 addr ((*i).ext_id_.get_ip_address ());
        u16 port ((*i).ext_id_.get_port_number ());
        u64 sn ((*i).int_id_);

        os << sn;
        os << addr;
        os << port;

      }
    }

    virtual void
    serialize_body (sstream& ss) const
    {
      for (Map::const_iterator i (map_), e (map_, 1); i != e; ++i)
      {
        u32 addr (0);
        u16 port (0);
        u64 sn (0);

        ss << sn;
        ss << addr;
        ss << port;
      }
    }

  private:
    typedef
    ACE_Hash_Map_Manager_Ex<Address,
                            u64,
                            AddressHasher,
                            ACE_Equal_To<Address>,
                            ACE_Null_Mutex>
    Map;

    Map map_;
  };


  //
  //
  //
  struct NoData;

  typedef
  ACE_Refcounted_Auto_Ptr<NoData, Mutex>
  NoData_ptr;

  struct NoData : Profile
  {
    static u16 const id;

  public:
    NoData (Header const& h, istream&)
        : Profile (h)
    {
    }

    NoData ()
        : Profile (id)
    {
      Profile::size (0);
    }

    NoData_ptr
    clone ()
    {
      return NoData_ptr (static_cast<NoData*> (clone_ ().release ()));
    }

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

    NoData (NoData const& no_data)
        : Profile (no_data)
    {
    }

  public:
    virtual void
    serialize_body (ostream&) const
    {
    }

    virtual void
    serialize_body (sstream&) const
    {
    }
  };


  //
  //
  //
  struct Part;

  typedef
  ACE_Refcounted_Auto_Ptr<Part, Mutex>
  Part_ptr;

  struct Part : Profile
  {
    static u16 const id;

  public:
    Part (Header const& h, istream& is)
        : Profile (h)
    {
      is >> num_;
      is >> of_;
      is >> total_size_;
    }

    Part (u32 num, u32 of, u64 total_size)
        : Profile (id),
          num_ (num),
          of_ (of),
          total_size_ (total_size)
    {
      size (calculate_size ());
    }

    Part_ptr
    clone ()
    {
      return Part_ptr (static_cast<Part*> (clone_ ().release ()));
    }

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

    Part (Part const& part)
        : Profile (part),
          num_ (part.num_),
          of_ (part.of_),
          total_size_ (part.total_size_)
    {
    }

  public:
    u32
    num () const
    {
      return num_;
    }

    u32
    of () const
    {
      return of_;
    }

    u64
    total_size () const
    {
      return total_size_;
    }

  public:
    virtual void
    serialize_body (ostream& os) const
    {
      os << num_;
      os << of_;
      os << total_size_;
    }

    virtual void
    serialize_body (sstream& ss) const
    {
      ss << num_;
      ss << of_;
      ss << total_size_;
    }


  private:
    u32 num_;
    u32 of_;
    u64 total_size_;
  };

}

/*
inline
std::ostream&
operator<< (std::ostream& os, ACE_RMCast::Address const& a)
{
  char buf[64];
  a.addr_to_string (buf, 64, 1);
  return os << buf;
}
*/

#endif  // ACE_RMCAST_PROTOCOL_H

⌨️ 快捷键说明

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