Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yuk1ty
Created March 3, 2018 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuk1ty/5625f776c89c213128d23306c0f6387e to your computer and use it in GitHub Desktop.
Save yuk1ty/5625f776c89c213128d23306c0f6387e to your computer and use it in GitHub Desktop.
private void doRead(SocketChannel socketChannel) throws NioHttpServerException {
try {
ByteBuffer buf = ByteBuffer.allocate(1024);
socketChannel.read(buf);
buf.flip();
Option<Request> maybeRequest = requestHandler.apply(buf);
buf.clear();
Option<Either<Exception, Response>> maybeResponse =
maybeRequest.map(
request -> {
try {
Response response = responseHandler.apply(request);
socketChannel.write(response.toByteBuf());
return Either.right(response);
} catch (NioHttpServerException | IOException err) {
return Either.left(err);
}
});
if (maybeResponse.isSome() && maybeResponse.some().isLeft()) {
throw new NioHttpServerException(maybeResponse.some().left().value());
}
} catch (IOException e) {
throw new NioHttpServerException(e);
} finally {
LOGGER.info("Disconnected: %s", socketChannel.socket().getRemoteSocketAddress().toString());
try {
socketChannel.close();
} catch (IOException e) {
throw new NioHttpServerException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment