开源一个简易轻量的reactor网络框架

前端之家收集整理的这篇文章主要介绍了开源一个简易轻量的reactor网络框架前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

github

https://github.com/sea-boat/net-reactor

net-reactor

it’s a simple and easy net framework with nio mode written by java

reactor model

how-to

just simply like:

  1. public class MyHandler implements Handler {
  2.  
  3. private static final Logger LOGGER = LoggerFactory.getLogger(MyHandler.class);
  4. private long readSize;
  5.  
  6. /** * The logic to deal with the received data. * * It means that reactor will trigger this function once the data is received. * @throws IOException */
  7. public void handle(FrontendConnection connection) throws IOException {
  8. Buffer buff = connection.getReadBuffer();
  9. readSize = +readSize + buff.position();
  10. LOGGER.info(connection.getId() + " connection has receive " + readSize);
  11.  
  12. }
  13.  
  14. }
  1. Handler handler = new MyHandler();
  2. ReactorPool reactorPool = new ReactorPool(Runtime.getRuntime().availableProcessors(),handler);
  3. new Acceptor(reactorPool,acceptorName,host,port).start();

adding a connection event or a connection multi-event:

  1. public class RegisterHandler implements ConnectionEventHandler {
  2. private static final Logger LOGGER = LoggerFactory
  3. .getLogger(RegisterHandler.class);
  4.  
  5. private static int INTERESTED = ConnectionEvents.REGISTE;
  6.  
  7. public void event(FrontendConnection connection) {
  8. if ((event & INTERESTED) != 0) {
  9. //do something here
  10. }
  11. }
  12.  
  13. }
  1. Handler handler = new NetHandler();
  2. ConnectionEventHandler connectionEventHandler = new RegisterHandler();
  3. ReactorPool reactorPool = new ReactorPool(Runtime.getRuntime().availableProcessors(),handler);
  4. Acceptor acceptor = new Acceptor(reactorPool,port);
  5. acceptor.addConnectionEventHandler(connectionEventHandler);
  6. acceptor.start();
  1. public class ConnectionLogHandler implements ConnectionEventHandler {
  2. private static final Logger LOGGER = LoggerFactory
  3. .getLogger(ConnectionLogHandler.class);
  4. private static int INTERESTED = ConnectionEvents.ACCEPT
  5. | ConnectionEvents.CLOSE;
  6.  
  7. public void event(Connection connection,int event) {
  8. if ((event & INTERESTED) != 0) {
  9. if ((event & ConnectionEvents.ACCEPT) != 0)
  10. LOGGER.info("accept connection,id is " + connection.getId());
  11. if ((event & ConnectionEvents.CLOSE) != 0)
  12. LOGGER.info("close connection,id is " + connection.getId());
  13. }
  14. }
  15. }

implements the connection

  1. public class XXXConnection extends Connection {
  2.  
  3. private String name;
  4.  
  5. public XXXConnection(SocketChannel channel,long id,Reactor reactor) {
  6. super(channel,id,reactor);
  7. }
  8.  
  9. public String getName() {
  10. return name;
  11. }
  12.  
  13. public void setName(String name) {
  14. this.name = name;
  15. }
  16.  
  17. }
  1. public class XXXConnectionFactory implements ConnectionFactory {
  2.  
  3. public XXXConnection createConnection(SocketChannel channel,Reactor reactor) {
  4. return new XXXConnection(channel,reactor);
  5. }
  6.  
  7. }
  1. Acceptor acceptor = new Acceptor(reactorPool,port);
  2. acceptor.setConnectionFactory(new xxxConnectionFactory());

猜你在找的React相关文章