Json-RPC在C++开发环境下的使用

曾翰飞
2023-12-01

背景:

为了能让C/C++开发的程序,能和java等其他语种的app能实现互联,在架构中使用了Json-RPC的作为了通信链路。

在java语言中很多的框架集成了Json-RPC功能,比如jsonrpc4j、jpoxy、json-rpc等。使用非常方便。但C++能找的到的确实不多,json-rpc-cpp是一个相对不错的实现功能的框架。

要使用它,必须先编译再使用。编译的步骤之前以前写过参考:

jsonrpc、jsoncpp和libjson-rpc-cpp概念和编译 - 知乎专栏

接下来通过例子来说明如何使用:

打开VS2017 , 新建一个console工程。


服务端代码如下:

/*
 *  JsonRpc-Cpp - JSON-RPC implementation.
 *  Copyright (C) 2008-2011 Sebastien Vincent <sebastien.vincent@cppextrem.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file tcp-server.cpp
 * \brief Simple JSON-RPC TCP server.
 * \author Sebastien Vincent
 */

#include <cstdio>
#include <cstdlib>
#include <csignal>

#include "jsonrpc.h"

#include "test-rpc.h"

/**
 * \var g_run
 * \brief Running state of the program.
 */
static volatile sig_atomic_t g_run = 0;

/**
 * \brief Signal management.
 * \param code signal code
 */
static void signal_handler(int code)
{
  switch(code)
  {
    case SIGINT:
    case SIGTERM:
      g_run = 0;
      break;
    default:
      break;
  }
}

/**
 * \brief Entry point of the program.
 * \param argc number of argument
 * \param argv array of arguments
 * \return EXIT_SUCCESS or EXIT_FAILURE
 */
int main(int argc, char** argv)
{
  TestRpc a;
  Json::Rpc::TcpServer server(std::string("127.0.0.1"), 8086);

  /* to avoid compilation warnings */
  (void)argc;
  (void)argv;

  if(!networking::init())
  {
    std::cerr << "Networking initialization failed" << std::endl;
    exit(EXIT_FAILURE);
  }

  if(signal(SIGTERM, signal_handler) == SIG_ERR)
  {
    std::cout << "Error signal SIGTERM will not be handled" << std::endl;
  }

  if(signal(SIGINT, signal_handler) == SIG_ERR)
  {
    std::cout << "Error signal SIGINT will not be handled" << std::endl;
  }

  server.AddMethod(new Json::Rpc::RpcMethod<TestRpc>(a, &TestRpc::Print,
        std::string("print")));
  server.AddMethod(new Json::Rpc::RpcMethod<TestRpc>(a, &TestRpc::Notify,
        std::string("notify")));
  
  /* server.SetEncapsulatedFormat(Json::Rpc::NETSTRING); */

  if(!server.Bind())
  {
    std::cout << "Bind failed" << std::endl;
    exit(EXIT_FAILURE);
  }

  if(!server.Listen())
  {
    std::cout << "Listen failed" << std::endl;
    exit(EXIT_FAILURE);
  }

  g_run = 1;

  std::cout << "Start JSON-RPC TCP server" << std::endl;

  while(g_run)
  {
    server.WaitMessage(1000);
  }

  std::cout << "Stop JSON-RPC TCP server" << std::endl;
  server.Close();
  networking::cleanup();

  return EXIT_SUCCESS;
}

原文链接:https://zhuanlan.zhihu.com/p/26336416  

 类似资料: