[go: up one dir, main page]

Menu

Tree [17d69b] NIO /
 History

HTTPS access


File Date Author Commit
 .github 2021-10-31 yuyenews yuyenews [17d69b] 修复手动配置TCPServerConfig时,magicianHandlerMap与webS...
 src 2021-10-10 yuyenews yuyenews [f196c8] 修复手动配置TCPServerConfig时,magicianHandlerMap与webS...
 .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-08-22 yuyenews yuyenews [ec2c76] 增加Kotlin示例
 pom.xml 2021-08-19 yuyenews yuyenews [d5a70d] 给channel加锁,解决给客户端并发写入数据导致异常的问题

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

@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);

More components

Use these components to easily develop web projects

image

Magician-Web |
Magician-JDBC |
Magician-Transaction |
Martian

TFB test results (second round, continuous optimization)

image

Documentation and examples

Document |
Example |
Kotlin-Example