1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Fake networking for js/wasm. It is intended to allow tests of other package to pass.
21 var listenersMu sync.Mutex
22 var listeners = make(map[string]*netFD)
24 var portCounterMu sync.Mutex
29 defer portCounterMu.Unlock()
34 // Network file descriptor.
43 // immutable until Close
53 isConnected bool // handshake completed or use of association with peer
56 // socket returns a network file descriptor that is ready for
57 // asynchronous I/O using the network poller.
58 func socket(ctx context.Context, net string, family, sotype, proto int, ipv6only bool, laddr, raddr sockaddr, ctrlFn func(string, string, syscall.RawConn) error) (*netFD, error) {
59 fd := &netFD{family: family, sotype: sotype, net: net}
61 if laddr != nil && raddr == nil { // listener
69 fd.incoming = make(chan *netFD, 1024)
71 listeners[fd.laddr.(*TCPAddr).String()] = fd
77 IP: IPv4(127, 0, 0, 1),
81 fd.r = newBufferedPipe(65536)
82 fd.w = newBufferedPipe(65536)
84 fd2 := &netFD{family: fd.family, sotype: sotype, net: net}
90 l, ok := listeners[fd.raddr.(*TCPAddr).String()]
93 return nil, syscall.ECONNREFUSED
101 func (fd *netFD) Read(p []byte) (n int, err error) {
105 func (fd *netFD) Write(p []byte) (nn int, err error) {
109 func (fd *netFD) Close() error {
120 delete(listeners, fd.laddr.String())
132 func (fd *netFD) closeRead() error {
137 func (fd *netFD) closeWrite() error {
142 func (fd *netFD) accept() (*netFD, error) {
143 c, ok := <-fd.incoming
145 return nil, syscall.EINVAL
150 func (fd *netFD) SetDeadline(t time.Time) error {
151 fd.r.SetReadDeadline(t)
152 fd.w.SetWriteDeadline(t)
156 func (fd *netFD) SetReadDeadline(t time.Time) error {
157 fd.r.SetReadDeadline(t)
161 func (fd *netFD) SetWriteDeadline(t time.Time) error {
162 fd.w.SetWriteDeadline(t)
166 func newBufferedPipe(softLimit int) *bufferedPipe {
167 p := &bufferedPipe{softLimit: softLimit}
173 type bufferedPipe struct {
184 func (p *bufferedPipe) Read(b []byte) (int, error) {
189 if p.closed && len(p.buf) == 0 {
192 if !p.rDeadline.IsZero() {
193 d := time.Until(p.rDeadline)
195 return 0, syscall.EAGAIN
197 time.AfterFunc(d, p.rCond.Broadcast)
211 func (p *bufferedPipe) Write(b []byte) (int, error) {
217 return 0, syscall.ENOTCONN
219 if !p.wDeadline.IsZero() {
220 d := time.Until(p.wDeadline)
222 return 0, syscall.EAGAIN
224 time.AfterFunc(d, p.wCond.Broadcast)
226 if len(p.buf) <= p.softLimit {
232 p.buf = append(p.buf, b...)
237 func (p *bufferedPipe) Close() {
246 func (p *bufferedPipe) SetReadDeadline(t time.Time) {
254 func (p *bufferedPipe) SetWriteDeadline(t time.Time) {
262 func sysSocket(family, sotype, proto int) (int, error) {
263 return 0, syscall.ENOSYS
266 func (fd *netFD) readFrom(p []byte) (n int, sa syscall.Sockaddr, err error) {
267 return 0, nil, syscall.ENOSYS
270 func (fd *netFD) readMsg(p []byte, oob []byte) (n, oobn, flags int, sa syscall.Sockaddr, err error) {
271 return 0, 0, 0, nil, syscall.ENOSYS
274 func (fd *netFD) writeTo(p []byte, sa syscall.Sockaddr) (n int, err error) {
275 return 0, syscall.ENOSYS
278 func (fd *netFD) writeMsg(p []byte, oob []byte, sa syscall.Sockaddr) (n int, oobn int, err error) {
279 return 0, 0, syscall.ENOSYS
282 func (fd *netFD) dup() (f *os.File, err error) {
283 return nil, syscall.ENOSYS