C++ Open Source Project Suitable for Embedded Systems – Workflow

Before joining Tencent, I had never really developed a complete project in C++. However, I was quickly thrown into the deep end, as the people at Tencent particularly enjoy using C++, and they do it exceptionally well. This time, I would like to recommend an open-source C++ project for those who love C++ or want to develop in that direction.

Procedural programming tends to lean towards the lower level, while object-oriented programming is more application-oriented. Don’t argue!

C++ Open Source Project Suitable for Embedded Systems - Workflow

Let’s introduce this C++ open-source project

Project Name: Workflow

Project Address: https://github.com/sogou/workflow

C++ Open Source Project Suitable for Embedded Systems - Workflow

C++ Open Source Project Suitable for Embedded Systems - Workflow

What scenarios is this project suitable for?

This project is the server engine of Sogou, and almost all backend C++ services at Sogou and several other companies are using this engine, processing over a hundred billion requests daily.

Moreover, this project is also very suitable for implementing network framework design in embedded applications. Framework design is a crucial factor for the success of a project, and networking is especially important in AIOT, smart homes, and smart hardware.

For example, if we need to use HTTP in the project, Workflow is designed to support multiple platforms like Windows, Linux, or MacOS through CMake configuration for compiling multiple projects, while maintaining the same interface across different platforms.

If you add a new embedded device, like ESP32, you only need to maintain one set of code for both the server and client to meet your application needs.

It truly achieves the goal of porting cloud technology to run on embedded devices.

C++ Open Source Project Suitable for Embedded Systems - Workflow

Major Features for Embedded Systems

1. Supports Multiple Platforms and Architectures

Multi-platform capability is a very important feature; multi-platform code indicates that many design details have been considered in configuration and coupling. Currently, Workflow can run happily not only on Linux, Windows, MacOS, and Android, but also on Raspberry Pi and domestic Longxin processors among various architectures.

2. Fast Compilation

Workflow does not depend on other libraries except OpenSSL, and there are no templates in the interface layer, thus the compilation speed is very fast, allowing a usable lib to be compiled in less than a minute.

3. Small Size and Support for Compilation Trimming

The Kafka protocol in Workflow is not compiled by default; additionally, other less commonly used modules can be trimmed away.

For example:

make REDIS=n MYSQL=n UPSTREAM=n

And you can use the strip command to remove symbolic links, reducing the library file size to around 400k.

4. Low Runtime Memory Usage and Fast Scheduling

As an asynchronous scheduling library, Workflow has consistently shown excellent scheduling performance. Additionally, its runtime memory usage is very small.

Memory usage test of the helloworld server service under the tutorial in default configuration

Run the service

C++ Open Source Project Suitable for Embedded Systems - Workflow

Find the process ID

C++ Open Source Project Suitable for Embedded Systems - Workflow

Check memory usage; physical memory usage is at 3824kb

C++ Open Source Project Suitable for Embedded Systems - Workflow

5. Very Convenient Custom Protocols

Additionally, the active community and the project leader’s prompt responses to issues are also highlights. If there are problems in framework construction and porting, asking questions in the community will yield quick responses.

C++ Open Source Project Suitable for Embedded Systems - Workflow

C++ Open Source Project Suitable for Embedded Systems - Workflow

Detailed Discussion on Compilation and Custom Protocols

Open source is probably the main reason I recommend this project. Another reason is that this project is very suitable for use in large embedded projects.

I previously spent a long time on communication protocols during my startup, and if I had discovered this project back then, I believe applying this framework would have made things much easier. This project uses CMake to configure, and I think the CMake documentation I previously shared would also be useful to everyone.

Supplement on Embedded Discussions and Makefile

CMake should indeed be learned when needed.

C++ Open Source Project Suitable for Embedded Systems - Workflow

My startup project involved many clients, and the embedded clients interacted with the server to exchange data. For this reason, we defined our own protocol and implemented many avoidance measures, although these measures had not withstood the test of time.

However, the example of Workflow’s custom protocol is directly provided in the tutorial, and interested friends can download it and

compile a custom protocol server and client with the following commands:

make
cd tutorial
make
./tutorial-10-user_defined_protocol/server

Execute the client in another window to communicate:

./tutorial-10-user_defined_protocol/client

Execution Process:

C++ Open Source Project Suitable for Embedded Systems - Workflow

C++ Open Source Project Suitable for Embedded Systems - Workflow

Open Source Support for Porting to Embedded Devices

It is reported that many users are already using it on embedded systems. I found on GitHub that the officials are particularly patient in explaining when problems arise, which is really great; this is how technology should be!

C++ Open Source Project Suitable for Embedded Systems - Workflow

C++ Open Source Project Suitable for Embedded Systems - Workflow

Project Examples

The official examples are quite numerous; I recommend beginners to check them out, as starting with these will help you get into the groove faster.

C++ Open Source Project Suitable for Embedded Systems - Workflow

A simple code for Client and Server

Server:

#include <stdio.h>
#include "workflow/WFHttpServer.h"

int main() {
    WFHttpServer server([](WFHttpTask *task) {
        task->get_resp()->append_output_body("Hello World!");
    });
    if (server.start(8888) == 0) { // start server on port 8888
        getchar(); // press "Enter" to end.
        server.stop();
    }
    return 0;
}

Client:

int main(int argc, char *argv[]) {
    WFMySQLTask *task = WFTaskFactory::create_mysql_task(url, RETRY_MAX, mysql_callback);
    task->get_req()->set_query("SHOW TABLES;");
    task->start();
}
C++ Open Source Project Suitable for Embedded Systems - Workflow

Task Flow Framework Design

In the author’s design philosophy, all business logic is a task, and multiple tasks can form a task flow, which can be organized into a graph. This graph can be a series graph, a parallel graph, or a combination of both, similar to this:

C++ Open Source Project Suitable for Embedded Systems - Workflow

It can also be this complex DAG graph:

C++ Open Source Project Suitable for Embedded Systems - Workflow

Of course, the hierarchy of the graph can be customized by the user. Personally, I think the most impressive aspect of the framework is its support for dynamically creating task flows.

This project is roughly introduced here.

Let me also mention the project’s resource summary.

Reference Materials:

https://zhuanlan.zhihu.com/p/358869362

https://zhuanlan.zhihu.com/p/165638263

Project Address is as follows:

https://github.com/sogou/workflow You can also clickto read the original text directly.

If you encounter difficulties accessing GitHub, you can use their official Gitee repository:

https://gitee.com/sogou/workflow

If you feel this project is worth learning, give it a star. Don’t just take advantage of it; it’s also a form of recognition and encouragement for the project team.

Leave a Comment