io.netty.channel.socket.nio.NioServerSocketChannel Scala Examples
The following examples show how to use io.netty.channel.socket.nio.NioServerSocketChannel.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: GrpcServerOwner.scala From daml with Apache License 2.0 | 6 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.apiserver import java.io.IOException import java.net.{BindException, InetAddress, InetSocketAddress} import java.util.concurrent.TimeUnit.SECONDS import com.daml.metrics.Metrics import com.daml.platform.apiserver.GrpcServerOwner._ import com.daml.ports.Port import com.daml.resources.{Resource, ResourceOwner} import com.google.protobuf.Message import io.grpc.netty.NettyServerBuilder import io.grpc._ import io.netty.channel.socket.nio.NioServerSocketChannel import io.netty.handler.ssl.SslContext import scala.concurrent.{ExecutionContext, Future} import scala.util.control.NoStackTrace final class GrpcServerOwner( address: Option[String], desiredPort: Port, maxInboundMessageSize: Int, sslContext: Option[SslContext] = None, interceptors: List[ServerInterceptor] = List.empty, metrics: Metrics, eventLoopGroups: ServerEventLoopGroups, services: Iterable[BindableService], ) extends ResourceOwner[Server] { override def acquire()(implicit executionContext: ExecutionContext): Resource[Server] = { val host = address.map(InetAddress.getByName).getOrElse(InetAddress.getLoopbackAddress) Resource(Future { val builder = NettyServerBuilder.forAddress(new InetSocketAddress(host, desiredPort.value)) builder.sslContext(sslContext.orNull) builder.channelType(classOf[NioServerSocketChannel]) builder.permitKeepAliveTime(10, SECONDS) builder.permitKeepAliveWithoutCalls(true) builder.directExecutor() builder.maxInboundMessageSize(maxInboundMessageSize) interceptors.foreach(builder.intercept) builder.intercept(new MetricsInterceptor(metrics)) eventLoopGroups.populate(builder) services.foreach { service => builder.addService(service) toLegacyService(service).foreach(builder.addService) } val server = builder.build() try { server.start() } catch { case e: IOException if e.getCause != null && e.getCause.isInstanceOf[BindException] => throw new UnableToBind(desiredPort, e.getCause) } server })(server => Future(server.shutdown().awaitTermination())) } // This exposes the existing services under com.daml also under com.digitalasset. // This is necessary to allow applications built with an earlier version of the SDK // to still work. // The "proxy" services will not show up on the reflection service, because of the way it // processes service definitions via protobuf file descriptors. private def toLegacyService(service: BindableService): Option[ServerServiceDefinition] = { val `com.daml` = "com.daml" val `com.digitalasset` = "com.digitalasset" val damlDef = service.bindService() val damlDesc = damlDef.getServiceDescriptor // Only add "proxy" services if it actually contains com.daml in the service name. // There are other services registered like the reflection service, that doesn't need the special treatment. if (damlDesc.getName.contains(`com.daml`)) { val digitalassetName = damlDesc.getName.replace(`com.daml`, `com.digitalasset`) val digitalassetDef = ServerServiceDefinition.builder(digitalassetName) damlDef.getMethods.forEach { methodDef => val damlMethodDesc = methodDef.getMethodDescriptor val digitalassetMethodName = damlMethodDesc.getFullMethodName.replace(`com.daml`, `com.digitalasset`) val digitalassetMethodDesc = damlMethodDesc.toBuilder.setFullMethodName(digitalassetMethodName).build() val _ = digitalassetDef.addMethod( digitalassetMethodDesc.asInstanceOf[MethodDescriptor[Message, Message]], methodDef.getServerCallHandler.asInstanceOf[ServerCallHandler[Message, Message]] ) } Option(digitalassetDef.build()) } else None } } object GrpcServerOwner { final class UnableToBind(port: Port, cause: Throwable) extends RuntimeException( s"The API server was unable to bind to port $port. Terminate the process occupying the port, or choose a different one.", cause) with NoStackTrace }
Example 2
Source File: EventLoopGroupOwner.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.platform.apiserver import java.util.UUID import java.util.concurrent.TimeUnit.MILLISECONDS import com.daml.resources.{Resource, ResourceOwner} import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.nio.{NioServerSocketChannel, NioSocketChannel} import io.netty.channel.{Channel, EventLoopGroup, ServerChannel} import io.netty.util.concurrent.DefaultThreadFactory import scala.concurrent.{ExecutionContext, Future, Promise} import scala.util.Try final class EventLoopGroupOwner(threadPoolName: String, parallelism: Int) extends ResourceOwner[EventLoopGroup] { override def acquire()(implicit executionContext: ExecutionContext): Resource[EventLoopGroup] = Resource( Future(new NioEventLoopGroup( parallelism, new DefaultThreadFactory(s"$threadPoolName-grpc-event-loop-${UUID.randomUUID()}", true))))( group => { val promise = Promise[Unit]() val future = group.shutdownGracefully(0, 0, MILLISECONDS) future.addListener((f: io.netty.util.concurrent.Future[_]) => promise.complete(Try(f.get).map(_ => ()))) promise.future } ) } object EventLoopGroupOwner { val clientChannelType: Class[_ <: Channel] = classOf[NioSocketChannel] val serverChannelType: Class[_ <: ServerChannel] = classOf[NioServerSocketChannel] }
Example 3
Source File: NettyServer.scala From lila-ws with GNU Affero General Public License v3.0 | 5 votes |
package lila.ws package netty import com.typesafe.config.Config import com.typesafe.scalalogging.Logger import io.netty.bootstrap.ServerBootstrap import io.netty.channel.{ Channel, ChannelInitializer } import io.netty.channel.epoll.{ EpollEventLoopGroup, EpollServerSocketChannel } import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.nio.NioServerSocketChannel import io.netty.handler.codec.http._ import scala.concurrent.ExecutionContext final class NettyServer( clients: ClientSystem, router: Router, config: Config )(implicit ec: ExecutionContext) { private val logger = Logger(getClass) def start(): Unit = { logger.info("Start") val port = config.getInt("http.port") val useEpoll = config.getBoolean("netty.useEpoll") val bossGroup = if (useEpoll) new EpollEventLoopGroup(1) else new NioEventLoopGroup(1) val workerGroup = if (useEpoll) new EpollEventLoopGroup else new NioEventLoopGroup val channelClz = if (useEpoll) classOf[EpollServerSocketChannel] else classOf[NioServerSocketChannel] try { val boot = new ServerBootstrap boot .group(bossGroup, workerGroup) .channel(channelClz) .childHandler(new ChannelInitializer[Channel] { override def initChannel(ch: Channel): Unit = { val pipeline = ch.pipeline() pipeline.addLast(new HttpServerCodec) pipeline.addLast(new HttpObjectAggregator(4096)) pipeline.addLast(new ProtocolHandler(clients, router)) pipeline.addLast(new FrameHandler) } }) val server = boot.bind(port).sync().channel() logger.info(s"Listening to $port") server.closeFuture().sync() logger.info(s"Closed $port") } finally { bossGroup.shutdownGracefully() workerGroup.shutdownGracefully() } } }
Example 4
Source File: NettyNioServer.scala From netty-in-action-scala with Apache License 2.0 | 5 votes |
package nia.chapter4 import io.netty.bootstrap.ServerBootstrap import io.netty.buffer.Unpooled import io.netty.channel._ import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.SocketChannel import io.netty.channel.socket.nio.NioServerSocketChannel import java.net.InetSocketAddress import java.nio.charset.Charset class NettyNioServer { @throws[Exception] def server(port: Int): Unit = { val buf = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Hi!\r\n", Charset.forName("UTF-8"))) //为非阻塞模式使用NioEventLoopGroup val group: EventLoopGroup = new NioEventLoopGroup try { //创建ServerBootstrap val b = new ServerBootstrap b.group(group) .channel(classOf[NioServerSocketChannel]) .localAddress(new InetSocketAddress(port)) //指定 ChannelInitializer,对于每个已接受的连接都调用它 .childHandler { new ChannelInitializer[SocketChannel]() { @throws[Exception] override def initChannel(ch: SocketChannel): Unit = { ch.pipeline.addLast(new ChannelInboundHandlerAdapter() { @throws[Exception] override def channelActive(ctx: ChannelHandlerContext): Unit = { //将消息写到客户端,并添加ChannelFutureListener, //以便消息一被写完就关闭连接 ctx.writeAndFlush(buf.duplicate) .addListener(ChannelFutureListener.CLOSE) } }) } } } //绑定服务器以接受连接 val f = b.bind.sync() f.channel.closeFuture.sync() } finally { //释放所有的资源 group.shutdownGracefully.sync() } } }
Example 5
Source File: BootstrapSharingEventLoopGroup.scala From netty-in-action-scala with Apache License 2.0 | 5 votes |
package nia.chapter8 import io.netty.bootstrap.Bootstrap import io.netty.bootstrap.ServerBootstrap import io.netty.buffer.ByteBuf import io.netty.channel.ChannelFuture import io.netty.channel.ChannelFutureListener import io.netty.channel.ChannelHandlerContext import io.netty.channel.SimpleChannelInboundHandler import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.nio.NioServerSocketChannel import io.netty.channel.socket.nio.NioSocketChannel import java.net.InetSocketAddress def bootstrap(): Unit = { //创建 ServerBootstrap 以创建 ServerSocketChannel,并绑定它 val bootstrap = new ServerBootstrap //设置 EventLoopGroup,其将提供用以处理 Channel 事件的 EventLoop bootstrap.group(new NioEventLoopGroup, new NioEventLoopGroup) //指定要使用的 Channel 实现 .channel(classOf[NioServerSocketChannel]) //设置用于处理已被接受的子 Channel 的 I/O 和数据的 ChannelInboundHandler .childHandler { new SimpleChannelInboundHandler[ByteBuf]() { private[chapter8] var connectFuture: ChannelFuture = _ @throws[Exception] override def channelActive(ctx: ChannelHandlerContext): Unit = { //创建一个 Bootstrap 类的实例以连接到远程主机 val bootstrap = new Bootstrap //指定 Channel 的实现 bootstrap.channel(classOf[NioSocketChannel]) .handler(new SimpleChannelInboundHandler[ByteBuf]() { @throws[Exception] override protected def channelRead0( ctx: ChannelHandlerContext, in: ByteBuf): Unit = { println("Received data") } }) //使用与分配给已被接受的子Channel相同的EventLoop bootstrap.group(ctx.channel.eventLoop) //连接到远程节点 connectFuture = bootstrap.connect(new InetSocketAddress("www.manning.com", 80)) } @throws[Exception] override protected def channelRead0( channelHandlerContext: ChannelHandlerContext, byteBuf: ByteBuf): Unit = { if (connectFuture.isDone) { //当连接完成时,执行一些数据操作(如代理) // do something with the data } } } } //通过配置好的 ServerBootstrap 绑定该 ServerSocketChannel val future = bootstrap.bind(new InetSocketAddress(8080)) future.addListener(new ChannelFutureListener() { @throws[Exception] override def operationComplete(channelFuture: ChannelFuture): Unit = { if (channelFuture.isSuccess) System.out.println("Server bound") else { System.err.println("Bind attempt failed") channelFuture.cause.printStackTrace() } } }) } }
Example 6
Source File: BootstrapServer.scala From netty-in-action-scala with Apache License 2.0 | 5 votes |
package nia.chapter8 import io.netty.bootstrap.ServerBootstrap import io.netty.buffer.ByteBuf import io.netty.channel.ChannelFuture import io.netty.channel.ChannelFutureListener import io.netty.channel.ChannelHandlerContext import io.netty.channel.SimpleChannelInboundHandler import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.nio.NioServerSocketChannel import java.net.InetSocketAddress def bootstrap(): Unit = { val group = new NioEventLoopGroup //创建 Server Bootstrap val bootstrap = new ServerBootstrap //设置 EventLoopGroup,其提供了用于处理 Channel 事件的EventLoop bootstrap.group(group) //指定要使用的 Channel 实现 .channel(classOf[NioServerSocketChannel]) //设置用于处理已被接受的子 Channel 的I/O及数据的 ChannelInboundHandler .childHandler { new SimpleChannelInboundHandler[ByteBuf]() { @throws[Exception] override protected def channelRead0(channelHandlerContext: ChannelHandlerContext, byteBuf: ByteBuf): Unit = { System.out.println("Received data") } } } //通过配置好的 ServerBootstrap 的实例绑定该 Channel val future = bootstrap.bind(new InetSocketAddress(8080)) future.addListener(new ChannelFutureListener() { @throws[Exception] override def operationComplete(channelFuture: ChannelFuture): Unit = { if (channelFuture.isSuccess) System.out.println("Server bound") else { System.err.println("Bind attempt failed") channelFuture.cause.printStackTrace() } } }) } }
Example 7
Source File: BootstrapWithInitializer.scala From netty-in-action-scala with Apache License 2.0 | 5 votes |
package nia.chapter8 import io.netty.bootstrap.ServerBootstrap import io.netty.channel._ import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.nio.NioServerSocketChannel import io.netty.handler.codec.http.HttpClientCodec import io.netty.handler.codec.http.HttpObjectAggregator import java.net.InetSocketAddress @throws[InterruptedException] def bootstrap(): Unit = { //创建 ServerBootstrap 以创建和绑定新的 Channel val bootstrap = new ServerBootstrap //设置 EventLoopGroup,其将提供用以处理 Channel 事件的 EventLoop bootstrap.group(new NioEventLoopGroup, new NioEventLoopGroup) .channel(classOf[NioServerSocketChannel]) //指定 Channel 的实现 //注册一个 ChannelInitializerImpl 的实例来设置 ChannelPipeline .childHandler(new ChannelInitializerImpl) //绑定到地址 val future = bootstrap.bind(new InetSocketAddress(8080)) future.sync } //用以设置 ChannelPipeline 的自定义 ChannelInitializerImpl 实现 final private[chapter8] class ChannelInitializerImpl extends ChannelInitializer[Channel] { //将所需的 ChannelHandler 添加到 ChannelPipeline @throws[Exception] override protected def initChannel(ch: Channel): Unit = { val pipeline = ch.pipeline pipeline.addLast(new HttpClientCodec) pipeline.addLast(new HttpObjectAggregator(Integer.MAX_VALUE)) } } }
Example 8
Source File: GrpcGatewayServerBuilder.scala From grpcgateway with MIT License | 5 votes |
package grpcgateway.server import grpcgateway.handlers.{GrpcGatewayHandler, MethodNotFoundHandler, SwaggerHandler} import io.netty.bootstrap.ServerBootstrap import io.netty.channel.ChannelInitializer import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.SocketChannel import io.netty.channel.socket.nio.NioServerSocketChannel import io.netty.handler.codec.http.{HttpObjectAggregator, HttpServerCodec} case class GrpcGatewayServerBuilder( port: Int = 80, services: Seq[GrpcGatewayHandler] = Nil ) { def forPort(port: Int): GrpcGatewayServerBuilder = { copy(port = port) } def addService(service: GrpcGatewayHandler): GrpcGatewayServerBuilder = { copy(services = services :+ service) } def build(): GrpcGatewayServer = { val masterGroup = new NioEventLoopGroup() val slaveGroup = new NioEventLoopGroup() val bootstrap = new ServerBootstrap() bootstrap .group(masterGroup, slaveGroup) .channel(classOf[NioServerSocketChannel]) .childHandler(new ChannelInitializer[SocketChannel] { override def initChannel(ch: SocketChannel): Unit = { ch.pipeline().addLast("codec", new HttpServerCodec()) ch.pipeline().addLast("aggregator", new HttpObjectAggregator(512 * 1024)) ch.pipeline().addLast("swagger", new SwaggerHandler(services)) services.foreach { handler => ch.pipeline().addLast(handler.name, handler) } ch.pipeline().addLast(new MethodNotFoundHandler()) } }) new GrpcGatewayServer(port, bootstrap, masterGroup, slaveGroup, services.toList) } } object GrpcGatewayServerBuilder { def forPort(port: Int): GrpcGatewayServerBuilder = new GrpcGatewayServerBuilder().forPort(port) def addService(service: GrpcGatewayHandler): GrpcGatewayServerBuilder = new GrpcGatewayServerBuilder().addService(service) }
Example 9
Source File: PacketProxy.scala From Neutrino with Apache License 2.0 | 5 votes |
package com.ebay.neutrino import java.net.{InetAddress, InetSocketAddress, SocketAddress} import com.ebay.neutrino.util.Utilities import scala.concurrent.Future import scala.util.{Failure, Success} import com.typesafe.scalalogging.slf4j.StrictLogging import io.netty.bootstrap.{Bootstrap, ServerBootstrap} import io.netty.channel.ChannelHandler.Sharable import io.netty.channel._ import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.nio.{NioServerSocketChannel, NioSocketChannel} import io.netty.util.AttributeKey override def channelRead(ctx: ChannelHandlerContext, msg: AnyRef) { println("Writing packet from downstream to upstream...") upstream.writeAndFlush(msg) //ctx.fireChannelRead(msg) } override def channelInactive(ctx: ChannelHandlerContext): Unit = { println("Downstream closing..") upstream.close() ctx.fireChannelInactive() } }
Example 10
Source File: NettyUtils.scala From aloha with Apache License 2.0 | 5 votes |
package me.jrwang.aloha.transport.util import io.netty.buffer.PooledByteBufAllocator import io.netty.channel.{Channel, EventLoopGroup, ServerChannel} import io.netty.channel.epoll.{EpollEventLoopGroup, EpollServerSocketChannel, EpollSocketChannel} import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.nio.{NioServerSocketChannel, NioSocketChannel} import io.netty.util.concurrent.DefaultThreadFactory import io.netty.util.internal.PlatformDependent import me.jrwang.aloha.common.Logging object NettyUtils extends Logging { private def getPrivateStaticField(name: String) = try { val f = PooledByteBufAllocator.DEFAULT.getClass.getDeclaredField(name) f.setAccessible(true) f.getInt(null) } catch { case e: Exception => throw new RuntimeException(e) } }
Example 11
Source File: FrontendService.scala From spark-sql-server with Apache License 2.0 | 5 votes |
package org.apache.spark.sql.server.service import io.netty.bootstrap.ServerBootstrap import io.netty.channel.ChannelInitializer import io.netty.channel.nio.NioEventLoopGroup import io.netty.channel.socket.SocketChannel import io.netty.channel.socket.nio.NioServerSocketChannel import io.netty.handler.logging.{LoggingHandler, LogLevel} import org.apache.spark.sql.internal.SQLConf import org.apache.spark.sql.server.SQLServerConf._ private[service] abstract class FrontendService extends CompositeService { var port: Int = _ var workerThreads: Int = _ var bossGroup: NioEventLoopGroup = _ var workerGroup: NioEventLoopGroup = _ def messageHandler: ChannelInitializer[SocketChannel] override def doInit(conf: SQLConf): Unit = { port = conf.sqlServerPort workerThreads = conf.sqlServerWorkerThreads bossGroup = new NioEventLoopGroup(1) workerGroup = new NioEventLoopGroup(workerThreads) } override def doStart(): Unit = { try { val b = new ServerBootstrap() // .option(ChannelOption.SO_KEEPALIVE, true) .group(bossGroup, workerGroup) .channel(classOf[NioServerSocketChannel]) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(messageHandler) // Binds and starts to accept incoming connections val f = b.bind(port).sync() // Blocked until the server socket is closed logInfo(s"Start running the SQL server (port=$port, workerThreads=$workerThreads)") f.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully() workerGroup.shutdownGracefully() } } }