Coder Social home page Coder Social logo

uv-cpp's Introduction

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 functional/bind style callback instead of C-style function pointer.
  • TCP and UDP wrapper.
  • TimerandTimerWheel:Heartbeat timeout judgment with time complexity of O(1).
  • Async:libuv async wrapper,but optimized the problem of calling multiple times but callback will only be called once.
  • PacketandPacketBuffer:Send and receive packet of Tcp data stream.
  • 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();
    
    uv::SocketAddr serverAddr("127.0.0.1", 10000, uv::SocketAddr::Ipv4);
    //Tcp Server
    uv::TcpServer server(loop);
    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, (unsigned int)size);
        std::cout << str << std::endl;
        conn->write(data, size,nullptr);
    });
    server.bindAndListen(serverAddr);


    //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();
}


uv-cpp's People

Contributors

wlgq2 avatar

Watchers

James Cloos 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.