本文整理汇总了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)
}
}
示例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
}
}
}
示例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()
}
}
}
}
示例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)
}
}
示例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)
}
}