Пример #1
0
    SelectableChannel doConnect() throws IOException, InterruptedException {

      final ServerSocketChannel serverChannel = ServerSocketChannel.open();

      serverChannel.socket().setReceiveBufferSize(BUFFER_SIZE);
      serverChannel.configureBlocking(false);
      final ServerSocket serverSocket = serverChannel.socket();
      serverSocket.setReuseAddress(true);

      serverSocket.bind(details.address());

      // these can be run on this thread
      addPendingRegistration(
          new Runnable() {
            @Override
            public void run() {
              final Attached attached = new Attached();
              attached.connector = ServerConnector.this;
              try {
                serverChannel.register(TcpReplicator.this.selector, OP_ACCEPT, attached);
              } catch (ClosedChannelException e) {
                LOG.error("", e);
              }
            }
          });

      selector.wakeup();

      return serverChannel;
    }
Пример #2
0
  /** called when the selector receives a OP_ACCEPT message */
  private void onAccept(@NotNull final SelectionKey key) throws IOException {

    final ServerSocketChannel server = (ServerSocketChannel) key.channel();
    final SocketChannel channel = server.accept();
    channel.configureBlocking(false);
    channel.socket().setReuseAddress(true);
    channel.socket().setTcpNoDelay(true);
    channel.socket().setSoTimeout(0);
    channel.socket().setSoLinger(false, 0);

    final Attached attached = new Attached();
    channel.register(selector, OP_WRITE | OP_READ, attached);

    throttle(channel);

    attached.entryReader = new TcpSocketChannelEntryReader();
    attached.entryWriter = new TcpSocketChannelEntryWriter();

    attached.isServer = true;
    attached.entryWriter.identifierToBuffer(localIdentifier);
  }