tcp

package
v0.0.0-...-68bdf18 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2018 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeartbeatPeriod = 30  //心跳周期,30s
	LostThreshold   = 150 //失联阈值,150s
)

Variables

View Source
var HeartbeatReqId uint = 666
View Source
var HeartbeatRspId uint = 777

Functions

func CreateSession

func CreateSession(server *Server, c *net.TCPConn)

创建会话

func HeartbeatReqHandler

func HeartbeatReqHandler(session *Session, _ proto.Message)

收到HeartBeatReqId的回调函数: 回复心跳

func HeartbeatRspHandler

func HeartbeatRspHandler(session *Session, _ proto.Message)

收到HeartbeatRspId的回调函数: 记录时间

Types

type ActiveList

type ActiveList struct {
	// contains filtered or unexported fields
}

func CreateActiveList

func CreateActiveList() *ActiveList

func (*ActiveList) Activate

func (list *ActiveList) Activate(s *Session)

收到心跳回包,激活连接: 更新收包时间

func (*ActiveList) AddSession

func (list *ActiveList) AddSession(s *Session)

新建连接就放入此集合

func (*ActiveList) HeartbeatProbe

func (list *ActiveList) HeartbeatProbe()

获取需要再次发送心跳的连接而发心跳、认为已经不再活跃的连接而回收连接

func (*ActiveList) RemoveSession

func (list *ActiveList) RemoveSession(s *Session)

连接关闭则从此集合中删除

type MsgHandler

type MsgHandler func(*Session, proto.Message)

遇到某消息ID的回调函数类型

type PacketFactory

type PacketFactory func(uint) proto.Message

消息ID与消息PB的映射函数类型,根据消息ID给出对应的PB包

type SendTask

type SendTask struct {
	// contains filtered or unexported fields
}

待发送数据

type Server

type Server struct {
	Name                string
	Ip                  string
	Port                int
	MaxConnectionNumber uint32         //最大连接个数
	NumberOfConnections int32          //目前连接个数,hold by atomic
	SubscribeBook       *SubscribeList //被订阅列表
	// contains filtered or unexported fields
}

func CreateServer

func CreateServer(serviceName string, ip string, port int, maxConnectionNumber uint32) (*Server, error)

https://www.cnblogs.com/concurrency/p/4043271.html 创建TCP服务 参数:服务名,IP,PORT,最大连接个数

func (*Server) EnableHeartbeat

func (server *Server) EnableHeartbeat()

打开发心跳机制

func (*Server) RegHandler

func (server *Server) RegHandler(cmdId uint, handler MsgHandler)

设置消息ID对应的回调

func (*Server) SetFactory

func (server *Server) SetFactory(f PacketFactory)

设置消息ID与消息PB的映射函数

func (*Server) Start

func (server *Server) Start() error

启动服务

type Session

type Session struct {
	HeartBeatReqTs int64 //上次发送心跳请求的时间戳
	HeartBeatRspTs int64 //上次收到心跳回复的时间戳
	// contains filtered or unexported fields
}

func (*Session) Addr

func (session *Session) Addr() string

func (*Session) Close

func (session *Session) Close()

关闭连接

func (*Session) IsClosed

func (session *Session) IsClosed() bool

连接是否存活

func (*Session) SendData

func (session *Session) SendData(cmdId uint, pb proto.Message) bool

发送消息

func (*Session) Subscribe

func (session *Session) Subscribe(infoKey interface{})

此会话订阅了某信息

type SubscribeList

type SubscribeList struct {
	// contains filtered or unexported fields
}

被订阅列表结构

func CreateSubscribeList

func CreateSubscribeList() *SubscribeList

创建订阅列表

func (*SubscribeList) GetSubscribers

func (sl *SubscribeList) GetSubscribers(infoKey interface{}) []*Session

获取订阅了key的连接们

func (*SubscribeList) Subscribe

func (sl *SubscribeList) Subscribe(infoKey interface{}, session *Session)

key被连接session所订阅

func (*SubscribeList) UnSubscribe

func (sl *SubscribeList) UnSubscribe(infoKey interface{}, session *Session)

连接session不再订阅key

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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