Giter VIP home page Giter VIP logo

displayname2023 / qtnetworkng Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hgoldfish/qtnetworkng

0.0 0.0 0.0 6.45 MB

QtNetwork Next Generation. A coroutine based network framework for Qt/C++, with more simpler API than boost::asio.

License: GNU Lesser General Public License v3.0

Shell 1.84% C++ 5.69% Python 0.01% C 54.19% Assembly 11.36% Makefile 11.11% QMake 0.05% CMake 0.39% Batchfile 0.05% M4 0.12% Roff 15.19%

qtnetworkng's Introduction

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 QDataStream
  • Cipher, MessageDigest, PublicKey, PrivateKey wrap complicate LibreSSL C API.

Examples

Here comes a simple example to get web pages.

#include <QtCore/qdebug.h>
#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 IPv4 tcp connection.

#include <QtCore/qdebug.h>
#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 IPv4 tcp server.

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

To create HTTP server is even more simpler:

TcpServer<SimpleHttpRequestHandler> httpd(HostAddress::LocalHost, 8000);
httpd.serveForever();

A Qt GUI example to fetch web page.

// main.cpp
#include <QApplication>
#include <QTextBrowser>
#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://www.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.pro
TEMPLATE = app
QT += widgets
SOURCES += main.cpp
include(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://www.gnu.org/licenses/lgpl-3.0.en.html

Dependencies

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

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

Supported Platforms

Linux, Android, MacOS, Windows and OpenBSD is supported.

iOS is not tested yet, as I have no iOS machines. FreeBSD is never tested either.

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.1 server.
  • HTTP support gzip compression.
  • HttpResponse support stream.
  • Support HTTP proxy and cache.
  • A simple replacement for libev in Windows.
  • Add more OpenSSL functions.
  • Support verification/ALPS for https connection.
  • Support MacOS and iOS platforms.
  • Remove the QtNetwork dependence.

Towards 2.0

  • Support HTTP/2
  • Support HTTP/3
  • Support Kademlia
  • Support Tox

Building

  1. Clone QtNetworkNg from github as git subrepository.
  2. include qtnetworkng/qtnetworkng.pri in your project.pro file.
  3. 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.

qtnetworkng's People

Contributors

hgoldfish avatar xuyouqiao2005 avatar olk avatar privatesummer avatar lianhansheng 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.