Code Monkey home page Code Monkey logo

znet's Introduction

znet - a simple C network library
---------------------------------

This library is inspired by Yawei Zhang's ZsummerX[1] library, but this one is
a pure C version. Now it implement a IOCP (IO Completion Port) backend on
Windows, and a epoll backend on Linux, a kqueue backend on BSD and Mac OS X
systems, and select backend for others.

The ZNet library itself is a single-header library[2], and have some utils
single-header libraries for common usages. all liraries are standalone and can
used without other headers.

See test.c for examples, and you can see more things in examples folder.

Utils:

zn_buffer.h: mainly based the luaL_Buffer in Lua code. But the interface is
targeted by send/recv method, so it's very useful to make znet send data
continually and receive packets by packets.

zn_deque.h: a double-direct pipe implement that can be used a queue or stack
cross threads. it implements block API like push/pop from deque, and non-block
API to fetch the front and back items from deque.

zn_task.h: a task thread pool implement. you can create a task pool and post
tasks to pool, task will run in other thread, you can use zn_deque.h to notice
original thread the completion of tasks.

The znet library and utils have the same license when Lua[3], have fun :)


[1]: https://github.com/zsummer/zsummerX
[2]: https://github.com/nothings/stb
[3]: https://www.lua.org/license.html

znet's People

Contributors

gajin2 avatar nburrus avatar ownwaterloo avatar starwing avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

znet's Issues

EPOLL_CTL_DEL and close

总之:

  • epoll_ctl(epoll, EPOLL_CTL_DEL, fd, 0) 肯定能将fd从epoll set中移除,epoll_wait不会报告fd。
  • 如果fd是其指向的file description的最后一个引用 close(fd) 会关闭file description并且从epoll set中移除。 否则epoll_wait有可能会报告。

man 7 epoll

Will closing a file descriptor cause it to be removed from all epoll sets automatically?

用户即使摸不到fd,也可以fork。。。 保险起见还是保持原样吧,也就多一个系统调用而已。。。
libuv也这样改过: linux: always deregister closing fds from epoll

看后关。。。

need zn_Tcp pool

我看每个tcp的buffer有个pool,那tcp本身需不需要一个pool呢?

ZN_MAX_ADDRLEN

不知道INET6_ADDRSTRLEN在windows下是个什么状况。
msvc/mingw(or cygwin?)的各个版本里自带的header里有没有?
因为windows下比较混乱的原因, 不直接用INET6_ADDRSTRLEN这个名字也行, 但目前用的值实在是太大了。。。 zn_PeerInfo和zn_Tcp非常大。。。

GetQueuedCompletionStatusEx and event buffer size

分摊系统调用开销。于是就和linux一样面临一个问题:用多大的buffer。。。?
太小的话,系统调用开销占的比例就高。比如znet_iocp.h里的znS_poll
太大也不好,(在最坏被填充满的情况下)会过多占用缓存。比如znet_epoll.h里的znS_poll。4096*12。。。

so。。。 将buffer大小的选择权交给,或者说责任推卸给,用户?
具体做法么。。。 可以是:

  • znS_poll(...,EVENT buf[], size_t size); EVENT被分别定义为OVERLAPPED_ENTRY或epoll_event。
    用户想分配多大就分配多大,想怎么分配就怎么分配。
    或者更狠一点:
  • znS_poll(...,void* buf, size_t size); 内部用一个除法。。。 也许还需要提供一个alignment,给那些不打算用malloc或new的人。

Feature request.

example.c file's cpp version would be very apriciated. Thank you.

error in zn_sendfinish function

if (b->sending->used == count) { /* all sent? */

need to changed to:

if (b->sending->used - b->sent_count == count) { /* all sent? */

problematic inet_addr

这个函数无法区分错误。。。 可以用getaddrinfo。。。 除了可以检查错误外,它可以同时处理v4(比如"127.0.0.1")和v6("::1")。 但用起来比较麻烦, 还不知道windows上是不是支持。。。
不管怎样也得把inet_addr给排除掉。。。

另外。。。 是不打算解析地址吧。。。 但const char *addr这样的参数。。。
zn_connect不解析地址,总有一个地方要解析, 一般就直接解析成二进制形式了, 然后再转成字符串传进来???

asynchronize getaddrinfo/getnameinfo

@ownwaterloo 计划加入 getaddrinfo 和 getnameinfo 的功能了。主要的动机其实是异步执行耗时任务,也就是线程池,如果不想自己写mysql的协议,那么异步执行阻塞任务其实是很重要的事情。

那么,目前的工作就成了:

  1. 加入异步任务功能,zn_Work
  2. 利用异步任务,实现异步的getaddrinfo和getnameinfo功能
  3. 添加异步任务的Lua绑定(可能会比较麻烦,毕竟Lua无法多线程执行,可能需要新建一个lua_State执行异步的任务)。

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.