1
0
mirror of https://github.com/golang/go synced 2024-11-22 03:54:39 -07:00

net: rearrange the call order of runtime-integrated network pollster and syscall functions

This CL rearranges the call order for raw networking primitives like
the following;

- For dialers that open active connections, pollDesc.Init will be
  called before syscall.Connect.

- For stream listeners that open passive stream connections,
  pollDesc.Init will be called just after syscall.Listen.

- For datagram listeners that open datagram connections,
  pollDesc.Init will be called just after syscall.Bind.

This is in preparation for runtime-integrated network pollster for BSD
variants.

Update #5199

R=golang-dev, alex.brainman
CC=golang-dev
https://golang.org/cl/12730043
This commit is contained in:
Mikio Hara 2013-08-15 16:40:33 +09:00
parent a454d2fd2e
commit 5d5defc77f
3 changed files with 14 additions and 16 deletions

View File

@ -53,10 +53,6 @@ func socket(net string, f, t, p int, ipv6only bool, laddr, raddr sockaddr, deadl
closesocket(s)
return nil, err
}
if err := fd.init(); err != nil {
fd.Close()
return nil, err
}
// This function makes a network file descriptor for stream
// and datagram dialers, stream and datagram listeners.
@ -75,7 +71,7 @@ func socket(net string, f, t, p int, ipv6only bool, laddr, raddr sockaddr, deadl
if laddr != nil && raddr == nil {
switch t {
case syscall.SOCK_STREAM, syscall.SOCK_SEQPACKET:
if err := fd.listenStream(laddr, toAddr); err != nil {
if err := fd.listenStream(laddr, listenerBacklog, toAddr); err != nil {
fd.Close()
return nil, err
}
@ -107,6 +103,9 @@ func (fd *netFD) dial(laddr, raddr sockaddr, deadline time.Time, toAddr func(sys
}
}
}
if err := fd.init(); err != nil {
return err
}
var rsa syscall.Sockaddr
if raddr != nil {
if rsa, err = raddr.sockaddr(fd.family); err != nil {
@ -133,7 +132,7 @@ func (fd *netFD) dial(laddr, raddr sockaddr, deadline time.Time, toAddr func(sys
return nil
}
func (fd *netFD) listenStream(laddr sockaddr, toAddr func(syscall.Sockaddr) Addr) error {
func (fd *netFD) listenStream(laddr sockaddr, backlog int, toAddr func(syscall.Sockaddr) Addr) error {
if err := setDefaultListenerSockopts(fd.sysfd); err != nil {
return err
}
@ -144,6 +143,12 @@ func (fd *netFD) listenStream(laddr sockaddr, toAddr func(syscall.Sockaddr) Addr
return os.NewSyscallError("bind", err)
}
}
if err := syscall.Listen(fd.sysfd, backlog); err != nil {
return os.NewSyscallError("listen", err)
}
if err := fd.init(); err != nil {
return err
}
lsa, _ := syscall.Getsockname(fd.sysfd)
fd.setAddr(toAddr(lsa), nil)
return nil
@ -180,6 +185,9 @@ func (fd *netFD) listenDatagram(laddr sockaddr, toAddr func(syscall.Sockaddr) Ad
return os.NewSyscallError("bind", err)
}
}
if err := fd.init(); err != nil {
return err
}
lsa, _ := syscall.Getsockname(fd.sysfd)
fd.setAddr(toAddr(lsa), nil)
return nil

View File

@ -301,10 +301,5 @@ func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error) {
if err != nil {
return nil, err
}
err = syscall.Listen(fd.sysfd, listenerBacklog)
if err != nil {
fd.Close()
return nil, &OpError{"listen", net, laddr, err}
}
return &TCPListener{fd}, nil
}

View File

@ -283,11 +283,6 @@ func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error) {
if err != nil {
return nil, err
}
err = syscall.Listen(fd.sysfd, listenerBacklog)
if err != nil {
fd.Close()
return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: err}
}
return &UnixListener{fd, laddr.Name}, nil
}