From 9f5420e081f77a6c565fa9158b296baebc799645 Mon Sep 17 00:00:00 2001 From: hslam <791874158@qq.com> Date: Tue, 1 Dec 2020 16:57:00 +0800 Subject: [PATCH] update methods --- README.md | 4 ++-- msg_test.go | 4 ++-- msg_unix.go | 54 +++++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 4187f4e..7ba4660 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ func main() { panic(err) } defer msg.Remove(msgid) - err = msg.Snd(msgid, 1, []byte("Hello World"), 0600) + err = msg.Send(msgid, 1, []byte("Hello World"), 0600) if err != nil { panic(err) } @@ -64,7 +64,7 @@ func main() { if err != nil { panic(err) } - text, err := msg.Rcv(msgid, 1, 0600) + text, err := msg.Receive(msgid, 1, 0600) if err != nil { panic(err) } diff --git a/msg_test.go b/msg_test.go index 9a23875..111c7a1 100644 --- a/msg_test.go +++ b/msg_test.go @@ -20,7 +20,7 @@ func TestMsg(t *testing.T) { panic(err) } defer Remove(msgid) - err = Snd(msgid, 1, []byte(context), 0600) + err = Send(msgid, 1, []byte(context), 0600) if err != nil { panic(err) } @@ -36,7 +36,7 @@ func TestMsg(t *testing.T) { if err != nil { panic(err) } - text, err := Rcv(msgid, 1, 0600) + text, err := Receive(msgid, 1, 0600) if err != nil { panic(err) } diff --git a/msg_unix.go b/msg_unix.go index 4af3e3c..d3f4d0d 100644 --- a/msg_unix.go +++ b/msg_unix.go @@ -54,13 +54,27 @@ func Get(key int, msgflg int) (int, error) { } // Snd calls the msgsnd system call. -func Snd(msgid int, msgType uint, msgText []byte, flags int) error { - if len(msgText) > maxText { - return ErrTooLong - } - m := message{Type: msgType} - copy(m.Text[:], msgText) - _, _, err := syscall.Syscall6(syscall.SYS_MSGSND, uintptr(msgid), uintptr(unsafe.Pointer(&m)), uintptr(len(msgText)), uintptr(flags), 0, 0) +// +// The msgsnd() and msgrcv() system calls are used to send messages to, +// and receive messages from, a System V message queue. The calling +// process must have write permission on the message queue in order to +// send a message, and read permission to receive a message. +// The msgp argument is a pointer to a caller-defined structure of the +// following general form: +// +// struct msgbuf { +// long mtype; /* message type, must be > 0 */ +// char mtext[1]; /* message data */ +// }; +// The mtext field is an array (or other structure) whose size is speci‐ +// fied by msgsz, a nonnegative integer value. Messages of zero length +// (i.e., no mtext field) are permitted. The mtype field must have a +// strictly positive integer value. This value can be used by the re‐ +// ceiving process for message selection (see the description of ms‐ +// grcv() below). +// +func Snd(msgid int, msgp uintptr, msgsz int, msgflg int) error { + _, _, err := syscall.Syscall6(syscall.SYS_MSGSND, uintptr(msgid), uintptr(msgp), uintptr(msgsz), uintptr(msgflg), 0, 0) if err != 0 { return err } @@ -68,10 +82,30 @@ func Snd(msgid int, msgType uint, msgText []byte, flags int) error { } // Rcv calls the msgrcv system call. -func Rcv(msgid int, msgType uint, flags int) ([]byte, error) { - m := message{Type: msgType} - length, _, err := syscall.Syscall6(syscall.SYS_MSGRCV, uintptr(msgid), uintptr(unsafe.Pointer(&m)), maxText, uintptr(msgType), uintptr(flags), 0) +func Rcv(msgid int, msgp uintptr, msgsz int, msgtyp uint, msgflg int) (int, error) { + r1, _, err := syscall.Syscall6(syscall.SYS_MSGRCV, uintptr(msgid), msgp, uintptr(msgsz), uintptr(msgtyp), uintptr(msgflg), 0) + length := int(r1) if err != 0 { + return length, err + } + return length, nil +} + +// Send calls the msgsnd system call. +func Send(msgid int, msgType uint, msgText []byte, flags int) error { + if len(msgText) > maxText { + return ErrTooLong + } + m := message{Type: msgType} + copy(m.Text[:], msgText) + return Snd(msgid, uintptr(unsafe.Pointer(&m)), len(msgText), flags) +} + +// Receive calls the msgrcv system call. +func Receive(msgid int, msgType uint, flags int) ([]byte, error) { + m := message{Type: msgType} + length, err := Rcv(msgid, uintptr(unsafe.Pointer(&m)), maxText, msgType, flags) + if err != nil { return nil, err } return m.Text[:length], nil