I just came into contact with RPC (remote procedure call), which is a method that can call a program on a remote machine locally. I saw a simple nodejs implementation, which is very good for learning the principles of RPC: nodejs light_rpc
Usage example:
Sample client:
Briefly talk about the whole process:
1. The server starts the program, listens to the port, implements the functions called by the client (such as combine and multiply in the above example), and saves them in an object.
2. The client starts the program, connects to the server, and sends the describe command after the connection is completed, asking the server to return the function name it can provide for calling.
3. The server receives the describe command, wraps its callable function name and sends it out ("combine", "multiply")
4. The client receives the function name sent by the server, registers it in its own object, and wraps a method for each function name, so that when calling these functions locally, it actually sends a request to the server:
5. The client calls the server-side function:
1) Generate a unique ID for the incoming callback function, called callbackId, and record it in an object of the client.
2) Package the following data and send it to the server: calling function name, JSON serialized parameter list, callbackId
6. The server receives the above information, parses the data, deserializes the parameter list, and calls the function according to the function name and parameters.
7. After the function is completed, serialize the result and send it back to the client together with the previously received callbackId
8. The client receives the function operation result and callbackId, takes out the callback function based on the callbackId, and passes the operation result into the callback function for execution.
9. The entire process is completed, please see the source code for details: https://github.com/romulka/nodejs-light_rpc
A few points to note:
1. The client and server remain connected during the entire process. Unlike the http protocol, the connection is disconnected after sending and receiving. Therefore, the completion of a data transmission cannot be judged by disconnecting the connection. In order to judge the completion of data reception, the data sent by the client and server follows a simple protocol: add the length of the data packet and the delimiter before the data. For example, the delimiter is n: [packet length data], so that after receiving the data, first take out the length of the data packet, and then continuously judge whether the accumulated data packets received are equal to or exceed this length. If so, one data transmission is completed, and you can start parsing and extracting the data.
2. This RPC is simple because it does not consider the function type in the parameters. For example, if the parameter is an object and there are function members under this object, the function will be ignored during JSON serialization, and this function cannot be executed on the server side. of.
To solve this problem, complex processing is required:
1. Deeply traverse each parameter to be sent to the remote end, extract the function members, generate a unique ID for this function, put it into a local object, replace this function member with this id string, and identify it This member is actually a function. In this way, the object can be serialized and sent out.
2. The server receives the call. When it wants to use the function in the parameter object, it determines that this is a function that has been processed by the client and has an id. It sends the id back to the client and uses the same method to set its own callback function. Pass the id to the client and wait for the callback from the client.
3. The client receives the function id, finds the function entity, calls it, and after completion, sends it back to the server according to the callback id given by the server
4. The server receives the result, finds the callback function, continues execution, and completes.
The recording method of functions can be completed in other ways. The general idea is to replace the function with something that can be serialized, and record the function so that the function can be found locally when called by the remote side. You can refer to the implementation of dnode.