delete Msg struct

This commit is contained in:
hslam 2020-11-29 01:54:53 +08:00
parent c5d942dbf9
commit 46567b17e9
3 changed files with 17 additions and 29 deletions

View File

@ -38,8 +38,7 @@ func main() {
panic(err)
}
defer msg.Remove(msgid)
m := &msg.Msg{Type: 1, Text: []byte("Hello World")}
err = msg.Snd(msgid, m, 0600)
err = msg.Snd(msgid, 1, []byte("Hello World"), 0600)
if err != nil {
panic(err)
}
@ -65,12 +64,11 @@ func main() {
if err != nil {
panic(err)
}
m := &msg.Msg{Type: 1}
err = msg.Rcv(msgid, m, 0600)
text, err := msg.Rcv(msgid, 1, 0600)
if err != nil {
panic(err)
}
fmt.Println(string(m.Text))
fmt.Println(string(text))
}
```

View File

@ -20,8 +20,7 @@ func TestMsg(t *testing.T) {
panic(err)
}
defer Remove(msgid)
m := &Msg{Type: 1, Text: []byte(context)}
err = Snd(msgid, m, 0600)
err = Snd(msgid, 1, []byte(context), 0600)
if err != nil {
panic(err)
}
@ -37,13 +36,12 @@ func TestMsg(t *testing.T) {
if err != nil {
panic(err)
}
m := &Msg{Type: 1}
err = Rcv(msgid, m, 0600)
text, err := Rcv(msgid, 1, 0600)
if err != nil {
panic(err)
}
if context != string(m.Text) {
t.Error(context, string(m.Text))
if context != string(text) {
t.Error(context, string(text))
}
<-done
}

View File

@ -27,12 +27,6 @@ type message struct {
Text [maxText]byte
}
// Msg represents a message.
type Msg struct {
Type uint
Text []byte
}
// Get calls the msgget system call.
func Get(key int, msgflg int) (uintptr, error) {
msgid, _, err := syscall.Syscall(syscall.SYS_MSGGET, uintptr(key), uintptr(msgflg), 0)
@ -43,13 +37,13 @@ func Get(key int, msgflg int) (uintptr, error) {
}
// Snd calls the msgsnd system call.
func Snd(msgid uintptr, msg *Msg, flags uint) error {
if len(msg.Text) > maxText {
func Snd(msgid uintptr, msgType uint, msgText []byte, flags uint) error {
if len(msgText) > maxText {
return ErrTooLong
}
m := message{Type: msg.Type}
copy(m.Text[:], msg.Text)
_, _, err := syscall.Syscall6(syscall.SYS_MSGSND, msgid, uintptr(unsafe.Pointer(&m)), uintptr(len(msg.Text)), uintptr(flags), 0, 0)
m := message{Type: msgType}
copy(m.Text[:], msgText)
_, _, err := syscall.Syscall6(syscall.SYS_MSGSND, msgid, uintptr(unsafe.Pointer(&m)), uintptr(len(msgText)), uintptr(flags), 0, 0)
if err != 0 {
return err
}
@ -57,15 +51,13 @@ func Snd(msgid uintptr, msg *Msg, flags uint) error {
}
// Rcv calls the msgrcv system call.
func Rcv(msgid uintptr, msg *Msg, flags uint) error {
m := message{Type: msg.Type}
length, _, err := syscall.Syscall6(syscall.SYS_MSGRCV, msgid, uintptr(unsafe.Pointer(&m)), maxText, uintptr(msg.Type), uintptr(flags), 0)
func Rcv(msgid uintptr, msgType uint, flags uint) ([]byte, error) {
m := message{Type: msgType}
length, _, err := syscall.Syscall6(syscall.SYS_MSGRCV, msgid, uintptr(unsafe.Pointer(&m)), maxText, uintptr(msgType), uintptr(flags), 0)
if err != 0 {
return err
return nil, err
}
msg.Type = m.Type
msg.Text = m.Text[:length]
return nil
return m.Text[:length], nil
}
// Remove removes the message queue with the given id.