Hunt NetD 語言網(wǎng)絡(luò)框架
Hunt Net 是基于 hunt 庫實現(xiàn)的 D 語言網(wǎng)絡(luò)傳輸協(xié)議層封裝,類似 netty / mina 但測試下來性能更高,內(nèi)置編解碼規(guī)則標準化自定義的傳輸協(xié)議,能夠?qū)崿F(xiàn)快速開發(fā)各種基于 TCP 的自定義網(wǎng)絡(luò)傳輸協(xié)議。
支持的網(wǎng)絡(luò)模型:
- IOCP: Windows 7 / 8 / 10
- kqueue: macOS / FreeBSD / netbsd / openbsd
- epoll: Linux / Android
服務(wù)端使用示例:
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);
}
評論
圖片
表情
