shm

package module
v0.0.0-...-98f3873 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2022 License: MIT Imports: 3 Imported by: 0

README

shm

Accessing system shared memory from Go applications. I use it to share host state (ACTIVE or STANDBY) between SCADA applications running on the same host. This is required for compatibility with legacy modules.

How to use

var hostState unsafe.Pointer
...

// HostState connect to shared memory to retrieve the host state
func HostState() (unsafe.Pointer, error) {
	segment, err := shm.OpenSegment()

	if err != nil {
		return nil, err
	}

	return segment.Attach()
}

// IsCurrentHostStateActive checking current host state. Server sends data only in the active state.
func IsCurrentHostStateActive() bool {
	if hostState == nil {
		return true
	}

	if *(*uint16)(hostState) != 2 {
		return true
	}

	return false
}

...

hostState, err = HostState()
	if err != nil {
		log.Errorf("Failed to get host state shared memory: %v", err)
	}

	if IsCurrentHostStateActive() {
    ...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Segment

type Segment struct {
	Id   int
	Size int64
	// contains filtered or unexported fields
}

func OpenSegment

func OpenSegment() (*Segment, error)

OpenSegment open existed segment in shared memory

func (*Segment) Attach

func (s *Segment) Attach() (unsafe.Pointer, error)

Attach segment and return its unsafe pointer

Jump to

Keyboard shortcuts

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