Magician is an asynchronous non-blocking network protocol analysis package, supports TCP, UDP protocol, built-in Http, WebSocket decoder

Related tags

Networking Magician
Overview



An asynchronous non-blocking network protocol analysis package

Project Description

Magician is an asynchronous non-blocking network protocol analysis package, supports TCP, UDP protocol, built-in Http, WebSocket decoder

Run environment

JDK11+


If you want to use it on a lower version of the JDK, you can download the source code of this repository and compile it yourself.

Import dependencies

<dependency>
    <groupId>com.github.yuyenews</groupId>
    <artifactId>Magician</artifactId>
    <version>last version</version>
</dependency>

<!-- This is the log package, which supports any package that can be bridged with slf4j -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
    <version>1.7.12</version>
</dependency>

1. create a TCP service (using http decoder by default)

Create Handler

@TCPHandler(path="/")
public class DemoHandler implements TCPBaseHandler<MagicianRequest> {

    @Override
    public void request(MagicianRequest magicianRequest) {
        // response data
        magicianRequest.getResponse()
                .sendJson(200, "{'status':'ok'}");
    }
}

Create TCP Server (Default thread pool configuration)

Magician.createTCPServer()
                    .scan("The package name of the handler")
                    .bind(8080);

Create TCP Server (custom thread pool configuration)

EventGroup ioEventGroup = new EventGroup(1, Executors.newCachedThreadPool());
EventGroup workerEventGroup = new EventGroup(10, Executors.newCachedThreadPool());

// When the current EventRunner has no tasks, it is allowed to steal tasks from other EventRunners
workerEventGroup.setSteal(EventEnum.STEAL.YES);

Magician.createTCPServer(ioEventGroup, workerEventGroup)
                    .scan("The package name of the handler")
                    .bind(8080);

Create TCP Server (Listen on multiple ports)

// Listen to n ports, write n as the first parameter of ioEventGroup
EventGroup ioEventGroup = new EventGroup(2, Executors.newCachedThreadPool());
EventGroup workerEventGroup = new EventGroup(10, Executors.newCachedThreadPool());

// When the current EventRunner has no tasks, it is allowed to steal tasks from other EventRunners
workerEventGroup.setSteal(EventEnum.STEAL.YES);

TCPServer tcpServer = Magician.createTCPServer(ioEventGroup, workerEventGroup)
                         .scan("The package name of the handler")

tcpServer.bind(8080);
tcpServer.bind(8088);

2. Create WebSocket

Just add a handler when creating the http service

@WebSocketHandler(path = "/websocket")
public class DemoSocketHandler implements WebSocketBaseHandler {
   
    @Override
    public void onOpen(WebSocketSession webSocketSession) {
     
    }
   
    @Override
    public void onClose(WebSocketSession webSocketSession) {
        
    }

    @Override
    public void onMessage(String message, WebSocketSession webSocketSession) {

    }
}

3. Create UDP Server

Create Handler

@UDPHandler
public class DemoUDPHandler implements UDPBaseHandler {

    @Override
    public void receive(ByteArrayOutputStream byteArrayOutputStream) {

    }
}

Create UDP Server

Magician.createUdpServer()
                .scan("The package name of the handler")
                .bind(8088);

In addition to this way of writing, you can also create a separate handler, add here

TFB test results (second round, continuous optimization)

image

TFB地址

Documentation and examples

You might also like...

FileServer - A multithreaded client-server program that uses Java Sockets to establish TCP/IP connection

A multithreaded client-server program that uses Java Sockets to establish TCP/IP connection. The server allows multiple clients to upload, retrieve and delete files on/from the server.

Nov 13, 2022

A barebones WebSocket client and server implementation written in 100% Java.

Java WebSockets This repository contains a barebones WebSocket server and client implementation written in 100% Java. The underlying classes are imple

Dec 30, 2022

Mats3: Message-based Asynchronous Transactional Staged Stateless Services

Mats3: Message-based Asynchronous Transactional Staged Stateless Services

Dec 28, 2022

A simple, fast integration of WebSocket spring-stater

websocket-spring-boot-starter readme 介绍 一个简单,快速,低配的spring-boot-starter,是对spring-boot-starter-websocket的扩展与二次封装,简化了springboot应用对websocket的操作 特点 简单,低配 支

Dec 24, 2021

WebSocket server with creatable/joinable channels.

bytesocks 🧦 bytesocks is a WebSocket server which allows clients to create "channels" and send messages in them. It's effectively an add-on for byteb

Nov 29, 2022

An netty based asynchronous socket library for benchion java applications

An netty based asynchronous socket library for benchion java applications

Benchion Sockets Library An netty based asynchronous socket library for benchion java applications 📖 Documents 📖 Report Bug · Request Feature Conten

Dec 25, 2022

Distributed WebSocket Server

Keeper 分布式 WebSocket 服务器。 注意事项 IO 线程和业务线程分离:对于小业务,依旧放到 worker 线程中处理,对于需要和中间件交互的丢到业务线程池处理,避免 worker 阻塞。 WebSocket 握手阶段支持参数列表。 插件 本服务功能插件化。

Dec 15, 2022

Simulating shitty network connections so you can build better systems.

Comcast Testing distributed systems under hard failures like network partitions and instance termination is critical, but it's also important we test

Dec 30, 2022

Lunar Network SoupPvP gamemode replica

SoupPvP Lunar Network SoupPvP gamemode replica Disclaimer This is a work-in-progress, for that reason, a lot of features and essential parts of Lunar'

Nov 30, 2022
Comments
  • 对于TCP部分代码的一些不成熟的建议

    对于TCP部分代码的一些不成熟的建议

    1,ByteBuffer.allocate 使用的是堆内内存,改为ByteBuffer.allocateDirect可以利用零拷贝提高性能

    2,循环读取没必要全部读完,全部读完再处理有OOM的风险,可以读一部分就开始处理,具体的“拆包”逻辑可以交由具体基于TCP的协议解析器负责

    3,

    这个位置不会抛出异常,没必要try…catch

    public TCPServer(EventGroup ioEventGroup, EventGroup workerEventGroup) {
            try {
                this.ioEventGroup = ioEventGroup;
                this.workerEventGroup = workerEventGroup;
                this.tcpServerConfig = new TCPServerConfig();
            } catch (Exception e) {
                log.error("打开serverSocketChannel,出现异常", e);
            }
        }
    

    4,

    EventGroup下面的所有EventRunner都公用一个线程池,建议中间这几个都删了,改为forkjoin线程池直接提交吧,也自带工作窃取

    5,TCP server读,写,连接都在单线程中,瓶颈较大,可以尝试改为连接绑定某一线程单线读写,单线程做连接处理

    这一段可以加入作为while的条件

    if(ioEventGroup.getThreadPool().isShutdown()){
                    logger.error("ioEventGroup里的线程池关闭了,所以Selector也停止了");
                    selector.close();
                    serverSocketChannel.close();
                    return;
                }
    

    6,TCPServerMonitorTask#run()最后一行

    selector.wakeup();
    

    我认为没有意义,这个操作是外部线程触发使得当前selector持有线程从select的阻塞 中唤醒,自己调用时已经是唤醒状态 7,TCP server是否与协议解析绑定太深? 8,EventLoop是个很好的模型,可以参考一些EventLoop的实现

    opened by dreamlike-ocean 2
Releases(v2.0.2)
Owner
贝克街的天才
贝克街的天才
Efficient reliable UDP unicast, UDP multicast, and IPC message transport

Aeron Efficient reliable UDP unicast, UDP multicast, and IPC message transport. Java and C++ clients are available in this repository, and a .NET clie

Real Logic 6.3k Dec 27, 2022
Efficient reliable UDP unicast, UDP multicast, and IPC message transport

Aeron Efficient reliable UDP unicast, UDP multicast, and IPC message transport. Java and C++ clients are available in this repository, and a .NET clie

Real Logic 6.3k Jan 9, 2023
Library for composability of interdependent non-blocking I/O tasks

Composer Composer helps you to organize and execute multiple interdependent asynchronous input/output tasks such as webservice calls, database read/wr

krupal 19 Oct 8, 2021
TCP/UDP client/server library for Java, based on Kryo

KryoNet can be downloaded on the releases page. Please use the KryoNet discussion group for support. Overview KryoNet is a Java library that provides

Esoteric Software 1.7k Jan 2, 2023
A small java project consisting of Client and Server, that communicate via TCP/UDP protocols.

Ninja Battle A small java project consisting of Client and Server, that communicate via TCP/UDP protocols. Client The client is equipped with a menu i

Steliyan Dobrev 2 Jan 14, 2022
A High Performance Network ( TCP/IP ) Library

Chronicle-Network About A High Performance Network library Purpose This library is designed to be lower latency and support higher throughputs by empl

Chronicle Software : Open Source 231 Dec 31, 2022
Netty project - an event-driven asynchronous network application framework

Netty Project Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol serv

The Netty Project 30.5k Jan 3, 2023
RSocket is a binary protocol for use on byte stream transports such as TCP, WebSockets, and Aeron

RSocket RSocket is a binary protocol for use on byte stream transports such as TCP, WebSockets, and Aeron. It enables the following symmetric interact

RSocket 2.2k Dec 30, 2022
A Java event based WebSocket and HTTP server

Webbit - A Java event based WebSocket and HTTP server Getting it Prebuilt JARs are available from the central Maven repository or the Sonatype Maven r

null 808 Dec 23, 2022
TCP/IP packet demultiplexer. Download from:

TCPFLOW 1.5.0 Downloads directory: http://digitalcorpora.org/downloads/tcpflow/ Installation Most common GNU/Linux distributions ship tcpflow in their

Simson L. Garfinkel 1.5k Jan 4, 2023