Esempio n. 1
0
 public NetAcceptor(int port) throws IOException {
   AsynchronousChannelGroup group =
       AsynchronousChannelGroup.withCachedThreadPool(
           Executors.newCachedThreadPool(), 1); // server连接接收线程池
   serverChannel = AsynchronousServerSocketChannel.open(group);
   serverChannel.bind(new InetSocketAddress(port));
 }
Esempio n. 2
0
  public void start() throws IOException, ExecutionException, InterruptedException {
    AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open();
    server.bind(new InetSocketAddress(getPort()));

    while (true) {
      new ChannelHandler(server.accept().get()).handle();
    }
  }
 public AsyncTimeServerHandler(final int port) {
   this.port = port;
   try {
     serverChannel = AsynchronousServerSocketChannel.open();
     serverChannel.bind(new InetSocketAddress(this.port));
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
 public AsyncTimeServerHandler(int port) {
   this.port = port;
   try {
     asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open();
     asynchronousServerSocketChannel.bind(new InetSocketAddress(port));
     System.out.println("The time server is start in port : " + port);
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
 private AsynchronousServerSocketChannel bind(String host, int port) {
   AsynchronousServerSocketChannel serverSocketChannel = null;
   try {
     serverSocketChannel = AsynchronousServerSocketChannel.open(group);
     serverSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
     serverSocketChannel.bind(new InetSocketAddress(host, port), BACKLOG);
   } catch (Exception e) {
     log.error("ServerSocket bind error", e);
   }
   return serverSocketChannel;
 }
  @Override
  protected Void call() throws Exception {

    try (AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open()) {
      server.bind(new InetSocketAddress(port));

      while (!isCancelled()) {
        Future<AsynchronousSocketChannel> sock = server.accept();
        hsErrProcPool.submit(new ErrorReportDecoder(crashList, sock.get()));
      }
    }

    return null;
  }
Esempio n. 7
0
 public void stopCompletionPort() {
   try {
     server.close();
     log.info("游戏服务器已关闭");
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Esempio n. 8
0
  public AIOSocketMgr() throws Exception {
    if (!allowInstance) {
      throw new Exception();
    }
    try {
      AsynchronousChannelGroup resourceGroup =
          AsynchronousChannelGroup.withCachedThreadPool(Executors.newCachedThreadPool(), 8);
      server = AsynchronousServerSocketChannel.open(resourceGroup);
      server.bind(new InetSocketAddress(PORT), 100);

      acceptHandler = new AcceptCompletionHandler();
      readHandler = new ReadCompletionHandler();
      authHandler = new AuthSessionCompletionHandler();

      bindProtocol();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Esempio n. 9
0
  private void pendingAccept() {
    serverChannel.accept(
        this,
        new CompletionHandler<AsynchronousSocketChannel, NetAcceptor>() {
          @Override
          public void completed(AsynchronousSocketChannel channel, NetAcceptor attachment) {
            try {
              accept(channel);
            } catch (IOException e) {
              e.printStackTrace();
            }
            attachment.pendingAccept();
          }

          @Override
          public void failed(Throwable exc, NetAcceptor attachment) {
            attachment.pendingAccept();
          }
        });
  }
  private void listen(final AsynchronousServerSocketChannel serverSocketChannel) {
    serverSocketChannel.accept(
        id.getAndIncrement(),
        new CompletionHandler<AsynchronousSocketChannel, Integer>() {

          @Override
          public void completed(AsynchronousSocketChannel socketChannel, Integer sessionId) {
            try {
              worker.registerChannel(socketChannel, sessionId);
            } finally {
              listen(serverSocketChannel);
            }
          }

          @Override
          public void failed(Throwable exc, Integer id) {
            try {
              log.error("server accepts channel " + id + " error occurs", exc);
            } finally {
              listen(serverSocketChannel);
            }
          }
        });
  }
 public void doAccept() {
   asynchronousServerSocketChannel.accept(this, new AcceptCompletionHandler());
 }
 @Override
 public boolean isOpen() {
   return ac.isOpen();
 }
 public void doAccept() {
   serverChannel.accept(this, new AcceptCompletionHandler());
 }
 @Override
 public void close() throws IOException {
   ac.close();
 }
Esempio n. 15
0
  public NioSocketServer() {
    try {
      // 创建一个 AsynchronousServerSocketChannel 侦听 5000 端口
      final AsynchronousServerSocketChannel listener =
          AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(5000));

      // 侦听新的请求
      listener.accept(
          null,
          new CompletionHandler<AsynchronousSocketChannel, Void>() {

            @Override
            public void completed(AsynchronousSocketChannel ch, Void att) {
              // 接受下一个连接
              listener.accept(null, this);

              // 向客户端发送问候信息
              ch.write(
                  ByteBuffer.wrap(
                      "Hello, I am Echo Server 2020, let's have an engaging conversation!n"
                          .getBytes()));

              // 分配(4K)字节缓冲用于从客户端读取信息
              ByteBuffer byteBuffer = ByteBuffer.allocate(4096);
              try {
                // Read the first line
                int bytesRead = ch.read(byteBuffer).get(20, TimeUnit.SECONDS);

                boolean running = true;
                while (bytesRead != -1 && running) {
                  System.out.println("bytes read: " + bytesRead);

                  // 确保有读取到数据
                  if (byteBuffer.position() > 2) {
                    // 准备缓存进行读取
                    byteBuffer.flip();

                    // 把缓存转换成字符串
                    byte[] lineBytes = new byte[bytesRead];
                    byteBuffer.get(lineBytes, 0, bytesRead);
                    String line = new String(lineBytes);

                    // Debug
                    System.out.println("Message: " + line);

                    // 向调用者回写
                    ch.write(ByteBuffer.wrap(line.getBytes()));

                    // 准备缓冲进行写操作
                    byteBuffer.clear();

                    // 读取下一行
                    bytesRead = ch.read(byteBuffer).get(20, TimeUnit.SECONDS);
                  } else {
                    // 在我们的协议中,空行表示会话结束
                    running = false;
                  }
                }
              } catch (InterruptedException e) {
                e.printStackTrace();
              } catch (ExecutionException e) {
                e.printStackTrace();
              } catch (TimeoutException e) {
                // 用户达到20秒超时,关闭连接
                ch.write(ByteBuffer.wrap("Good Byen".getBytes()));
                System.out.println("Connection timed out, closing connection");
              }

              System.out.println("End of conversation");
              try {
                // 如果需要,关闭连接
                if (ch.isOpen()) {
                  ch.close();
                }
              } catch (IOException e1) {
                e1.printStackTrace();
              }
            }

            @Override
            public void failed(Throwable exc, Void att) {
              // /...
            }
          });
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
 @Override
 public AsyncFiberServerSocketChannel bind(SocketAddress local, int backlog) throws IOException {
   ac.bind(local, backlog);
   return this;
 }
Esempio n. 17
0
 public void startCompletionPort() {
   log.info("服务器已启动");
   server.accept(this, acceptHandler);
 }
 @Override
 public final AsynchronousChannelProvider provider() {
   return ac.provider();
 }
 @Override
 public <T> AsyncFiberServerSocketChannel setOption(SocketOption<T> name, T value)
     throws IOException {
   ac.setOption(name, value);
   return this;
 }
 @Override
 public Set<SocketOption<?>> supportedOptions() {
   return ac.supportedOptions();
 }
 @Override
 public <T> T getOption(SocketOption<T> name) throws IOException {
   return ac.getOption(name);
 }
 @Override
 public SocketAddress getLocalAddress() throws IOException {
   return ac.getLocalAddress();
 }