Documentation
¶
Overview ¶
Teleport是一款适用于分布式系统的高并发API框架,它采用socket全双工通信,实现S/C对等工作,支持长、短两种连接模式,支持断开后自动连接与手动断开连接,内部数据传输格式为JSON。
Index ¶
- Constants
- func BytesToInt(b []byte) int
- func HashString(encode string) uint64
- func IntToBytes(n int) []byte
- func MakeHash(s string) string
- func MakeMd5(obj interface{}, length int) string
- func MakeUnique(obj interface{}) string
- type API
- type Connect
- type Handle
- type NetData
- type Protocol
- type TP
- func (self *TP) Client(serverAddr string, port string, isShort ...bool)
- func (self *TP) Close(nodeuid ...string)
- func (self *TP) CountNodes() int
- func (self *TP) GetMode() int
- func (self *TP) Request(body interface{}, operation string, flag string, nodeuid ...string)
- func (self *TP) Server(port ...string)
- func (self *TP) SetAPI(api API) Teleport
- func (self *TP) SetApiRChan(length int) Teleport
- func (self *TP) SetConnBuffer(length int) Teleport
- func (self *TP) SetConnWChan(length int) Teleport
- func (self *TP) SetPackHeader(header string) Teleport
- func (self *TP) SetTimeout(long time.Duration) Teleport
- func (self *TP) SetUID(mine string, server ...string) Teleport
- type Teleport
Constants ¶
View Source
const ( // 返回成功 SUCCESS = 0 // 返回失败 FAILURE = -1 // 返回非法请求 LLLEGAL = -2 )
View Source
const ( SERVER = iota + 1 CLIENT )
mode
View Source
const ( // 身份登记 IDENTITY = "+identity+" // 心跳操作符 HEARTBEAT = "+heartbeat+" // 默认包头 DEFAULT_PACK_HEADER = "henrylee2cn" // SERVER默认UID DEFAULT_SERVER_UID = "server" // 默认端口 DEFAULT_PORT = ":8080" // 服务器默认心跳间隔时长 DEFAULT_TIMEOUT_S = 20e9 // 客户端默认心跳间隔时长 DEFAULT_TIMEOUT_C = 15e9 // 等待连接的轮询时长 LOOP_TIMEOUT = 1e9 )
API中定义操作时必须保留的字段
View Source
const ( // 支持数据最大长度为 2 << 61 // DataLengthOfLenth = 8 // 支持数据最大长度为 2 << 30 DataLengthOfLenth = 4 )
Variables ¶
This section is empty.
Functions ¶
func HashString ¶
Types ¶
type Connect ¶
type Connect struct {
// 标准包conn接口实例,继承该接口所有方法
net.Conn
// 标记连接是否有效
Usable bool
// 是否为短链接模式
Short bool
// 专用写入数据缓存通道
WriteChan chan *NetData
// 从连接循环接收数据
Buffer []byte
// 临时缓冲区,用来存储被截断的数据
TmpBuffer []byte
}
封装连接
func NewConnect ¶
创建Connect实例,默认为长连接(Short=false)
type NetData ¶
type NetData struct {
// 消息体
Body interface{}
// 操作代号
Operation string
// 发信节点uid
From string
// 收信节点uid
To string
// 返回状态
Status int
// 标识符
Flag string
}
定义数据传输结构
func NewNetData ¶
func ReturnData ¶
***********************************************常用函数*************************************************** \\ API中生成返回结果的方法 OpAndToAndFrom[0]参数为空时,系统将指定与对端相同的操作符 OpAndToAndFrom[1]参数为空时,系统将指定与对端为接收者 OpAndToAndFrom[2]参数为空时,系统将指定自身为发送者
type Protocol ¶
type Protocol struct {
// contains filtered or unexported fields
}
通讯协议处理,主要处理封包和解包的过程
func NewProtocol ¶
type TP ¶
type TP struct {
// 粘包处理
*Protocol
// contains filtered or unexported fields
}
func (*TP) SetPackHeader ¶
设置包头字符串,默认为henrylee2cn
type Teleport ¶
type Teleport interface {
// *以服务器模式运行,端口默认为常量DEFAULT_PORT
Server(port ...string)
// *以客户端模式运行,port为空时默认等于常量DEFAULT_PORT
Client(serverAddr string, port string, isShort ...bool)
// *主动推送信息,不写nodeuid默认随机发送给一个节点
Request(body interface{}, operation string, flag string, nodeuid ...string)
// 指定自定义的应用程序API
SetAPI(api API) Teleport
// 断开连接,参数为空则断开所有连接,服务器模式下还将停止监听
Close(nodeuid ...string)
// 设置唯一标识符,mine为本节点UID(默认ip:port)
// server为服务器UID(默认为常量DEFAULT_SERVER_UID,此参数仅客户端模式下有用)
// 可不调用该方法,此时UID均为默认
SetUID(mine string, server ...string) Teleport
// 设置包头字符串,默认为henrylee2cn
SetPackHeader(string) Teleport
// 设置指定API处理的数据的接收缓存通道长度
SetApiRChan(int) Teleport
// 设置每个连接对象的发送缓存通道长度
SetConnWChan(int) Teleport
// 设置每个连接对象的接收缓冲区大小
SetConnBuffer(int) Teleport
// 设置连接超时(心跳频率)
SetTimeout(time.Duration) Teleport
// 返回运行模式
GetMode() int
// 返回当前有效连接节点数
CountNodes() int
}
Source Files
¶
Click to show internal directories.
Click to hide internal directories.

