mirror of
https://github.com/golang/sys.git
synced 2026-02-08 03:36:03 +03:00
unix: add Send on Linux
Added Send function which in turn calls existing Sendto. Fixes golang/go#47288 Change-Id: I5c928a19cc4f10961a9e6d2802e197cc3b799438 Reviewed-on: https://go-review.googlesource.com/c/sys/+/336569 Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Matt Layher <mdlayher@gmail.com> Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Matt Layher <mdlayher@gmail.com>
This commit is contained in:
committed by
Tobias Klauser
parent
b9628f2c71
commit
15123e1e1f
@@ -313,6 +313,10 @@ func Recvfrom(fd int, p []byte, flags int) (n int, from Sockaddr, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func Send(s int, buf []byte, flags int) (err error) {
|
||||
return sendto(s, buf, flags, nil, 0)
|
||||
}
|
||||
|
||||
func Sendto(fd int, p []byte, flags int, to Sockaddr) (err error) {
|
||||
ptr, n, err := to.sockaddr()
|
||||
if err != nil {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
package unix_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
@@ -890,6 +891,44 @@ func TestUtimesNanoAt(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSend(t *testing.T) {
|
||||
ec := make(chan error, 2)
|
||||
ts := []byte("HELLO GOPHER")
|
||||
|
||||
fds, err := unix.Socketpair(unix.AF_LOCAL, unix.SOCK_STREAM, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("Socketpair: %v", err)
|
||||
}
|
||||
defer unix.Close(fds[0])
|
||||
defer unix.Close(fds[1])
|
||||
|
||||
go func() {
|
||||
data := make([]byte, len(ts))
|
||||
|
||||
_, _, err := unix.Recvfrom(fds[1], data, 0)
|
||||
if err != nil {
|
||||
ec <- err
|
||||
}
|
||||
if !bytes.Equal(ts, data) {
|
||||
ec <- fmt.Errorf("data sent != data received. Received %q", data)
|
||||
}
|
||||
ec <- nil
|
||||
}()
|
||||
err = unix.Send(fds[0], ts, 0)
|
||||
if err != nil {
|
||||
ec <- err
|
||||
}
|
||||
|
||||
select {
|
||||
case err = <-ec:
|
||||
if err != nil {
|
||||
t.Fatalf("Send: %v", err)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("Send: nothing received after 2 seconds")
|
||||
}
|
||||
}
|
||||
|
||||
// mktmpfifo creates a temporary FIFO and provides a cleanup function.
|
||||
func mktmpfifo(t *testing.T) (*os.File, func()) {
|
||||
err := unix.Mkfifo("fifo", 0666)
|
||||
|
||||
Reference in New Issue
Block a user