2014-11-28 10:41:57 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
2014-12-16 11:48:42 +01:00
|
|
|
package akka.stream.scaladsl
|
2014-11-28 10:41:57 +01:00
|
|
|
|
|
|
|
|
import java.net.{ InetSocketAddress, URLEncoder }
|
2015-01-28 14:19:50 +01:00
|
|
|
import akka.stream.impl.StreamLayout.Module
|
2014-11-27 22:57:10 +01:00
|
|
|
import scala.collection.immutable
|
|
|
|
|
import scala.concurrent.{ Promise, ExecutionContext, Future }
|
2014-12-16 11:48:42 +01:00
|
|
|
import scala.concurrent.duration.Duration
|
|
|
|
|
import scala.util.{ Failure, Success }
|
|
|
|
|
import scala.util.control.NoStackTrace
|
2015-05-29 16:43:02 +02:00
|
|
|
import akka.actor._
|
2014-11-27 22:57:10 +01:00
|
|
|
import akka.io.Inet.SocketOption
|
2015-04-24 13:15:02 +02:00
|
|
|
import akka.io.{ Tcp ⇒ IoTcp }
|
2015-01-28 14:19:50 +01:00
|
|
|
import akka.stream._
|
2014-11-27 22:57:10 +01:00
|
|
|
import akka.stream.impl._
|
2015-03-03 10:57:25 +01:00
|
|
|
import akka.stream.impl.ReactiveStreamsCompliance._
|
2014-12-16 11:48:42 +01:00
|
|
|
import akka.stream.scaladsl._
|
|
|
|
|
import akka.util.ByteString
|
2015-01-28 14:19:50 +01:00
|
|
|
import org.reactivestreams.{ Publisher, Processor, Subscriber, Subscription }
|
2014-12-16 11:48:42 +01:00
|
|
|
import akka.stream.impl.io.TcpStreamActor
|
|
|
|
|
import akka.stream.impl.io.TcpListenStreamActor
|
|
|
|
|
import akka.stream.impl.io.DelayedInitProcessor
|
|
|
|
|
import akka.stream.impl.io.StreamTcpManager
|
2014-11-28 10:41:57 +01:00
|
|
|
|
2015-04-24 13:15:02 +02:00
|
|
|
object Tcp extends ExtensionId[Tcp] with ExtensionIdProvider {
|
2014-11-28 10:41:57 +01:00
|
|
|
|
|
|
|
|
/**
|
2015-01-28 14:19:50 +01:00
|
|
|
* * Represents a succdessful TCP server binding.
|
2014-11-28 10:41:57 +01:00
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
case class ServerBinding(localAddress: InetSocketAddress)(private val unbindAction: () ⇒ Future[Unit]) {
|
|
|
|
|
def unbind(): Future[Unit] = unbindAction()
|
2014-11-27 22:57:10 +01:00
|
|
|
}
|
2014-11-28 10:41:57 +01:00
|
|
|
|
|
|
|
|
/**
|
2014-11-27 22:57:10 +01:00
|
|
|
* Represents an accepted incoming TCP connection.
|
2014-11-28 10:41:57 +01:00
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
case class IncomingConnection(
|
|
|
|
|
localAddress: InetSocketAddress,
|
|
|
|
|
remoteAddress: InetSocketAddress,
|
|
|
|
|
flow: Flow[ByteString, ByteString, Unit]) {
|
2014-11-27 22:57:10 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handles the connection using the given flow, which is materialized exactly once and the respective
|
2015-01-28 14:19:50 +01:00
|
|
|
* materialized instance is returned.
|
2014-11-27 22:57:10 +01:00
|
|
|
*
|
|
|
|
|
* Convenience shortcut for: `flow.join(handler).run()`.
|
|
|
|
|
*/
|
2015-02-26 22:42:34 +01:00
|
|
|
def handleWith[Mat](handler: Flow[ByteString, ByteString, Mat])(implicit materializer: FlowMaterializer): Mat =
|
2015-01-28 14:19:50 +01:00
|
|
|
flow.joinMat(handler)(Keep.right).run()
|
2014-11-27 22:57:10 +01:00
|
|
|
|
|
|
|
|
}
|
2014-11-28 10:41:57 +01:00
|
|
|
|
|
|
|
|
/**
|
2014-11-27 22:57:10 +01:00
|
|
|
* Represents a prospective outgoing TCP connection.
|
2014-11-28 10:41:57 +01:00
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
case class OutgoingConnection(remoteAddress: InetSocketAddress, localAddress: InetSocketAddress)
|
2014-11-27 22:57:10 +01:00
|
|
|
|
2015-04-24 13:15:02 +02:00
|
|
|
def apply()(implicit system: ActorSystem): Tcp = super.apply(system)
|
2014-11-27 22:57:10 +01:00
|
|
|
|
2015-04-24 13:15:02 +02:00
|
|
|
override def get(system: ActorSystem): Tcp = super.get(system)
|
2014-11-27 22:57:10 +01:00
|
|
|
|
2015-04-24 13:15:02 +02:00
|
|
|
def lookup() = Tcp
|
2014-11-27 22:57:10 +01:00
|
|
|
|
2015-04-24 13:15:02 +02:00
|
|
|
def createExtension(system: ExtendedActorSystem): Tcp = new Tcp(system)
|
2014-11-27 22:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
2015-04-24 13:15:02 +02:00
|
|
|
class Tcp(system: ExtendedActorSystem) extends akka.actor.Extension {
|
|
|
|
|
import Tcp._
|
2014-11-27 22:57:10 +01:00
|
|
|
|
2015-04-13 13:47:17 +02:00
|
|
|
private val manager: ActorRef = system.systemActorOf(Props[StreamTcpManager]
|
2015-05-29 16:43:02 +02:00
|
|
|
.withDispatcher(IoTcp(system).Settings.ManagementDispatcher).withDeploy(Deploy.local), name = "IO-TCP-STREAM")
|
2014-11-28 10:41:57 +01:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
private class BindSource(
|
|
|
|
|
val endpoint: InetSocketAddress,
|
|
|
|
|
val backlog: Int,
|
|
|
|
|
val options: immutable.Traversable[SocketOption],
|
2015-06-19 13:30:48 +02:00
|
|
|
val halfClose: Boolean,
|
2015-01-28 14:19:50 +01:00
|
|
|
val idleTimeout: Duration = Duration.Inf,
|
2015-06-23 17:32:55 +02:00
|
|
|
val attributes: Attributes,
|
2015-01-28 14:19:50 +01:00
|
|
|
_shape: SourceShape[IncomingConnection]) extends SourceModule[IncomingConnection, Future[ServerBinding]](_shape) {
|
|
|
|
|
|
2015-04-10 14:39:48 +02:00
|
|
|
override def create(context: MaterializationContext): (Publisher[IncomingConnection], Future[ServerBinding]) = {
|
2015-01-28 14:19:50 +01:00
|
|
|
val localAddressPromise = Promise[InetSocketAddress]()
|
|
|
|
|
val unbindPromise = Promise[() ⇒ Future[Unit]]()
|
|
|
|
|
val publisher = new Publisher[IncomingConnection] {
|
|
|
|
|
|
|
|
|
|
override def subscribe(s: Subscriber[_ >: IncomingConnection]): Unit = {
|
2015-03-03 10:57:25 +01:00
|
|
|
requireNonNullSubscriber(s)
|
2015-01-28 14:19:50 +01:00
|
|
|
manager ! StreamTcpManager.Bind(
|
|
|
|
|
localAddressPromise,
|
|
|
|
|
unbindPromise,
|
|
|
|
|
s.asInstanceOf[Subscriber[IncomingConnection]],
|
|
|
|
|
endpoint,
|
|
|
|
|
backlog,
|
2015-06-19 13:30:48 +02:00
|
|
|
halfClose,
|
2015-01-28 14:19:50 +01:00
|
|
|
options,
|
|
|
|
|
idleTimeout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-13 13:47:17 +02:00
|
|
|
import system.dispatcher
|
2015-01-28 14:19:50 +01:00
|
|
|
val bindingFuture = unbindPromise.future.zip(localAddressPromise.future).map {
|
|
|
|
|
case (unbindAction, localAddress) ⇒
|
|
|
|
|
ServerBinding(localAddress)(unbindAction)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(publisher, bindingFuture)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override protected def newInstance(s: SourceShape[IncomingConnection]): SourceModule[IncomingConnection, Future[ServerBinding]] =
|
2015-06-19 13:30:48 +02:00
|
|
|
new BindSource(endpoint, backlog, options, halfClose, idleTimeout, attributes, shape)
|
2015-06-23 17:32:55 +02:00
|
|
|
override def withAttributes(attr: Attributes): Module =
|
2015-06-19 13:30:48 +02:00
|
|
|
new BindSource(endpoint, backlog, options, halfClose, idleTimeout, attr, shape)
|
2015-01-28 14:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-11-28 10:41:57 +01:00
|
|
|
/**
|
2015-04-24 13:15:02 +02:00
|
|
|
* Creates a [[Tcp.ServerBinding]] instance which represents a prospective TCP server binding on the given `endpoint`.
|
2015-06-19 13:30:48 +02:00
|
|
|
*
|
|
|
|
|
* @param interface The interface to listen on
|
|
|
|
|
* @param port The port to listen on
|
|
|
|
|
* @param backlog Controls the size of the connection backlog
|
|
|
|
|
* @param options TCP options for the connections, see [[akka.io.Tcp]] for details
|
|
|
|
|
* @param halfClose
|
|
|
|
|
* Controls whether the connection is kept open even after writing has been completed to the accepted
|
|
|
|
|
* TCP connections.
|
|
|
|
|
* If set to true, the connection will implement the TCP half-close mechanism, allowing the client to
|
|
|
|
|
* write to the connection even after the server has finished writing. The TCP socket is only closed
|
|
|
|
|
* after both the client and server finished writing.
|
|
|
|
|
* If set to false, the connection will immediately closed once the server closes its write side,
|
|
|
|
|
* independently whether the client is still attempting to write. This setting is recommended
|
|
|
|
|
* for servers, and therefore it is the default setting.
|
2014-11-28 10:41:57 +01:00
|
|
|
*/
|
2015-04-24 12:31:23 +02:00
|
|
|
def bind(interface: String,
|
|
|
|
|
port: Int,
|
2014-11-27 22:57:10 +01:00
|
|
|
backlog: Int = 100,
|
|
|
|
|
options: immutable.Traversable[SocketOption] = Nil,
|
2015-06-19 13:30:48 +02:00
|
|
|
halfClose: Boolean = false,
|
2015-01-28 14:19:50 +01:00
|
|
|
idleTimeout: Duration = Duration.Inf): Source[IncomingConnection, Future[ServerBinding]] = {
|
2015-06-19 13:30:48 +02:00
|
|
|
new Source(new BindSource(new InetSocketAddress(interface, port), backlog, options, halfClose, idleTimeout,
|
2015-06-23 17:32:55 +02:00
|
|
|
Attributes.none, SourceShape(new Outlet("BindSource.out"))))
|
2015-01-28 14:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
2015-06-19 13:30:48 +02:00
|
|
|
/**
|
|
|
|
|
* Creates a [[Tcp.ServerBinding]] instance which represents a prospective TCP server binding on the given `endpoint`
|
|
|
|
|
* handling the incoming connections using the provided Flow.
|
|
|
|
|
*
|
|
|
|
|
* @param handler A Flow that represents the server logic
|
|
|
|
|
* @param interface The interface to listen on
|
|
|
|
|
* @param port The port to listen on
|
|
|
|
|
* @param backlog Controls the size of the connection backlog
|
|
|
|
|
* @param options TCP options for the connections, see [[akka.io.Tcp]] for details
|
|
|
|
|
* @param halfClose
|
|
|
|
|
* Controls whether the connection is kept open even after writing has been completed to the accepted
|
|
|
|
|
* TCP connections.
|
|
|
|
|
* If set to true, the connection will implement the TCP half-close mechanism, allowing the client to
|
|
|
|
|
* write to the connection even after the server has finished writing. The TCP socket is only closed
|
|
|
|
|
* after both the client and server finished writing.
|
|
|
|
|
* If set to false, the connection will immediately closed once the server closes its write side,
|
|
|
|
|
* independently whether the client is still attempting to write. This setting is recommended
|
|
|
|
|
* for servers, and therefore it is the default setting.
|
|
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
def bindAndHandle(
|
|
|
|
|
handler: Flow[ByteString, ByteString, _],
|
2015-04-24 12:31:23 +02:00
|
|
|
interface: String,
|
|
|
|
|
port: Int,
|
2015-01-28 14:19:50 +01:00
|
|
|
backlog: Int = 100,
|
|
|
|
|
options: immutable.Traversable[SocketOption] = Nil,
|
2015-06-19 13:30:48 +02:00
|
|
|
halfClose: Boolean = false,
|
2015-02-26 22:42:34 +01:00
|
|
|
idleTimeout: Duration = Duration.Inf)(implicit m: FlowMaterializer): Future[ServerBinding] = {
|
2015-06-19 13:30:48 +02:00
|
|
|
bind(interface, port, backlog, options, halfClose, idleTimeout).to(Sink.foreach { conn: IncomingConnection ⇒
|
2015-01-28 14:19:50 +01:00
|
|
|
conn.flow.join(handler).run()
|
|
|
|
|
}).run()
|
2014-11-28 10:41:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-04-24 13:15:02 +02:00
|
|
|
* Creates an [[Tcp.OutgoingConnection]] instance representing a prospective TCP client connection to the given endpoint.
|
2015-06-19 13:30:48 +02:00
|
|
|
*
|
|
|
|
|
* @param remoteAddress The remote address to connect to
|
|
|
|
|
* @param localAddress Optional local address for the connection
|
|
|
|
|
* @param options TCP options for the connections, see [[akka.io.Tcp]] for details
|
|
|
|
|
* @param halfClose
|
|
|
|
|
* Controls whether the connection is kept open even after writing has been completed to the accepted
|
|
|
|
|
* TCP connections.
|
|
|
|
|
* If set to true, the connection will implement the TCP half-close mechanism, allowing the server to
|
|
|
|
|
* write to the connection even after the client has finished writing. The TCP socket is only closed
|
|
|
|
|
* after both the client and server finished writing. This setting is recommended for clients and
|
|
|
|
|
* therefore it is the default setting.
|
|
|
|
|
* If set to false, the connection will immediately closed once the client closes its write side,
|
|
|
|
|
* independently whether the server is still attempting to write.
|
2014-11-28 10:41:57 +01:00
|
|
|
*/
|
2014-11-27 22:57:10 +01:00
|
|
|
def outgoingConnection(remoteAddress: InetSocketAddress,
|
|
|
|
|
localAddress: Option[InetSocketAddress] = None,
|
|
|
|
|
options: immutable.Traversable[SocketOption] = Nil,
|
2015-06-19 13:30:48 +02:00
|
|
|
halfClose: Boolean = true,
|
2014-11-27 22:57:10 +01:00
|
|
|
connectTimeout: Duration = Duration.Inf,
|
2015-01-28 14:19:50 +01:00
|
|
|
idleTimeout: Duration = Duration.Inf): Flow[ByteString, ByteString, Future[OutgoingConnection]] = {
|
|
|
|
|
|
2014-11-27 22:57:10 +01:00
|
|
|
val remoteAddr = remoteAddress
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
|
|
|
Flow[ByteString].andThenMat(() ⇒ {
|
2014-11-27 22:57:10 +01:00
|
|
|
val processorPromise = Promise[Processor[ByteString, ByteString]]()
|
|
|
|
|
val localAddressPromise = Promise[InetSocketAddress]()
|
2015-06-19 13:30:48 +02:00
|
|
|
manager ! StreamTcpManager.Connect(processorPromise, localAddressPromise, remoteAddress, localAddress, halfClose, options,
|
2014-11-27 22:57:10 +01:00
|
|
|
connectTimeout, idleTimeout)
|
2015-04-13 13:47:17 +02:00
|
|
|
import system.dispatcher
|
2015-01-28 14:19:50 +01:00
|
|
|
val outgoingConnection = localAddressPromise.future.map(OutgoingConnection(remoteAddress, _))
|
|
|
|
|
(new DelayedInitProcessor[ByteString, ByteString](processorPromise.future), outgoingConnection)
|
|
|
|
|
})
|
|
|
|
|
|
2014-11-27 22:57:10 +01:00
|
|
|
}
|
2015-04-24 12:31:23 +02:00
|
|
|
|
|
|
|
|
/**
|
2015-04-24 13:15:02 +02:00
|
|
|
* Creates an [[Tcp.OutgoingConnection]] without specifying options.
|
2015-04-24 12:31:23 +02:00
|
|
|
* It represents a prospective TCP client connection to the given endpoint.
|
|
|
|
|
*/
|
|
|
|
|
def outgoingConnection(host: String, port: Int): Flow[ByteString, ByteString, Future[OutgoingConnection]] =
|
|
|
|
|
outgoingConnection(new InetSocketAddress(host, port))
|
2014-11-27 22:57:10 +01:00
|
|
|
}
|
2014-11-28 10:41:57 +01:00
|
|
|
|