github.com/cbeuw/gotfo@v0.0.0-20180331191851-f2b091af84de/net_windows.go (about) 1 // Copyright 2009 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. 4 5 package gotfo 6 7 import ( 8 "context" 9 "net" 10 "syscall" 11 ) 12 13 const TCP_FASTOPEN = 15 14 15 func init() { 16 sysInit() 17 } 18 19 type TFOListener struct { 20 *net.TCPListener 21 fd *netFD 22 } 23 24 func (ln *TFOListener) ok() bool { return ln != nil && ln.fd != nil } 25 26 func (ln *TFOListener) accept() (*net.TCPConn, error) { 27 fd, err := ln.fd.accept() 28 if err != nil { 29 return nil, err 30 } 31 return newTCPConn(fd), nil 32 } 33 34 func (l *TFOListener) AcceptTCP() (*net.TCPConn, error) { 35 if !l.ok() { 36 return nil, syscall.EINVAL 37 } 38 c, err := l.accept() 39 if err != nil { 40 return nil, &net.OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err} 41 } 42 return c, nil 43 } 44 45 func (l *TFOListener) Accept() (net.Conn, error) { 46 return l.AcceptTCP() 47 } 48 49 func Dial(address string, fastOpen bool, data []byte) (*net.TCPConn, error) { 50 return DialContext(context.Background(), address, fastOpen, data) 51 } 52 53 func DialContext(ctx context.Context, address string, fastOpen bool, data []byte) (*net.TCPConn, error) { 54 raddr, err := net.ResolveTCPAddr("tcp", address) 55 if err != nil { 56 return nil, err 57 } 58 59 if fd, err := socket(ctx, syscall.AF_INET, false, raddr, true, fastOpen, data); err != nil { 60 return nil, err 61 } else { 62 return newTCPConn(fd), nil 63 } 64 } 65 66 func Listen(address string, fastOpen bool) (net.Listener, error) { 67 laddr, err := net.ResolveTCPAddr("tcp", address) 68 if err != nil { 69 return nil, err 70 } 71 72 if fd, err := socket(context.Background(), syscall.AF_INET, false, laddr, false, fastOpen, nil); err != nil { 73 return nil, err 74 } else { 75 return newTCPListener(fd, true), nil 76 } 77 }