Esempio n. 1
0
  /* will create new Client object and add it to the 'clients' list
   * and will also register it's socket channel with 'readSelector'.
   * Use 'sendBufferSize' to specify socket's send buffer size. */
  public static Client addNewClient(SocketChannel chan, Selector readSelector, int sendBufferSize) {

    Client client = new Client(chan);
    clients.add(client);

    // register the channel with the selector
    // store a new Client as the Key's attachment
    try {
      chan.configureBlocking(false);
      chan.socket().setSendBufferSize(sendBufferSize);
      // ***chan.socket().setSoTimeout(TIMEOUT_LENGTH); -> this doesn't seem to have an effect with
      // java.nio
      client.selKey = chan.register(readSelector, SelectionKey.OP_READ, client);
    } catch (ClosedChannelException cce) {
      killClient(client);
      return null;
    } catch (IOException ioe) {
      killClient(client);
      return null;
    } catch (Exception e) {
      killClient(client);
      return null;
    }

    return client;
  }