How to Write and Use kiss-rpc IDL Protocol

What is IDL

1. IDL is the kiss rpc interface code generation protocol. Writing IDL protocol can generate the corresponding common RPC code invocation interface between the server and the client. Generating the corresponding flatbuffer protocol interface.
2. Unification of specifications, unification of interfaces, simplicity of use, functional invocation in the true sense.

IDL usage

1. [idl file path] [output name] [output path, default to current directory]. E. "/ root/home/kiss-rpc.idl" kiss-rpc "/ root/home/rpc/"
2. Output by module name, module path is ".": E. "/ root/home/kiss-rpc.idl" module. test. kiss-rpc "/ root/home/rpc/"
3. Output client and server file code at the same time, just copy to the corresponding client and server directory.    
4. The type name of message must be capitalized, and the type member must be marked with a serial number, otherwise it cannot be compiled and passed.
5. Function parameter list can only be one, otherwise it cannot be compiled and passed.

Types supported by IDL

IDL D lang
bool bool
byte byte
ubyte ubyte
short short
ushort ushort
int int
uint uint
long long
ulong ulong
float float
double double
char char
string string
[] DynamicArrayList
@message struct

IDL Code Usage

1. The server only needs to fill in the function interface code of the service file in the server directory.
2. The client only needs to call the function of the interface of the service file in the client directory.

Kis-rpc IDL Writing Example

    //kiss rpc idl demo

    @message:UserInfo
    {
        string phone:3;
        string userName:1;
        int age:2;
        double wiget:4;
        
        string[] addressList:5;
    }

    @message:contacts
    {
        int number:1;
        UserInfo[] userInfoList:2;        
    }


    @service:AddressBook    //Interface class
    {
        contacts getContactList(string accountName);
    }

Client remote invocation (sample directory: IDL-Example/client/source/app.d)

IDL generates both synchronous and asynchronous interfaces, both of which are parameter callbacks.
  • Pour in the header file

import KissRpc.IDL.kissidlService;
import KissRpc.IDL.kissidlMessage;
import KissRpc.Unit;
  • Client Synchronization Call

            try{
                auto c = addressBookService.getContactList(name);
                foreach(v; c.userInfoList)
                {
                    writefln("sync number:%s, name:%s, phone:%s, age:%s", c.number, v.name, v.widget, v.age);
                    
                }

            }catch(Exception e)
            {
                writeln(e.msg);
            }
  • Client-side asynchronous invocation

            try{

                addressBookService.getContactList(name, delegate(Contacts c){
                        
                        foreach(v; c.userInfoList)
                        {
                            writefln("async number:%s, name:%s, phone:%s, age:%s", c.number, v.name, v.widget, v.age);
                        }
                    }
                );
            }catch(Exception e)
            {
                writeln(e.msg);
            }
Called in a compressed manner (supports dynamic compression and forced compression)
  • Binding socket compression

RpcClient.setSocketCompress(RPC_PACKAGE_COMPRESS_TYPE.RPCT_DYNAMIC); //Dynamic compression mode, default over 200 bytes compression.
RpcClient.setSocketCompress(RPC_PACKAGE_COMPRESS_TYPE.RPCT_COMPRESS); //Compression mode
  • Single request compression, synchronous invocation, forced compression

            //use compress demo
            try{
                auto c = addressBookService.getContactList(name, RPC_PACKAGE_COMPRESS_TYPE.RPCT_COMPRESS);

                foreach(v; c.userInfoList)
                {
                    writefln("compress test: sync number:%s, name:%s, phone:%s, age:%s", c.number, v.name, v.widget, v.age);
                }
                
            }catch(Exception e)
            {
                writeln(e.msg);
            }
  • Single request compression, asynchronous call, set 100 bytes of dynamic compression, request timeout 30 seconds

            //use dynamic compress and set request timeout


            try{
                RPC_PACKAGE_COMPRESS_DYNAMIC_VALUE = 100; //reset compress dynamaic value 100 byte, default:200 byte

                addressBookService.getContactList(name, delegate(Contacts c){
                        
                        foreach(v; c.userInfoList)
                        {
                            writefln("dynamic compress test: sync number:%s, name:%s, phone:%s, age:%s", c.number, v.name, v.widget, v.age);
                        }

                    }, RPC_PACKAGE_COMPRESS_TYPE.RPCT_DYNAMIC, 30
                );

            }catch(Exception e)
            {
                writeln(e.msg);
            }

Server-side service file code (example directory: IDL-Example/server/source/IDL/kissidlInterface.d):

Server-side interfaces handle asynchronous events.
  • RpcAddressBookService.getContactList

    Contacts getContactList(AccountName accountName){

        Contacts contactsRet;
        //input service code for Contacts class
        contactsRet.number = accountName.count;

        for(int i = 0; i < 10; i++)
        {
            UserInfo userInfo;
            userInfo.age = 18+i;
            userInfo.name = accountName.name ~ to!string(i);
            userInfo.widget = 120+i;
            contactsRet.userInfoList ~= userInfo;
        }

        return contactsRet;
    }

Keywords: Linux socket

Added by metin on Sat, 08 Jun 2019 01:01:25 +0300