本周日【珠海源创会】一起聊聊:PingCAP分布式事务、支付宝移动端实践、GSBN技术框架选型,点此报名占座

GVPsmartboot / smart-socketJavaApache-2.0

6 Watch 465 Star 1.5k Fork 383
加入码云
与超过 300 万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
848行代码实现极简、易用、高性能的国产java AIO通信框架,也许是IM、RPC、IoT开发的最佳选择。玩转通信只有两个接口的学习成本,如果觉得netty门槛太高,那就来试试smart-socket吧 展开 收起

https://smartboot.gitee.io/docs/smart-socket/

README_EN.md 3.67 KB
一键复制 编辑 Web IDE 原始数据 按行查看 历史
三刀 提交于 2018-10-17 13:51 . 移除fairIo功能,实测效果不明显

smart-socket is a AIO client server framework which enables quick and easy development of network applications such as protocol servers and clients.It greatly simplifies and streamlines network programming such as TCP socket server(unsupport UDP).

Features

  • Better throughput, lower latency
  • Less resource consumptions
  • less code

Quick Start

Maven Dependency

The smart-socket jar file is available from Maven Central and can be integrated into your dependency manager of choice from there.

<dependency>
    <groupId>org.smartboot.socket</groupId>
    <artifactId>aio-core</artifactId>
    <version>1.3.22</version>
</dependency>

Demo

Protocol

public class IntegerProtocol implements Protocol<Integer> {

    private static final int INT_LENGTH = 4;

    @Override
    public Integer decode(ByteBuffer data, AioSession<Integer> session, boolean eof) {
        if (data.remaining() < INT_LENGTH)
            return null;
        return data.getInt();
    }

    @Override
    public ByteBuffer encode(Integer s, AioSession<Integer> session) {
        ByteBuffer b = ByteBuffer.allocate(INT_LENGTH);
        b.putInt(s);
        b.flip();
        return b;
    }
}

Writing a server

public class IntegerServerProcessor implements MessageProcessor<Integer> {
    @Override
    public void process(AioSession<Integer> session, Integer msg) {
        Integer respMsg = msg + 1;
        System.out.println("receive data from client: " + msg + " ,rsp:" + (respMsg));
        try {
            session.write(respMsg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void stateEvent(AioSession<Integer> session, StateMachineEnum stateMachineEnum, Throwable throwable) {

    }
}
public class IntegerServer {
    public static void main(String[] args) {
        AioQuickServer<Integer> server = new AioQuickServer<Integer>()
                .bind(8888)
                .setProtocol(new IntegerProtocol())
                .setProcessor(new IntegerServerProcessor());
        try {
            server.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Writing a client

In our case all we want to do is print the value out the the console.

public class IntegerClientProcessor implements MessageProcessor<Integer> {
    private AioSession<Integer> session;

    @Override
    public void process(AioSession<Integer> session, Integer msg) {
        System.out.println("receive data from server:" + msg);
    }

    @Override
    public void stateEvent(AioSession<Integer> session, StateMachineEnum stateMachineEnum, Throwable throwable) {
        switch (stateMachineEnum) {
            case NEW_SESSION:
                this.session = session;
                break;
            default:
                System.out.println("other state:" + stateMachineEnum);
        }

    }

    public AioSession<Integer> getSession() {
        return session;
    }
}
public class IntegerClient {
    public static void main(String[] args) throws Exception {
        IntegerClientProcessor processor = new IntegerClientProcessor();
        AioQuickClient<Integer> aioQuickClient = new AioQuickClient<Integer>()
                .connect("localhost", 8888)
                .setProtocol(new IntegerProtocol())
                .setProcessor(processor);
        aioQuickClient.start();
        processor.getSession().write(1);
        Thread.sleep(1000);
        aioQuickClient.shutdown();
    }
}

License

About us

Edit By 三刀(sandao)
E-mail:zhengjunweimail@163.com

Update Date: 2017-11-20

评论 ( 0 )

你可以在登录后,发表评论

搜索帮助

12_float_left_people 12_float_left_close