-
Notifications
You must be signed in to change notification settings - Fork 4
/
goami2.go
43 lines (36 loc) · 1.3 KB
/
goami2.go
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
// Package goami2 library for Asterisk Manager Interface
package goami2
import (
"context"
"errors"
"fmt"
"net"
"time"
)
const (
promptPrefix = "Asterisk Call Manager/"
netTimeout = 1 * time.Second // default timeout for network read/write
chanGiveup = 10 * time.Millisecond // timeout to giveup sending to a channel
)
var (
Error = errors.New("goami2")
ErrConn = fmt.Errorf("%w: net conn", Error)
ErrAMI = fmt.Errorf("%w: AMI proto", Error)
ErrEOF = fmt.Errorf("%w: terminated", Error)
)
// NewClient creates client. It is using NewClientWithContext in the background
// with a bogus context. For better context control use NewClientWithContext function.
func NewClient(conn net.Conn, username, password string) (*Client, error) {
return NewClientWithContext(context.Background(), conn, username, password)
}
// NewClientWithContext creates client with provided connection net.Conn and login into
// AMI server. It returns error if fials to login. Runs internal connection loop and
// provides AMI messages via AllMessages and error via Err methods.
func NewClientWithContext(ctx context.Context, conn net.Conn, username, password string) (*Client, error) {
cl := makeClient(conn)
if err := cl.login(username, password); err != nil {
return nil, err
}
go cl.loop(ctx)
return cl, nil
}