Source file
src/net/file_wasip1.go
1
2
3
4
5
6
7 package net
8
9 import (
10 "os"
11 "syscall"
12 _ "unsafe"
13 )
14
15 func fileListener(f *os.File) (Listener, error) {
16 filetype, err := fd_fdstat_get_type(f.PollFD().Sysfd)
17 if err != nil {
18 return nil, err
19 }
20 net, err := fileListenNet(filetype)
21 if err != nil {
22 return nil, err
23 }
24 pfd := f.PollFD().Copy()
25 fd := newPollFD(net, pfd)
26 if err := fd.init(); err != nil {
27 pfd.Close()
28 return nil, err
29 }
30 return newFileListener(fd), nil
31 }
32
33 func fileConn(f *os.File) (Conn, error) {
34 filetype, err := fd_fdstat_get_type(f.PollFD().Sysfd)
35 if err != nil {
36 return nil, err
37 }
38 net, err := fileConnNet(filetype)
39 if err != nil {
40 return nil, err
41 }
42 pfd := f.PollFD().Copy()
43 fd := newPollFD(net, pfd)
44 if err := fd.init(); err != nil {
45 pfd.Close()
46 return nil, err
47 }
48 return newFileConn(fd), nil
49 }
50
51 func filePacketConn(f *os.File) (PacketConn, error) {
52 return nil, syscall.ENOPROTOOPT
53 }
54
55 func fileListenNet(filetype syscall.Filetype) (string, error) {
56 switch filetype {
57 case syscall.FILETYPE_SOCKET_STREAM:
58 return "tcp", nil
59 case syscall.FILETYPE_SOCKET_DGRAM:
60 return "", syscall.EOPNOTSUPP
61 default:
62 return "", syscall.ENOTSOCK
63 }
64 }
65
66 func fileConnNet(filetype syscall.Filetype) (string, error) {
67 switch filetype {
68 case syscall.FILETYPE_SOCKET_STREAM:
69 return "tcp", nil
70 case syscall.FILETYPE_SOCKET_DGRAM:
71 return "udp", nil
72 default:
73 return "", syscall.ENOTSOCK
74 }
75 }
76
77 func newFileListener(fd *netFD) Listener {
78 switch fd.net {
79 case "tcp":
80 return &TCPListener{fd: fd}
81 default:
82 panic("unsupported network for file listener: " + fd.net)
83 }
84 }
85
86 func newFileConn(fd *netFD) Conn {
87 switch fd.net {
88 case "tcp":
89 return &TCPConn{conn{fd: fd}}
90 case "udp":
91 return &UDPConn{conn{fd: fd}}
92 default:
93 panic("unsupported network for file connection: " + fd.net)
94 }
95 }
96
97
98
99
100
101
102 func fd_fdstat_get_type(fd int) (uint8, error)
103
View as plain text