RCF interprocess Communication Demo Program

Source: Internet
Author: User

In the previous article, RPC Communication framework--RCF introduced the advantages of RCF, this article demonstrates from the beginning how to use RCF to write a demo program for cross-process communication.

To compile RCF as a static library

The source code downloaded from the official website contains a RCF project, but the project is used to compile the dynamic library. You can refer to this project to set up a static library.

First create an empty project file, and then set the compile to static library, add source file RCF.cpp, only need this one file is enough, because, this file contains all other source files. This source code reference way, compiled into a dynamic library, but also acceptable, but compiled into a static library there will be some problems, later in the article discussed again, does not affect the demo demonstration.

Then add the preprocessor:

win32_lean_and_mean_win32_winnt=0x0500

To disable a specific warning (not required):

4275 4251 4510 4511 4512 4127 4702

To add an additional dependency:

Ws2_32.lib

This makes it possible to compile a static library.

Client, Server project configuration

The project configuration of Client and server, first of all, is a very common configuration that refers to the path of the RCF static library, and one thing to note is that you need to add the following preprocessor:

win32_lean_and_mean_win32_winnt=0x0500
Writing interface Definitions

After the project is configured, you need to add the client, server communication functionality, and their communication is based on a predefined interface, defined as follows:

#pragmaOnce#include"rcf/rcf.hpp"#include<windows.h>#include<iostream>#include<string>using namespacestd; Rcf_begin (I_hello,"I_hello") RCF_METHOD_V1 (void, SayHello,Const string&) Rcf_method_r1 (int, add,int&) Rcf_method_v0 (void, test) Rcf_end (I_hello)
To write the server implementation of an interface

The definition of an interface is used to define the function of communication support and the Protocol of communication. With these rules, you need to implement specific functions, and this is the server-side implementation, the client side does not need to know the details of these implementations, the code is as follows:

#pragmaOnce#include"rcf/rcf.hpp"#include<windows.h>#include<iostream>#include<string>using namespacestd; #include"hello.h"classhelloimpl{ Public:    voidSayHello (Const string&World ) {cout<<"Hello"<< World <<Endl; }    intAddint&a) {a= A +A; returnA +2; }    voidTest () {}};
Writing the Server service

So next, you need to write a console or a Windows service to host this server service, and choose a simple console program here.

RCF supports three kinds of communication protocols under Windows: TCP, UDP, Named Pipes,

Here we choose TCP:

#include"stdafx.h"#include"hello.h"int_tmain (intARGC, _tchar*argv[])    {Rcf::rcfinitdeinit rcf_init;    Helloimpl Hello; Rcf::rcfserver Server (Rcf::tcpendpoint (50001)); Server.bind<I_HELLO>(hello);    Server.start ();  while(true) {Sleep ( +); }    return 0;}

As the code shows, at the entrance of the program, it is necessary to use the constructor of the Rcfinitdeinit class to initiate RCF-related initialization, and when the program exits, the RCF is cleared by Rcfinitdeinit's destructor function.

After RCF initialization, through the Rcfserver bind method, the binding interface corresponds to the specific implementation, and then start the service by starting the method.

Writing client Programs

The client program, before calling the RCF-related method, also needs to be initialized through Rcfinitdeinit, with the following code:

#include"stdafx.h"#include"hello.h"#include"stop_watch.h"int_tmain (intARGC, _tchar*argv[])    {Rcf::rcfinitdeinit rcf_init; Rcfclient<I_HELLO> Client (Rcf::tcpendpoint (50001)); stringstr ="Test"; Client.    SayHello (str); intA =3; intb =Client.add (a); cout<<"A ="<< a <<", B ="<< b <<Endl;    Stop_watch Watch;    Watch.start ();  for(inti =0; I <20000; ++i) {client.test ();    } watch.stop (); cout<< Watch.elapsed_ms () <<"Ms"<<Endl; return 0;}
Simple performance Test

In my Notebook Windows7 Professional Edition SP1 x64, Intel (R) Core (TM) i5-2450m CPU @ 2.5GHz, 12G memory on the machine, through this demo, the RCF was tested.

Calls 20,000 times, takes about 1647ms, the average can be called 12,143 times per second, averaging 82 subtle calls per call.

Chronograph tool--stop_watch

Timing tool Stop_watch class, you can refer to the C + + high-precision timer-microsecond time statistics.

Resources

RCF User Guide

RCF interprocess Communication Demo Program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.