All Projects → OpenHFT → Chronicle Network

OpenHFT / Chronicle Network

Licence: apache-2.0
A High Performance Network ( TCP/IP ) Library

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Chronicle Network

Yasio
A multi-platform support c++11 library with focus on asio (asynchronous socket I/O) for any client application.
Stars: ✭ 483 (+198.15%)
Mutual labels:  socket, udp
Cocoaasyncsocket demo
基于AsyncSocket搭建即时通讯体系 . 包含TCP连接 , 消息发送 , 消息接收 , 心跳处理 ,断网重连 , 消息超时 , 消息分发 , 数据库结构设计 , 消息丢失等 . 以及UI设计, 文本表情消息/语音消息/图片消息/视频消息/文件消息/撤回消息/提示语消息的实现思路讲解
Stars: ✭ 981 (+505.56%)
Mutual labels:  socket, tcp-ip
Elixir Socket
Socket wrapping for Elixir.
Stars: ✭ 642 (+296.3%)
Mutual labels:  socket, udp
Kalm.js
The socket manager
Stars: ✭ 155 (-4.32%)
Mutual labels:  socket, udp
Go Netstat
A netstat implementation written in Go
Stars: ✭ 121 (-25.31%)
Mutual labels:  socket, udp
Netcat
💻 Netcat client and server modules written in pure Javascript for Node.js.
Stars: ✭ 315 (+94.44%)
Mutual labels:  socket, udp
Godsharp.socket
An easy-to-use .NET socket server and client.
Stars: ✭ 35 (-78.4%)
Mutual labels:  socket, udp
Socketify
Raw TCP and UDP Sockets API on Desktop Browsers
Stars: ✭ 67 (-58.64%)
Mutual labels:  socket, udp
Goproxy
🔥 Proxy is a high performance HTTP(S) proxies, SOCKS5 proxies,WEBSOCKET, TCP, UDP proxy server implemented by golang. Now, it supports chain-style proxies,nat forwarding in different lan,TCP/UDP port forwarding, SSH forwarding.Proxy是golang实现的高性能http,https,websocket,tcp,socks5代理服务器,支持内网穿透,链式代理,通讯加密,智能HTTP,SOCKS5代理,黑白名单,限速,限流量,限连接数,跨平台,KCP支持,认证API。
Stars: ✭ 11,334 (+6896.3%)
Mutual labels:  socket, udp
Kcp
⚡ KCP - A Fast and Reliable ARQ Protocol
Stars: ✭ 10,473 (+6364.81%)
Mutual labels:  udp, socket
http-connection-lifecycle
Complete and detailed explanation of HTTP connection lifecycle
Stars: ✭ 43 (-73.46%)
Mutual labels:  udp, tcp-ip
Async Sockets Cpp
Simple thread-based asynchronous TCP & UDP Socket classes in C++.
Stars: ✭ 127 (-21.6%)
Mutual labels:  socket, udp
ddos
Simple dos attack utility
Stars: ✭ 36 (-77.78%)
Mutual labels:  socket, udp
Hp Socket
High Performance TCP/UDP/HTTP Communication Component
Stars: ✭ 4,420 (+2628.4%)
Mutual labels:  socket, udp
DatagramTunneler
Simple C++ cross-platform client/server app forwarding UDP datagrams through a TCP connection.
Stars: ✭ 116 (-28.4%)
Mutual labels:  socket, udp
Tinytcpserver
A small tcp server working under Mono or .NET (4.0) and provides hooks for handling data exchange with clients (works under mono and .net). Behaviour/protocol/reaction could be specified via custom C# script.
Stars: ✭ 14 (-91.36%)
Mutual labels:  socket, tcp-ip
Socket
The Hoa\Socket library.
Stars: ✭ 61 (-62.35%)
Mutual labels:  socket, udp
socket
Dazzle Async Socket
Stars: ✭ 19 (-88.27%)
Mutual labels:  socket, udp
T Io
解决其它网络框架没有解决的用户痛点,让天下没有难开发的网络程序
Stars: ✭ 1,331 (+721.6%)
Mutual labels:  socket, udp
Socket
Non-blocking socket and TLS functionality for PHP based on Amp.
Stars: ✭ 122 (-24.69%)
Mutual labels:  socket, udp

Chronicle-Network

Maven Central

Maven Central

About

A High Performance Network library

Purpose

This library is designed to be lower latency and support higher throughputs by employing techniques used in low latency trading systems.

Transports

Network currently support TCP only.

Planned support for

  • Shared Memory

UDP support can be found in Chronicle Network Enterprise (commercial product - contact [email protected])

Example

TCP Client/Server : Echo Example

The client sends a message to the server, the server immediately responds with the same message back to the client.

The full source code of this example can be found at:

net.openhft.performance.tests.network.SimpleServerAndClientTest.test

Below are some of the key parts of this code explained, in a bit more detail.

TCPRegistry

The TCPRegistry is most useful for unit tests, it allows you to either provide a true host and port, say "localhost:8080" or if you would rather let the application allocate you a free port at random, you can just provide a text reference to the port, such as, "host.port", you can provide any text you want. It will always be taken as a reference. That is unless it's correctly formed like "hostname:port”, then it will use the exact host and port you provide. The reason we offer this functionality is quite often in unit tests you wish to start a test via loopback, followed often by another test, if the first test does not shut down correctly it can impact on the second test. Giving each test a unique port is one solution, but then managing those ports can become a problem in its self. So we created the TCPRegistry which manages those ports for you, when you come to clean up at the end of each test, all you have to do is call TCPRegistry.reset() and this will ensure that any open ports, will be closed.

