27 GroupSock概述(一)——live555源码阅读(四)网络
27 GroupSock概述(一)——live555源码阅读(四)网络
本文由乌合之众 lym瞎编,欢迎转载 blog.cnblogs.net/oloroso
本文由乌合之众 lym瞎编,欢迎转载 my.oschina.net/oloroso
简介
group是组/群的意思,socket是网络接口的代名词了。这个部分很庞大,主要是与网络相关的。而live555的网络模块很多都涉及到组播的概念。
作为一个跨平台的流媒体服务库,live555对网络的封装很全面,值得一看。
1.网络通用数据类型定义
因为live555跨平台的特点,需要定义一些在数据类型来适应各个平台环境。
这写代码在live555sourcecontrol\groupsock\include\NetCommon.h文件中
#if defined(__WIN32__) || defined(_WIN32) || defined(_WIN32_WCE)
/* Windows */
#if defined(WINNT) || defined(_WINNT) || defined(__BORLANDC__) || defined(__MINGW32__) || defined(_WIN32_WCE)
#define _MSWSOCK_
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <windows.h>
#include <string.h>
#define closeSocket closesocket //关闭socket函数
#define EWOULDBLOCK WSAEWOULDBLOCK //10035L 可能会被阻塞
#define EINPROGRESS WSAEWOULDBLOCK //10035L 操作正在进行
#define EAGAIN WSAEWOULDBLOCK //10035L 再试一次
#define EINTR WSAEINTR //10004L 中断
#if defined(_WIN32_WCE)
#define NO_STRSTREAM 1
#endif
/* Definitions of size-specific types: 定义特定大小的类型*/
typedef __int64 int64_t;
typedef unsigned __int64 u_int64_t;
typedef unsigned u_int32_t;
typedef unsigned short u_int16_t;
typedef unsigned char u_int8_t;
// For "uintptr_t" and "intptr_t", we assume that if they're not already defined, then this must be
// “uintptr_t”和“intptr_t”,我们认为如果他们不是已经定义,那么这一定是
// an old, 32-bit version of Windows: 一个老的,32位版本的Windows:
#if !defined(_MSC_STDINT_H_) && !defined(_UINTPTR_T_DEFINED) && !defined(_UINTPTR_T_DECLARED) && !defined(_UINTPTR_T)
typedef unsigned uintptr_t;
#endif
#if !defined(_MSC_STDINT_H_) && !defined(_INTPTR_T_DEFINED) && !defined(_INTPTR_T_DECLARED) && !defined(_INTPTR_T)
typedef int intptr_t;
#endif
#elif defined(VXWORKS)
/* VxWorks */
#include <time.h>
#include <timers.h>
#include <sys/times.h>
#include <sockLib.h>
#include <hostLib.h>
#include <resolvLib.h>
#include <ioLib.h>
typedef unsigned int u_int32_t;
typedef unsigned short u_int16_t;
typedef unsigned char u_int8_t;
#else
/* Unix */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <strings.h>
#include <ctype.h>
#include <stdint.h>
#if defined(_QNX4)
#include <sys/select.h>
#include <unix.h>
#endif
#define closeSocket close
#ifdef SOLARIS
#define u_int64_t uint64_t
#define u_int32_t uint32_t
#define u_int16_t uint16_t
#define u_int8_t uint8_t
#endif
#endif
#ifndef SOCKLEN_T
#define SOCKLEN_T int
#endif
2.Tunnel隧道封装
这里代码里面已经注释得很明白了。这个首先需要了解以下什么是Tunnel(隧道)。(简单的说,它就像是披着羊皮的狼)
tunnel中文译为隧道。网络隧道(Tunnelling)技术是个关键技术。网络隧道技术指的是利用一种网络协议来传输另一种网络协议,它主要利用网络隧道协议来实现这种功能。网络隧道技术涉及了三种网络协议,即网络隧道协议、隧道协议下面的承载协议和隧道协议所承载的被承载协议。
这里实现的TunnelEncapsulationTrailer类是一个很特殊的类,它不应该被用来创建对象。
对于这一部分,这里先不多说,先看后面的。
其定义在live555sourcecontrol\groupsock\include\TunnelEncaps.hh文件中
typedef u_int16_t Cookie;
/* cookie(储存在用户本地终端上的数据)
Cookie,有时也用其复数形式Cookies,指某些网站为了辨别用户身份、进行session跟踪而
储存在用户本地终端上的数据(通常经过加密)。定义于RFC2109和2965都已废弃,最新规范是RFC6265 。
*/
/*tunnel中文译为隧道。网络隧道(Tunnelling)技术是个关键技术。网络隧道技术指的是利用一种网络协议来传输另一种网络协议,它主要利用网络隧道协议来实现这种功能。网络隧道技术涉及了三种网络协议,即网络隧道协议、隧道协议下面的承载协议和隧道协议所承载的被承载协议。
*/
// 这个类很有意思,它内部并无数据成员,其函数成员的返回都是以this为基准进行偏移
// 后,转换这个偏移后的地址为相应的指针类型,再取指针指向内存的内容。
// 所以这个类并不会用来创建对象,而是作为一种类型来使用。可能诸如以下代码
// unsigned long long t= 0x1239874560864216L;
// cout << ((TunnelEncapsulationTrailer*)&t)->address() << endl;
// cout << 0x12398745 << endl;
class TunnelEncapsulationTrailer {
// The trailer is layed out as follows:
// bytes 0-1: source 'cookie' 源Cookie
// bytes 2-3: destination 'cookie' 目的Cookie
// bytes 4-7: address 地址
// bytes 8-9: port 端口
// byte 10: ttl TTL
// byte 11: command 命令
// Optionally, there may also be a 4-byte 'auxilliary address'
// 随意,也可能有一个4字节的"辅助地址"
// (e.g., for 'source-specific multicast' preceding this)
// (例如,“特定源组播”在此之前)
// bytes -4 through -1: auxilliary address
// -4到-1字节(this之前4个字节),辅助地址
public:
Cookie& srcCookie()
{ return *(Cookie*)byteOffset(0); }
Cookie& dstCookie()
{ return *(Cookie*)byteOffset(2); }
u_int32_t& address()
{ return *(u_int32_t*)byteOffset(4); }
Port& port()
{ return *(Port*)byteOffset(8); }
u_int8_t& ttl()
{ return *(u_int8_t*)byteOffset(10); }
u_int8_t& command()
{ return *(u_int8_t*)byteOffset(11); }
u_int32_t& auxAddress()
{ return *(u_int32_t*)byteOffset(-4); }
private:
//取this偏移charIndex
inline char* byteOffset(int charIndex)
{ return ((char*)this) + charIndex; }
};
const unsigned TunnelEncapsulationTrailerSize = 12; // bytes隧道封装拖车尺寸
const unsigned TunnelEncapsulationTrailerAuxSize = 4; // bytes辅助的尺寸
const unsigned TunnelEncapsulationTrailerMaxSize //最大尺寸
= TunnelEncapsulationTrailerSize + TunnelEncapsulationTrailerAuxSize;
// Command codes:命令码
// 0: unused
const u_int8_t TunnelDataCmd = 1; //隧道的数据命令
const u_int8_t TunnelJoinGroupCmd = 2; //隧道连接组命令
const u_int8_t TunnelLeaveGroupCmd = 3; //隧道离开组命令
const u_int8_t TunnelTearDownCmd = 4; //隧道拆除命令
const u_int8_t TunnelProbeCmd = 5; //隧道探针命令
const u_int8_t TunnelProbeAckCmd = 6; //隧道探针ACK命令
const u_int8_t TunnelProbeNackCmd = 7; //隧道探针NACK命令
const u_int8_t TunnelJoinRTPGroupCmd = 8; //隧道加入RTP组命令
const u_int8_t TunnelLeaveRTPGroupCmd = 9; //隧道离开RTP组命令
// 0x0A through 0x10: currently unused.0x0a到0x10:目前未使用
// a flag, not a cmd code一个标识,不是命令码。隧道扩展标识
const u_int8_t TunnelExtensionFlag = 0x80; //bits:1000 0000
const u_int8_t TunnelDataAuxCmd //隧道数据辅助命令
= (TunnelExtensionFlag|TunnelDataCmd);
const u_int8_t TunnelJoinGroupAuxCmd //隧道连接组辅助命令
= (TunnelExtensionFlag|TunnelJoinGroupCmd);
const u_int8_t TunnelLeaveGroupAuxCmd //隧道离开组辅助命令
= (TunnelExtensionFlag|TunnelLeaveGroupCmd);
// Note: the TearDown, Probe, ProbeAck, ProbeNack cmds have no Aux version
// 注意:TearDown(拆除),Probe(探针),ProbeAck(Ack探针),ProbeNack(NACK探针)没有辅助版命令
// 0x84 through 0x87: currently unused.
const u_int8_t TunnelJoinRTPGroupAuxCmd //隧道加入RTP组辅助命令
= (TunnelExtensionFlag|TunnelJoinRTPGroupCmd);
const u_int8_t TunnelLeaveRTPGroupAuxCmd//隧道离开RTP组辅助命令
= (TunnelExtensionFlag|TunnelLeaveRTPGroupCmd);
// 0x8A through 0xFF: currently unused
//判断参数cmd是否是辅助命令
inline Boolean TunnelIsAuxCmd(u_int8_t cmd) {
return (cmd&TunnelExtensionFlag) != 0;
}
27 GroupSock概述(一)——live555源码阅读(四)网络的更多相关文章
- 32 GroupSock(AddressPortLookupTable)——live555源码阅读(四)网络
32 GroupSock(AddressPortLookupTable)——live555源码阅读(四)网络 32 GroupSock(AddressPortLookupTable)——live555 ...
- 31 GroupSock(AddressString)——live555源码阅读(四)网络
31 GroupSock(AddressString)——live555源码阅读(四)网络 31 GroupSock(AddressString)——live555源码阅读(四)网络 简介 Addre ...
- 30 GroupSock(Port)——live555源码阅读(四)网络
30 GroupSock(Port)——live555源码阅读(四)网络 30 GroupSock(Port)——live555源码阅读(四)网络 简介 Port类的定义 Port的构造与全局的 &l ...
- 29 GroupSock(NetAddressList)——live555源码阅读(四)网络
29 GroupSock(NetAddressList)——live555源码阅读(四)网络 29 GroupSock(NetAddressList)——live555源码阅读(四)网络 简介 Net ...
- 28 GroupSock(NetAddress)——live555源码阅读(四)网络
28 GroupSock(NetAddress)——live555源码阅读(四)网络 28 GroupSock(NetAddress)——live555源码阅读(四)网络 简介 1) NetAddre ...
- 40 网络相关函数(八)——live555源码阅读(四)网络
40 网络相关函数(八)——live555源码阅读(四)网络 40 网络相关函数(八)——live555源码阅读(四)网络 简介 15)writeSocket向套接口写数据 TTL的概念 函数send ...
- 39 网络相关函数(七)——live555源码阅读(四)网络
39 网络相关函数(七)——live555源码阅读(四)网络 39 网络相关函数(七)——live555源码阅读(四)网络 简介 14)readSocket从套接口读取数据 recv/recvfrom ...
- 38 网络相关函数(六)——live555源码阅读(四)网络
38 网络相关函数(六)——live555源码阅读(四)网络 38 网络相关函数(六)——live555源码阅读(四)网络 简介 12)makeSocketNonBlocking和makeSocket ...
- 37 网络相关函数(五)——live555源码阅读(四)网络
37 网络相关函数(五)——live555源码阅读(四)网络 37 网络相关函数(五)——live555源码阅读(四)网络 简介 10)MAKE_SOCKADDR_IN构建sockaddr_in结构体 ...
随机推荐
- Java数据结构——容器总结
4大容器——List.Set.Queue.Map List 1.ArrayList 优点:随机访问元素 缺点:插入和移除元素时较慢 2.LinkedList 优点:插入和删除元素 缺点:随机访问方面相 ...
- CSS实时编辑显示
方法 CSS实时编辑显示:通过display:block让css文本显示出来,再加上contentEditable使文本可编辑 <!DOCTYPE html> <html> & ...
- C#--网络流Stream、字节数组保存到字符串中
第一种方法: HttpWebRequest httpwebr = (HttpWebRequest)HttpWebRequest.Create(rstr); httpwebr.Method = &quo ...
- new和alloc init的区别
背景说明,new是较为老式的写法,后来发现只有一个new不好使,才引入了alloc和init这种写 法,保留new一是向后兼容,二是很多时候是一种更简单的写法.其实是一样的,new在内部调用 的all ...
- asp.net(C#)页面事件顺序
asp.net(C#)页面事件顺序 http://www.cnblogs.com/henw/archive/2012/02/09/2343994.html 1 using System.Data; ...
- Python 对象的引用计数和拷贝
Python 对象的引用计数和拷贝 Python是一种面向对象的语言,包括变量.函数.类.模块等等一切皆对象. 在python中,每个对象有以下三个属性: 1.id,每个对象都有一个唯一的身份标识自己 ...
- Mysql InnoDB行锁实现方式(转)
Mysql InnoDB行锁实现方式 InnoDB行锁是通过给索引上的索引项加锁来实现的,这一点MySQL与Oracle不同,后者是通过在数据块中对相应数据行加锁来实现的.InnoDB这种行锁实现特点 ...
- PHP 短连接生成
<?php #短连接生成算法 class Short_Url { #字符表 public static $charset = "0123456789ABCDEFGHIJKLMNOPQR ...
- checkstyle配置文件说明
属性说明 basedir代码所在的位置 AbstractClassNameformat: 定义抽象类的命名规则 PackageNameformat: 定义包名的命名规则 TypeNameformat: ...
- jQuery.lazyload使用及源码分析
前言: 貌似以前自己也写过图片懒加载插件,但是新公司使用的是jQuery.lazyload插件,为了更好的运用,自己还是把源码看了遍,分别记录了如何使用, 插件原理,各个配置属性的完整解释,demo实 ...