evio/evio_loop.go

419 lines
8.0 KiB
Go
Raw Normal View History

2017-11-02 18:08:18 +03:00
// Copyright 2017 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
2017-10-28 22:23:13 +03:00
// +build netbsd openbsd freebsd darwin dragonfly linux
2017-10-26 23:43:31 +03:00
2017-10-29 00:58:59 +03:00
package evio
2017-10-26 23:43:31 +03:00
import (
"errors"
"net"
"os"
"sort"
"strconv"
"sync"
"syscall"
"time"
2017-10-28 22:23:13 +03:00
2017-10-29 00:58:59 +03:00
"github.com/tidwall/evio/internal"
2017-10-26 23:43:31 +03:00
)
func (ln *listener) close() {
if ln.fd != 0 {
syscall.Close(ln.fd)
}
if ln.f != nil {
ln.f.Close()
}
if ln.ln != nil {
ln.ln.Close()
}
if ln.network == "unix" {
os.RemoveAll(ln.addr)
}
}
func (ln *listener) system() error {
var err error
switch netln := ln.ln.(type) {
default:
ln.close()
2017-11-02 03:36:35 +03:00
return errors.New("network not supported")
2017-10-26 23:43:31 +03:00
case *net.TCPListener:
ln.f, err = netln.File()
case *net.UnixListener:
ln.f, err = netln.File()
}
if err != nil {
ln.close()
return err
}
ln.fd = int(ln.f.Fd())
return syscall.SetNonblock(ln.fd, true)
}
2017-10-31 00:02:10 +03:00
type unixConn struct {
2017-10-26 23:43:31 +03:00
id, fd, p int
outbuf []byte
outpos int
action Action
opts Options
2017-11-01 03:44:57 +03:00
raddr net.Addr
laddr net.Addr
err error
2017-10-26 23:43:31 +03:00
wake bool
writeon bool
2017-10-31 00:02:10 +03:00
detached bool
2017-11-02 15:03:54 +03:00
closed bool
2017-10-31 00:02:10 +03:00
}
func (c *unixConn) Read(p []byte) (n int, err error) {
2017-11-01 03:44:57 +03:00
return syscall.Read(c.fd, p)
2017-10-31 00:02:10 +03:00
}
func (c *unixConn) Write(p []byte) (n int, err error) {
if c.detached {
2017-11-01 03:44:57 +03:00
if len(c.outbuf) > 0 {
for len(c.outbuf) > 0 {
n, err = syscall.Write(c.fd, c.outbuf)
if n > 0 {
c.outbuf = c.outbuf[n:]
}
if err != nil {
return 0, err
}
2017-10-31 00:02:10 +03:00
}
2017-11-01 03:44:57 +03:00
c.outbuf = nil
}
var tn int
if len(p) > 0 {
for len(p) > 0 {
n, err = syscall.Write(c.fd, p)
if n > 0 {
p = p[n:]
tn += n
}
if err != nil {
return tn, err
}
2017-10-31 00:02:10 +03:00
}
2017-11-01 03:44:57 +03:00
p = nil
2017-10-31 00:02:10 +03:00
}
2017-11-01 03:44:57 +03:00
return tn, nil
2017-10-31 00:02:10 +03:00
}
2017-11-01 03:44:57 +03:00
return syscall.Write(c.fd, p)
2017-10-31 00:02:10 +03:00
}
func (c *unixConn) Close() error {
2017-11-02 15:03:54 +03:00
if c.closed {
return syscall.EINVAL
}
2017-10-31 00:02:10 +03:00
err := syscall.Close(c.fd)
2017-11-02 15:03:54 +03:00
c.fd = -1
c.closed = true
2017-10-31 00:02:10 +03:00
return err
2017-10-26 23:43:31 +03:00
}
func serve(events Events, lns []*listener) error {
2017-10-28 22:23:13 +03:00
p, err := internal.MakePoll()
2017-10-26 23:43:31 +03:00
if err != nil {
return err
}
defer syscall.Close(p)
for _, ln := range lns {
2017-10-28 22:23:13 +03:00
if err := internal.AddRead(p, ln.fd); err != nil {
2017-10-26 23:43:31 +03:00
return err
}
}
var mu sync.Mutex
lock := func() { mu.Lock() }
unlock := func() { mu.Unlock() }
2017-10-31 00:02:10 +03:00
fdconn := make(map[int]*unixConn)
idconn := make(map[int]*unixConn)
2017-10-28 22:23:13 +03:00
wake := func(id int) bool {
2017-10-26 23:43:31 +03:00
var ok = true
var err error
lock()
c := idconn[id]
if c == nil {
ok = false
2017-10-28 22:23:13 +03:00
} else if !c.wake {
c.wake = true
err = internal.AddWrite(c.p, c.fd, &c.writeon)
2017-10-26 23:43:31 +03:00
}
unlock()
if err != nil {
panic(err)
}
return ok
}
if events.Serving != nil {
2017-11-02 03:36:35 +03:00
addrs := make([]net.Addr, len(lns))
for i, ln := range lns {
addrs[i] = ln.naddr
}
switch events.Serving(wake, addrs) {
2017-10-26 23:43:31 +03:00
case Shutdown:
return nil
}
}
defer func() {
lock()
2017-11-01 03:44:57 +03:00
type fdid struct {
fd, id int
opts Options
}
2017-10-26 23:43:31 +03:00
var fdids []fdid
2017-11-01 03:44:57 +03:00
for fd, c := range fdconn {
fdids = append(fdids, fdid{fd, c.id, c.opts})
2017-10-26 23:43:31 +03:00
}
sort.Slice(fdids, func(i, j int) bool {
return fdids[j].id < fdids[i].id
})
for _, fdid := range fdids {
syscall.Close(fdid.fd)
if events.Closed != nil {
unlock()
2017-11-01 03:44:57 +03:00
events.Closed(fdid.id, nil)
2017-10-26 23:43:31 +03:00
lock()
}
}
unlock()
}()
var id int
var packet [0xFFFF]byte
2017-10-28 22:23:13 +03:00
var evs = internal.MakeEvents(64)
2017-10-26 23:43:31 +03:00
var lastTicker time.Time
var tickerDelay time.Duration
if events.Tick == nil {
tickerDelay = time.Hour
}
for {
2017-10-28 22:23:13 +03:00
pn, err := internal.Wait(p, evs, tickerDelay)
2017-10-26 23:43:31 +03:00
if err != nil && err != syscall.EINTR {
return err
}
if events.Tick != nil {
now := time.Now()
if now.Sub(lastTicker) > tickerDelay {
var action Action
tickerDelay, action = events.Tick()
if action == Shutdown {
return nil
}
lastTicker = now
}
}
lock()
for i := 0; i < pn; i++ {
var in []byte
2017-10-31 00:02:10 +03:00
var c *unixConn
2017-10-26 23:43:31 +03:00
var nfd int
var n int
var out []byte
var ln *listener
2017-10-29 23:09:40 +03:00
var lnidx int
2017-10-28 22:23:13 +03:00
var fd = internal.GetFD(evs, i)
2017-10-26 23:43:31 +03:00
var sa syscall.Sockaddr
2017-10-29 23:09:40 +03:00
for lnidx, ln = range lns {
2017-10-26 23:43:31 +03:00
if fd == ln.fd {
goto accept
}
}
c = fdconn[fd]
if c == nil {
syscall.Close(fd)
goto next
}
goto read
accept:
nfd, sa, err = syscall.Accept(fd)
if err != nil {
goto next
}
if err = syscall.SetNonblock(nfd, true); err != nil {
goto fail
}
2017-10-28 22:23:13 +03:00
if err = internal.AddRead(p, nfd); err != nil {
2017-10-26 23:43:31 +03:00
goto fail
}
id++
2017-10-31 00:02:10 +03:00
c = &unixConn{id: id, fd: nfd, p: p}
2017-10-26 23:43:31 +03:00
fdconn[nfd] = c
idconn[id] = c
2017-11-01 03:44:57 +03:00
c.laddr = getlocaladdr(fd, ln.ln)
c.raddr = getaddr(sa, ln.ln)
2017-10-26 23:43:31 +03:00
if events.Opened != nil {
unlock()
2017-11-01 03:44:57 +03:00
out, c.opts, c.action = events.Opened(c.id, Addr{lnidx, c.laddr, c.raddr})
2017-10-26 23:43:31 +03:00
lock()
if c.opts.TCPKeepAlive > 0 {
if _, ok := ln.ln.(*net.TCPListener); ok {
2017-10-28 22:23:13 +03:00
if err = internal.SetKeepAlive(c.fd, int(c.opts.TCPKeepAlive/time.Second)); err != nil {
2017-10-26 23:43:31 +03:00
goto fail
}
}
}
if len(out) > 0 {
c.outbuf = append(c.outbuf, out...)
}
}
goto write
read:
if c.action != None {
goto write
}
if c.wake {
c.wake = false
} else {
2017-10-31 00:02:10 +03:00
n, err = c.Read(packet[:])
2017-10-26 23:43:31 +03:00
if n == 0 || err != nil {
if err == syscall.EAGAIN {
goto write
}
2017-11-01 03:44:57 +03:00
c.err = err
2017-10-26 23:43:31 +03:00
goto close
}
in = append([]byte{}, packet[:n]...)
}
if events.Data != nil {
unlock()
out, c.action = events.Data(c.id, in)
lock()
}
if len(out) > 0 {
c.outbuf = append(c.outbuf, out...)
}
goto write
write:
if len(c.outbuf)-c.outpos > 0 {
if events.Prewrite != nil {
unlock()
action := events.Prewrite(c.id, len(c.outbuf[c.outpos:]))
lock()
2017-10-28 03:01:03 +03:00
if action == Shutdown {
2017-10-26 23:43:31 +03:00
c.action = Shutdown
}
}
2017-10-31 00:02:10 +03:00
n, err = c.Write(c.outbuf[c.outpos:])
2017-10-26 23:43:31 +03:00
if events.Postwrite != nil {
amount := n
if amount < 0 {
amount = 0
}
unlock()
action := events.Postwrite(c.id, amount, len(c.outbuf)-c.outpos-amount)
lock()
2017-10-28 03:01:03 +03:00
if action == Shutdown {
2017-10-26 23:43:31 +03:00
c.action = Shutdown
}
}
if n == 0 || err != nil {
if c.action == Shutdown {
goto close
}
if err == syscall.EAGAIN {
2017-10-28 22:23:13 +03:00
if err = internal.AddWrite(c.p, c.fd, &c.writeon); err != nil {
2017-10-26 23:43:31 +03:00
goto fail
}
goto next
}
2017-11-01 03:44:57 +03:00
c.err = err
2017-10-26 23:43:31 +03:00
goto close
}
c.outpos += n
if len(c.outbuf)-c.outpos == 0 {
c.outpos = 0
2017-11-03 04:31:36 +03:00
c.outbuf = c.outbuf[:0]
2017-10-26 23:43:31 +03:00
}
}
if c.action == Shutdown {
goto close
}
if len(c.outbuf)-c.outpos == 0 {
if !c.wake {
2017-10-28 22:23:13 +03:00
if err = internal.DelWrite(c.p, c.fd, &c.writeon); err != nil {
2017-10-26 23:43:31 +03:00
goto fail
}
}
if c.action != None {
goto close
}
} else {
2017-10-28 22:23:13 +03:00
if err = internal.AddWrite(c.p, c.fd, &c.writeon); err != nil {
2017-10-26 23:43:31 +03:00
goto fail
}
}
goto next
close:
delete(fdconn, c.fd)
delete(idconn, c.id)
if c.action == Detach {
if events.Detached != nil {
2017-10-31 00:02:10 +03:00
c.detached = true
if len(c.outbuf)-c.outpos > 0 {
c.outbuf = append(c.outbuf[:0], c.outbuf[c.outpos:]...)
} else {
c.outbuf = nil
}
c.outpos = 0
syscall.SetNonblock(c.fd, false)
2017-10-26 23:43:31 +03:00
unlock()
2017-10-31 00:02:10 +03:00
c.action = events.Detached(c.id, c)
2017-10-26 23:43:31 +03:00
lock()
if c.action == Shutdown {
goto fail
}
goto next
}
}
syscall.Close(c.fd)
if events.Closed != nil {
unlock()
2017-11-01 03:44:57 +03:00
action := events.Closed(c.id, c.err)
2017-10-26 23:43:31 +03:00
lock()
if action == Shutdown {
c.action = Shutdown
}
}
if c.action == Shutdown {
err = nil
goto fail
}
goto next
fail:
unlock()
return err
next:
}
unlock()
}
}
2017-10-29 23:09:40 +03:00
func getlocaladdr(fd int, ln net.Listener) net.Addr {
sa, _ := syscall.Getsockname(fd)
return getaddr(sa, ln)
}
func getaddr(sa syscall.Sockaddr, ln net.Listener) net.Addr {
2017-10-26 23:43:31 +03:00
switch ln.(type) {
case *net.UnixListener:
2017-10-29 23:09:40 +03:00
return ln.Addr()
2017-10-26 23:43:31 +03:00
case *net.TCPListener:
var addr net.TCPAddr
switch sa := sa.(type) {
case *syscall.SockaddrInet4:
addr.IP = net.IP(sa.Addr[:])
addr.Port = sa.Port
2017-11-02 03:36:35 +03:00
return &addr
2017-10-26 23:43:31 +03:00
case *syscall.SockaddrInet6:
addr.IP = net.IP(sa.Addr[:])
addr.Port = sa.Port
if sa.ZoneId != 0 {
addr.Zone = strconv.FormatInt(int64(sa.ZoneId), 10)
}
2017-11-02 03:36:35 +03:00
return &addr
2017-10-26 23:43:31 +03:00
}
}
2017-11-02 03:36:35 +03:00
return nil
2017-10-26 23:43:31 +03:00
}