Created
June 17, 2014 20:38
-
-
Save aamedina/c97a994a82a200c64c0e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| (defn server-handler [] | |
| (proxy [ChannelHandlerAdapter] [] | |
| (channelRead [^ChannelHandlerContext ctx ^ByteBuf msg] | |
| (.write ctx msg) | |
| (.flush ctx)) | |
| (exceptionCaught [^ChannelHandlerContext ctx ^Throwable cause] | |
| (.printStackTrace cause) | |
| (.close ctx)))) | |
| (defn handlers | |
| [] | |
| (into-array ChannelHandler [(server-handler)])) | |
| (defn run | |
| [port] | |
| (let [bosses (NioEventLoopGroup.) | |
| workers (NioEventLoopGroup.)] | |
| (try | |
| (let [bootstrap (doto (ServerBootstrap.) | |
| (.group bosses workers) | |
| (.channel NioServerSocketChannel) | |
| (.childHandler (proxy [ChannelInitializer] [] | |
| (initChannel [^SocketChannel ch] | |
| (-> (.pipeline ch) | |
| (.addLast (handlers)))))) | |
| (.option ChannelOption/SO_BACKLOG (int 128)) | |
| (.childOption ChannelOption/SO_KEEPALIVE true)) | |
| f (.sync (.bind bootstrap (int port))) | |
| channel (.channel f)] | |
| (.sync (.closeFuture channel))) | |
| (finally | |
| (.shutdownGracefully workers) | |
| (.shutdownGracefully bosses))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment