[go: up one dir, main page]

Menu

Tree [4997c9] 1.1.13 /
 History

HTTPS access


File Date Author Commit
 .github 2021-06-24 贝克街的天才 贝克街的天才 [333bae] Create FUNDING.yml
 src 2021-07-01 yuyenews yuyenews [4997c9] 修复细节
 .gitignore 2021-04-10 yuyenews yuyenews [31b4d7] 首次提交
 BACKERS.md 2021-06-25 yuyenews yuyenews [727653] 添加支持者榜单
 LICENSE 2021-04-12 yuyenews yuyenews [15e460] 首次提交
 README.md 2021-06-30 yuyenews yuyenews [77d964] 修复细节
 pom.xml 2021-07-01 yuyenews yuyenews [4997c9] 修复细节

Read Me




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

public class DemoHandler implements MagicianHandler<MagicianRequest> {

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

Create TCP Server (Default thread pool configuration)

Magician.createTCPServer()
                    .handler("/", new DemoHandler())
                    .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)
                    .handler("/", new DemoHandler())
                    .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)
                         .handler("/", new DemoHandler())

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

2. Create WebSocket

Just add a handler when creating the http service

Magician.createTCPServer()
                    .handler("/", new DemoHandler())
                    .webSocketHandler("/websocket", new DemoSocketHandler())
                    .bind(8080);

3. Create UDP Server

Magician.createUdpServer()
                .handler(outputStream -> {
                    // outputStream is of type ByteArrayOutputStream
                    // It is the data sent by the client, you can parse it by yourself
                }).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