2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2020-01-02 07:24:59 -05:00
|
|
|
* Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
|
2013-02-05 11:48:47 +01:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2013-02-05 11:48:47 +01:00
|
|
|
package akka.io
|
|
|
|
|
|
2015-04-30 09:23:18 +02:00
|
|
|
import java.net.DatagramSocket
|
|
|
|
|
import java.net.ServerSocket
|
|
|
|
|
import java.net.Socket
|
2020-04-27 20:32:18 +08:00
|
|
|
import java.nio.channels.DatagramChannel
|
2013-02-05 11:48:47 +01:00
|
|
|
|
2018-12-14 17:48:08 -05:00
|
|
|
import akka.util.unused
|
|
|
|
|
|
2013-02-05 11:48:47 +01:00
|
|
|
object Inet {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SocketOption is a package of data (from the user) and associated
|
2014-08-19 14:02:23 +03:00
|
|
|
* behavior (how to apply that to a channel).
|
2013-02-05 11:48:47 +01:00
|
|
|
*/
|
|
|
|
|
trait SocketOption {
|
|
|
|
|
|
2013-09-08 09:29:45 -05:00
|
|
|
/**
|
|
|
|
|
* Action to be taken for this option before bind() is called
|
|
|
|
|
*/
|
2018-12-14 17:48:08 -05:00
|
|
|
def beforeDatagramBind(@unused ds: DatagramSocket): Unit = ()
|
2013-02-05 11:48:47 +01:00
|
|
|
|
2013-09-08 09:29:45 -05:00
|
|
|
/**
|
|
|
|
|
* Action to be taken for this option before bind() is called
|
|
|
|
|
*/
|
2018-12-14 17:48:08 -05:00
|
|
|
def beforeServerSocketBind(@unused ss: ServerSocket): Unit = ()
|
2013-02-05 11:48:47 +01:00
|
|
|
|
|
|
|
|
/**
|
2015-04-30 09:23:18 +02:00
|
|
|
* Action to be taken for this option before calling connect()
|
|
|
|
|
*/
|
2018-12-14 17:48:08 -05:00
|
|
|
def beforeConnect(@unused s: Socket): Unit = ()
|
2015-04-30 09:23:18 +02:00
|
|
|
|
|
|
|
|
/**
|
2020-06-18 08:17:46 +02:00
|
|
|
* Action to be taken for this option after connect returned.
|
2013-02-05 11:48:47 +01:00
|
|
|
*/
|
2018-12-14 17:48:08 -05:00
|
|
|
def afterConnect(@unused s: Socket): Unit = ()
|
2015-04-30 09:23:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Java API: AbstractSocketOption is a package of data (from the user) and associated
|
|
|
|
|
* behavior (how to apply that to a channel).
|
|
|
|
|
*/
|
|
|
|
|
abstract class AbstractSocketOption extends SocketOption
|
2013-09-08 09:29:45 -05:00
|
|
|
|
2015-04-30 09:23:18 +02:00
|
|
|
trait SocketOptionV2 extends SocketOption {
|
2019-03-11 10:38:24 +01:00
|
|
|
|
2013-09-08 09:29:45 -05:00
|
|
|
/**
|
2020-06-18 08:17:46 +02:00
|
|
|
* Action to be taken for this option after connect returned.
|
2013-09-08 09:29:45 -05:00
|
|
|
*/
|
2018-12-14 17:48:08 -05:00
|
|
|
def afterBind(@unused s: DatagramSocket): Unit = ()
|
2013-09-08 09:29:45 -05:00
|
|
|
|
|
|
|
|
/**
|
2020-06-18 08:17:46 +02:00
|
|
|
* Action to be taken for this option after connect returned.
|
2013-09-08 09:29:45 -05:00
|
|
|
*/
|
2018-12-14 17:48:08 -05:00
|
|
|
def afterBind(@unused s: ServerSocket): Unit = ()
|
2013-09-08 09:29:45 -05:00
|
|
|
|
2013-02-05 11:48:47 +01:00
|
|
|
/**
|
2020-06-18 08:17:46 +02:00
|
|
|
* Action to be taken for this option after connect returned.
|
2013-02-05 11:48:47 +01:00
|
|
|
*/
|
2018-12-14 17:48:08 -05:00
|
|
|
def afterConnect(@unused s: DatagramSocket): Unit = ()
|
2015-04-30 09:23:18 +02:00
|
|
|
|
2013-02-05 11:48:47 +01:00
|
|
|
}
|
|
|
|
|
|
2015-04-30 09:23:18 +02:00
|
|
|
/**
|
|
|
|
|
* Java API
|
|
|
|
|
*/
|
|
|
|
|
abstract class AbstractSocketOptionV2 extends SocketOptionV2
|
|
|
|
|
|
2014-08-19 14:02:23 +03:00
|
|
|
/**
|
|
|
|
|
* DatagramChannel creation behavior.
|
|
|
|
|
*/
|
|
|
|
|
class DatagramChannelCreator extends SocketOption {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Open and return new DatagramChannel.
|
|
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
* `throws` is needed because `DatagramChannel.open` method
|
2014-08-19 14:02:23 +03:00
|
|
|
* can throw an exception.
|
|
|
|
|
*/
|
|
|
|
|
@throws(classOf[Exception])
|
|
|
|
|
def create(): DatagramChannel = DatagramChannel.open()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object DatagramChannelCreator {
|
|
|
|
|
val default = new DatagramChannelCreator()
|
|
|
|
|
def apply() = default
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-05 11:48:47 +01:00
|
|
|
object SO {
|
|
|
|
|
|
|
|
|
|
/**
|
2013-07-05 12:46:39 +02:00
|
|
|
* [[akka.io.Inet.SocketOption]] to set the SO_RCVBUF option
|
2013-02-05 11:48:47 +01:00
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
* For more information see [[java.net.Socket#setReceiveBufferSize]]
|
2013-02-05 11:48:47 +01:00
|
|
|
*/
|
2014-03-07 13:20:01 +01:00
|
|
|
final case class ReceiveBufferSize(size: Int) extends SocketOption {
|
2013-02-05 11:48:47 +01:00
|
|
|
require(size > 0, "ReceiveBufferSize must be > 0")
|
2015-04-30 09:23:18 +02:00
|
|
|
override def beforeServerSocketBind(s: ServerSocket): Unit = s.setReceiveBufferSize(size)
|
|
|
|
|
override def beforeDatagramBind(s: DatagramSocket): Unit = s.setReceiveBufferSize(size)
|
|
|
|
|
override def beforeConnect(s: Socket): Unit = s.setReceiveBufferSize(size)
|
2013-02-05 11:48:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// server socket options
|
|
|
|
|
|
|
|
|
|
/**
|
2013-07-05 12:46:39 +02:00
|
|
|
* [[akka.io.Inet.SocketOption]] to enable or disable SO_REUSEADDR
|
2013-02-05 11:48:47 +01:00
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
* For more information see [[java.net.Socket#setReuseAddress]]
|
2013-02-05 11:48:47 +01:00
|
|
|
*/
|
2014-03-07 13:20:01 +01:00
|
|
|
final case class ReuseAddress(on: Boolean) extends SocketOption {
|
2015-04-30 09:23:18 +02:00
|
|
|
override def beforeServerSocketBind(s: ServerSocket): Unit = s.setReuseAddress(on)
|
|
|
|
|
override def beforeDatagramBind(s: DatagramSocket): Unit = s.setReuseAddress(on)
|
|
|
|
|
override def beforeConnect(s: Socket): Unit = s.setReuseAddress(on)
|
2013-02-05 11:48:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-07-05 12:46:39 +02:00
|
|
|
* [[akka.io.Inet.SocketOption]] to set the SO_SNDBUF option.
|
2013-02-05 11:48:47 +01:00
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
* For more information see [[java.net.Socket#setSendBufferSize]]
|
2013-02-05 11:48:47 +01:00
|
|
|
*/
|
2014-03-07 13:20:01 +01:00
|
|
|
final case class SendBufferSize(size: Int) extends SocketOption {
|
2013-02-05 11:48:47 +01:00
|
|
|
require(size > 0, "SendBufferSize must be > 0")
|
2015-04-30 09:23:18 +02:00
|
|
|
override def afterConnect(s: Socket): Unit = s.setSendBufferSize(size)
|
2013-02-05 11:48:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-07-05 12:46:39 +02:00
|
|
|
* [[akka.io.Inet.SocketOption]] to set the traffic class or
|
2013-02-05 11:48:47 +01:00
|
|
|
* type-of-service octet in the IP header for packets sent from this
|
|
|
|
|
* socket.
|
|
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
* For more information see [[java.net.Socket#setTrafficClass]]
|
2013-02-05 11:48:47 +01:00
|
|
|
*/
|
2014-03-07 13:20:01 +01:00
|
|
|
final case class TrafficClass(tc: Int) extends SocketOption {
|
2013-02-05 11:48:47 +01:00
|
|
|
require(0 <= tc && tc <= 255, "TrafficClass needs to be in the interval [0, 255]")
|
2015-04-30 09:23:18 +02:00
|
|
|
override def afterConnect(s: Socket): Unit = s.setTrafficClass(tc)
|
2013-02-05 11:48:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-06 11:38:42 +01:00
|
|
|
trait SoForwarders {
|
2019-03-11 10:38:24 +01:00
|
|
|
|
2013-05-26 18:29:23 +02:00
|
|
|
/**
|
2013-07-05 12:46:39 +02:00
|
|
|
* [[akka.io.Inet.SocketOption]] to set the SO_RCVBUF option
|
2013-05-26 18:29:23 +02:00
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
* For more information see [[java.net.Socket#setReceiveBufferSize]]
|
2013-05-26 18:29:23 +02:00
|
|
|
*/
|
2013-02-06 11:38:42 +01:00
|
|
|
val ReceiveBufferSize = SO.ReceiveBufferSize
|
2013-05-26 18:29:23 +02:00
|
|
|
|
|
|
|
|
/**
|
2013-07-05 12:46:39 +02:00
|
|
|
* [[akka.io.Inet.SocketOption]] to enable or disable SO_REUSEADDR
|
2013-05-26 18:29:23 +02:00
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
* For more information see [[java.net.Socket#setReuseAddress]]
|
2013-05-26 18:29:23 +02:00
|
|
|
*/
|
2013-02-06 11:38:42 +01:00
|
|
|
val ReuseAddress = SO.ReuseAddress
|
2013-05-26 18:29:23 +02:00
|
|
|
|
|
|
|
|
/**
|
2013-07-05 12:46:39 +02:00
|
|
|
* [[akka.io.Inet.SocketOption]] to set the SO_SNDBUF option.
|
2013-05-26 18:29:23 +02:00
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
* For more information see [[java.net.Socket#setSendBufferSize]]
|
2013-05-26 18:29:23 +02:00
|
|
|
*/
|
2013-02-06 11:38:42 +01:00
|
|
|
val SendBufferSize = SO.SendBufferSize
|
2013-05-26 18:29:23 +02:00
|
|
|
|
|
|
|
|
/**
|
2013-07-05 12:46:39 +02:00
|
|
|
* [[akka.io.Inet.SocketOption]] to set the traffic class or
|
2013-05-26 18:29:23 +02:00
|
|
|
* type-of-service octet in the IP header for packets sent from this
|
|
|
|
|
* socket.
|
|
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
* For more information see [[java.net.Socket#setTrafficClass]]
|
2013-05-26 18:29:23 +02:00
|
|
|
*/
|
2013-02-06 11:38:42 +01:00
|
|
|
val TrafficClass = SO.TrafficClass
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-15 09:13:39 +01:00
|
|
|
trait SoJavaFactories {
|
|
|
|
|
import SO._
|
2019-03-11 10:38:24 +01:00
|
|
|
|
2013-05-26 18:29:23 +02:00
|
|
|
/**
|
2013-07-05 12:46:39 +02:00
|
|
|
* [[akka.io.Inet.SocketOption]] to set the SO_RCVBUF option
|
2013-05-26 18:29:23 +02:00
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
* For more information see [[java.net.Socket#setReceiveBufferSize]]
|
2013-05-26 18:29:23 +02:00
|
|
|
*/
|
2013-02-15 09:13:39 +01:00
|
|
|
def receiveBufferSize(size: Int) = ReceiveBufferSize(size)
|
2013-05-26 18:29:23 +02:00
|
|
|
|
|
|
|
|
/**
|
2013-07-05 12:46:39 +02:00
|
|
|
* [[akka.io.Inet.SocketOption]] to enable or disable SO_REUSEADDR
|
2013-05-26 18:29:23 +02:00
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
* For more information see [[java.net.Socket#setReuseAddress]]
|
2013-05-26 18:29:23 +02:00
|
|
|
*/
|
2013-02-15 09:13:39 +01:00
|
|
|
def reuseAddress(on: Boolean) = ReuseAddress(on)
|
2013-05-26 18:29:23 +02:00
|
|
|
|
|
|
|
|
/**
|
2013-07-05 12:46:39 +02:00
|
|
|
* [[akka.io.Inet.SocketOption]] to set the SO_SNDBUF option.
|
2013-05-26 18:29:23 +02:00
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
* For more information see [[java.net.Socket#setSendBufferSize]]
|
2013-05-26 18:29:23 +02:00
|
|
|
*/
|
2013-02-15 09:13:39 +01:00
|
|
|
def sendBufferSize(size: Int) = SendBufferSize(size)
|
2013-05-26 18:29:23 +02:00
|
|
|
|
|
|
|
|
/**
|
2013-07-05 12:46:39 +02:00
|
|
|
* [[akka.io.Inet.SocketOption]] to set the traffic class or
|
2013-05-26 18:29:23 +02:00
|
|
|
* type-of-service octet in the IP header for packets sent from this
|
|
|
|
|
* socket.
|
|
|
|
|
*
|
2015-05-15 16:53:24 +02:00
|
|
|
* For more information see [[java.net.Socket#setTrafficClass]]
|
2013-05-26 18:29:23 +02:00
|
|
|
*/
|
2013-02-15 09:13:39 +01:00
|
|
|
def trafficClass(tc: Int) = TrafficClass(tc)
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-05 11:48:47 +01:00
|
|
|
}
|