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


Scala SocketChannel类代码示例

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


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

示例1: TcpIncomingConnection

//设置package包名称以及导入依赖的类
package akka.io

import java.nio.channels.SocketChannel
import scala.collection.immutable
import akka.actor.ActorRef
import akka.io.Inet.SocketOption


private[io] class TcpIncomingConnection(_tcp: TcpExt,
                                        _channel: SocketChannel,
                                        registry: ChannelRegistry,
                                        bindHandler: ActorRef,
                                        options: immutable.Traversable[SocketOption],
                                        readThrottling: Boolean)
  extends TcpConnection(_tcp, _channel, readThrottling) {

  signDeathPact(bindHandler)

  registry.register(channel, initialOps = 0)

  def receive = {
    case registration: ChannelRegistration ? completeConnect(registration, bindHandler, options)
  }
} 
开发者ID:love1314sea,项目名称:akka-2.3.16,代码行数:25,代码来源:TcpIncomingConnection.scala

示例2: TcpWriteActor

//设置package包名称以及导入依赖的类
package spooky.io

import java.nio.channels.SocketChannel
import spooky.actor.Props
import spooky.actor.Actor
import java.nio.ByteBuffer
import spooky.actor.ActorRef
import spooky.io.TcpThread._
import java.util.concurrent.ConcurrentHashMap

object TcpWriteActor {
  def props(channel: SocketChannel, actors: ConcurrentHashMap[Tcp.Address, Tuple2[MessageActorRef, WriteActorRef]]): Props = Props(classOf[TcpWriteActor], channel, actors)
}
private[io] class TcpWriteActor(private val channel: SocketChannel, actors: ConcurrentHashMap[Tcp.Address, Tuple2[MessageActorRef, WriteActorRef]]) extends Actor {

  def receive: PartialFunction[Any, Unit] = {
    case Tcp.Register(messageActor, address, _, _) => {
      val previous = actors.put(address, (messageActor, self))
      assert(previous == null)
      context.become(traffic())
    }

  }
  private def traffic(): PartialFunction[Any, Unit] = {
//    case Tcp.Close => {
//      sender() ! Tcp.Closed
//    }
    case Tcp.Write(data, Tcp.NoAck) => {
      write(data.toByteBuffer)
    }
    case Tcp.Write(data, ack) => {
      if (write(data.toByteBuffer)) {
        sender ! ack
      }
    }
  }

  private def write(bb: ByteBuffer): Boolean = {
    if (!channel.isConnected) {
      Thread.currentThread.interrupt()
      false
    } else {
      while (bb.hasRemaining()) {
        channel.write(bb)
      }
      true
    }
  }
} 
开发者ID:zpooky,项目名称:bittorrent,代码行数:50,代码来源:TcpWriteActor.scala

示例3: NioSocketChannel

//设置package包名称以及导入依赖的类
package knot.remote.channel

import java.net.SocketAddress
import java.nio.ByteBuffer
import java.nio.channels.SocketChannel

object NioSocketChannel {
  def apply(): NioSocketChannel = new NioSocketChannel(SocketChannel.open())
}

class NioSocketChannel(override val channel: SocketChannel) extends NioChannel(channel) {
  lazy val incomingBuffer: ByteBuffer = ByteBuffer.allocate(1)
  lazy val outgoingBuffer: ByteBuffer = ByteBuffer.allocate(1)

  def connect(socketAddress: SocketAddress): Boolean = channel.connect(socketAddress)

  def finishConnect: Boolean = channel.finishConnect()

  override def bind(address: SocketAddress): Unit = channel.bind(address)

  override def readInternal(): Int = {
    val r = channel.read(incomingBuffer)
    incomingBuffer.flip()
    r
  }

  override def writeInternal(bytes: Array[Byte]): Unit = {
    outgoingBuffer.put(bytes)
  }

  override def flushInternal(): Unit = {
    if (!isFlushPending) {
      setOpWrite()
      outgoingBuffer.flip()
      val r = channel.write(outgoingBuffer)
      log.debug(s"CLIENT: Write. $r")
      if (!outgoingBuffer.hasRemaining) {
        outgoingBuffer.clear()
        clearOpWrite()
      }
    }
  }
} 
开发者ID:defvar,项目名称:knot,代码行数:44,代码来源:NioSocketChannel.scala

示例4: TcpIncomingConnection

//设置package包名称以及导入依赖的类
package akka.io

import java.nio.channels.SocketChannel
import scala.collection.immutable
import akka.actor.ActorRef
import akka.io.Inet.SocketOption


private[io] class TcpIncomingConnection(
  _tcp: TcpExt,
  _channel: SocketChannel,
  registry: ChannelRegistry,
  bindHandler: ActorRef,
  options: immutable.Traversable[SocketOption],
  readThrottling: Boolean
)
    extends TcpConnection(_tcp, _channel, readThrottling) {

  signDeathPact(bindHandler)

  registry.register(channel, initialOps = 0)

  def receive = {
    case registration: ChannelRegistration ? completeConnect(registration, bindHandler, options)
  }
} 
开发者ID:rorygraves,项目名称:perf_tester,代码行数:27,代码来源:TcpIncomingConnection.scala

示例5: TcpIncomingConnection

//设置package包名称以及导入依赖的类
package akka.io

import java.nio.channels.SocketChannel
import scala.collection.immutable
import akka.actor.ActorRef
import akka.io.Inet.SocketOption


private[io] class TcpIncomingConnection(
  _tcp: TcpExt,
  _channel: SocketChannel,
  registry: ChannelRegistry,
  bindHandler: ActorRef,
  options: immutable.Traversable[SocketOption],
  readThrottling: Boolean
)
    extends TcpConnection(_tcp, _channel, readThrottling) {

  signDeathPact(bindHandler)

  registry.register(channel, initialOps = 0)

  def receive: PartialFunction[Any, Unit] = {
    case registration: ChannelRegistration ? completeConnect(registration, bindHandler, options)
  }
} 
开发者ID:rorygraves,项目名称:perf_tester,代码行数:27,代码来源:TcpIncomingConnection.scala


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