-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
139 lines (129 loc) · 3.03 KB
/
conn.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
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
package smpp
import (
"context"
"io"
"math/rand"
"net"
"time"
. "github.com/NiceLabs/go-smpp/pdu"
)
type Conn struct {
parent net.Conn
ctx context.Context
cancel context.CancelFunc
receiveQueue chan interface{}
pending map[int32]func(interface{})
NextSequence func() int32
ReadTimeout time.Duration
WriteTimeout time.Duration
}
func NewConn(ctx context.Context, parent net.Conn) *Conn {
ctx, cancel := context.WithCancel(ctx)
return &Conn{
parent: parent,
ctx: ctx,
cancel: cancel,
receiveQueue: make(chan interface{}),
pending: make(map[int32]func(interface{})),
NextSequence: rand.Int31,
ReadTimeout: time.Minute * 15,
WriteTimeout: time.Minute * 15,
}
}
//goland:noinspection SpellCheckingInspection
func (c *Conn) Watch() {
defer c.cancel()
var err error
var packet interface{}
for {
select {
case <-c.ctx.Done():
return
default:
}
if c.ReadTimeout > 0 {
_ = c.parent.SetReadDeadline(time.Now().Add(c.ReadTimeout))
}
if packet, err = ReadPDU(c.parent); err == io.EOF {
return
} else if status, ok := err.(CommandStatus); err != nil {
if !ok {
status = ErrUnknownError
}
sequence := ReadSequence(packet)
_ = c.Send(&GenericNACK{
Header: Header{CommandStatus: status, Sequence: sequence},
Tags: Tags{0xFFFF: []byte(err.Error())},
})
continue
} else if callback, ok := c.pending[ReadSequence(packet)]; ok {
callback(packet)
} else {
c.receiveQueue <- packet
}
}
}
func (c *Conn) Submit(ctx context.Context, packet Responsable) (resp interface{}, err error) {
sequence := c.NextSequence()
WriteSequence(packet, sequence)
if err = c.Send(packet); err != nil {
return
}
returns := make(chan interface{}, 1)
c.pending[sequence] = func(resp interface{}) { returns <- resp }
defer delete(c.pending, sequence)
select {
case <-c.ctx.Done():
err = ErrConnectionClosed
case <-ctx.Done():
err = ctx.Err()
case resp = <-returns:
}
return
}
func (c *Conn) Send(packet interface{}) (err error) {
sequence := ReadSequence(packet)
if sequence == 0 || sequence < 0 {
err = ErrInvalidSequence
return
}
if c.WriteTimeout > 0 {
err = c.parent.SetWriteDeadline(time.Now().Add(c.WriteTimeout))
}
if err == nil {
_, err = Marshal(c.parent, packet)
}
if err == io.EOF {
err = ErrConnectionClosed
}
return
}
func (c *Conn) EnquireLink(tick time.Duration, timeout time.Duration) {
ticker := time.NewTicker(tick)
defer ticker.Stop()
sendEnquireLink := func() {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if _, err := c.Submit(ctx, new(EnquireLink)); err != nil {
ticker.Stop()
_ = c.Close()
}
}
for {
sendEnquireLink()
<-ticker.C
}
}
func (c *Conn) Close() (err error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
defer c.cancel()
if _, err = c.Submit(ctx, new(Unbind)); err == nil {
close(c.receiveQueue)
err = c.parent.Close()
}
return
}
func (c *Conn) PDU() <-chan interface{} {
return c.receiveQueue
}