Package com.tangosol.net
Class TcpDatagramSocket.Impl
java.lang.Object
java.net.DatagramSocketImpl
com.tangosol.net.TcpDatagramSocket.Impl
- All Implemented Interfaces:
SocketOptions
- Direct Known Subclasses:
NonBlockingTcpDatagramSocket.Impl
- Enclosing class:
TcpDatagramSocket
A specialized version of
DatagramSocketImpl
.-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The fixed header size for packets.static final int
Protcol identifier used to identify that peers are also TcpDatagramSockets.Fields inherited from class java.net.DatagramSocketImpl
fd, localPort
Fields inherited from interface java.net.SocketOptions
IP_MULTICAST_IF, IP_MULTICAST_IF2, IP_MULTICAST_LOOP, IP_TOS, SO_BINDADDR, SO_BROADCAST, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_RCVBUF, SO_REUSEADDR, SO_REUSEPORT, SO_SNDBUF, SO_TIMEOUT, TCP_NODELAY
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
bind
(int nPort, InetAddress addr) protected void
bind
(SocketAddress addr) Bind the socket to the specified address.protected void
close()
protected void
closeInbound
(com.tangosol.net.TcpDatagramSocket.Impl.ConnectionStatus status, SocketChannel chan) Close the inboundchannel
.protected void
closeOutbound
(SocketAddress addr) Close the outbound socket.protected void
create()
protected com.tangosol.net.TcpDatagramSocket.Impl.Connection
Obtain a Connection for the specified address.protected static SocketException
Convert an IOException into a SocketException.protected int
getOption
(int nId) protected int
protected byte
getTTL()
protected void
join
(InetAddress inetaddr) protected void
joinGroup
(SocketAddress mcastaddr, NetworkInterface netIf) protected void
leave
(InetAddress inetaddr) protected void
leaveGroup
(SocketAddress mcastaddr, NetworkInterface netIf) protected void
logException
(SocketAddress addr, IOException e) Log an exception which is handled internally by the TcpDatagramSocket.protected void
logProtocolWarning
(SocketAddress addr, com.tangosol.net.TcpDatagramSocket.Impl.ConnectionStatus status, int nMagic) Periodically log a warning when connections are made using an unrecognized protocol.protected com.tangosol.net.TcpDatagramSocket.Impl.Connection
makeConnection
(SocketAddress addr) Produce a new Connection for the specified destination address.protected SelectionKey
nextKey
(ByteBuffer buffPacket) Perform a blocking read, waiting for a complete packet.protected void
onAccept
(SelectionKey key) Called when a channel is identified as have a new connection to accept.protected boolean
onConnectionHeader
(com.tangosol.net.TcpDatagramSocket.Impl.ConnectionStatus status, SocketChannel chan) Process a pending connection header.protected boolean
onRead
(SelectionKey key, ByteBuffer buffPacket) Called when a channel is detected as readable.protected int
peek
(InetAddress addr) protected int
peekData
(DatagramPacket packet) protected void
Process any scheduled selector registrations.provider()
Return the SelectorProvider associated with this socket.protected void
receive
(DatagramPacket packet) protected void
scheduleRegistration
(SocketChannel chan, com.tangosol.net.TcpDatagramSocket.Impl.ConnectionStatus status) Schedule a registration with the selector, and wake it up.protected void
send
(DatagramPacket packet) void
void
setPacketMagic
(int nMagic, int nMask) Specify the packet header which is included at the start of every packet.protected void
setTimeToLive
(int ttl) protected void
setTTL
(byte ttl) toString()
protected int
transferBytes
(ByteBuffer buffSrc, ByteBuffer buffDst) Transfer bytes from the source to the destination buffer based on their limits.Methods inherited from class java.net.DatagramSocketImpl
connect, disconnect, getFileDescriptor, getOption, setOption, supportedOptions
-
Field Details
-
HEADER_SIZE
public static final int HEADER_SIZEThe fixed header size for packets.- See Also:
-
PROTOCOL_MAGIC
public static final int PROTOCOL_MAGICProtcol identifier used to identify that peers are also TcpDatagramSockets. This is necessary so that we don't try to act upon garbage in this class, for instance trying to allocate a negative or gigabit sized packet.- See Also:
-
-
Constructor Details
-
Impl
Create a new new Impl.- Throws:
SocketException
- if any error happens during the bind, or if the port is unavailable
-
Impl
Create a new Impl using aprovider
.- Parameters:
provider
- theprovider
used to create internal sockets- Throws:
SocketException
- if any error happens during the bind, or if the port is unavailable
-
-
Method Details
-
provider
Return the SelectorProvider associated with this socket.- Returns:
- the SelectorProvider
-
setPacketMagic
public void setPacketMagic(int nMagic, int nMask) Specify the packet header which is included at the start of every packet. Because this implementation is TCP based these headers can be stripped off, and replaced on the far side without consuming any network resources.- Parameters:
nMagic
- the packet headernMask
- the packet header bitmask identifying the bits used the mask must be in byte increments
-
create
- Specified by:
create
in classDatagramSocketImpl
- Throws:
SocketException
-
bind
- Specified by:
bind
in classDatagramSocketImpl
- Throws:
SocketException
-
getLocalPort
protected int getLocalPort()- Overrides:
getLocalPort
in classDatagramSocketImpl
-
send
- Specified by:
send
in classDatagramSocketImpl
- Throws:
IOException
-
peek
- Specified by:
peek
in classDatagramSocketImpl
- Throws:
IOException
-
peekData
- Specified by:
peekData
in classDatagramSocketImpl
- Throws:
IOException
-
receive
- Specified by:
receive
in classDatagramSocketImpl
- Throws:
IOException
-
nextKey
Perform a blocking read, waiting for a complete packet.- Parameters:
buffPacket
- the packet buffer- Returns:
- the corresponding SelectionKey
- Throws:
SocketTimeoutException
- if SO_TIMEOUT is exceededIOException
-
onAccept
Called when a channel is identified as have a new connection to accept.- Parameters:
key
- the associated SelectionKey
-
onRead
Called when a channel is detected as readable.- Parameters:
key
- the associated SelectionKeybuffPacket
- a buffer in which to place any available full packet- Returns:
- true iff a packet has been read
-
onConnectionHeader
protected boolean onConnectionHeader(com.tangosol.net.TcpDatagramSocket.Impl.ConnectionStatus status, SocketChannel chan) throws IOException Process a pending connection header.- Parameters:
status
- the associated ConnectionStatuschan
- the associated channel- Returns:
- true if EOS has been reached
- Throws:
IOException
- if an I/O error occurs
-
close
protected void close()- Specified by:
close
in classDatagramSocketImpl
-
setOption
- Throws:
SocketException
-
getOption
- Throws:
SocketException
-
setTTL
- Specified by:
setTTL
in classDatagramSocketImpl
- Throws:
IOException
-
getTTL
- Specified by:
getTTL
in classDatagramSocketImpl
- Throws:
IOException
-
setTimeToLive
- Specified by:
setTimeToLive
in classDatagramSocketImpl
- Throws:
IOException
-
getTimeToLive
- Specified by:
getTimeToLive
in classDatagramSocketImpl
- Throws:
IOException
-
join
- Specified by:
join
in classDatagramSocketImpl
- Throws:
IOException
-
leave
- Specified by:
leave
in classDatagramSocketImpl
- Throws:
IOException
-
joinGroup
- Specified by:
joinGroup
in classDatagramSocketImpl
- Throws:
IOException
-
leaveGroup
- Specified by:
leaveGroup
in classDatagramSocketImpl
- Throws:
IOException
-
bind
Bind the socket to the specified address.- Parameters:
addr
- the address to bind to- Throws:
SocketException
- if an I/O error occurs
-
ensureSocketException
Convert an IOException into a SocketException.- Parameters:
e
- the IOExcepotion- Returns:
- the SocketException
-
makeConnection
protected com.tangosol.net.TcpDatagramSocket.Impl.Connection makeConnection(SocketAddress addr) throws IOException Produce a new Connection for the specified destination address.- Parameters:
addr
- the destination address- Returns:
- the corresponding connection
- Throws:
IOException
- if an I/O error occurs
-
ensureConnection
protected com.tangosol.net.TcpDatagramSocket.Impl.Connection ensureConnection(SocketAddress addr) throws IOException Obtain a Connection for the specified address.- Parameters:
addr
- the destination address- Returns:
- the corresponding connection
- Throws:
IOException
- if an I/O error occurs
-
closeInbound
protected void closeInbound(com.tangosol.net.TcpDatagramSocket.Impl.ConnectionStatus status, SocketChannel chan) Close the inboundchannel
.- Parameters:
status
- theConnectionStatus
corresponding to the channelchan
- the channel to close
-
closeOutbound
Close the outbound socket.- Parameters:
addr
- theaddress
of the outbound socket
-
scheduleRegistration
protected void scheduleRegistration(SocketChannel chan, com.tangosol.net.TcpDatagramSocket.Impl.ConnectionStatus status) Schedule a registration with the selector, and wake it up.- Parameters:
chan
- the channel to scheduled registration forstatus
- the associated ConnectionStatus to register
-
processRegistrations
protected void processRegistrations()Process any scheduled selector registrations. -
transferBytes
Transfer bytes from the source to the destination buffer based on their limits.- Parameters:
buffSrc
- the source bufferbuffDst
- the destination buffer- Returns:
- the number of bytes transfered
-
logException
Log an exception which is handled internally by the TcpDatagramSocket.- Parameters:
addr
- the associated addresse
- the exception
-
logProtocolWarning
protected void logProtocolWarning(SocketAddress addr, com.tangosol.net.TcpDatagramSocket.Impl.ConnectionStatus status, int nMagic) Periodically log a warning when connections are made using an unrecognized protocol.- Parameters:
addr
- the source address of the connectionstatus
- the connection statusnMagic
- the "magic" header they sent
-
toString
-