zoukankan      html  css  js  c++  java
  • 32 GroupSock(AddressPortLookupTable)——live555源码阅读(四)网络

    32 GroupSock(AddressPortLookupTable)——live555源码阅读(四)网络

    本文由乌合之众 lym瞎编,欢迎转载 blog.cnblogs.net/oloroso
    本文由乌合之众 lym瞎编,欢迎转载 my.oschina.net/oloroso

    简介

    AddressPortLookupTable地址端口查找表类
    AddressPortLookupTable类内部定义了一个HashTable* fTable用于保存哈希表的地址。在构造函数中动态创建了一个哈希表对象给它。AddressPortLookupTable使用了两个地址和一个端口号组合作为一个keyvalueAdd方法的时候确定的。

    AddressPortLookupTable类只提供了增删查三种操作,没有提供修改表项的操作。
    使用哈希表的优点在于可以快速的查找key对应的value

    AddressPortLookupTable的定义

     1 // A generic table for looking up objects by (address1, address2, port)
     2 // 用于查找对象,通过一个通用表(地址1,地址2,端口)
     3 class AddressPortLookupTable {
     4 public:
     5     // 为内部哈希表fTable创建对象,哈希表的key是3个元素的unsigned int数组
     6     AddressPortLookupTable();
     7     // 释放内部哈希表fTable
     8     virtual ~AddressPortLookupTable();
     9 
    10     // 使用address1、address2、port组成key,value为值添加到哈希表 
    11     // 如果对应key的条目已经存在,返回旧的value,否则返回NULL
    12     void* Add(netAddressBits address1, netAddressBits address2,
    13         Port port, void* value);
    14     // Returns the old value if different, otherwise 0
    15 
    16     //从哈希表中移除key对应的条目,对应条目存在返回true
    17     Boolean Remove(netAddressBits address1, netAddressBits address2,
    18         Port port);
    19     // 从哈希表中查找key对应的value,没找到返回NULL
    20     void* Lookup(netAddressBits address1, netAddressBits address2,
    21         Port port);
    22     // Returns 0 if not found
    23 
    24     // Used to iterate through the entries in the table
    25     // 用于遍历在表中的条目
    26     class Iterator {
    27     public:
    28         Iterator(AddressPortLookupTable& table);
    29         virtual ~Iterator();
    30 
    31         void* next(); // NULL iff none
    32 
    33     private:
    34         HashTable::Iterator* fIter; //哈希表迭代器
    35     };
    36 
    37 private:
    38     friend class Iterator;
    39     HashTable* fTable;      //哈希表
    40 };

    AddressPortLookupTable构造与析构

     1 AddressPortLookupTable在构造的时候创建哈希表
     2 
     3 AddressPortLookupTable::AddressPortLookupTable()
     4 : fTable(HashTable::create(3)) { // three-word keys are used 键使用3个元素的unsigned int数组
     5 }
     6 
     7 析构的时候释放哈希表
     8 
     9 AddressPortLookupTable::~AddressPortLookupTable() {
    10     delete fTable;
    11 }

    Add方法(添加表项)

    Add方法使用前三个参数来组合作为一个key,第四个参数是value。创建一个表项添加到哈希表。
    如果key对应的表项在哈希表中已经存在,那么返回值是已经存在表项的旧value,这个表项的value替换为参数value。如果不存在,那就返回NULL。(表项=条目)

     1 // 使用address1、address2、port组成key,value为值添加到哈希表
     2 void* AddressPortLookupTable::Add(netAddressBits address1,
     3     netAddressBits address2,
     4     Port port, void* value) {
     5     int key[3];
     6     key[0] = (int)address1;
     7     key[1] = (int)address2;
     8     key[2] = (int)port.num();
     9     return fTable->Add((char*)key, value);
    10 }

    Remove方法(移除表项)

    Remove方法用于从哈希表中移除表项,这三个参数依然是用于组成key的。如果key在表中存在对应的表项,那么移除后函数返回true,否则返回false

     1 //从哈希表中移除key对应的条目,对应条目存在返回true
     2 Boolean AddressPortLookupTable::Remove(netAddressBits address1,
     3     netAddressBits address2,
     4     Port port) {
     5     int key[3];
     6     key[0] = (int)address1;
     7     key[1] = (int)address2;
     8     key[2] = (int)port.num();
     9     return fTable->Remove((char*)key);
    10 }

    Lookup方法(查找表项)

    这里说查找表项,不是很准确,应该是查找表项的value。如果key对应的表项不存在,那么就返回NULL。存在就返回表项的value

     1 // 从哈希表中查找key对应的value,没找到返回NULL
     2 void* AddressPortLookupTable::Lookup(netAddressBits address1,
     3     netAddressBits address2,
     4     Port port) {
     5     int key[3];
     6     key[0] = (int)address1;
     7     key[1] = (int)address2;
     8     key[2] = (int)port.num();
     9     return fTable->Lookup((char*)key);
    10 }

    AddressPortLookupTable迭代器方法

    AddressPortLookupTable迭代器还有三个方法,构造析构next。其实质是对HashTable::Iterator的操作。迭代器创建的时候指向哈希表的第一个条目。
    构造函数,构造的时候必须绑定一个AddressPortLookupTable对象。

     1 // 创建迭代器,绑定地址端口查找表
     2 AddressPortLookupTable::Iterator::Iterator(AddressPortLookupTable& table)
     3 // 创建哈希表迭代器,绑定哈希表
     4 : fIter(HashTable::Iterator::create(*(table.fTable))) {
     5 }
     6 
     7 析构函数,删除迭代器HashTable::Iterator fIter。
     8 
     9 AddressPortLookupTable::Iterator::~Iterator() {
    10     delete fIter;
    11 }

    next方法的返回值需要注意一下,返回的是当前迭代器指向表中条目的value。然后迭代器会走向下一个,如果走到哈希表的尾部元素之后,那么返回NULL

    1 // 返回当前迭代器指向条目的value,迭代器走向下一个
    2 void* AddressPortLookupTable::Iterator::next() {
    3     char const* key; // dummy
    4     return fIter->next(key);
    5 }
  • 相关阅读:
    面向对象
    标准库内置模块
    json迭代器生成器装饰器
    基本数据操作
    列表元组字典字符串操作
    深入了解Spring之IoC
    认识OAuth 2.0及实例
    web.xml中context-param和init-param的区别
    虚拟机centos6网卡配置及提示Device does not seem to be present
    JUC之深入理解ReentrantReadWriteLock
  • 原文地址:https://www.cnblogs.com/oloroso/p/4629201.html
Copyright © 2011-2022 走看看