public class TcpConnection extends Object implements IConnection
OPC UA TCP is a simple TCP based protocol that establishes a full duplex channel between a client and server. This protocol has two key features that differentiate it from HTTP. First, this protocol allows responses to be returned in any order. Second, this protocol allows responses to be returned on a different socket if communication failures causes temporary socket interruption. The OPC UA TCP protocol is designed to work with the SecureChannel implemented by a layer higher in the stack. For this reason, the OPC UA TCP protocol defines its interactions with the SecureChannel in addition to the wire protocol.
Features included in this class: o Establishing connection, Handshake o Sync and Async encryption
Features excluded in this class: o Reconnect (See SecureChannel) o Token Renewal (See
SecureChannel)
TcpConnection is instantiated and managed by SecureChannel which also handles
Reconnection and token renewal.
OpenSecureChannelRequest and OpenSecureChannelResponse is ciphered with
asymmetric encryption using the certificates set in initialize. Asymmetric encryption is omited
if server certificate is null. (This is not allowed in Part 4, but is in Part 6). DiscoveryClient
uses unencrypted connection.
TcpConnection captures security tokens from OpenSecureChannel conversation and uses them for
symmetric messaging. The oldest non-expired token is used.
| Modifier and Type | Class and Description |
|---|---|
static class |
TcpConnection.DefaultExecutorProvider
The default implementation, which always returns StackUtils.getBlockingWorkExecutor().
|
static class |
TcpConnection.DefaultReverseConnectionProvider
The
TcpConnection.ReverseConnectionProvider that TcpConnection uses by default. |
static class |
TcpConnection.DefaultSocketFactory
The
TcpConnection.SocketFactory that TcpConnection uses by default. |
static class |
TcpConnection.DelegatingSocketWrapper
Simple implementation of
TcpConnection.SocketWrapper that delegates the all calls to the given
Socket. |
static interface |
TcpConnection.ExecutorProvider
Provides an Executor to be used for message decoding and potential other tasks.
|
static interface |
TcpConnection.ReverseConnectionProvider
Provides "ReverseHello" socket connections for
TcpConnection. |
static interface |
TcpConnection.SocketFactory
Allows implementing custom
TcpConnection.SocketWrapper implementations to allow usage of custom
Sockets (or anything that can be abstracted as input output stream). |
static interface |
TcpConnection.SocketWrapper
A wrapper for a
Socket or anything that can be made to same abstraction. |
IConnection.IMessageListener| Constructor and Description |
|---|
TcpConnection() |
| Modifier and Type | Method and Description |
|---|---|
void |
addConnectionListener(IConnectionListener listener)
Add response listener
|
void |
addMessageListener(IConnection.IMessageListener listener)
Add input stream message listener.
|
void |
close()
Close the socket connection.
|
void |
dispose()
Optional dispose closes connection and clears all variables
|
static int |
getDefaultHandshakeTimeout()
|
static int |
getDefaultReverseHelloAcceptTimeout()
|
EndpointConfiguration |
getEndpointConfiguration()
Getter for the field
endpointConfiguration. |
EndpointDescription |
getEndpointDescription()
Getter for the field
endpointDescription. |
static TcpConnection.ExecutorProvider |
getExecutorProvider()
Returns the current
TcpConnection.ExecutorProvider used for all connections. |
int |
getHandshakeTimeout()
Getter for the field
handshakeTimeout. |
EncoderContext |
getMessageContext()
getMessageContext.
|
int |
getProtocolVersion()
Get the protocol version agreen in the hand-shake
|
static int |
getReceiveBufferSize()
Getter for the field
receiveBufferSize. |
static TcpConnection.ReverseConnectionProvider |
getReverseConnectionProvider()
Returns the current
TcpConnection.ReverseConnectionProvider implementation in use. |
int |
getReverseHelloAcceptTimeout()
|
static int |
getSendBufferSize()
Getter for the field
sendBufferSize. |
protected TcpConnection.SocketWrapper |
getSocket()
Getter for the field
socket. |
SocketAddress |
getSocketAddress()
Get the initialized socket address
|
static TcpConnection.SocketFactory |
getSocketFactory()
Returns the current
TcpConnection.SocketFactory implementation in use. |
void |
initialize(InetSocketAddress addr,
TransportChannelSettings settings,
EncoderContext ctx)
Set connection configuration parameters.
|
void |
initialize(String url,
TransportChannelSettings settings,
EncoderContext ctx) |
void |
initialize(TransportChannelSettings settings,
EncoderContext ctx)
initialize.
|
void |
open()
open.
|
void |
reconnect()
reconnect.
|
void |
removeConnectionListener(IConnectionListener listener)
Add response listener
|
void |
removeMessageListener(IConnection.IMessageListener listener)
removeMessageListener.
|
void |
sendRequest(ServiceRequest request,
int secureChannelId,
int requestId)
Send request to the connection.
|
static void |
setDefaultHandshakeTimeout(int defaultHandshakeTimeout)
Set the default timeout (in milliseconds) for making the initial connection handshake.
|
static void |
setDefaultReverseHelloAcceptTimeout(int defaultReverseHelloAcceptTimeout)
Set the default timeout used for accepting incoming ReverseHello connections.
|
static void |
setExecutorProvider(TcpConnection.ExecutorProvider executorProvider)
Sets an
TcpConnection.ExecutorProvider that will be used for all connections. |
void |
setHandshakeTimeout(int handshakeTimeout)
Setter for the field
handshakeTimeout. |
static void |
setReceiveBufferSize(int receiveBufferSize)
Define the receiveBufferSize to use for the connection socket.
|
static void |
setReverseConnectionProvider(TcpConnection.ReverseConnectionProvider reverseConnectionProvider)
Set a custom
TcpConnection.ReverseConnectionProvider which is used to make sockets in reverse
connections. |
void |
setReverseHelloAcceptTimeout(int reverseHelloAcceptTimeout)
|
static void |
setSendBufferSize(int sendBufferSize)
Define the sendBufferSize to use for the connection socket.
|
protected void |
setSocket(TcpConnection.SocketWrapper socket)
Setter for the field
socket. |
static void |
setSocketFactory(TcpConnection.SocketFactory newSocketFactory)
Set a
TcpConnection.SocketFactory to be used to generate the underlying sockets. |
public static int getDefaultHandshakeTimeout()
public static int getDefaultReverseHelloAcceptTimeout()
public static TcpConnection.ExecutorProvider getExecutorProvider()
TcpConnection.ExecutorProvider used for all connections.public static int getReceiveBufferSize()
Getter for the field receiveBufferSize.
public static TcpConnection.ReverseConnectionProvider getReverseConnectionProvider()
TcpConnection.ReverseConnectionProvider implementation in use.public static int getSendBufferSize()
Getter for the field sendBufferSize.
public static TcpConnection.SocketFactory getSocketFactory()
TcpConnection.SocketFactory implementation in use.public static void setDefaultHandshakeTimeout(int defaultHandshakeTimeout)
public static void setDefaultReverseHelloAcceptTimeout(int defaultReverseHelloAcceptTimeout)
ServerSocket.accept(). The timeout for receiving a ReverseHello from
the Server after a connection is accepted is determined by
getDefaultHandshakeTimeout(). Parameter is in milliseconds. Default value is 0,
meaning infinite wait.public static void setExecutorProvider(TcpConnection.ExecutorProvider executorProvider)
TcpConnection.ExecutorProvider that will be used for all connections. By default uses
TcpConnection.DefaultExecutorProvider, which uses the StackUtils.getBlockingWorkExecutor().
Important! The returned Executor is used for message decoding. By default message
decoding can resolve unknown Structures i.e. will do service calls. If that feature is enabled,
the returned Executor must/shall exec the Runnables in another thread (i.e. returning an
executor that uses the current thread can lead to problems). Additionally there should be
preferably be a number of threads backed by the Executor, otherwise there might be performance
hits.public static void setReceiveBufferSize(int receiveBufferSize)
Default value: 0, which omits the parameter and the default value for the socket (depending on the operating system) is used.
receiveBufferSize - the new size in bytespublic static void setReverseConnectionProvider(TcpConnection.ReverseConnectionProvider reverseConnectionProvider)
TcpConnection.ReverseConnectionProvider which is used to make sockets in reverse
connections.public static void setSendBufferSize(int sendBufferSize)
Default value: 0, which omits the parameter and the default value for the socket (depending on the operating system) is used.
sendBufferSize - the new size in bytespublic static void setSocketFactory(TcpConnection.SocketFactory newSocketFactory)
TcpConnection.SocketFactory to be used to generate the underlying sockets.public void addConnectionListener(IConnectionListener listener)
addConnectionListener in interface IConnectionlistener - a IConnectionListener object.public void addMessageListener(IConnection.IMessageListener listener)
addMessageListener in interface IConnectionlistener - message listenerpublic void close()
close in interface IConnectionpublic void dispose()
dispose in interface IConnectionpublic EndpointConfiguration getEndpointConfiguration()
Getter for the field endpointConfiguration.
EndpointConfiguration object.public EndpointDescription getEndpointDescription()
Getter for the field endpointDescription.
EndpointDescription object.public int getHandshakeTimeout()
Getter for the field handshakeTimeout.
public EncoderContext getMessageContext()
getMessageContext.
EncoderContext object.public int getProtocolVersion()
public int getReverseHelloAcceptTimeout()
public SocketAddress getSocketAddress()
public void initialize(InetSocketAddress addr, TransportChannelSettings settings, EncoderContext ctx) throws ServiceResultException
initialize in interface IConnectionaddr - a InetSocketAddress object.settings - a TransportChannelSettings object.ctx - a EncoderContext object.ServiceResultException - if any.public void initialize(String url, TransportChannelSettings settings, EncoderContext ctx) throws ServiceResultException
ServiceResultExceptionpublic void initialize(TransportChannelSettings settings, EncoderContext ctx) throws ServiceResultException
initialize.
settings - a TransportChannelSettings object.ctx - a EncoderContext object.ServiceResultException - if any.public void open()
throws ServiceResultException
open.
open in interface IConnectionServiceResultException - if any.public void reconnect()
throws ServiceResultException
reconnect.
reconnect in interface IConnectionServiceResultException - if any.public void removeConnectionListener(IConnectionListener listener)
removeConnectionListener in interface IConnectionlistener - a IConnectionListener object.public void removeMessageListener(IConnection.IMessageListener listener)
removeMessageListener.
removeMessageListener in interface IConnectionlistener - a IMessageListener object.public void sendRequest(ServiceRequest request, int secureChannelId, int requestId) throws ServiceResultException
Thread.interrupt().sendRequest in interface IConnectionsecureChannelId - a int.ServiceResultException - varies. Bad_NotConnected if
connection is not establishedpublic void setHandshakeTimeout(int handshakeTimeout)
Setter for the field handshakeTimeout.
handshakeTimeout - a int.public void setReverseHelloAcceptTimeout(int reverseHelloAcceptTimeout)
protected TcpConnection.SocketWrapper getSocket()
Getter for the field socket.
Socket object.protected void setSocket(TcpConnection.SocketWrapper socket)
Setter for the field socket.
socket - the socket to setCopyright © 2026. All rights reserved.