From 3a5168acb98804c616adc2c6eda21637010f610b Mon Sep 17 00:00:00 2001 From: hslam <791874158@qq.com> Date: Tue, 1 Dec 2020 16:58:26 +0800 Subject: [PATCH] update msg methods --- README.md | 4 ++-- ipc_test.go | 27 ++++++++++++++++++++------- msg.go | 18 ++++++++++++++---- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 5a5f1d5..4611111 100644 --- a/README.md +++ b/README.md @@ -79,14 +79,14 @@ func main() { return } n := binary.PutUvarint(buf, uint64(len(text))) - if err := ipc.Msgsnd(msgid, 1, buf[:n], 0600); err != nil { + if err := ipc.Msgsend(msgid, 1, buf[:n], 0600); err != nil { return } } } else { fmt.Println("Recv:") for { - m, err := ipc.Msgrcv(msgid, 1, 0600) + m, err := ipc.Msgreceive(msgid, 1, 0600) if err != nil { return } diff --git a/ipc_test.go b/ipc_test.go index 94db7cc..9d342c1 100644 --- a/ipc_test.go +++ b/ipc_test.go @@ -41,7 +41,7 @@ func TestIPC(t *testing.T) { if _, err := Semv(semid, semnum, SEM_UNDO); err != nil { return } - if err := Msgsnd(msgid, 1, []byte{byte(len(context))}, 0600); err != nil { + if err := Msgsend(msgid, 1, []byte{byte(len(context))}, 0600); err != nil { return } @@ -69,7 +69,7 @@ func TestIPC(t *testing.T) { msgid, _ := Msgget(key, 0600) defer Msgrm(msgid) - m, err := Msgrcv(msgid, 1, 0600) + m, err := Msgreceive(msgid, 1, 0600) if err != nil { return } @@ -97,6 +97,7 @@ func TestMore(t *testing.T) { context := strings.Repeat("1", 64) done := make(chan struct{}) semnum := 0 + msgType := uint(1) go func() { key, _ := Ftok("/tmp", 0x22) semid, err := Semget(key, 1, 0600) @@ -123,10 +124,15 @@ func TestMore(t *testing.T) { if _, err := Semv(semid, semnum, SEM_UNDO); err != nil { return } - if err := Msgsnd(msgid, 1, []byte{byte(len(context))}, 0600); err != nil { + st := struct { + Type uint + Text [8192]byte + }{} + st.Type = msgType + n := copy(st.Text[:], []byte{byte(len(context))}) + if err := Msgsnd(msgid, uintptr(unsafe.Pointer(&st)), n, 0600); err != nil { return } - time.Sleep(time.Millisecond * 200) close(done) }() @@ -158,12 +164,19 @@ func TestMore(t *testing.T) { defer Shmdt(shmaddr) msgid, _ := Msgget(key, 0600) defer Msgrm(msgid) - - m, err := Msgrcv(msgid, 1, 0600) + st := struct { + Type uint + Text [8192]byte + }{} + st.Type = msgType + sz, err := Msgrcv(msgid, uintptr(unsafe.Pointer(&st)), 8192, msgType, 0600) if err != nil { return } - length := int(m[0]) + if sz < 1 { + t.Error() + } + length := int(st.Text[0]) if ret, err := Semgetvalue(semid, semnum); err != nil { t.Error(err) } else if ret < 1 { diff --git a/msg.go b/msg.go index 70b929e..18e6038 100644 --- a/msg.go +++ b/msg.go @@ -13,13 +13,23 @@ func Msgget(key int, msgflg int) (int, error) { } // Msgsnd calls the msgsnd system call. -func Msgsnd(msgid int, msgType uint, msgText []byte, flags int) error { - return msg.Snd(msgid, msgType, msgText, flags) +func Msgsnd(msgid int, msgp uintptr, msgsz int, msgflg int) error { + return msg.Snd(msgid, msgp, msgsz, msgflg) } // Msgrcv calls the msgrcv system call. -func Msgrcv(msgid int, msgType uint, flags int) ([]byte, error) { - return msg.Rcv(msgid, msgType, flags) +func Msgrcv(msgid int, msgp uintptr, msgsz int, msgtyp uint, msgflg int) (int, error) { + return msg.Rcv(msgid, msgp, msgsz, msgtyp, msgflg) +} + +// Msgsend calls the msgsnd system call. +func Msgsend(msgid int, msgType uint, msgText []byte, flags int) error { + return msg.Send(msgid, msgType, msgText, flags) +} + +// Msgreceive calls the msgrcv system call. +func Msgreceive(msgid int, msgType uint, flags int) ([]byte, error) { + return msg.Receive(msgid, msgType, flags) } // Msgrm removes the shm with the given id.