Skip to content

wlgq2/uv-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Sep 30, 2019
7da8d52 · Sep 30, 2019
Sep 30, 2019
May 25, 2019
Sep 26, 2019
Sep 30, 2019
Aug 15, 2017
Sep 6, 2019
Aug 6, 2018
Sep 20, 2019
Sep 20, 2019

Repository files navigation

uv-cpp

Github release Platform License Project Status: Active – The project has reached a stable, usable state and is being actively developed.


Language Translations:


Features

  • C++11 style callbacks instead of C: You can use class member functions or lambda as callbacks.
  • TCP and UDP wrapper.
  • TimerandTimerWheel:Heartbeat timeout judgment mechanism with time complexity of O(1).
  • Async:Optimized the problem of calling multiple times, perhaps only once, compared to the libuv's async.
  • PacketandPacketBuffer:Send and receive packet data, used to resolve TCP stubs/stickers.
  • Log interface.

Simple test

one thread 1k bytes ping-pong。
environment:Intel Core i5 6402 + ubuntu14.04.5 + gcc5.5.0 + libuv1.22.0 + O2优化

  libuv_cpp  no use PacketBuffer CycleBuffer ListBuffer
Times/Sec 192857 141487 12594

Quick start

#include <iostream>
#include <uv/uv11.h>


int main(int argc, char** args)
{
    //event's loop
    //uv::EventLoop* loop = new uv::EventLoop();
    //or
    uv::EventLoop* loop = uv::EventLoop::DefalutLoop();
    //Tcp Server
    uv::SocketAddr serverAddr("0.0.0.0", 10002, uv::SocketAddr::Ipv4);
    uv::TcpServer server(loop, serverAddr);
    server.setMessageCallback(
        [](uv::TcpConnectionPtr conn, const char* data , ssize_t size)
    {
        std::cout << std::string(data, size) << std::endl;
        std::string str("hex :");
        uv::LogWriter::ToHex(str, data, size);
        std::cout << str << std::endl;
        conn->write(data, size,nullptr);
    });
    server.start();

    //Tcp Client
    uv::TcpClient client(loop);
    client.setConnectStatusCallback(
        [&client](uv::TcpClient::ConnectStatus status)
    {
        if (status == uv::TcpClient::ConnectStatus::OnConnectSuccess)
        {
            char data[] = "hello world!";
            client.write(data, sizeof(data));
        }
        else
        {
            std::cout << "Error : connect to server fail" << std::endl;
        }
    });
    client.connect(serverAddr);

    loop->run();
}