gxnet

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2025 License: GPL-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package gxnet provides TCP/UDP based media for Gurux components. It implements the common IGXMedia-style contract: open/close a connection, send/receive data (optionally framed by an EOP marker), and emit events for received data, errors, tracing and state changes.

Features

  • Protocols: TCP and UDP (see NetworkType).
  • Framing: optional EOP (End Of Packet) marker (byte, string or []byte).
  • Timeouts: connection and I/O timeouts via time.Duration.
  • Tracing: configurable trace level/mask for sent/received/error/info.
  • Events: Received, Error, Trace and MediaState callbacks.
  • Concurrency: safe for concurrent reads/writes; Close unblocks pending I/O.

Construction

Use NewGXNet to create a connection with protocol, host and port. Additional options (such as timeout, EOP, tracing) can be configured through setters.

Example

n := gxnet.NewGXNet(gxnet.TCP, "127.0.0.1", 4059)
n.SetTimeout(5_000) // 5000 ms; or use time.Duration setters if exposed

n.SetOnReceived(func(m IGXMedia, e ReceiveEventArgs) {
    // handle e.Data(), e.SenderInfo()
})
n.SetOnError(func(m IGXMedia, err error) {
    // log/handle error
})

if err := n.Open(); err != nil {
    // handle connect error
}
defer n.Close()

// send bytes; receive happens via the callback or via a blocking Receive.
_, _ = n.Send([]byte{0x01, 0x02, 0x03})

EOP framing

When an EOP is configured, incoming bytes are buffered until the marker is observed. The marker can be a single byte (e.g. 0x7E), a string (e.g. "OK"), or an arbitrary byte slice. Disable EOP to read raw stream data.

Errors and timeouts

Network and protocol errors are returned from calls or routed to Error handlers. Timeouts follow Go conventions (context/deadline or Duration-based configuration). Error messages are lowercased per Go style guidelines.

Notes

The zero value of GXNet is not ready for use; always construct via NewGXNet. Long-running work in event handlers should be offloaded to a separate goroutine to avoid blocking I/O paths.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GXNet

type GXNet struct {
	Protocol NetworkType
	HostName string
	Port     int

	// UseIPv6 defines if IPv6 is used. Default is False (IPv4).
	UseIPv6 bool
	// contains filtered or unexported fields
}

GXNet holds connection configuration and tracing settings for a network media.

func NewGXNet

func NewGXNet(protocol NetworkType, hostName string, port int) *GXNet

NewGXNet creates a GXNet configured with the given protocol, host, and port. It also initializes the internal stop channel used to signal shutdown.

func (*GXNet) Close

func (g *GXNet) Close() error

Close implements IGXMedia

func (*GXNet) Copy

func (g *GXNet) Copy(target gxcommon.IGXMedia) error

Copy implements IGXMedia

func (*GXNet) GetBytesReceived

func (g *GXNet) GetBytesReceived() uint64

GetBytesReceived implements IGXMedia

func (*GXNet) GetBytesSent

func (g *GXNet) GetBytesSent() uint64

GetBytesSent implements IGXMedia

func (*GXNet) GetEop

func (g *GXNet) GetEop() any

GetEop implements IGXMedia

func (*GXNet) GetMediaType

func (g *GXNet) GetMediaType() string

GetMediaType implements IGXMedia

func (*GXNet) GetName

func (g *GXNet) GetName() string

GetName implements IGXMedia

func (*GXNet) GetSettings

func (g *GXNet) GetSettings() string

GetSettings implements IGXMedia

func (*GXNet) GetSynchronous

func (g *GXNet) GetSynchronous() func()

GetSynchronous implements IGXMedia

func (*GXNet) GetTimeout

func (g *GXNet) GetTimeout() uint32

GetTimeout returns the connection timeout in milliseconds.

func (*GXNet) GetTrace

func (g *GXNet) GetTrace() gxcommon.TraceLevel

GetTrace implements IGXMedia

func (*GXNet) IsOpen

func (g *GXNet) IsOpen() bool

IsOpen implements IGXMedia

func (*GXNet) IsSynchronous

func (g *GXNet) IsSynchronous() bool

IsSynchronous implements IGXMedia

func (*GXNet) Open

func (g *GXNet) Open() error

Open implements IGXMedia

func (*GXNet) Receive

func (g *GXNet) Receive(args *gxcommon.ReceiveParameters) (bool, error)

Receive implements IGXMedia

func (*GXNet) ResetByteCounters

func (g *GXNet) ResetByteCounters()

ResetByteCounters implements IGXMedia

func (*GXNet) ResetSynchronousBuffer

func (g *GXNet) ResetSynchronousBuffer()

ResetSynchronousBuffer implements IGXMedia

func (*GXNet) Send

func (g *GXNet) Send(data any, receiver string) error

Send implements IGXMedia

func (*GXNet) SetEop

func (g *GXNet) SetEop(eop any)

SetEop implements IGXMedia

func (*GXNet) SetOnError

func (g *GXNet) SetOnError(value gxcommon.ErrorEventHandler)

SetOnError implements IGXMedia

func (*GXNet) SetOnMediaStateChange

func (g *GXNet) SetOnMediaStateChange(value gxcommon.MediaStateHandler)

SetOnMediaStateChange implements IGXMedia

func (*GXNet) SetOnReceived

func (g *GXNet) SetOnReceived(value gxcommon.ReceivedEventHandler)

SetOnReceived implements IGXMedia

func (*GXNet) SetOnTrace

func (g *GXNet) SetOnTrace(value gxcommon.TraceEventHandler)

SetOnTrace implements IGXMedia

func (*GXNet) SetSettings

func (g *GXNet) SetSettings(value string) error

SetSettings implements IGXMedia

func (*GXNet) SetTimeout

func (g *GXNet) SetTimeout(value uint32) error

SetTimeout sets the connection timeout in milliseconds.

func (*GXNet) SetTrace

func (g *GXNet) SetTrace(traceLevel gxcommon.TraceLevel) error

SetTrace implements IGXMedia

func (*GXNet) String

func (g *GXNet) String() string

String implements IGXMedia

func (*GXNet) Validate

func (g *GXNet) Validate() error

Validate implements IGXMedia

type NetworkType

type NetworkType int

NetworkType determines which network protocol is used for data transfer.

const (
	// NetworkTypeUDP defines that UDP protocol is used.
	NetworkTypeUDP NetworkType = iota
	// NetworkTypeTCP defines that the TCP/IP protocol is used.
	NetworkTypeTCP
)

func NetworkTypeParse

func NetworkTypeParse(value string) (NetworkType, error)

NetworkTypeParse parses the enumerated value from the string.

func (NetworkType) String

func (value NetworkType) String() string

String returns the canonical name of the network type. It satisfies fmt.Stringer.

Jump to

Keyboard shortcuts

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