Dynamically creating IPC channels for C++ development
Dynamic creation of an IPC channel on the client side includes the following steps:
- Include the generated description header file (
*.edl.cpp.h
) in the client program. - Include the generated header files of the descriptions of the utilized interfaces (
*.idl.cpp.h
). - Include the header files:
/opt/KasperskyOS-Community-Edition-<version>/sysroot-*-kos/include/kosipc/application.h
/opt/KasperskyOS-Community-Edition-<version>/sysroot-*-kos/include/kosipc/make_application.h
/opt/KasperskyOS-Community-Edition-<version>/sysroot-*-kos/include/kosipc/connect_dynamic_channel.h
- Get the pointers to the server name and the qualified name of the endpoint by using a name server, which is a special kernel service provided by the
NameServer
program. To do so, you must connect to the name server by calling theNsCreate()
function and find the server that provides the required endpoint by using theNsEnumServices()
function. For more details, refer to Dynamically creating IPC channels (cm_api.h, ns_api.h). - Create an application object by calling the
kosipc::MakeApplicationAutodetect()
function. (You can also use thekosipc::MakeApplication()
andkosipc::MakeApplicationPureClient()
functions.) - Create a proxy object for the required endpoint by calling the
MakeProxy()
function. Use thekosipc::ConnectDynamicChannel()
function call as the input parameter of theMakeProxy()
function. Pass the pointers for the server name and qualified name of the endpoint obtained at step 4 to thekosipc::ConnectDynamicChannel()
function.
After successful initialization of the proxy object, the client can call methods of the required endpoint.
Example
Dynamic creation of an IPC channel on the server side includes the following steps:
- Include the generated header file (
*.edl.cpp.h
) containing a description of the component structure of the server, including all provided endpoints, in the server program. - Include the header files:
/opt/KasperskyOS-Community-Edition-<version>/sysroot-*-kos/include/kosipc/application.h
/opt/KasperskyOS-Community-Edition-<version>/sysroot-*-kos/include/kosipc/event_loop.h
/opt/KasperskyOS-Community-Edition-<version>/sysroot-*-kos/include/kosipc/make_application.h
/opt/KasperskyOS-Community-Edition-<version>/sysroot-*-kos/include/kosipc/root_component.h
/opt/KasperskyOS-Community-Edition-<version>/sysroot-*-kos/include/kosipc/serve_dynamic_channel.h
/opt/KasperskyOS-Community-Edition-<version>/sysroot-*-kos/include/kosipc/simple_connection_acceptor.h
- Create classes containing the implementations of interfaces that the server provides as endpoints. Create and initialize the objects of these classes.
- Create an application object by calling the
kosipc::MakeApplicationAutodetect()
function. - Create and initialize the
kosipc::components::Root
class object that describes the structure of components and endpoints of the server. This structure is generated from the descriptions in the CDL and EDL files. - Bind the
kosipc::components::Root
class object to the class objects created at step 3. - Create and initialize the
kosipc::EventLoop
class object that implements a loop for dispatching incoming IPC messages by calling theMakeEventLoop()
function. Use theServeDynamicChannel()
function call as an input parameter of theMakeEventLoop()
function. Pass thekosipc::components::Root
class object created at step 5 to theServeDynamicChannel()
function. - Start the loop for dispatching incoming IPC messages in a separate thread by calling the
Run()
method of thekosipc::EventLoop
object. - Create and initialize the object that implements the handler for receiving incoming requests to dynamically create an IPC channel.
When creating an object, you can use the
kosipc::SimpleConnectionAcceptor
class, which is the standard implementation of thekosipc::IConnectionAcceptor
interface. (Thekosipc::IConnectionAcceptor
interface is defined in the file named/opt/KasperskyOS-Community-Edition-<version>/sysroot-*-kos/include/kosipc/connection_acceptor.h
.) In this case, the handler will implement the following logic: if the endpoint requested by the client was published on the server, the request from the client will be accepted. Otherwise, it will be rejected.If you need to create your own handler, you should implement your own request handling logic in the
OnConnectionRequest()
method inherited from thekosipc::IConnectionAcceptor
interface. This method will be called by the server when it receives a request for dynamic IPC channel creation from the client. - Create a
kosipc::EventLoop
class object that implements a loop for receiving incoming requests to dynamically create an IPC channel by calling theMakeEventLoop()
function. Use theServeConnectionRequests()
function call as an input parameter of theMakeEventLoop()
function. Pass the object created at step 9 to theServeConnectionRequests()
function.There can only be one loop for receiving incoming requests to dynamically create an IPC channel. The loop must work in one thread. The loop for receiving incoming requests to dynamically create an IPC channel must be created after the loop for dispatching incoming IPC channels is created (see step 7).
- Start the loop for receiving incoming requests for a dynamic connection in the current thread by calling the
Run()
method of thekosipc::EventLoop
object.
Example
If necessary, you can create and initialize multiple kosipc::components::Root
class objects combined into a list of objects of the ServiceList
type using the AddServices()
method. For example, use of multiple objects enables you to separate components and endpoints of the server into groups or publish endpoints under different names.
Example