-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.go
215 lines (179 loc) · 4.54 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
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
package elio
import (
"errors"
"fmt"
"net"
"reflect"
"go.uber.org/atomic"
)
const invalidFd = -1
// Session session
type Session struct {
refCount atomic.Int32 // reference counting
fd int // file descriptor
sa Sockaddr // remote socket address
conn net.Conn
context interface{}
stats interface{}
io *Io
addrLocal net.Addr // local addre
addrRemote net.Addr // remote addr
ipRemote string
buffer []byte
bufferKeep *Buffer
outQueue SafeSlice
}
// NewSession new session
func NewSession(f int, c net.Conn, i *Io) *Session {
n := new(Session) //poolSession.Get().(*Session) //
if nil != n {
AppDebug().Str(LogSession, n.String()).Msg("new session")
n.fd = f
n.conn = c
n.io = i
n.buffer = make([]byte, i.Config.ReadBufferLen)
n.bufferKeep = &Buffer{}
n.ipRemote, _ = n.GetAddr()
n.IncRef()
return n
}
return n
}
// DeleteSession delete session
func DeleteSession(n *Session) {
AppDebug().Str(LogSession, n.String()).Msg("delete session")
//n.init()
//poolSession.Put(n)
}
// GetFdFromLisener this function is linux only
func GetFdFromLisener(listener net.Listener) int {
fdValue := reflect.Indirect(reflect.Indirect(reflect.ValueOf(listener)).FieldByName("fd"))
//return uintptr(fdValue.FieldByName("sysfd").Int())
return int(fdValue.FieldByName("sysfd").Int())
}
// GetFdFromConn this function is linux only
func GetFdFromConn(conn net.Conn) int {
//tls := reflect.TypeOf(conn.UnderlyingConn()) == reflect.TypeOf(&tls.Conn{})
// Extract the file descriptor associated with the connection
//connVal := reflect.Indirect(reflect.ValueOf(conn)).FieldByName("conn").Elem()
tcpConn := reflect.Indirect(reflect.ValueOf(conn)).FieldByName("conn")
//if tls {
// tcpConn = reflect.Indirect(tcpConn.Elem())
//}
fdVal := tcpConn.FieldByName("fd")
pfdVal := reflect.Indirect(fdVal).FieldByName("pfd")
return int(pfdVal.FieldByName("sysfd").Int())
}
// String ...
func (n *Session) String() string {
return fmt.Sprintf("Session::%p;%d;%v", n, n.GetFd(), n.GetRemoteIP())
}
// init init
func (n *Session) init() {
n.fd = 0
n.conn = nil
n.context = nil
n.stats = nil
n.io = nil
//n.buffer = nil
n.bufferKeep.Clear(-1)
n.addrLocal = nil
n.addrRemote = nil
n.ipRemote = ""
n.bufferKeep = nil
//_ = n.outQueue.Fetch()
}
// IncRef increase reference count
func (n *Session) IncRef() int32 {
return n.refCount.Inc()
}
// SubRef subscribe reference count
func (n *Session) SubRef(ref int32) int32 {
r := n.refCount.Sub(ref)
if 0 == r {
DeleteSession(n)
}
return r
}
// DecRef decrease reference count
func (n *Session) DecRef() int32 {
r := n.refCount.Dec()
if 0 == r {
DeleteSession(n)
}
return r
}
// GetFd get fd
func (n *Session) GetFd() int {
return n.fd
}
// GetConn get conn
func (n *Session) GetConn() net.Conn {
return n.conn
}
// GetService get service
func (n *Session) GetIo() *Io {
return n.io
}
// GetContext get context
func (n *Session) GetContext() interface{} {
return n.context
}
// SetContext set context
func (n *Session) SetContext(c interface{}) {
n.context = c
}
// GetStats get stats
func (n *Session) GetStats() interface{} {
return n.stats
}
// SetStats set stats
func (n *Session) SetStats(s interface{}) {
n.stats = s
}
// GetRemoteIP get remote ip
func (n *Session) GetRemoteIP() string {
return n.ipRemote
}
// GetKeepBuffer get keep buffer
func (n *Session) GetKeepBuffer() *Buffer {
return n.bufferKeep
}
// Close close
func (n *Session) Close() error {
return n.io.ioModel.Close(n)
}
// Shutdown shutdown
func (n *Session) Shutdown(how int) error {
return n.io.ioModel.Shutdown(n, how)
}
// Write write
func (n *Session) Write(out []byte) (sent int, err error) {
defer func() {
if r := recover(); r != nil {
AppError().Str(LogObject, n.String()).
Msgf("panic in session.Write fd:%d with recover:%s", n.fd, r)
sent = -1
err = errors.New("elf.net failed to session.Write with panic")
}
}()
return n.io.ioModel.Write(n, out)
}
// PostWrite post write
func (n *Session) PostWrite(out []byte) (sent int, err error) {
return n.io.ioModel.PostWrite(n, out)
}
// CountOutQueue count out queue
func (n *Session) CountOutQueue() int {
return n.outQueue.Count()
}
// set keep alive
// func SetKeepAlive(fd, secs int) error {
// if err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_KEEPALIVE, 1); err != nil {
// return err
// }
// if err := unix.SetsockoptInt(fd, unix.IPPROTO_TCP, unix.TCP_KEEPINTVL, secs); err != nil {
// return err
// }
// return unix.SetsockoptInt(fd, unix.IPPROTO_TCP, unix.TCP_KEEPIDLE, secs)
// }