本文整理汇总了Scala中io.netty.channel.ChannelFuture类的典型用法代码示例。如果您正苦于以下问题:Scala ChannelFuture类的具体用法?Scala ChannelFuture怎么用?Scala ChannelFuture使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ChannelFuture类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: InitReqHandler
//设置package包名称以及导入依赖的类
package tk.dasb.handler
import io.netty.channel.{ChannelFuture, ChannelFutureListener, ChannelHandlerContext, ChannelInboundHandlerAdapter}
import tk.dasb.codec.{InitReplyEncoder, InitReqDecoder, ReplyEncoder, ReqDecoder}
import tk.dasb.protocol.{InitReply, InitReq}
import tk.dasb.util.{FutureConv, Log}
class InitReqHandler extends ChannelInboundHandlerAdapter with Log with FutureConv {
override def channelRead(ctx: ChannelHandlerContext, msg: scala.Any): Unit = {
val req = msg.asInstanceOf[InitReq]
log.debug("receive init req:{}",req)
if (req.ver == 0x05 && req.methods == 0x00) {
ctx.writeAndFlush(InitReply(0x05, 0x00)).success {
writeFuture=>
ctx.pipeline.remove(classOf[InitReqDecoder])
ctx.pipeline.remove(classOf[InitReplyEncoder])
ctx.pipeline.remove(InitReqHandler.this)
ctx.pipeline.addLast(new ReqDecoder)
ctx.pipeline.addLast(new ReplyEncoder)
ctx.pipeline.addLast(new ReqHandler)
}.failed{
writeFuture=>
ctx.channel.close()
log.info("unsupport {}", req)
}
}
}
}
示例2: RichChannelFuture
//设置package包名称以及导入依赖的类
package tk.dasb.util
import io.netty.channel.{Channel, ChannelFuture, ChannelFutureListener}
trait FutureConv {
implicit class RichChannelFuture(val ch: ChannelFuture) {
def success[T](block: ChannelFuture => T): ChannelFuture = {
ch.addListener(new ChannelFutureListener {
override def operationComplete(future: ChannelFuture): Unit = if (future.isSuccess) block(future)
})
}
def failed[T](block: ChannelFuture => T): ChannelFuture = {
ch.addListener(new ChannelFutureListener {
override def operationComplete(future: ChannelFuture): Unit = if (!future.isSuccess) block(future)
})
}
def Close(): ChannelFuture = {
ch.addListener(ChannelFutureListener.CLOSE)
}
def failedClose(): ChannelFuture = {
ch.addListener(ChannelFutureListener.CLOSE_ON_FAILURE)
}
def callBack[P >: ChannelFuture, T](block: P => T): ChannelFuture = {
ch.addListener(new ChannelFutureListener {
override def operationComplete(future: ChannelFuture): Unit = block(future)
})
}
}
}
示例3: InboundConnectionFilter
//设置package包名称以及导入依赖的类
package com.wavesplatform.network
import java.net.{InetAddress, InetSocketAddress}
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicInteger
import io.netty.channel.ChannelHandler.Sharable
import io.netty.channel.{ChannelFuture, ChannelHandlerContext}
import io.netty.handler.ipfilter.AbstractRemoteAddressFilter
import scorex.utils.ScorexLogging
@Sharable
class InboundConnectionFilter(peerDatabase: PeerDatabase, maxInboundConnections: Int, maxConnectionsPerHost: Int)
extends AbstractRemoteAddressFilter[InetSocketAddress] with ScorexLogging {
private val inboundConnectionCount = new AtomicInteger(0)
private val perHostConnectionCount = new ConcurrentHashMap[InetAddress, Int]
private def dec(remoteAddress: InetAddress) = {
inboundConnectionCount.decrementAndGet()
perHostConnectionCount.compute(remoteAddress, (_, cnt) => cnt - 1)
null.asInstanceOf[ChannelFuture]
}
override def accept(ctx: ChannelHandlerContext, remoteAddress: InetSocketAddress) = {
val newTotal = inboundConnectionCount.incrementAndGet()
val newCountPerHost = perHostConnectionCount.compute(remoteAddress.getAddress, (_, cnt) => Option(cnt).fold(1)(_ + 1))
val isBlacklisted = peerDatabase.blacklistedHosts.contains(remoteAddress.getAddress)
log.trace(s"Check inbound connection from $remoteAddress: new inbound total = $newTotal, " +
s"connections with this host = $newCountPerHost, address ${if (isBlacklisted) "IS" else "is not"} blacklisted")
newTotal <= maxInboundConnections &&
newCountPerHost <= maxConnectionsPerHost &&
!isBlacklisted
}
override def channelAccepted(ctx: ChannelHandlerContext, remoteAddress: InetSocketAddress) =
ctx.channel().closeFuture().addListener((_: ChannelFuture) => dec(remoteAddress.getAddress))
override def channelRejected(ctx: ChannelHandlerContext, remoteAddress: InetSocketAddress) =
dec(remoteAddress.getAddress)
}
示例4: NettyScalaFuture
//设置package包名称以及导入依赖的类
package com.wavesplatform.it
import java.util.concurrent.CancellationException
import io.netty.channel.{Channel, ChannelFuture}
import scala.concurrent.{Future, Promise}
import scala.util.Try
package object network {
implicit class NettyScalaFuture(nettyFuture: ChannelFuture) {
def asScala: Future[Channel] = {
val p = Promise[Channel]()
nettyFuture.addListener((future: ChannelFuture) => p complete Try(
if (future.isSuccess) future.channel()
else if (future.isCancelled) throw new CancellationException
else throw future.cause()))
p.future
}
}
}
示例5: NettyFutureConverters
//设置package包名称以及导入依赖的类
package com.lightbend.lagom.internal
import io.netty.channel.{ Channel, ChannelFuture, ChannelFutureListener }
import io.netty.util.concurrent.{ GenericFutureListener, Future => NettyFuture }
import scala.concurrent.{ Future, Promise }
object NettyFutureConverters {
implicit class ToFuture[T](future: NettyFuture[T]) {
def toScala: Future[T] = {
val promise = Promise[T]()
future.addListener(new GenericFutureListener[NettyFuture[T]] {
def operationComplete(future: NettyFuture[T]) = {
if (future.isSuccess) {
promise.success(future.getNow)
} else if (future.isCancelled) {
promise.failure(new RuntimeException("Future cancelled"))
} else {
promise.failure(future.cause())
}
}
})
promise.future
}
}
implicit class ChannelFutureToFuture(future: ChannelFuture) {
def channelFutureToScala: Future[Channel] = {
val promise = Promise[Channel]()
future.addListener(new ChannelFutureListener {
def operationComplete(future: ChannelFuture) = {
if (future.isSuccess) {
promise.success(future.channel())
} else if (future.isCancelled) {
promise.failure(new RuntimeException("Future cancelled"))
} else {
promise.failure(future.cause())
}
}
})
promise.future
}
}
}
示例6: GrpcGatewayServer
//设置package包名称以及导入依赖的类
package grpcgateway.server
import grpcgateway.handlers.GrpcGatewayHandler
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.{ChannelFuture, EventLoopGroup}
class GrpcGatewayServer private[server] (
port: Int,
bootstrap: ServerBootstrap,
masterGroup: EventLoopGroup,
slaveGroup: EventLoopGroup,
services: List[GrpcGatewayHandler]
) {
private var channel: Option[ChannelFuture] = None
def start(): Unit = {
channel = Option(bootstrap.bind(port).sync())
}
def shutdown(): Unit = {
slaveGroup.shutdownGracefully()
masterGroup.shutdownGracefully()
services.foreach(_.shutdown())
channel.foreach(_.channel().closeFuture().sync())
}
}