Created
March 3, 2018 09:14
-
-
Save yuk1ty/5625f776c89c213128d23306c0f6387e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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