-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconn.go
65 lines (53 loc) · 1.48 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package vsock
import (
"errors"
"io"
"net"
"golang.org/x/sys/unix"
)
var (
ErrVSockConnectionBadFileDescriptor = errors.New("bad file descriptor in VSock connection")
ErrCouldNotShutdownVSockConnection = errors.New("could not shut down VSock connection")
ErrCouldNotCloseVSockConn = errors.New("could not close VSock connection")
)
type conn struct {
fd int
}
func (c *conn) Read(b []byte) (int, error) {
n, err := unix.Read(c.fd, b)
if err != nil {
if errors.Is(err, unix.EBADF) { // Report bad file descriptor errors as closed errors
err = errors.Join(net.ErrClosed, ErrVSockConnectionBadFileDescriptor, err)
}
return n, err
}
if n == 0 {
return 0, io.EOF
}
return n, nil
}
func (v *conn) Write(b []byte) (int, error) {
n, err := unix.Write(v.fd, b)
if err != nil {
if errors.Is(err, unix.EBADF) { // Report bad file descriptor errors as closed errors
err = errors.Join(net.ErrClosed, ErrVSockConnectionBadFileDescriptor, err)
}
return n, err
}
return n, nil
}
func (v *conn) Close() error {
if err := unix.Shutdown(v.fd, unix.SHUT_RDWR); err != nil {
// Always close the file descriptor even if shutdown fails
if e := unix.Close(v.fd); e != nil {
err = errors.Join(ErrCouldNotCloseVSockConn, ErrCouldNotShutdownVSockConnection, e, err)
} else {
err = errors.Join(ErrCouldNotShutdownVSockConnection, err)
}
return err
}
if err := unix.Close(v.fd); err != nil {
return errors.Join(ErrCouldNotCloseVSockConn, err)
}
return nil
}