Hunt Net - D 语言网络框架


Apache
跨平台
D语言

软件简介

Hunt Net 是基于 hunt 库实现的 D 语言网络传输协议层封装,类似 netty / mina
但测试下来性能更高,内置编解码规则标准化自定义的传输协议,能够实现快速开发各种基于 TCP 的自定义网络传输协议。

支持的网络模型:

  1. IOCP: Windows 7 / 8 / 10
  2. kqueue: macOS / FreeBSD / netbsd / openbsd
  3. epoll: Linux / Android

服务端使用示例:

import hunt.net;
import hunt.net.codec.textline;

import hunt.logging;

void main()
{
    NetServerOptions options = new NetServerOptions();
    NetServer server = NetUtil.createNetServer!(ThreadMode.Single)(options);

    server.setCodec(new TextLineCodec);
    server.setHandler(new class ConnectionEventHandler
    {
        override void messageReceived(Connection connection, Object message)
        {
            import std.format;

            string str = format("data received: %s", message.toString());
            connection.write(str);
        }
    }).listen("0.0.0.0", 9999);
}

客户端使用示例:

import hunt.net;
import hunt.net.codec.textline;

import hunt.logging;

void main()
{
    NetClient client = NetUtil.createNetClient();

    client.setCodec(new TextLineCodec);
    client.setHandler(new class ConnectionEventHandler
    {
        override void messageReceived(Connection connection, Object message)
        {
            import std.format;
            import hunt.String;

            string str = format("data received: %s", message.toString());

            connection.write(new String(str));
        }
    }).connect("localhost", 9999);
}