Groupsock (netaddress)--live555 source Read (iv) network
- Groupsock (netaddress)--live555 source Read (iv) network
- Brief introduction
- 1) netaddress Network address class Brief
- Here is its definition
- Assign method (allocated space)
- The structure of netaddress
- Clean method (cleanup) and destruction
- Operate= Overloaded Assignment operation
This article by Mob Lym fabricated, welcome reprintblog.cnblogs.net/oloroso
This article by Mob Lym fabricated, welcome reprintmy.oschina.net/oloroso
Brief introduction
A network connection that uses a socket, usually consisting of an address ( IP
) and a port ( port
).
Some data types are defined in live555, indicating the type of network address currently supported.
1 // Definition of a type representing a low-level network address. 2 // at present, which is the 32-bits, for IPV4. Later, generalize it,3// to allow for IPv6. 4 // a representation of the underlying network address definition. Currently, it defaults to 32 bits, IPv4. In the future, IPV6 can be extended to support. 5typedef u_int32_t netaddressbits; 6 typedef u_int16_t Portnumbits;
defined in the filelive555sourcecontrol\groupsock\include\NetAddress.hh
1) netaddress Network address class Brief
NetAddress
is a class that holds the network address, which is not the encapsulation of the pair struct sockaddr
. It internally defines two data members, each of which is used to hold address data u_int8_t* fData
and to indicate the length of the address unsigned fLength
.
Here is its definition
1 classnetaddress {2 Public:3Netaddress (u_int8_tConst*data,4unsigned length =4 /*default:32 bits IPv4*/);5netaddress (Unsigned length =4);//sets address data to All-zeros6Netaddress (netaddressConst&orig);7netaddress&operator= (netaddressConst&rightside);8 Virtual~netaddress ();9 TenUnsigned length ()Const{returnflength;} Oneu_int8_tConst* Data ()Const //Always in network byte order A{returnFData;} - - Private: the voidAssign (u_int8_tConst*data, unsigned length); - voidClean (); - - unsigned flength; +u_int8_t*FData; -};
definition of netaddress
Assign method (allocated space)
This is not a constructor, because this method is a key method. The constructor also uses it.
assign
fData
dynamically allocates memory space and copies data to members. Parameters are length
used to determine the size of the allocated space, and the parameters are data
copied to the requested new space as a data source. It is important to note that the permissions of this method are private
correct, so no check is data==NULL
possible.
The interesting thing about C + + here is that it is new分配失败不是返回NULL
抛出异常(std::bad_alloc e)
. Unless it is overloaded with new or uses no throw new (std::nothrow)
. However, earlier implementations of the C + + compiler might be the same as new and the malloc behavior, all of which return null.
1 //request a length byte memory space for FDate and copy data to the new space2 voidNetaddress::assign (u_int8_tConst*data, unsigned length) {3FData =NewU_int8_t[length];4 if(FData = =NULL) {5Flength =0;6 return;7 }8 9 for(Unsigned i =0; i < length; ++i) Fdata[i] =Data[i];TenFlength =length; One}
The structure of netaddress
NetAddress
Three constructors are defined, two common parameter constructs and one copy construction (copy construction is also a kind of parameter structure).
The consistent feature of the three constructors is that the fData
memory space is applied dynamically for the member. The code is simple, not detailed.
1 //constructor to request a length byte memory space for fdate and to copy data to the new space2Netaddress::netaddress (u_int8_tConst*data, unsigned length) {3 assign (data, length);4 }5 //request a length byte memory space for FDate and clear the new space by 06 netaddress::netaddress (unsigned length) {7FData =NewU_int8_t[length];8 if(FData = =NULL) {9Flength =0;Ten return; One } A - for(Unsigned i =0; i < length; ++i) Fdata[i] =0; -Flength =length; the } - - //Copy Construction -Netaddress::netaddress (netaddressConst&orig) { + Assign (Orig.data (), orig.length ()); -}
constructor FunctionClean method (cleanup) and destruction
clean
method is used to fData
dispose of the memory space that is pointed to. is to NetAddress
save the object 数据给清理掉
, note that this method is private
permission.
Off-topic: clean and clear mean a little bit different.
1 // Clear Address Data 2 void Netaddress::clean () {3 Delete [] fData; FData = NULL; 4 0 ; 5 }
A destructor is a call to clean.
1 // Destruction 2 netaddress::~netaddress () {3clean (); 4 }
Operate= Overloaded Assignment operation
This is very simple, not detailed.
1 //overloading = Assignment2netaddress& netaddress::operator= (netaddressConst&rightside) {3 if(&rightside! = This) {4 Clean ();5 Assign (Rightside.data (), rightside.length ());6 }7 return* This;8}
Groupsock (netaddress)--live555 source Read (iv) network