posix_mq

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2020 License: MIT Imports: 4 Imported by: 0

README

posix_mq

a Go wrapper for POSIX Message Queues

posix_mq is a Go wrapper for POSIX Message Queues. It's important you read the manual for POSIX Message Queues, ms_send(2) and mq_receive(2) before using this library. posix_mq is a very light wrapper, and will not hide any errors from you.

posix_mq is tested on only Linux in Docker container.

Example

  • sender
package main

import (
	"fmt"
	"log"
	"time"

	"github.com/syucream/posix_mq/src/posix_mq"
)

const maxTickNum = 10

func main() {
	oflag := posix_mq.O_WRONLY | posix_mq.O_CREAT
	mq, err := posix_mq.NewMessageQueue("/posix_mq_example", oflag, 0666, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer mq.Close()

	count := 0
	for {
		count++
		mq.Send([]byte(fmt.Sprintf("Hello, World : %d\n", count)), 0)
		fmt.Println("Sent a new message")

		if count >= maxTickNum {
			break
		}

		time.Sleep(1 * time.Second)
	}
}
  • receiver
package main

import (
	"fmt"
	"log"

	"github.com/syucream/posix_mq/src/posix_mq"
)

const maxTickNum = 10

func main() {
	oflag := posix_mq.O_RDONLY
	mq, err := posix_mq.NewMessageQueue("/posix_mq_example", oflag, 0666, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer mq.Close()

	fmt.Println("Start receiving messages")

	count := 0
	for {
		count++

		msg, _, err := mq.Receive()
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf(string(msg))

		if count >= maxTickNum {
			break
		}
	}
}

Acknowledgement

It's inspired by Shopify/sysv_mq

Documentation

Index

Constants

View Source
const (
	O_RDONLY = C.O_RDONLY
	O_WRONLY = C.O_WRONLY
	O_RDWR   = C.O_RDWR

	O_CLOEXEC  = C.O_CLOEXEC
	O_CREAT    = C.O_CREAT
	O_EXCL     = C.O_EXCL
	O_NONBLOCK = C.O_NONBLOCK

	// Based on Linux 3.5+
	MSGSIZE_MAX     = 16777216
	MSGSIZE_DEFAULT = MSGSIZE_MAX
)

Variables

View Source
var (
	MemoryAllocationError = fmt.Errorf("Memory Allocation Error")
)

Functions

This section is empty.

Types

type MessageQueue

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

Represents the message queue

func NewMessageQueue

func NewMessageQueue(name string, oflag int, mode int, attr *MessageQueueAttribute) (*MessageQueue, error)

NewMessageQueue returns an instance of the message queue given a QueueConfig.

func (*MessageQueue) Close

func (mq *MessageQueue) Close() error

Close closes the message queue.

func (*MessageQueue) Notify

func (mq *MessageQueue) Notify(sigNo syscall.Signal) error

FIXME Don't work because of signal portability. Notify set signal notification to handle new messages.

func (*MessageQueue) Receive

func (mq *MessageQueue) Receive() ([]byte, uint, error)

Receive receives message from the message queue.

func (*MessageQueue) Send

func (mq *MessageQueue) Send(data []byte, priority uint) error

Send sends message to the message queue.

func (mq *MessageQueue) Unlink() error

Unlink deletes the message queue.

type MessageQueueAttribute

type MessageQueueAttribute struct {
	Flags   int
	MaxMsg  int
	MsgSize int
	// contains filtered or unexported fields
}

Represents the message queue attribute

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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