本文整理汇总了Scala中io.netty.channel.ChannelInitializer类的典型用法代码示例。如果您正苦于以下问题:Scala ChannelInitializer类的具体用法?Scala ChannelInitializer怎么用?Scala ChannelInitializer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ChannelInitializer类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: NpnOrAlpnHandler
//设置package包名称以及导入依赖的类
package com.twitter.finagle.http2.transport
import com.twitter.finagle.Stack
import com.twitter.finagle.netty4.http.exp._
import io.netty.channel.{Channel, ChannelHandlerContext, ChannelInitializer}
import io.netty.handler.codec.http2.{Http2Codec, Http2ServerDowngrader}
import io.netty.handler.ssl.{ApplicationProtocolNames, ApplicationProtocolNegotiationHandler}
private[http2] class NpnOrAlpnHandler(init: ChannelInitializer[Channel], params: Stack.Params)
extends ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) {
@throws(classOf[Exception])
protected def configurePipeline(ctx: ChannelHandlerContext, protocol: String) {
protocol match {
case ApplicationProtocolNames.HTTP_2 =>
// Http2 has been negotiated, replace the HttpCodec with an Http2Codec
val initializer = new ChannelInitializer[Channel] {
def initChannel(ch: Channel): Unit = {
ch.pipeline.addLast(new Http2ServerDowngrader(false , initializer))
case ApplicationProtocolNames.HTTP_1_1 =>
// The Http codec is already in the pipeline, so we are good!
case _ =>
throw new IllegalStateException("unknown protocol: " + protocol)
}
}
}
示例2: Http2TlsServerInitializer
//设置package包名称以及导入依赖的类
package com.twitter.finagle.http2
import com.twitter.finagle.Stack
import com.twitter.finagle.http2.transport.NpnOrAlpnHandler
import io.netty.channel.{Channel, ChannelInitializer}
import io.netty.channel.socket.SocketChannel
private[http2] class Http2TlsServerInitializer (
init: ChannelInitializer[Channel],
params: Stack.Params)
extends ChannelInitializer[SocketChannel] {
def initChannel(ch: SocketChannel): Unit = {
val p = ch.pipeline()
p.addLast(new NpnOrAlpnHandler(init, params))
p.addLast(init)
}
}
示例3: ServerBridge
//设置package包名称以及导入依赖的类
package com.twitter.finagle.netty4.channel
import com.twitter.finagle.transport.Transport
import io.netty.channel.{ChannelInitializer, Channel}
import io.netty.channel.ChannelHandler.Sharable
@Sharable
private[netty4] class ServerBridge[In, Out](
transportFac: Channel => Transport[In, Out],
serveTransport: Transport[In, Out] => Unit)
extends ChannelInitializer[Channel] {
def initChannel(ch: Channel): Unit = {
val transport: Transport[In, Out] = transportFac(ch)
serveTransport(transport)
}
}
示例4: LegacyChannelInitializer
//设置package包名称以及导入依赖的类
package com.wavesplatform.it.network.client
import com.wavesplatform.network.{HandshakeDecoder, HandshakeHandler, HandshakeTimeoutHandler, LegacyFrameCodec}
import io.netty.channel.ChannelInitializer
import io.netty.channel.socket.SocketChannel
import io.netty.handler.codec.{LengthFieldBasedFrameDecoder, LengthFieldPrepender}
import scala.concurrent.duration._
class LegacyChannelInitializer(handshakeHandler: HandshakeHandler) extends ChannelInitializer[SocketChannel] {
override def initChannel(ch: SocketChannel): Unit =
ch.pipeline()
.addLast(
new HandshakeDecoder,
new HandshakeTimeoutHandler(30.seconds),
handshakeHandler,
new LengthFieldPrepender(4),
new LengthFieldBasedFrameDecoder(1024*1024, 0, 4, 0, 4),
new LegacyFrameCodec(NopPeerDatabase))
}
示例5: InputStreamingChannelInitializer
//设置package包名称以及导入依赖的类
package com.bwsw.sj.engine.input.connection.tcp.server
import java.util.concurrent.ArrayBlockingQueue
import com.bwsw.sj.engine.core.input.InputStreamingExecutor
import io.netty.buffer.ByteBuf
import io.netty.channel.socket.SocketChannel
import io.netty.channel.{ChannelHandlerContext, ChannelInitializer}
import io.netty.handler.codec.string.StringEncoder
import io.netty.handler.logging.{LogLevel, LoggingHandler}
import scala.collection.concurrent
class InputStreamingChannelInitializer(executor: InputStreamingExecutor,
channelContextQueue: ArrayBlockingQueue[ChannelHandlerContext],
bufferForEachContext: concurrent.Map[ChannelHandlerContext, ByteBuf])
extends ChannelInitializer[SocketChannel] {
def initChannel(channel: SocketChannel) = {
val pipeline = channel.pipeline()
pipeline.addLast("logger", new LoggingHandler(LogLevel.WARN))
pipeline.addLast("encoder", new StringEncoder())
pipeline.addLast("handler", new InputStreamingServerHandler(executor, channelContextQueue, bufferForEachContext))
}
}
示例6: ServerInitializer
//设置package包名称以及导入依赖的类
package com.bwsw.tstreamstransactionserver.netty.server
import com.bwsw.tstreamstransactionserver.netty.Message
import com.bwsw.tstreamstransactionserver.options.ServerOptions.TransportOptions
import io.netty.buffer.ByteBuf
import io.netty.channel.socket.SocketChannel
import io.netty.channel.{ChannelInitializer, SimpleChannelInboundHandler}
import io.netty.handler.codec.LengthFieldBasedFrameDecoder
import io.netty.handler.codec.bytes.ByteArrayEncoder
class ServerInitializer(serverHandler: => SimpleChannelInboundHandler[ByteBuf]) extends ChannelInitializer[SocketChannel] {
override def initChannel(ch: SocketChannel): Unit = {
ch.pipeline()
.addLast(new ByteArrayEncoder())
.addLast(new LengthFieldBasedFrameDecoder(
Int.MaxValue,
//packageTransmissionOpts.maxDataPackageSize max packageTransmissionOpts.maxMetadataPackageSize,
Message.headerFieldSize,
Message.lengthFieldSize)
)
.addLast(serverHandler)
}
}
示例7: ClientInitializer
//设置package包名称以及导入依赖的类
package com.bwsw.tstreamstransactionserver.netty.client
import java.util.concurrent.ConcurrentHashMap
import com.twitter.scrooge.ThriftStruct
import com.bwsw.tstreamstransactionserver.netty.Message
import io.netty.channel.ChannelInitializer
import io.netty.channel.socket.SocketChannel
import io.netty.handler.codec.LengthFieldBasedFrameDecoder
import io.netty.handler.codec.bytes.ByteArrayEncoder
import scala.concurrent.{ExecutionContext, Promise => ScalaPromise}
class ClientInitializer(reqIdToRep: ConcurrentHashMap[Long, ScalaPromise[ThriftStruct]], client: Client, context: ExecutionContext) extends ChannelInitializer[SocketChannel] {
override def initChannel(ch: SocketChannel): Unit = {
ch.pipeline()
.addLast(new ByteArrayEncoder())
.addLast(new LengthFieldBasedFrameDecoder(
Int.MaxValue,
Message.headerFieldSize,
Message.lengthFieldSize)
)
.addLast(new ClientHandler(reqIdToRep, client, context))
}
}
示例8: XmppServerInitializer
//设置package包名称以及导入依赖的类
package com.scxmpp.xmpp
import akka.actor.{ActorSystem, Props}
import com.scxmpp.c2s.C2SManager
import com.scxmpp.cluster.ClusterListener
import com.scxmpp.modules.support.{ModuleManager, HandlerManager}
import com.scxmpp.netty.{XmlElementDecoder, XmlElementEncoder, XmlFrameDecoder}
import com.scxmpp.routing.Router
import com.scxmpp.server.{ServerContext, SslContextHelper}
import com.typesafe.config.Config
import io.netty.channel.socket.SocketChannel
import io.netty.channel.{ChannelInitializer, ChannelPipeline}
import io.netty.handler.codec.string.StringEncoder
import io.netty.util.CharsetUtil
class XmppServerInitializer(context: ServerContext, config: Config) extends ChannelInitializer[SocketChannel] {
context.actorSystem.actorOf(Props[ClusterListener], "clusterListener")
context.actorSystem.actorOf(Props(classOf[Router], context, config), "router")
context.actorSystem.actorOf(Props(classOf[C2SManager], config), "c2s")
context.actorSystem.actorOf(Props(classOf[ModuleManager], context, config), "module")
context.actorSystem.actorOf(Props(classOf[HandlerManager], context, config), "handler")
var sslContext = SslContextHelper.getContext(config)
override def initChannel(s: SocketChannel): Unit = {
val p: ChannelPipeline = s.pipeline
if (sslContext.isDefined)
p.addLast(sslContext.get.newHandler(s.alloc))
p.addLast("xmlFrameDecoder", new XmlFrameDecoder())
p.addLast("xmlElementDecoder", new XmlElementDecoder())
p.addLast("xmlElementEncoder", new XmlElementEncoder())
p.addLast("handler", new XmppServerHandler(context.actorSystem))
}
}
示例9: MappedWebServerInitializer
//设置package包名称以及导入依赖的类
package com.scxmpp.http
import com.scxmpp.server.{ServerContext, SslContextHelper}
import com.typesafe.config.Config
import io.netty.channel.ChannelInitializer
import io.netty.channel.socket.SocketChannel
import io.netty.handler.codec.http.{HttpObjectAggregator, HttpServerCodec}
import io.netty.handler.stream.ChunkedWriteHandler
class MappedWebServerInitializer(context: ServerContext, config: Config) extends ChannelInitializer[SocketChannel] {
var sslContext = SslContextHelper.getContext(config)
override def initChannel(s: SocketChannel): Unit = {
val p = s.pipeline()
if (sslContext.isDefined)
p.addLast(sslContext.get.newHandler(s.alloc))
p.addLast("codec", new HttpServerCodec())
p.addLast("aggregator", new HttpObjectAggregator(65536))
p.addLast("chunked", new ChunkedWriteHandler())
p.addLast("handler", new MappedWebServerHandler(context, config))
}
}
示例10: Server
//设置package包名称以及导入依赖的类
package com.scxmpp.server
import java.net.InetSocketAddress
import io.netty.bootstrap.ServerBootstrap
import io.netty.buffer.PooledByteBufAllocator
import io.netty.channel.epoll.{Epoll, EpollEventLoopGroup, EpollServerSocketChannel}
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.channel.{Channel, ChannelOption, EventLoopGroup, ServerChannel, ChannelInitializer}
import scala.collection.JavaConversions._
import scala.collection.mutable.ArrayBuffer
import com.typesafe.config.Config
class Server(context: ServerContext) {
var channels = ArrayBuffer.empty[Channel]
var bootstraps = ArrayBuffer.empty[ServerBootstrap]
private def doRun(group: EventLoopGroup, clazz: Class[_ <: ServerChannel]): Unit = {
try {
for(server <- context.servers) {
val bootstrap = new ServerBootstrap()
bootstrap.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true))
val initializer = context.dynamicAccess.createInstanceFor[ChannelInitializer[Channel]](
server.getString("module"), List(classOf[ServerContext] -> context, classOf[Config] -> server)).get
bootstrap.group(group).channel(clazz).childHandler(initializer)
val inet: InetSocketAddress = new InetSocketAddress(
server.getString("endpoint.address"), server.getInt("endpoint.port"))
channels.add(bootstrap.bind(inet).sync.channel)
}
} finally {
for (channel <- channels)
channel.closeFuture.sync
group.shutdownGracefully.sync
}
}
def run(): Unit = {
if (Epoll.isAvailable) {
doRun(new EpollEventLoopGroup, classOf[EpollServerSocketChannel])
}
else {
doRun(new NioEventLoopGroup, classOf[NioServerSocketChannel])
}
}
}
示例11: BoshServerInitializer
//设置package包名称以及导入依赖的类
package com.scxmpp.bosh
import akka.actor.Props
import com.scxmpp.netty.XmlElementDecoder
import com.scxmpp.server.{ServerContext, SslContextHelper}
import com.typesafe.config.Config
import io.netty.channel.{ChannelInitializer, ChannelPipeline}
import io.netty.channel.socket.SocketChannel
import io.netty.handler.codec.http._
import io.netty.handler.stream.ChunkedWriteHandler
class BoshServerInitializer(context: ServerContext, config: Config) extends ChannelInitializer[SocketChannel] {
var sslContext = SslContextHelper.getContext(config)
context.actorSystem.actorOf(Props(classOf[BoshConnectionManager], config), "bosh")
override def initChannel(s: SocketChannel): Unit = {
val p: ChannelPipeline = s.pipeline
if (sslContext.isDefined)
p.addLast(sslContext.get.newHandler(s.alloc))
// Decoders
p.addLast("httpDecoder", new HttpRequestDecoder())
p.addLast("httpEncoder", new HttpResponseEncoder())
p.addLast("httpAggregator", new HttpObjectAggregator(65536))
p.addLast("xmlFrameDecoder", new HttpXmlFrameDecoder())
p.addLast("xmlElementDecoder", new XmlElementDecoder(false))
// Encoders
p.addLast("xmlHttpResponseEncoder", new XmlHttpResponseEncoder())
p.addLast("chunked", new ChunkedWriteHandler())
// Handler
p.addLast("handler", new BoshXmppServerHandler(context))
}
}
示例12: NettyHttpServer
//设置package包名称以及导入依赖的类
package woshilaiceshide.sserver.benchmark.netty
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.Channel
import io.netty.channel.ChannelInitializer
import io.netty.channel.ChannelOption
import io.netty.channel.EventLoopGroup
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.SocketChannel
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.example.http.helloworld.HttpHelloWorldServerHandler
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.channel.socket.nio.NioServerSocketChannel
object NettyHttpServer extends App with woshilaiceshide.sserver.benchmark.ServerProperty {
class HttpHelloWorldServerInitializer extends ChannelInitializer[SocketChannel] {
override def initChannel(ch: SocketChannel) {
//ch.config().setAllowHalfClosure(true)
val p = ch.pipeline();
p.addLast(new HttpServerCodec());
p.addLast(new HttpHelloWorldServerHandler());
}
}
val bossGroup: EventLoopGroup = new NioEventLoopGroup(1);
val workerGroup: EventLoopGroup = new NioEventLoopGroup(2);
try {
val b = new ServerBootstrap();
b.option[java.lang.Integer](ChannelOption.SO_BACKLOG, 1024);
b.group(bossGroup, workerGroup).channel(classOf[NioServerSocketChannel]).childHandler(new HttpHelloWorldServerInitializer());
val ch: Channel = b.bind(interface, port).sync().channel();
//??? just check for ipv4
System.err.println(s"Open your web browser and navigate to http://${if ("0.0.0.0" == interface) "127.0.0.1" else interface}:${port}/");
ch.closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
示例13: GrpcGatewayServerBuilder
//设置package包名称以及导入依赖的类
package grpcgateway.server
import grpcgateway.handlers.{GrpcGatewayHandler, 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.foreach { handler =>
ch.pipeline().addLast(handler.name, handler)
}
}
})
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)
}
示例14: LegacyChannelInitializer
//设置package包名称以及导入依赖的类
package com.wavesplatform.it.network.client
import com.wavesplatform.network.{HandshakeDecoder, HandshakeHandler, HandshakeTimeoutHandler, LegacyFrameCodec}
import io.netty.channel.ChannelInitializer
import io.netty.channel.socket.SocketChannel
import io.netty.handler.codec.{LengthFieldBasedFrameDecoder, LengthFieldPrepender}
import scala.concurrent.duration._
class LegacyChannelInitializer(handshakeHandler: HandshakeHandler) extends ChannelInitializer[SocketChannel] {
override def initChannel(ch: SocketChannel): Unit =
ch.pipeline()
.addLast(
new HandshakeDecoder,
new HandshakeTimeoutHandler(30.seconds),
handshakeHandler,
new LengthFieldPrepender(4),
new LengthFieldBasedFrameDecoder(1024*1024, 0, 4, 0, 4),
new LegacyFrameCodec)
}