In the customer attendant model, the interaction between processes is a process that sends a message request service to another process, then waits for an answer; The service process receives a request and then sends an answer. Such an interaction is much like a process call of the usual meaning. However, in computer network systems, such calls may be performed on different machines, and are therefore called Remote Procedure calls (procedure Call). The basis for remote procedure calls is the XDR protocol.
11.1 XDR Standard
the problem of 11.1.1 data structure transmission
In heterogeneous networked systems, there may be a need to pass complex data structures between customer processes and server processes that can be used to control the behavior of processes or to return process processing results. The problems that may exist in the process of data structure transmission are:
1. Network byte order problem
Different types of computer systems may have different storage formats for the data, such as when the low byte is in front of an integer INT,PC machine store, and the high byte is behind it, while the sun workstation stores the low byte behind and the high byte in front. This will cause them to understand the 2-order sequence of the same integer differently.
2. The transfer of floating point numbers
Floating point numbers are more difficult to pass than integers, usually floating-point numbers use several bits to represent an integral part, and other bits represent the fractional part. Different types of floating-point numbers float and double, they use different bits, which makes it difficult to pass them over the network.
For floating point number processing, the user can take two parts before and after floating-point number as two integers, respectively, and can pass the floating point number as a string.
3. Processing of pointers
In the data structure pass, the pointer is the most difficult to pass, because the meaning of the pointer is the address of a data stored on the computer, this address on the remote host does not make sense. So the user must pass the contents of the pointer rather than the pointer itself. For example, for a string pointer, the user needs to include the contents of the string in the data content, as well as the length information of the string.
11.1.2 XDR Standard
Data types can be transmitted in a variety of ways, and users can use their own defined rules to satisfy the application's data structure delivery. But if you want network programs to interoperate well with other network programs, you need to follow a common standard. The standard that is actually used in the data transfer process is the XDR standard designed by Sun Microsystem.
data types contained in the 11.1.2.1 XDR standard
Sunmicrosystem's XDR standard sets out how data is represented as a public form in the network, and it has become the de facto standard for most customer-attendant applications. The data types in table 11-1 are defined in the XDR standard.
table 11-1 The XDR standard is a data type
Data type |
Length (BITS) |
Meaning |
Int |
32 |
32-bit 2-symbol integer |
unsigned int |
32 |
32-bit 2-binary unsigned integer |
bool |
32 |
Boolean value, expressed in 1 or 0 |
Enum |
Any |
Enumeration types, values are defined as constants |
Hyper |
64 |
64-bit 2-symbol integer |
unsigned Hyper |
64 |
64-bit 2-binary unsigned integer |
Float |
32 |
Single-precision floating-point numbers |
Double |
32 |
Double-precision floating point numbers |
Opaque |
Any |
No conversion of such byte sequences |
String |
Any |
ASCII string |
Fixed array |
Any |
Fixed-length arrays of any other data type |
Counted array |
Any |
The type in the array has a fixed upper bound, but the upper limit size of each array is different |
Structure |
Any |
Aggregation of data, similar to the structure in C language |
Discriminated Union |
Any |
Like the Union in C, you can choose a data type in several forms |
void |
0 |
If the data item is optional and it does not give specific data, use this type |
Symbolic constant |
Any |
A symbolic constant and related values |
Optional data |
Any |
Allow one data to appear 0 or 1 times |
The data types in the XDR standard are very similar to the data types in C, which allow for a structured array, where multiple fields can be used, and each field member can be an array, struct, or union that is fully adaptable to the delivery of complex data structures.
the principle of 11.1.2.2 XDR implementation
XDR provides a way of encoding for various data types. The user can use function xdrmem_create to create an XDR stream in memory that holds the data structure that the user will send. After the initialized XDR stream contains the head of a stream, the function xdrmem_create is used as follows:
#include <rpc/xdr.h>
Externvoid xdrmem_create (XDR *xdrs, const caddr_t addr, u_int size, enum XDR_OPXOP));
Where the variable XDRs is the pointer to creating the XDR stream, the variable addr is the starting address in memory for the XDR flow space, and the variable size is the length of the space, and the variable XOP is the operation that describes the call to the function xdrmem_create, which is defined as follows:
Enumxdr_op {
Xdr_encode=0,
Xdr_decode=1,
xdr_free=2
};
If the variable xop xdr_encode, create an XDR stream for sending, and if the variable XOP fetch Xdr_decode, create an XDR stream for reception, and release the XDR stream if the variable XOP fetch xdr_free.
Then if the user calls the corresponding function to fill in the result of an integer 0X00000004,XDR stream as shown in Figure 11-1.
XDR does not provide information about the type of data in the encoding. For example, when XDR encodes a 32-bit integer, the result of the encoding is still 32 bits, so only the recipient of the data knows the 32-bit data type, and the recipient of the data recovers the data correctly. Therefore, the user must write the send and receive functions in pairs, processing each data item separately.
11.1.2.3 XDR conversion function library
1. converting function libraries
Functions that encode and decode various data types are provided in the XDR library function. Whether these functions are coded or decoded is not determined by these functions, but by the nature of the XDR stream in the function. When the XDR stream is created as an encoded stream, the functions encode the data, and when the XDR stream is created as a decoding stream, the functions decode the data.
Table 11-2 lists the functions of type conversion in the XDR library function. The user must include the header file rpc/xdr.h before calling these functions.
table 11-2 type conversion functions in XDR
Function call |
Description |
extern bool_t xdr_void ((void)); |
Xdr_void is used to process empty options with no data |
extern bool_t Xdr_short (XDR *xdrs, short *sp)); |
Xdr_short is used to process short type data. XDRs is an XDR stream pointer, and the SP is a pointer to a data space that holds the short type |
extern bool_t Xdr_u_short (XDR *xdrs, U_short *usp)); |
Xdr_u_short is used to process u_short type data. XDRs is an XDR stream pointer, and USP is a pointer to a u_short type data space |
extern bool_t Xdr_int (XDR *xdrs, int *ip)); |
Xdr_int is used to process int type data. XDRs is an XDR stream pointer, and IP is a pointer to a data space that holds the int type |
extern bool_t Xdr_u_int (XDR *xdrs, int *up)); |
Xdr_u_int is used to process u_int type data. XDRs is an XDR stream pointer and up is a pointer to the data space where the U_int type is stored |
extern bool_t Xdr_long (XDR *xdrs, Long *LP)); |
Xdr_long is used to process data of type long. XDRs is an XDR stream pointer, and LP is a pointer to a long data space |
extern bool_t Xdr_u_long ((XDR *xdrs,u_long *ulp)); |
Xdr_u_long used to process u_long type data |