uacp

package
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 11, 2024 License: MIT Imports: 11 Imported by: 9

Documentation

Overview

Package uacp provides encoding/decoding and automated connection handling for the OPC UA Connection Protocol.

To establish the connection as a client, call the Dial() function.

To wait for the client to connect to, call Listen() method, and to establish connection with the Accept() method.

Once you have a connection you can call Read() to receive full UACP messages including the header.

In uacp, *Conn also implements Local/RemoteEndpoint() methods which returns EndpointURL of client or server.

The data on top of UACP connection is passed as it is as long as the connection is established. In other words, uacp never cares the data even if it seems invalid. Users of this package should check the data to make sure it is what they want or not.

Index

Constants

View Source
const (
	KB = 1024
	MB = 1024 * KB

	DefaultReceiveBufSize = 0xffff
	DefaultSendBufSize    = 0xffff
	DefaultMaxChunkCount  = 512
	DefaultMaxMessageSize = 2 * MB
)
View Source
const (
	MessageTypeHello        = "HEL"
	MessageTypeAcknowledge  = "ACK"
	MessageTypeError        = "ERR"
	MessageTypeReverseHello = "RHE"
)

MessageType definitions.

Specification: Part 6, 7.1.2.2

View Source
const (
	ChunkTypeIntermediate = 'C'
	ChunkTypeFinal        = 'F'
	ChunkTypeAbort        = 'A'
)

ChunkType definitions.

Specification: Part 6, 6.7.2.2

Variables

View Source
var (
	// DefaultClientACK is the ACK handshake message sent to the server
	// for client connections.
	DefaultClientACK = &Acknowledge{
		ReceiveBufSize: DefaultReceiveBufSize,
		SendBufSize:    DefaultSendBufSize,
		MaxChunkCount:  0,
		MaxMessageSize: 0,
	}

	// DefaultServerACK is the ACK handshake message sent to the client
	// for server connections.
	DefaultServerACK = &Acknowledge{
		ReceiveBufSize: DefaultReceiveBufSize,
		SendBufSize:    DefaultSendBufSize,
		MaxChunkCount:  DefaultMaxChunkCount,
		MaxMessageSize: DefaultMaxMessageSize,
	}
)

Functions

func ResolveEndpoint

func ResolveEndpoint(endpoint string) (network string, addr *net.TCPAddr, err error)

ResolveEndpoint returns network type, address, and error splitted from EndpointURL.

Expected format of input is "opc.tcp://<addr[:port]/path/to/somewhere"

Types

type Acknowledge

type Acknowledge struct {
	Version        uint32
	ReceiveBufSize uint32
	SendBufSize    uint32
	MaxMessageSize uint32
	MaxChunkCount  uint32
}

Acknowledge represents a OPC UA Acknowledge.

Specification: Part6, 7.1.2.4

func (*Acknowledge) Decode

func (a *Acknowledge) Decode(b []byte) (int, error)

func (*Acknowledge) Encode

func (a *Acknowledge) Encode() ([]byte, error)

type Conn

type Conn struct {
	*net.TCPConn
	// contains filtered or unexported fields
}

func Dial

func Dial(ctx context.Context, endpoint string) (*Conn, error)

Dial uses the default dialer to establish a connection to the endpoint

func NewConn added in v0.2.0

func NewConn(c *net.TCPConn, ack *Acknowledge) (*Conn, error)

func (*Conn) Close

func (c *Conn) Close() (err error)

func (*Conn) Handshake added in v0.2.0

func (c *Conn) Handshake(ctx context.Context, endpoint string) error

func (*Conn) ID

func (c *Conn) ID() uint32

func (*Conn) MaxChunkCount

func (c *Conn) MaxChunkCount() uint32

func (*Conn) MaxMessageSize

func (c *Conn) MaxMessageSize() uint32

func (*Conn) Receive

func (c *Conn) Receive() ([]byte, error)