// this the name of a reference to the host name and port,
// allocated automatically when to a free port on localhost
final String desc = "host.port";
TCPRegistry.createServerSocketChannelFor(desc);

// we use an event loop rather than lots of threads
EventLoop eg = new EventGroup(true);
eg.start();

Create and Start the Server

The server is configured with TextWire, so the client must also be configured with TextWire. The port that we will use will be ( in this example ) determined by the TCP Registry, of course in a real life production environment you may decide not to use the TcpRegistry or if you still use the TcpRegistry you can use a fixed host:port.

final String expectedMessage = "<my message>";
AcceptorEventHandler eah = new AcceptorEventHandler(desc,
    () -> new WireEchoRequestHandler(WireType.TEXT), VanillaSessionDetails::new, 0, 0);
eg.addHandler(eah);
final SocketChannel sc = TCPRegistry.createSocketChannel(desc);
sc.configureBlocking(false);

Server Message Processing

The server code that processes a message, in this simple example we receive and update a message and then immediately send back a response, however there are other solutions that can be implemented using Chronicle-Network, such as the server responding later to a client subscription.

/**
 * This code is used to read the tid and payload from a wire message,
 * and send the same tid and message back to the client
 */
public class WireEchoRequestHandler extends WireTcpHandler {

    public WireEchoRequestHandler(@NotNull Function<Bytes, Wire> bytesToWire) {
        super(bytesToWire);
    }

    /**
     * simply reads the csp,tid and payload and sends back the tid and payload
     *
     * @param inWire  the wire from the client
     * @param outWire the wire to be sent back to the server
     * @param sd      details about this session
     */
    @Override
    protected void process(@NotNull WireIn inWire,
                           @NotNull WireOut outWire,
                           @NotNull SessionDetailsProvider sd) {

        inWire.readDocument(m -> {
            outWire.writeDocument(true, meta -> meta.write("tid")
                    .int64(inWire.read("tid").int64()));
        }, d -> {
            outWire.writeDocument(false, data -> data.write("payloadResponse")
                    .text(inWire.read("payload").text()));
        });
    }
}

Create and Start the Client

The client code that creates the TcpChannelHub,

The TcpChannelHub is used to send your messages to the server and then read the servers response.

The TcpChannelHub ensures that each response is marshalled back onto the appropriate client thread. It does this through the use of a unique transaction ID ( we call this transaction ID the "tid" ), when the server responds to the client, its expected that the server sends back the tid as the very first field in the message. The TcpChannelHub will look at each message and read the tid, and then marshall the message onto your appropriate client thread.

TcpChannelHub tcpChannelHub = TcpChannelHub(null, eg, WireType.TEXT, "",
    SocketAddressSupplier.uri(desc), false);

in this example we are not implementing fail-over support, so the simple SocketAddressSupplier.uri(desc) is used.

Client Message

Creates the message the client sends to the server

// the tid must be unique, its reflected back by the server, it must be at the start
// of each message sent from the server to the client. Its use by the client to identify which
// thread will handle this message
final long tid = tcpChannelHub.nextUniqueTransaction(System.currentTimeMillis());

// we will use a text wire backed by a elasticByteBuffer
final Wire wire = new TextWire(Bytes.elasticByteBuffer());

wire.writeDocument(true, w -> w.write("tid").int64(tid));
wire.writeDocument(false, w -> w.write("payload").text(expectedMessage));

Write the Data to the Socket

When you have multiple client threads its important to lock before writing the data to the socket.

tcpChannelHub.lock(() -> tcpChannelHub.writeSocket(wire));

Read the Reply from the Server

In order that the correct reply can be send to your thread you have to specify the tid

Wire reply = tcpChannelHub.proxyReply(TimeUnit.SECONDS.toMillis(1), tid);

Check the Result of the Reply

// read the reply and check the result
reply.readDocument(null, data -> {
    final String text = data.read("payloadResponse").text();
    Assert.assertEquals(expectedMessage, text);
});

Shutdown and Cleanup

eg.stop();
TcpChannelHub.closeAllHubs();
TCPRegistry.reset();
tcpChannelHub.close();

Server Threading Strategy

By default the Chronicle-Network server uses a single thread, to process all messages. However, if you wish to dedicate each client connection to its own thread. Then you can change the server threading strategy, to :

-DServerThreadingStrategy= CONCURRENT

see the following enum for more details net.openhft.chronicle.network.ServerThreadingStrategy

Java Version

This library will require Java 8

Testing

The target environment is to support TCP over 10 Gig-E ethernet. In prototype testing, this library has half the latency and support 30% more bandwidth.

A key test is that it shouldn't GC more than once (to allow for warm up) with -mx64m

Downsides

This comes at the cost of scalability for large number os connections. In this situation, this library should perform at least as well as netty.

Comparisons

Netty

Netty has a much wider range of functionality, however it creates some garbage in it's operation (less than using plain NIO Selectors) and isn't designed to support busy waiting which gives up a small but significant delay.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].