Git Product home page Git Product logo

lafrpc's Introduction

laf_rpc

A simple RPC implemented based on QtNetworkNg. Many features:

  • Using TCP protocol
  • Long connection
  • Connected peers can call each other.
  • Support sending file

Here comes an hello world example:

The server source:

// server.cpp
#include "lafrpc.h"

using namespace lafrpc;

class Hello: public QObject
{
    Q_OBJECT
public slots:
    QString sayHello(const QString &name) { return QStringLiteral("Hello, %1").arg(name); }
};

int main(int argc, char **argv)
{
    QSharedPointer<Rpc> rpc = RpcBuilder(MessagePack).myPeerName("server").create();
    QSharedPointer<Hello> hello(new Hello());
    rpc->registerInstance(hello, "demo");
    rpc->startServer("tcp://127.0.0.1:8002");
    return 0;
}

#include "server.moc"

The client source:

// client.cpp
#include "lafrpc.h"

using namespace lafrpc;

int main(int argc, char **argv)
{
    QSharedPointer<Rpc> rpc = RpcBuilder(MessagePack).myPeerName("client").create();
    QSharedPointer<Peer> peer = rpc->connect("tcp://127.0.0.1:8002");
    if (peer.isNull()) {
        qDebug() << "can not connect to peer.";
        return 2;
    }
    QString result = peer->call("demo.sayHello", "Goldfish").toString();
    qDebug() << result;
    return 0;
}

The cmake build file:

CMAKE_MINIMUM_REQUIRED(VERSION 3.3.0 FATAL_ERROR)

PROJECT(pbook)

SET(CMAKE_AUTOMOC ON)

FIND_PACKAGE(Qt5Core REQUIRED)

ADD_SUBDIRECTORY(lafrpc/cpp/ lafrpc)

ADD_EXECUTABLE(server server.cpp)
ADD_EXECUTABLE(client client.cpp)

TARGET_LINK_LIBRARIES(server Qt5::Core lafrpc)
TARGET_LINK_LIBRARIES(client Qt5::Core lafrpc)

Now your project have 3 files:

hello/
    server.cpp
    client.cpp
    CMakeLists.cpp

Clone this project to project directory:

git clone --recursive https://github.com/hgoldfish/lafrpc

Then you have 3 files and a subdirectory:

hello/
    server.cpp
    client.cpp
    CMakeLists.txt
    lafrpc/

Build and test:

mkdir build
cmake ..
make -j8
./server
./client   # in another console.

lafrpc's People

Contributors

dependabot[bot] avatar hgoldfish avatar singo27 avatar xuyouqiao2005 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.