forked from qaz9877/go-smpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
136 lines (126 loc) · 2.94 KB
/
session.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
package smpp
import (
"context"
"io"
"math/rand"
"net"
"sync"
"time"
"github.com/M2MGateway/go-smpp/pdu"
)
type Session struct {
parent net.Conn
receiveQueue chan any
pending map[int32]func(any)
pendingLock sync.Mutex
NextSequence func() int32
ReadTimeout time.Duration
WriteTimeout time.Duration
}
func NewSession(ctx context.Context, parent net.Conn) (session *Session) {
random := rand.New(rand.NewSource(time.Now().Unix()))
session = &Session{
parent: parent,
receiveQueue: make(chan any),
pending: make(map[int32]func(any)),
NextSequence: random.Int31,
ReadTimeout: time.Minute * 15,
WriteTimeout: time.Minute * 15,
}
go session.watch(ctx)
return
}
//goland:noinspection SpellCheckingInspection
func (c *Session) watch(ctx context.Context) {
var err error
var packet any
for {
select {
case <-ctx.Done():
return
default:
}
if c.ReadTimeout > 0 {
_ = c.parent.SetReadDeadline(time.Now().Add(c.ReadTimeout))
}
if packet, err = pdu.Unmarshal(c.parent); err == io.EOF {
return
}
if packet == nil {
continue
}
if status, ok := err.(pdu.CommandStatus); ok {
_ = c.Send(&pdu.GenericNACK{
Header: pdu.Header{CommandStatus: status, Sequence: pdu.ReadSequence(packet)},
Tags: pdu.Tags{0xFFFF: []byte(err.Error())},
})
continue
}
if callback, ok := c.pending[pdu.ReadSequence(packet)]; ok {
callback(packet)
} else {
c.receiveQueue <- packet
}
}
}
func (c *Session) Submit(ctx context.Context, packet pdu.Responsable) (resp any, err error) {
c.pendingLock.Lock()
defer c.pendingLock.Unlock()
sequence := c.NextSequence()
pdu.WriteSequence(packet, sequence)
if err = c.Send(packet); err != nil {
return
}
returns := make(chan any, 1)
c.pending[sequence] = func(resp any) { returns <- resp }
defer delete(c.pending, sequence)
select {
case <-ctx.Done():
err = ErrConnectionClosed
case resp = <-returns:
}
return
}
func (c *Session) Send(packet any) (err error) {
sequence := pdu.ReadSequence(packet)
if sequence == 0 || sequence < 0 {
err = pdu.ErrInvalidSequence
return
}
if c.WriteTimeout > 0 {
err = c.parent.SetWriteDeadline(time.Now().Add(c.WriteTimeout))
}
if err == nil {
_, err = pdu.Marshal(c.parent, packet)
}
if err == io.EOF {
err = ErrConnectionClosed
}
return
}
func (c *Session) EnquireLink(ctx context.Context, tick time.Duration, timeout time.Duration) (err error) {
ticker := time.NewTicker(tick)
defer ticker.Stop()
for {
ctx, cancel := context.WithTimeout(ctx, timeout)
if _, err = c.Submit(ctx, new(pdu.EnquireLink)); err != nil {
ticker.Stop()
err = c.Close(ctx)
}
cancel()
<-ticker.C
}
}
func (c *Session) Close(ctx context.Context) (err error) {
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
_, err = c.Submit(ctx, new(pdu.Unbind))
if err != nil {
return
}
close(c.receiveQueue)
return c.parent.Close()
}
func (c *Session) PDU() <-chan any {
return c.receiveQueue
}