Receive reads a full UACP message from the underlying connection. The size of b must be at least ReceiveBufSize. Otherwise, the function returns an error.

func (*Conn) ReceiveBufSize

func (c *Conn) ReceiveBufSize() uint32

func (*Conn) Send

func (c *Conn) Send(typ string, msg interface{}) error

func (*Conn) SendBufSize

func (c *Conn) SendBufSize() uint32

func (*Conn) SendError

func (c *Conn) SendError(code ua.StatusCode)

type Dialer added in v0.2.0

type Dialer struct {
	// Dialer establishes the TCP connection. Defaults to net.Dialer.
	Dialer *net.Dialer

	// ClientACK defines the connection parameters requested by the client.
	// Defaults to DefaultClientACK.
	ClientACK *Acknowledge
}

Dialer establishes a connection to an endpoint.

func (*Dialer) Dial added in v0.2.0

func (d *Dialer) Dial(ctx context.Context, endpoint string) (*Conn, error)

type Error

type Error struct {
	ErrorCode uint32
	Reason    string
}

Error represents a OPC UA Error.

Specification: Part6, 7.1.2.5

func (*Error) Decode

func (e *Error) Decode(b []byte) (int, error)

func (*Error) Encode

func (e *Error) Encode() ([]byte, error)

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap added in v0.3.4

func (e *Error) Unwrap() error

Unwrap returns the wrapped error code.

type Header struct {
	MessageType string
	ChunkType   byte
	MessageSize uint32
}

Header represents a OPC UA Connection Header.

Specification: Part 6, 7.1.2.2

func (*Header) Decode

func (h *Header) Decode(b []byte) (int, error)

func (*Header) Encode

func (h *Header) Encode() ([]byte, error)

type Hello

type Hello struct {
	Version        uint32
	ReceiveBufSize uint32
	SendBufSize    uint32
	MaxMessageSize uint32
	MaxChunkCount  uint32
	EndpointURL    string
}

Hello represents a OPC UA Hello.

Specification: Part6, 7.1.2.3

func (*Hello) Decode

func (h *Hello) Decode(b []byte) (int, error)

func (*Hello) Encode

func (h *Hello) Encode() ([]byte, error)

type Listener

type Listener struct {
	// contains filtered or unexported fields
}

Listener is a OPC UA Connection Protocol network listener.

func Listen

func Listen(endpoint string, ack *Acknowledge) (*Listener, error)

Listen acts like net.Listen for OPC UA Connection Protocol networks.

Currently the endpoint can only be specified in "opc.tcp://<addr[:port]>/path" format.

If the IP field of laddr is nil or an unspecified IP address, Listen listens on all available unicast and anycast IP addresses of the local system. If the Port field of laddr is 0, a port number is automatically chosen.

func (*Listener) Accept

func (l *Listener) Accept(ctx context.Context) (*Conn, error)

Accept accepts the next incoming call and returns the new connection.

The first param ctx is to be passed to monitor(), which monitors and handles incoming messages automatically in another goroutine.

func (*Listener) Addr

func (l *Listener) Addr() net.Addr

Addr returns the listener's network address.

func (*Listener) Close

func (l *Listener) Close() error

Close closes the Listener.

func (*Listener) Endpoint

func (l *Listener) Endpoint() string

Endpoint returns the listener's EndpointURL.

type Message

type Message struct {
	Data []byte
}

func (*Message) Decode

func (m *Message) Decode(b []byte) (int, error)

func (*Message) Encode

func (m *Message) Encode() ([]byte, error)

type ReverseHello

type ReverseHello struct {
	ServerURI   string
	EndpointURL string
}

ReverseHello represents a OPC UA ReverseHello.

Specification: Part6, 7.1.2.6

func (*ReverseHello) Decode

func (r *ReverseHello) Decode(b []byte) (int, error)

func (*ReverseHello) Encode

func (r *ReverseHello) Encode() ([]byte, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL