コード例 #1
0
  public boolean update(long timeDiff) {
    if (channel != null) {
      if (!channel.isOpen()) {}
    }

    ProtocolPackage pack;
    while (!recvQueue.isEmpty()) {
      pack = recvQueue.remove(0);
      ProtocolRouter.getInstance().Trigger(pack.protocolId, pack, this);
    }
    return true;
  }
コード例 #2
0
ファイル: ServerWorker.java プロジェクト: qza/gft-crw
 private boolean writeMessage(AsynchronousSocketChannel worker, String message) {
   boolean success = false;
   byte[] databytes = message.getBytes();
   ByteBuffer data = ByteBuffer.wrap(databytes);
   if (worker.isOpen()) {
     try {
       worker.write(data).get();
       success = true;
     } catch (InterruptedException | ExecutionException e) {
       data = null;
       throw new RuntimeException("Interupted");
     }
   }
   return success;
 }
コード例 #3
0
ファイル: ServerWorker.java プロジェクト: qza/gft-crw
 private boolean readMessage(AsynchronousSocketChannel socket) {
   boolean success = false;
   ByteBuffer readBuffer = ByteBuffer.allocate(4096);
   if (socket.isOpen()) {
     try {
       socket.read(readBuffer).get();
     } catch (InterruptedException | ExecutionException e) {
       readBuffer = null;
       throw new RuntimeException("Interupted");
     }
     byte[] data = readBuffer.array();
     if (data != null && data.length > 0) {
       processData(data);
       success = true;
     }
   }
   return success;
 }
コード例 #4
0
ファイル: Main.java プロジェクト: PlamenStilyianov/Java
  public static void main(String[] args) {

    final int DEFAULT_PORT = 5555;
    final String IP = "127.0.0.1";

    // create asynchronous socket channel bound to the default group
    try (AsynchronousSocketChannel asynchronousSocketChannel = AsynchronousSocketChannel.open()) {

      if (asynchronousSocketChannel.isOpen()) {

        // set some options
        asynchronousSocketChannel.setOption(StandardSocketOptions.SO_RCVBUF, 128 * 1024);
        asynchronousSocketChannel.setOption(StandardSocketOptions.SO_SNDBUF, 128 * 1024);
        asynchronousSocketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);

        // connect this channel's socket
        asynchronousSocketChannel.connect(
            new InetSocketAddress(IP, DEFAULT_PORT),
            null,
            new CompletionHandler<Void, Void>() {

              final ByteBuffer helloBuffer = ByteBuffer.wrap("Hello !".getBytes());
              final ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
              CharBuffer charBuffer = null;
              ByteBuffer randomBuffer;
              final Charset charset = Charset.defaultCharset();
              final CharsetDecoder decoder = charset.newDecoder();

              @Override
              public void completed(Void result, Void attachment) {
                try {
                  System.out.println(
                      "Successfully connected at: " + asynchronousSocketChannel.getRemoteAddress());

                  // transmitting data
                  asynchronousSocketChannel.write(helloBuffer).get();

                  while (asynchronousSocketChannel.read(buffer).get() != -1) {

                    buffer.flip();

                    charBuffer = decoder.decode(buffer);
                    System.out.println(charBuffer.toString());

                    if (buffer.hasRemaining()) {
                      buffer.compact();
                    } else {
                      buffer.clear();
                    }

                    int r = new Random().nextInt(100);
                    if (r == 50) {
                      System.out.println(
                          "50 was generated! Close the asynchronous socket channel!");
                      break;
                    } else {
                      randomBuffer =
                          ByteBuffer.wrap("Random number:".concat(String.valueOf(r)).getBytes());
                      asynchronousSocketChannel.write(randomBuffer).get();
                    }
                  }
                } catch (IOException | InterruptedException | ExecutionException ex) {
                  System.err.println(ex);
                } finally {
                  try {
                    asynchronousSocketChannel.close();
                  } catch (IOException ex) {
                    System.err.println(ex);
                  }
                }
              }

              @Override
              public void failed(Throwable exc, Void attachment) {
                throw new UnsupportedOperationException("Connection cannot be established!");
              }
            });

        System.in.read();

      } else {
        System.out.println("The asynchronous socket channel cannot be opened!");
      }

    } catch (IOException ex) {
      System.err.println(ex);
    }
  }
コード例 #5
0
 public void startRecv() {
   if (channel.isOpen()) {
     readBuffer = BufferPool.getInstance().getBuffer();
     channel.read(readBuffer, this, AIOSocketMgr.getInstance().getReadHandler());
   }
 }