transaction

package
v1.3.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2024 License: GPL-3.0 Imports: 12 Imported by: 0

README

事务

transaction事务是SIP的基本组成部分。 一个事务是客户发送的一个请求事务(通过通讯层)发送到一个服务器事务,连同服务器事务的所有的该请求的应答发送回客户端事务。 事务层处理应用服务层的重发,匹配请求的应答,以及应用服务层的超时。 任何一个用户代理客户端(user agent client UAC)完成的事情都是由一组事务构成的。 用户代理包含一个事务层,来实现有状态的代理服务器。 事务层包含一个客户元素(可以认为是一个客户事务)和一个服务器元素(可以认为是一个服务器事务),他们都可以用一个有限状态机来处理特定的请求。

在状态机层面,事务分为ict、ist、nict、nist四种。 但底层事务方面,仅根据 method 等信息,分支处理。

关于事务管理

事务在map里面管理,事务ID的选择是要和事务匹配相关。

客户端事件的匹配

当客户端中的传输层收到响应时,它必须确定哪个客户端事务将处理该响应,以便可以进行17.1.1和17.1.2节的处理。头域 Via 字段中的branch参数用于匹配规则。 一个匹配的响应应该满足下面两个条件:
1.如果响应的头域 Via头字段中的branch参数值与创建事务的请求的头域 Via头字段中的branch参数值相同。
2.如果CSeq标头字段中的method参数与创建事务的请求的方法匹配。(由于CANCEL请求会创建新的事务,但共享相同的branch参数值。所以仅用branch参数是不够的)


服务端事务匹配

首先要检查请求中的Via头域的branch参数。如果他以”z9hG4bk”开头,那么这个请求一定是由客户端事务根据本规范产生的。因此,branch参数在该客户端发出的所有的事务中都是唯一的。根据下列规则我们可以判定请求是否和事务匹配:

1、 请求的Via头域的branch参数和创建本事务的请求的Via头域的branch参数一样,并且:
2、 请求的Via头域的send-by参数和创建本事务的请求的Via头域的send-by参数一样(可能存在来自不同客户端的branch参数的意外或恶意重复,所以将 send-by 值用作匹配的一部分),并且:
3、 请求的方法与创建事务的方法匹配,但ACK除外,在ACK中,创建事务的请求的方法为INVITE。

所以,根据匹配规则,事务的ID 使用 branch,然后在匹配逻辑里面,再做条件判断。而因为branch可能会重复,所以如果使用map来简化transaction的管理,key的取值应该:

type key
客户端事务: branch+method
服务端事务: branch + sendby + method,method中ack还要除外。所以只能用branch + sendby

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorSyntax  = errors.New("message syntax error")
	ErrorCheck   = errors.New("message check failed")
	ErrorParse   = errors.New("message parse failed")
	ErrorUnknown = errors.New("message unknown")
)

transaction 的错误定义

Functions

func GetTXKey

func GetTXKey(msg *sip.Message) (key string)

Types

type Config

type Config struct {
	//sip服务器的配置
	SipNetwork string //传输协议,默认UDP,可选TCP
	SipIP      string //sip 服务器公网IP
	SipPort    uint16 //sip 服务器端口,默认 5060
	Serial     string //sip 服务器 id, 默认 34020000002000000001
	Realm      string //sip 服务器域,默认 3402000000
	Username   string //sip 服务器账号
	Password   string //sip 服务器密码
	LocPort    uint16 //sip 本地端口,默认 15060
	LocIp      string //sip 本地IP

	AckTimeout        uint16 //sip 服务应答超时,单位秒
	RegisterValidity  int    //注册有效期,单位秒,默认 3600
	RegisterInterval  int    //注册间隔,单位秒,默认 60
	HeartbeatInterval int    //心跳间隔,单位秒,默认 60
	HeartbeatRetry    int    //心跳超时次数,默认 3
}

type Core

type Core struct {
	*Config    //sip server配置信息
	OnRegister func(msg *Request, tx *GBTx)
	OnMessage  func(msg *Request, tx *GBTx)
	// contains filtered or unexported fields
}

Core: transactions manager 管理所有 transactions,以及相关全局参数、运行状态机

func NewCliCore added in v1.1.0

func NewCliCore(config *Config) (*Core, error)

初始化一个 Core,需要能响应请求,也要能发起请求 client 发起请求 server 响应请求 TODO:根据角色,增加相关配置信息 TODO:通过context管理子线程 TODO:单元测试

func NewCore

func NewCore(config *Config) *Core

初始化一个 Core,需要能响应请求,也要能发起请求 client 发起请求 server 响应请求 TODO:根据角色,增加相关配置信息 TODO:通过context管理子线程 TODO:单元测试

func (*Core) Close added in v1.1.2

func (c *Core) Close()

func (*Core) GetLocalAddr added in v1.1.2

func (c *Core) GetLocalAddr() string

func (*Core) GetLocalIp added in v1.2.0

func (c *Core) GetLocalIp() string

func (*Core) GetTX

func (c *Core) GetTX(key string) *GBTx

func (*Core) HandleReceiveMessage

func (c *Core) HandleReceiveMessage(p *transport.Packet) (err error)

接收到的消息处理 收到消息有两种:1、请求消息 2、响应消息 请求消息则直接响应处理。 响应消息则需要匹配到请求,让请求的transaction来处理。 TODO:参考srs和osip的流程,以及文档,做最终处理。需要将逻辑分成两层:TU 层和 transaction 层

func (*Core) MustTX

func (c *Core) MustTX(key string) *GBTx

func (*Core) NewTX

func (c *Core) NewTX(key string) *GBTx

func (*Core) RegistHandler

func (c *Core) RegistHandler(method Method, handler RequestHandler)

RegistHandler RegistHandler

func (*Core) Request

func (c *Core) Request(req *Request) (*GBTx, error)

Request Request

func (*Core) Respond

func (c *Core) Respond(resp *Response) (*GBTx, error)

Request Request

func (*Core) SipRequestForResponse

func (c *Core) SipRequestForResponse(req *Request) (response *Response, err error)

func (*Core) StartAndWait

func (c *Core) StartAndWait() error

type GBTx

type GBTx struct {
	*Core
	// contains filtered or unexported fields
}

GBTx Gb28181 Transaction

func NewTransaction

func NewTransaction(key string, conn Connection) *GBTx

NewTransaction create a new GBtx

func (*GBTx) Close

func (tx *GBTx) Close()

Close the Close function closes the GBTx

func (*GBTx) Conn added in v1.1.1

func (tx *GBTx) Conn() Connection

func (*GBTx) GetResponse

func (tx *GBTx) GetResponse() *sip.Response

GetResponse GetResponse

func (*GBTx) Key

func (tx *GBTx) Key() string

Key returns the GBTx Key

func (*GBTx) ReceiveResponse

func (tx *GBTx) ReceiveResponse(msg *sip.Response)

ReceiveResponse receive a Response

func (*GBTx) Request

func (tx *GBTx) Request(req *sip.Request) error

Request Request

func (*GBTx) Respond

func (tx *GBTx) Respond(res *sip.Response) error

Respond Respond

func (*GBTx) SipRequestForResponse

func (tx *GBTx) SipRequestForResponse(req *sip.Request) (response *sip.Response, err error)

func (*GBTx) SipResponse

func (tx *GBTx) SipResponse() (*sip.Response, error)

type GBTxs

type GBTxs struct {
	Txs map[string]*GBTx
	RWM *sync.RWMutex
}

GBTxs a GBTxs stands for a Gb28181 Transaction collection

var ActiveTX *GBTxs

func (*GBTxs) GetTX

func (txs *GBTxs) GetTX(key string) *GBTx

func (*GBTxs) NewTX

func (txs *GBTxs) NewTX(key string, conn Connection) *GBTx

type MediaConfig added in v1.0.6

type MediaConfig struct {
	//媒体服务器配置
	MediaIP           string //媒体服务器地址
	MediaPort         uint16 //媒体服务器端口
	MediaPortMin      uint16
	MediaPortMax      uint16
	MediaIdleTimeout  uint16 //推流超时时间,超过则断开链接,让设备重连
	AudioEnable       bool   //是否开启音频
	WaitKeyFrame      bool   //是否等待关键帧,如果等待,则在收到第一个关键帧之前,忽略所有媒体流
	RemoveBanInterval int    //移除禁止设备间隔
	UdpCacheSize      int    //udp缓存大小
}

type RequestHandler

type RequestHandler func(req *Request, tx *GBTx)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL