mirror of https://github.com/q191201771/lal.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
4.1 KiB
Go
141 lines
4.1 KiB
Go
4 years ago
|
// 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"
|
||
|
)
|
||
|
|
||
3 years ago
|
type BasicHttpSubSession struct {
|
||
|
BasicHttpSubSessionOption
|
||
4 years ago
|
|
||
3 years ago
|
suffix string
|
||
|
conn connection.Connection
|
||
|
sessionStat BasicSessionStat
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
type BasicHttpSubSessionOption struct {
|
||
4 years ago
|
Conn net.Conn
|
||
|
ConnModOption connection.ModOption
|
||
3 years ago
|
SessionType SessionType
|
||
4 years ago
|
UrlCtx UrlContext
|
||
4 years ago
|
IsWebSocket bool
|
||
|
WebSocketKey string
|
||
|
}
|
||
|
|
||
3 years ago
|
func NewBasicHttpSubSession(option BasicHttpSubSessionOption) *BasicHttpSubSession {
|
||
|
s := &BasicHttpSubSession{
|
||
|
BasicHttpSubSessionOption: option,
|
||
|
conn: connection.New(option.Conn, option.ConnModOption),
|
||
|
sessionStat: NewBasicSessionStat(option.SessionType, ""),
|
||
4 years ago
|
}
|
||
|
return s
|
||
|
}
|
||
|
|
||
4 years ago
|
// ---------------------------------------------------------------------------------------------------------------------
|
||
|
// IServerSessionLifecycle interface
|
||
|
// ---------------------------------------------------------------------------------------------------------------------
|
||
|
|
||
3 years ago
|
func (session *BasicHttpSubSession) RunLoop() error {
|
||
4 years ago
|
buf := make([]byte, 128)
|
||
|
_, err := session.conn.Read(buf)
|
||
|
return err
|
||
|
}
|
||
|
|
||
3 years ago
|
func (session *BasicHttpSubSession) Dispose() error {
|
||
4 years ago
|
return session.conn.Close()
|
||
|
}
|
||
|
|
||
|
// ---------------------------------------------------------------------------------------------------------------------
|
||
|
|
||
3 years ago
|
func (session *BasicHttpSubSession) WriteHttpResponseHeader(b []byte) {
|
||
4 years ago
|
if session.IsWebSocket {
|
||
4 years ago
|
session.write(UpdateWebSocketHeader(session.WebSocketKey))
|
||
4 years ago
|
} else {
|
||
4 years ago
|
session.write(b)
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
func (session *BasicHttpSubSession) Write(b []byte) {
|
||
4 years ago
|
if session.IsWebSocket {
|
||
4 years ago
|
wsHeader := WsHeader{
|
||
4 years ago
|
Fin: true,
|
||
|
Rsv1: false,
|
||
|
Rsv2: false,
|
||
|
Rsv3: false,
|
||
4 years ago
|
Opcode: Wso_Binary,
|
||
4 years ago
|
PayloadLength: uint64(len(b)),
|
||
|
Masked: false,
|
||
|
}
|
||
4 years ago
|
session.write(MakeWsFrameHeader(wsHeader))
|
||
4 years ago
|
}
|
||
|
session.write(b)
|
||
|
}
|
||
|
|
||
4 years ago
|
// ---------------------------------------------------------------------------------------------------------------------
|
||
|
// IObject interface
|
||
|
// ---------------------------------------------------------------------------------------------------------------------
|
||
|
|
||
3 years ago
|
func (session *BasicHttpSubSession) UniqueKey() string {
|
||
3 years ago
|
return session.sessionStat.UniqueKey()
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
// ---------------------------------------------------------------------------------------------------------------------
|
||
|
// ISessionUrlContext interface
|
||
|
// ---------------------------------------------------------------------------------------------------------------------
|
||
|
|
||
3 years ago
|
func (session *BasicHttpSubSession) Url() string {
|
||
4 years ago
|
return session.UrlCtx.Url
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
func (session *BasicHttpSubSession) AppName() string {
|
||
4 years ago
|
return session.UrlCtx.PathWithoutLastItem
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
func (session *BasicHttpSubSession) StreamName() string {
|
||
4 years ago
|
var suffix string
|
||
3 years ago
|
switch session.SessionType {
|
||
|
case SessionTypeFlvSub:
|
||
4 years ago
|
suffix = ".flv"
|
||
3 years ago
|
case SessionTypeTsSub:
|
||
4 years ago
|
suffix = ".ts"
|
||
|
default:
|
||
3 years ago
|
Log.Warnf("[%s] acquire stream name but protocol unknown.", session.UniqueKey())
|
||
4 years ago
|
}
|
||
4 years ago
|
return strings.TrimSuffix(session.UrlCtx.LastItemOfPath, suffix)
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
func (session *BasicHttpSubSession) RawQuery() string {
|
||
4 years ago
|
return session.UrlCtx.RawQuery
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
// ----- ISessionStat --------------------------------------------------------------------------------------------------
|
||
4 years ago
|
|
||
3 years ago
|
func (session *BasicHttpSubSession) GetStat() StatSession {
|
||
3 years ago
|
return session.sessionStat.GetStat()
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
func (session *BasicHttpSubSession) UpdateStat(intervalSec uint32) {
|
||
3 years ago
|
session.sessionStat.UpdateStatWitchConn(session.conn, intervalSec)
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
func (session *BasicHttpSubSession) IsAlive() (readAlive, writeAlive bool) {
|
||
3 years ago
|
return session.sessionStat.IsAliveWitchConn(session.conn)
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
// ---------------------------------------------------------------------------------------------------------------------
|
||
|
|
||
3 years ago
|
func (session *BasicHttpSubSession) write(b []byte) {
|
||
4 years ago
|
// TODO(chef) handle write error
|
||
4 years ago
|
_, _ = session.conn.Write(b)
|
||
|
}
|