This section describes the first class Tsocket that implements a specific transport feature, which is an interface that implements Ttransport based on a TCP socket. The following is a detailed description of the function implementation of the related functions of this class.
1. Constructor function
To analyze the function of a class, first look at its definition and constructor implementation, first look at its definition:
Class Tsocket:public tvirtualtransport<tsocket> {...}
By definition you can read tsocket inherit to a virtual transport class, and pass yourself as a template parameter, so a virtual function (such as Read_virt) that inherits from a virtual transport class calls a non-virtual function (such as read) to be implemented by Tsocket itself.
There are 4 constructors for the Tsocket class, and of course there is a destructor. The four constructors are constructed from different parameters, and their declarations are as follows:
Tsocket ();//All parameters default
tsocket (std::string host, int port);//construct a socket
tsocket (std::string path) based on host name and port;// Constructs a socket
tsocket (int socket) for a UNIX domain;//constructs an original UNIX handle socket
The four constructors are used in different situations to produce different tsocket objects, but these constructors are simply initializing some of the most basic member variables without actually connecting the socket. The variables they initialize are basically as follows:
Tsocket::tsocket ():
host_ (""),
port_ (0),
path_ (""),
socket_ ( -1),
conntimeout_ (0),
Sendtimeout_ (0),
recvtimeout_ (0),
lingeron_ (1),
lingerval_ (0),
nodelay_ (1),
Maxrecvretries_ (5) {
recvtimeval_.tv_sec = (int) (recvtimeout_/1000);
recvtimeval_.tv_usec = (int) ((recvtimeout_%1000) *1000);
cachedPeerAddr_.ipv4.sin_family = Af_unspec;
}
Most of the simple parameters are initialized with the initialization list, and the simple calculations are initialized in the function body, and several others are. The following is a separate introduction to the UNIX domain socket.
The socket API was originally designed for network communications, but later on the socket framework developed an IPC mechanism, is the UNIX Domain socket. Although network sockets can also be used for interprocess communication with the same host (via the loopback address 127.0.0.1), Unix Domain sockets are more efficient for IPC: No network protocol stack is required, no package unpacking, calculation checksum, maintenance sequence number and answer, etc. , just copy application-tier data from one process to another. This is because the IPC mechanism is inherently reliable communication, and network protocols are designed for unreliable communications. UNIX domain sockets also provide both streaming and packet-oriented API interfaces, similar to TCP and UDP, but message-oriented UNIX domain sockets are also reliable, and messages are neither lost nor sequentially disordered.
UNIX domain socket is Full-duplex, API interface is rich in semantics, compared with other IPC mechanisms have obvious advantages, now has become the most widely used IPC mechanism, such as X Window server and GUI program is through the UNIX Domain socket communication.
The process of using a UNIX Domain socket is very similar to the network socket, but you also need to call the socket () to create a socket file descriptor, and the address family specified as Af_unix,type can be selected Sock_ The Dgram or Sock_stream,protocol parameter is still specified as 0.
UNIX domain socket and network socket programming the most obvious difference is that the address format is different, with the structure of Sockaddr_un, network programming socket address is the IP address plus port number, and UNIX Domain socket address is a The path of the socket type file in the file system, which is created by bind () invocation, and the bind () error is returned if the file already exists when bind () is invoked.