示例#1
0
  @JRubyMethod(name = "accept")
  public IRubyObject accept(ThreadContext context) {
    Ruby runtime = context.runtime;
    RubyTCPSocket socket = new RubyTCPSocket(runtime, runtime.getClass("TCPSocket"));

    try {
      RubyThread thread = context.getThread();

      while (true) {
        boolean ready = thread.select(this, SelectionKey.OP_ACCEPT);

        if (!ready) {
          // we were woken up without being selected...poll for thread events and go back to sleep
          context.pollThreadEvents();

        } else {
          SocketChannel connected = ssc.accept();
          if (connected == null) continue;

          connected.finishConnect();

          // Force the client socket to be blocking
          synchronized (connected.blockingLock()) {
            connected.configureBlocking(false);
            connected.configureBlocking(true);
          }

          // otherwise one key has been selected (ours) so we get the channel and hand it off
          socket.initSocket(
              runtime, new ChannelDescriptor(connected, newModeFlags(runtime, ModeFlags.RDWR)));

          return socket;
        }
      }

    } catch (IOException e) {
      throw SocketUtils.sockerr(runtime, "problem when accepting");
    }
  }