-
Notifications
You must be signed in to change notification settings - Fork 458
/
basic_http_sub_session.go
140 lines (114 loc) 路 4.11 KB
/
basic_http_sub_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
// Copyright 2019, Chef. All rights reserved.
// https://github.com/q191201771/lal
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package base
import (
"net"
"strings"
"github.com/q191201771/naza/pkg/connection"
)
type BasicHttpSubSession struct {
BasicHttpSubSessionOption
suffix string
conn connection.Connection
sessionStat BasicSessionStat
}
type BasicHttpSubSessionOption struct {
Conn net.Conn
ConnModOption connection.ModOption
SessionType SessionType
UrlCtx UrlContext
IsWebSocket bool
WebSocketKey string
}
func NewBasicHttpSubSession(option BasicHttpSubSessionOption) *BasicHttpSubSession {
s := &BasicHttpSubSession{
BasicHttpSubSessionOption: option,
conn: connection.New(option.Conn, option.ConnModOption),
sessionStat: NewBasicSessionStat(option.SessionType, option.Conn.RemoteAddr().String()),
}
return s
}
// ---------------------------------------------------------------------------------------------------------------------
// IServerSessionLifecycle interface
// ---------------------------------------------------------------------------------------------------------------------
func (session *BasicHttpSubSession) RunLoop() error {
buf := make([]byte, 128)
_, err := session.conn.Read(buf)
return err
}
func (session *BasicHttpSubSession) Dispose() error {
return session.conn.Close()
}
// ---------------------------------------------------------------------------------------------------------------------
func (session *BasicHttpSubSession) WriteHttpResponseHeader(b []byte) {
if session.IsWebSocket {
session.write(UpdateWebSocketHeader(session.WebSocketKey, ""))
} else {
session.write(b)
}
}
func (session *BasicHttpSubSession) Write(b []byte) {
if session.IsWebSocket {
wsHeader := WsHeader{
Fin: true,
Rsv1: false,
Rsv2: false,
Rsv3: false,
Opcode: Wso_Binary,
PayloadLength: uint64(len(b)),
Masked: false,
}
session.write(MakeWsFrameHeader(wsHeader))
}
session.write(b)
}
// ---------------------------------------------------------------------------------------------------------------------
// IObject interface
// ---------------------------------------------------------------------------------------------------------------------
func (session *BasicHttpSubSession) UniqueKey() string {
return session.sessionStat.UniqueKey()
}
// ---------------------------------------------------------------------------------------------------------------------
// ISessionUrlContext interface
// ---------------------------------------------------------------------------------------------------------------------
func (session *BasicHttpSubSession) Url() string {
return session.UrlCtx.Url
}
func (session *BasicHttpSubSession) AppName() string {
return session.UrlCtx.PathWithoutLastItem
}
func (session *BasicHttpSubSession) StreamName() string {
var suffix string
switch session.SessionType {
case SessionTypeFlvSub:
suffix = ".flv"
case SessionTypeTsSub:
suffix = ".ts"
default:
Log.Warnf("[%s] acquire stream name but protocol unknown.", session.UniqueKey())
}
return strings.TrimSuffix(session.UrlCtx.LastItemOfPath, suffix)
}
func (session *BasicHttpSubSession) RawQuery() string {
return session.UrlCtx.RawQuery
}
// ----- ISessionStat --------------------------------------------------------------------------------------------------
func (session *BasicHttpSubSession) GetStat() StatSession {
return session.sessionStat.GetStat()
}
func (session *BasicHttpSubSession) UpdateStat(intervalSec uint32) {
session.sessionStat.UpdateStatWitchConn(session.conn, intervalSec)
}
func (session *BasicHttpSubSession) IsAlive() (readAlive, writeAlive bool) {
return session.sessionStat.IsAliveWitchConn(session.conn)
}
// ---------------------------------------------------------------------------------------------------------------------
func (session *BasicHttpSubSession) write(b []byte) {
// TODO(chef) handle write error
_, _ = session.conn.Write(b)
}