QtNetworkNg:基于协程的Qt / C ++网络框架,API比boost :: asio更简单

网友投稿 1933 2022-10-14

QtNetworkNg:基于协程的Qt / C ++网络框架,API比boost :: asio更简单

QtNetworkNg:基于协程的Qt / C ++网络框架,API比boost :: asio更简单

QtNetworkNg

Introduction

QtNetworkgNg is a coroutine-based network toolkit. Compare to boost::asio and Qt's QtNetwork, QtNetworkNg has more simpler API which is similar to python-gevent. As the name suggests, QtNetworkNg requires Qt5 framework. For more detail visit:

Introduction to QtNetworkNg

Documents

Visit https://qtng.org/

Features

General Coroutine with similar API to QThread.Socket supports UDP and TCP.SSLSocket with similar API to Socket.KcpSocket implements KCP over UDP.HttpSession implements a HTTP 1.0/1.1 client, supports connection via SOCKS5/HTTP proxy.HttpServr implements a static HTTP 1.0/1.1 server, can be used for reversed http proxy.MsgPackStream is a new MessagePack implementation similar to QDataStreamCipher, MessageDigest, PublicKey, PrivateKey wrap complicate LibreSSL C API.

Examples

Here comes a simple example to get web pages.

#include "qtnetworkng.h"int main(int argc, char **argv){ qtng::HttpSession session; qtng::HttpResponse r = session.get("http://example.com/"); qDebug() << r.html(); return 0;}

And another exmaple to make tcp connection.

#include "qtnetworkng.h"int main(int argc, char **argv){ qtng::Socket conn; conn.connect("example.com", 80); conn.sendall("GET / HTTP/1.0\r\n\r\n"); qDebug() << conn.recv(1024 * 8); return 0;}

To create tcp server.

Socket s;CoroutineGroup workers;s.bind(QHostAddress::Any, 8000);s.listen(100);while (true) { QSharedPointer request(s.accept()); if (request.isNull()) { break; } workers.spawn([request] { request->sendall("hello!"); request->close(); });}

To create HTTP server is even more simpler:

TcpServer httpd(QHostAddress::LocalHost, 8000);httpd.serveForever();

A Qt GUI example to fetch web page.

// main.cpp#include #include #include "qtnetworkng.h"using namespace qtng;class HtmlWindow: public QTextBrowser{public: HtmlWindow(); virtual ~HtmlWindow() override;private: CoroutineGroup *operations;};HtmlWindow::HtmlWindow() :operations(new CoroutineGroup){ operations->spawn([this] { Coroutine::sleep(1); HttpSession session; HttpResponse response = session.get("http://example.com/"); if(response.isOk()) { setHtml(response.html()); } else { setHtml("failed"); } });}HtmlWindow::~HtmlWindow(){ delete operations;}int main(int argc, char **argv){ QApplication app(argc, argv); HtmlWindow w; w.show(); return startQtLoop(); // Qt GUI application start the eventloop using startQtLoop() instead of app.exec()}

And its project file.

# fetch_web_content.proTEMPLATE = appQT += widgetsSOURCES += main.cppinclude(qtnetworkng/qtnetworkng.pri)

As you can see, networking programming is done with very simple API.

License

The QtNetworkNg is distributed under LGPL 3.0 license.

You can obtain a copy of LGPL 3.0 license at: https://gnu.org/licenses/lgpl-3.0.en.html

Dependencies

QtNetworkNg require QtCore, QtNetwork to build. SSL and crypto is supported using embedded LibreSSL.

Qt 5 - https://qt.io/download

Supported Platforms

Linux, Android and OpenBSD is supported.

Macos, iOS is not tested yet, as I have no mac machines.

Windows is supported partially. Because the Qt eventloop is not very efficient, a separate libev event loop is provided in Linux which is not available in Windows. GZip compression is not supported under Windows if zlib library not present.

QtNetworkNg uses more effective boost::context asm code in arm, arm64, x86, amd64 machines, and uses native ucontext or windows fiber API in other architectures.

Towards 1.0

Complete reference documents Implements an HTTP 1.0 server. HTTP support gzip compression. HttpResponse support stream. Support HTTP proxy and cache. Built as shared library(DLL) A simple replacement for libev in Windows. Add more OpenSSL functions. Support verification/ALPS for https connection. Support MacOS and iOS platforms. Remove QtNetwork dependence.

Towards 2.0

Support HTTP/2 Support HTTP/3 Support Kademlia

Building

Clone QtNetworkNg from github as git subrepository.include qtnetworkng/qtnetworkng.pri in your project.pro file.include qtnetworkng.h in you cpp files.

How to Contribute

Create a pull request on github.com with your patch, then make a pull request to me.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:多线程_继承Thread类的方式卖电影票案例
下一篇:Spring事务管理下synchronized锁失效问题的解决方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~