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 ¶
- type GXNet
- func (g *GXNet) Close() error
- func (g *GXNet) Copy(target gxcommon.IGXMedia) error
- func (g *GXNet) GetBytesReceived() uint64
- func (g *GXNet) GetBytesSent() uint64
- func (g *GXNet) GetEop() any
- func (g *GXNet) GetMediaType() string
- func (g *GXNet) GetName() string
- func (g *GXNet) GetSettings() string
- func (g *GXNet) GetSynchronous() func()
- func (g *GXNet) GetTimeout() uint32
- func (g *GXNet) GetTrace() gxcommon.TraceLevel
- func (g *GXNet) IsOpen() bool
- func (g *GXNet) IsSynchronous() bool
- func (g *GXNet) Open() error
- func (g *GXNet) Receive(args *gxcommon.ReceiveParameters) (bool, error)
- func (g *GXNet) ResetByteCounters()
- func (g *GXNet) ResetSynchronousBuffer()
- func (g *GXNet) Send(data any, receiver string) error
- func (g *GXNet) SetEop(eop any)
- func (g *GXNet) SetOnError(value gxcommon.ErrorEventHandler)
- func (g *GXNet) SetOnMediaStateChange(value gxcommon.MediaStateHandler)
- func (g *GXNet) SetOnReceived(value gxcommon.ReceivedEventHandler)
- func (g *GXNet) SetOnTrace(value gxcommon.TraceEventHandler)
- func (g *GXNet) SetSettings(value string) error
- func (g *GXNet) SetTimeout(value uint32) error
- func (g *GXNet) SetTrace(traceLevel gxcommon.TraceLevel) error
- func (g *GXNet) String() string
- func (g *GXNet) Validate() error
- type NetworkType
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) GetBytesReceived ¶
GetBytesReceived implements IGXMedia
func (*GXNet) GetSynchronous ¶
func (g *GXNet) GetSynchronous() func()
GetSynchronous implements IGXMedia
func (*GXNet) GetTimeout ¶
GetTimeout returns the connection timeout in milliseconds.
func (*GXNet) GetTrace ¶
func (g *GXNet) GetTrace() gxcommon.TraceLevel
GetTrace implements IGXMedia
func (*GXNet) IsSynchronous ¶
IsSynchronous 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) 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 ¶
SetSettings implements IGXMedia
func (*GXNet) SetTimeout ¶
SetTimeout sets the connection timeout in milliseconds.
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.