1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
package ami
import (
"bufio"
"context"
"fmt"
"net"
"regexp"
"sync/atomic"
"time"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/wenerme/astgo/ami/amimodels"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
// CustomDialer can be used to specify any dialer, not necessarily
// a *net.Dialer.
type CustomDialer interface {
Dial(network, address string) (net.Conn, error)
}
type ConnErrHandler func(*Conn, error)
type ConnHandler func(*Conn)
type ConnectOptions struct {
Context context.Context
Timeout time.Duration
AllowReconnect bool
Username string // login username
Secret string // login secret
Logger *zap.Logger
Dialer CustomDialer
OnConnectErr ConnErrHandler
OnConnected ConnHandler
subscribers []struct {
sub SubscribeFunc
opts []SubscribeOption
}
}
type ConnectOption func(c *ConnectOptions) error
func WithAuth(username string, secret string) ConnectOption {
return func(c *ConnectOptions) error {
c.Username = username
c.Secret = secret
return nil
}
}
func WithSubscribe(cb SubscribeFunc, opts ...SubscribeOption) ConnectOption {
return func(c *ConnectOptions) error {
c.subscribers = append(c.subscribers, struct {
sub SubscribeFunc
opts []SubscribeOption
}{sub: cb, opts: opts})
return nil
}
}
func Connect(addr string, opts ...ConnectOption) (conn *Conn, err error) {
opt := &ConnectOptions{
Timeout: 10 * time.Second,
Context: context.Background(),
Logger: zap.L(),
}
for _, v := range opts {
if err = v(opt); err != nil {
return nil, err
}
}
if opt.Dialer == nil {
opt.Dialer = &net.Dialer{
Timeout: opt.Timeout,
}
}
var id uint64
conn = &Conn{
ctx: opt.Context,
conf: opt,
logger: opt.Logger,
recv: make(chan *Message, 4096),
pending: make(chan *asyncMsg, 100),
nextID: func() string {
return fmt.Sprint(atomic.AddUint64(&id, 1))
},
}
for _, sub := range opt.subscribers {
_, err = conn.Subscribe(sub.sub, sub.opts...)
if err != nil {
return nil, err
}
}
return conn, conn.dial(addr)
}
func (c *Conn) Close() error {
if c.closer != nil {
c.closed = true
c.closer()
c.closer = nil
err := c.g.Wait()
if errors.Is(err, errClose) {
return nil
}
return err
}
if c.closed {
return nil
}
return errors.New("not init")
}
func (c *Conn) dial(addr string) (err error) {
conf := c.conf
if conf.AllowReconnect {
// NOTE reconnect keep pending, but fail all async
go func() {
log := c.logger
onErr := conf.OnConnectErr
if onErr == nil {
onErr = func(conn *Conn, err error) {
}
}
var err error
for !c.closed {
err = c.dialOnce(addr)
if err != nil {
log.Sugar().With("err", err).Warn("ami.Conn: dial")
onErr(c, err)
// fixme improve wait strategy
<-time.NewTimer(time.Second).C
continue
}
if conf.OnConnected != nil {
conf.OnConnected(c)
log.Sugar().Info("ami.Conn: connected")
}
err = c.g.Wait()
if err != nil {
log.Sugar().With("err", err).Warn("ami.Conn: error")
onErr(c, err)
}
c.g = nil
}
log.Sugar().Info("ami.Conn: stop reconnect, conn closed")
}()
return nil
}
return c.dialOnce(addr)
}
func (c *Conn) dialOnce(addr string) (err error) {
conf := c.conf
conn, err := conf.Dialer.Dial("tcp", addr)
if err != nil {
return err
}
defer func() {
if err != nil {
if e := conn.Close(); e != nil {
err = multierror.Append(err, e)
}
}
}()
return c.connect(conn)
}
var errClose = errors.New("Close")
func (c *Conn) connect(conn net.Conn) (err error) {
log := c.logger
r := bufio.NewReader(conn)
c.reader = r
c.conn = conn
line, err := r.ReadString('\n')
if err != nil {
return errors.Wrap(err, "scan ami initial line")
}
// check connection
match := regexp.MustCompile("Asterisk Call Manager/([0-9.]+)").FindStringSubmatch(line)
if len(match) > 1 {
c.version = match[1]
log.Sugar().With("version", c.version).Debug("AMI Version")
} else {
err = errors.Errorf("Invalid server header: %q", line)
return
}
ctx := c.ctx
if ctx == nil {
ctx = context.Background()
}
c.g, ctx = errgroup.WithContext(ctx)
c.g.Go(func() error {
return c.read(ctx)
})
c.g.Go(func() error {
return c.loop(ctx)
})
// manual close
waitCtx, closer := context.WithCancel(ctx)
c.g.Go(func() error {
<-ctx.Done()
closer()
return conn.Close()
})
c.g.Go(func() error {
<-waitCtx.Done()
return errClose
})
c.closer = closer
conf := c.conf
if conf.Username != "" {
var resp *Message
resp, err = c.Request(amimodels.LoginAction{
UserName: conf.Username,
Secret: conf.Secret,
}, RequestTimeout(2*time.Second))
if err != nil {
err = errors.Wrap(err, "request login")
} else if !resp.Success() {
err = errors.Wrap(resp.Error(), "login")
}
if err != nil {
log.Sugar().With("err", err).Info("login failed")
return err
}
log.Info("login success")
}
//log.Sugar().Debug("do conn check ping")
// be ready
// may not FullyBooted
// _, err = c.Request(amimodels.PingAction{})
return
}
|