当前位置: 首页>>代码示例>>Scala>>正文


Scala NioServerSocketChannel类代码示例

本文整理汇总了Scala中io.netty.channel.socket.nio.NioServerSocketChannel的典型用法代码示例。如果您正苦于以下问题:Scala NioServerSocketChannel类的具体用法?Scala NioServerSocketChannel怎么用?Scala NioServerSocketChannel使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了NioServerSocketChannel类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: SockServer

//设置package包名称以及导入依赖的类
package tk.dasb.server

import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.ChannelOption
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.nio.NioServerSocketChannel
import tk.dasb.handler.Sock5Handler
import tk.dasb.util.Log

object SockServer extends Log {

  def startServer(port: Int) = {
    val boss = new NioEventLoopGroup()
    val worker = new NioEventLoopGroup()
    try {
      val b = new ServerBootstrap()
      b.group(boss, worker).channel(classOf[NioServerSocketChannel])
        .option[java.lang.Integer](ChannelOption.SO_BACKLOG, 128)
        .childOption[java.lang.Boolean](ChannelOption.SO_KEEPALIVE, true)
        .childHandler(new Sock5Handler)
      log.info("starting server on port:{}", port)
      b.bind(port).sync().channel().closeFuture().sync()
    } finally {
      boss.shutdownGracefully()
      worker.shutdownGracefully()
    }
  }

} 
开发者ID:xqdxqd,项目名称:tiny-sock5-server,代码行数:30,代码来源:SockServer.scala

示例2: InputStreamingServer

//设置package包名称以及导入依赖的类
package com.bwsw.sj.engine.input.connection.tcp.server

import java.util.concurrent.{Callable, ArrayBlockingQueue}

import com.bwsw.sj.engine.core.input.InputStreamingExecutor
import io.netty.bootstrap.ServerBootstrap
import io.netty.buffer.ByteBuf
import io.netty.channel._
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.handler.logging.{LogLevel, LoggingHandler}
import org.slf4j.LoggerFactory

import scala.collection.concurrent



class InputStreamingServer(host: String,
                           port: Int,
                           executor: InputStreamingExecutor,
                           channelContextQueue: ArrayBlockingQueue[ChannelHandlerContext],
                           bufferForEachContext: concurrent.Map[ChannelHandlerContext, ByteBuf]) extends Callable[Unit] {

  private val logger = LoggerFactory.getLogger(this.getClass)

  override def call() = {
    logger.info(s"Launch input streaming server on: '$host:$port'\n")
    val bossGroup: EventLoopGroup = new NioEventLoopGroup()
    val workerGroup = new NioEventLoopGroup()
    try {
      val bootstrapServer = new ServerBootstrap()
      bootstrapServer.group(bossGroup, workerGroup)
        .channel(classOf[NioServerSocketChannel])
        .handler(new LoggingHandler(LogLevel.INFO))
        .childHandler(new InputStreamingChannelInitializer(executor,  channelContextQueue, bufferForEachContext))

      bootstrapServer.bind(host, port).sync().channel().closeFuture().sync()
    } finally {
      workerGroup.shutdownGracefully()
      bossGroup.shutdownGracefully()
    }
  }
} 
开发者ID:bwsw,项目名称:sj-platform,代码行数:44,代码来源:InputStreamingServer.scala

示例3: 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])
    }
  }
} 
开发者ID:madprogrammer,项目名称:scxmppd,代码行数:50,代码来源:Server.scala

示例4: 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();
  }

} 
开发者ID:woshilaiceshide,项目名称:s-server-benchmark,代码行数:47,代码来源:NettyHttpServer.scala

示例5: 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)
} 
开发者ID:btlines,项目名称:grpcgateway,代码行数:54,代码来源:GrpcGatewayServerBuilder.scala

示例6: init

//设置package包名称以及导入依赖的类
package org.nerver.core.server

import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.ChannelOption
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.handler.logging.{LogLevel, LoggingHandler}
import org.nerver.core.HandlerScanner
import org.nerver.core.annotation.Method
import org.nerver.core.handler.HttpServerHandlerInitializer



  def init(): Unit = {
    //configure server
    val bossGroup = new NioEventLoopGroup()
    val workGroup = new NioEventLoopGroup()
    try {
      val serverBootstrap = new ServerBootstrap()
      serverBootstrap.option[java.lang.Integer](ChannelOption.SO_BACKLOG, 1024)
      serverBootstrap.childOption[java.lang.Boolean](ChannelOption.TCP_NODELAY, true)
      serverBootstrap.childOption[java.lang.Boolean](ChannelOption.SO_KEEPALIVE, true)

      serverBootstrap.group(bossGroup, workGroup)
          .channel(classOf[NioServerSocketChannel])
          .handler(new LoggingHandler(LogLevel.INFO))
          .childHandler(new HttpServerHandlerInitializer())
      // TODO: add http handler
      val channel = serverBootstrap.bind(port).sync().channel()
      channel.closeFuture().sync()
    } finally {
      bossGroup.shutdownGracefully()
      workGroup.shutdownGracefully()
    }
  }
}

object BaseServer {
  val getMethodMap: scala.collection.mutable.HashMap[String, java.lang.reflect.Method] = scala.collection.mutable.HashMap()
  val postMethodMap: scala.collection.mutable.HashMap[String, java.lang.reflect.Method] = scala.collection.mutable.HashMap()
  val getHandlerMap: scala.collection.mutable.HashMap[String, java.lang.Class[_]] = scala.collection.mutable.HashMap()
  val postHandlerMap: scala.collection.mutable.HashMap[String, java.lang.Class[_]] = scala.collection.mutable.HashMap()
  val typeMap = scala.collection.mutable.HashMap(Method.GET -> getHandlerMap, Method.POST -> postHandlerMap)
  val methodTypeMap = scala.collection.mutable.HashMap(Method.GET -> getMethodMap, Method.POST -> postMethodMap)

  def main(args: Array[String]): Unit = {
    val handlerScanner = new HandlerScanner(args(1))
    handlerScanner.init()
    val baseServer = new BaseServer(Integer.parseInt(args(0)))
    baseServer.init()
  }
} 
开发者ID:Presageee,项目名称:nerver,代码行数:53,代码来源:BaseServer.scala


注:本文中的io.netty.channel.socket.nio.NioServerSocketChannel类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。