示例#1
0
  /**
   * @throws IOException
   * @throws InterruptedException
   */
  protected synchronized void reConnect() throws IOException, InterruptedException {
    // 0. Don't send connect request if it is connecting.
    if (isNotConnected()) {
      SocketAddress remoteAddress = new InetSocketAddress(this.host, this.port);

      // 1. Create socket channel
      SocketChannel channel = SocketChannel.open();
      channel.configureBlocking(false);
      channel.socket().setTcpNoDelay(true);
      channel.socket().setReceiveBufferSize(48 * 1024);
      channel.socket().setSendBufferSize(48 * 1024);
      channel.connect(remoteAddress);

      // 2. Create NioSession for each client connection
      IoSession client =
          new DefaultIoSession().setChannel(channel).setIoWork(ioWorker).setEventWork(eventWorker);
      // 3. Register event
      reactorManager.nextReactor().registerSession(client, SelectionKey.OP_CONNECT);

      // 4. Wait to connect
      if (!client.waitToConnect(connectTimeout, TimeUnit.MILLISECONDS)) {
        client.close(); // TODO:asyncClose();
        throw new IOException("connect timed out to " + this.host + ":" + this.port);
      }

      IoSession.Status status = client.getStatus();
      if (status == IoSession.Status.NOT_CONNECT || status == IoSession.Status.CLOSED) {
        client.close(); // TODO:.asyncClose();
        throw new IOException("connect failed to " + this.host + ":" + this.port);
      }

      this.session = client;
    }
  }
示例#2
0
 /** @return */
 protected boolean isNotConnected() {
   if (session == null) return true;
   IoSession.Status status = session.getStatus();
   return status == IoSession.Status.NOT_CONNECT || status == IoSession.Status.CLOSED;
 }