Coder Social home page Coder Social logo

net's Introduction

net

个人毕设作品。。。。。。

目前初步完成。继续完善中。。。

net是一个基于Reactor 模式的现代化C++11网络库。

自带TCP协议的异步非阻塞式的服务器和客户端库。

实现上深度借鉴muduoevpp两个开源网络库。

性能上没太多测试,不过个人感觉性能还不错。。。

没啥第三方库依赖,只要支持c++11就行。。。

需要的linux版本没测试。。。有时间再说。。。

使用时,需要确保TcpClient和TcpServer的生命周期至少在EvnetLoop::run()函数退出之后结束

特性

现代版的C++11接口

非阻塞异步接口都是C++11的functional形式的回调仿函数

非阻塞纯异步多线程TCP服务器/客户端

单核环境支持

线程安全

线程安全的退出,重启

客户端断线自动重连

服务端可同时监听多个ip/port

ipv6支持

例子

Tcp Echo Server

#include"TcpServer.h"
#include"NetFwd.h"

using namespace net;

int main(int argc, char* argv[]) {
    InetAddress addr("127.0.0.1",55555);

    EventLoop loop;

    TcpServer server(&loop, addr, "TCPEchoServer", 3);

    server.set_message_cb([](const TCPConnPtr& conn, Buffer* msg) {
        conn->send(msg);
    });

    server.set_connection_cb([](const TCPConnPtr & conn) {
        if (conn->is_connected()) {
            LOG_INFO << "A new connection from " << conn->get_peer_addr().toIpPort();
        } else {
            LOG_INFO << "Lost the connection from " << conn->get_peer_addr().to_ip_port();
        }
    });

    server.run();
    loop.run();
}

Tcp Echo Client

#include <iostream>
#include <unistd.h>
#include "TcpClient.h"
#include "NetFwd.h"
#include "Socket.h"
#include <string>

using namespace std;
using namespace net;


int main() {
    EventLoop loop;

    TcpClient client(&loop,InetAddress("127.0.0.1", 55555),"ChatClient");

    client.set_message_cb([](const TCPConnPtr& conn, Buffer* msg) {
        LOG_INFO<<"receive:"<<msg->read_ptr();
    });

    Socket::setNonBlockAndCloseOnExec(STDIN_FILENO);

    impl::Event e{&loop,STDIN_FILENO};

    e.set_read_cb([&client]{
        string s;
        getline(cin,s);
        client.connection()->send(s);
    });

    client.set_connection_cb([&e](const TCPConnPtr& conn) {
        if(conn->is_connected())
            e.enable_read();
        else
            e.disable_all();
    });

    client.set_message_cb([&e](const TCPConnPtr& conn,Buffer*msg) {
        msg->append("\0",1);
        LOG_INFO<<"receive:"<<msg->read_ptr();
        msg->clear();
    });

    client.connect();
    loop.run();
}

net's People

Contributors

oocpp avatar

Watchers

 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.