Documentation ¶
Index ¶
- Constants
- Variables
- func BacklogLoad()
- func ByteToObj(data []byte, obj interface{}) error
- func CloseLog()
- func DesECBDecrypt(data, key []byte) []byte
- func DesECBEncrypt(data, key []byte) []byte
- func Error(args ...interface{})
- func ErrorF(format string, args ...interface{})
- func GzipCompress(src []byte) []byte
- func GzipDecompress(src []byte) ([]byte, error)
- func ID64() (int64, error)
- func Info(args ...interface{})
- func InfoF(format string, args ...interface{})
- func ObjToByte(obj interface{}) ([]byte, error)
- func PacketEncoder(cmd CommandCode, name, sign, secret string, data []byte) ([]byte, error)
- func SetLogFile(name string)
- func SignCheck(addr, sign string) bool
- func SignGet(addr string) string
- func SignStore(addr, sign string)
- func Version() string
- func ZlibCompress(src []byte) []byte
- func ZlibDecompress(src []byte) ([]byte, error)
- type Client
- func (c *Client) Close()
- func (c *Client) ConnectServers()
- func (c *Client) DefaultClientName()
- func (c *Client) DefaultConnectCode()
- func (c *Client) DefaultSecretKey()
- func (c *Client) Get(funcLabel string, param []byte) ([]byte, error)
- func (c *Client) GetHandleFunc(label string, f func(c *Client, param []byte) (int, []byte))
- func (c *Client) GetName() string
- func (c *Client) GetTimeOut(funcLabel string, param []byte, timeOut int) ([]byte, error)
- func (c *Client) NoticeHandleFunc(label string, f func(c *Client, data []byte))
- func (c *Client) Put(funcLabel string, data []byte)
- func (c *Client) ReplyGet(id int64, state int, data []byte)
- func (c *Client) Run()
- func (c *Client) SendBacklog()
- func (c *Client) SetClientName(name string) error
- func (c *Client) SetConnectCode(code string)
- func (c *Client) SetSecretKey(key string) error
- func (c *Client) Write(data []byte)
- type ClientConf
- type ClientConnInfo
- type ClientConnectObj
- type ClientGetFunc
- type ClientInfo
- type ClientNoticeFunc
- type CommandCode
- type GetData
- type IdWorker
- type Level
- type NoticeData
- type NoticeRetry
- type Packet
- type PutData
- type Reply
- type Servers
- func (s *Servers) ClientDiscard(name, ip string)
- func (s *Servers) DefaultConnectCode()
- func (s *Servers) DefaultSecretKey()
- func (s *Servers) DefaultServersName()
- func (s *Servers) Get(funcLabel, name string, param []byte) ([]byte, error)
- func (s *Servers) GetAtIP(funcLabel, name, ip string, param []byte) ([]byte, error)
- func (s *Servers) GetAtIPTimeOut(timeOut int, funcLabel, name, ip string, param []byte) ([]byte, error)
- func (s *Servers) GetAtNameTimeOut(timeOut int, funcLabel, name string, param []byte) ([]byte, error)
- func (s *Servers) GetClientAllName() []string
- func (s *Servers) GetClientConn(name string) (map[string]*ClientConnectObj, bool)
- func (s *Servers) GetClientConnFromIP(name, ip string) (*net.UDPAddr, bool)
- func (s *Servers) GetHandleFunc(label string, f func(s *Servers, param []byte) (int, []byte))
- func (s *Servers) GetServersName() string
- func (s *Servers) Notice(name, label string, data []byte, retryConf *NoticeRetry) (string, error)
- func (s *Servers) NoticeAll(label string, data []byte, retryConf *NoticeRetry)
- func (s *Servers) OnLineTable() map[string]*ClientConnInfo
- func (s *Servers) PutHandleFunc(label string, f func(s *Servers, c *ClientInfo, body []byte))
- func (s *Servers) ReplyGet(client *net.UDPAddr, id int64, state int, data []byte)
- func (s *Servers) ReplyPut(client *net.UDPAddr, id, state int64)
- func (s *Servers) Run()
- func (s *Servers) SetConnectCode(code string)
- func (s *Servers) SetNoticeRetry(maxRetry, retryTimer int) *NoticeRetry
- func (s *Servers) SetSecretKey(key string) error
- func (s *Servers) SetServersName(name string) error
- func (s *Servers) Write(client *net.UDPAddr, data []byte)
- type ServersConf
- type ServersGetFunc
- type ServersPutFunc
Constants ¶
View Source
const ( SignLetterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_+=~!@#$%^&*()<>{},.?~" DefaultConnectCode = "c" DefaultServersName = "servers" DefaultClientName = "client" DefaultSecretKey = "12345678" DefaultSGetTimeOut = 1000 // 单位 ms DefaultNoticeMaxRetry = 10 // 通知消息最大重试次数 DefaultNoticeRetryTimer = 100 // 重试等待时间 单位ms HeartbeatTime = 5 // 5s HeartbeatTimeLast = 6 // 6s ServersTimeWheel = 2 // 2s servers 时间轮 )
Variables ¶
View Source
var ( ErrNmeLengthAbove = fmt.Errorf("名字不能超过7个长度") ErrDataLengthAbove = fmt.Errorf("数据大于 540个字节, 建议拆分") ErrNonePacket = fmt.Errorf("空包") ErrSGetTimeOut = func(label, name, ip string) error { return fmt.Errorf("请求客户端 FuncLabel:%s | name:%s | IP:%s 超时", label, name, ip) } ErrNotFondClient = func(name string) error { return fmt.Errorf("未找到客户端 name:%s ", name) } PanicGetHandleFuncExist = func(label string) { panic(fmt.Sprintf("get handle func label:%s is exist.", label)) } PanicPutHandleFuncExist = func(label string) { panic(fmt.Sprintf("put handle func label:%s is exist.", label)) } ErrServersSecretKey = fmt.Errorf("秘钥的长度只能为8,并且与Client端统一") ErrClientNameErr = fmt.Errorf("client name 不能含特殊字符 @") ErrClientSecretKey = fmt.Errorf("秘钥的长度只能为8,并且与Servers端统一") )
err
View Source
var GetDataMap sync.Map
View Source
var LevelMap = map[Level]string{
1: "[Info] ",
4: "[Error] ",
}
View Source
var LogClose bool = true
LogClose 是否关闭日志
View Source
var NoticeDataMap sync.Map
Functions ¶
func DesECBDecrypt ¶
func DesECBEncrypt ¶
func PacketEncoder ¶
func PacketEncoder(cmd CommandCode, name, sign, secret string, data []byte) ([]byte, error)
PacketEncoder 封包
func SetLogFile ¶
func SetLogFile(name string)
Types ¶
type Client ¶
type Client struct { ServersHost string // serversIP:port Conn *net.UDPConn // 连接对象 SConn *net.UDPAddr // s端连接信息 GetHandle ClientGetFunc // get方法 NoticeHandle ClientNoticeFunc // 接收通知的方法 // contains filtered or unexported fields }
func (*Client) ConnectServers ¶
func (c *Client) ConnectServers()
ConnectServers 请求连接服务器,获取签名 内容是发送 Connect code
func (*Client) DefaultClientName ¶
func (c *Client) DefaultClientName()
func (*Client) DefaultConnectCode ¶
func (c *Client) DefaultConnectCode()
func (*Client) DefaultSecretKey ¶
func (c *Client) DefaultSecretKey()
func (*Client) GetHandleFunc ¶
func (*Client) GetTimeOut ¶
func (*Client) NoticeHandleFunc ¶
func (*Client) SetClientName ¶
func (*Client) SetConnectCode ¶
func (*Client) SetSecretKey ¶
type ClientConf ¶
func SetClientConf ¶
func SetClientConf(clientName, connectCode, secretKey string) ClientConf
type ClientConnInfo ¶
type ClientConnectObj ¶
type ClientInfo ¶
type ClientNoticeFunc ¶
type CommandCode ¶
type CommandCode uint8
const ( CommandConnect CommandCode = 0x0 // 首次连接确认身份信息 CommandPut CommandCode = 0x1 // 发送消息 CommandReply CommandCode = 0x2 // 收到回应, ackType: put, heartbeat, sign CommandHeartbeat CommandCode = 0x3 // 发送心跳 CommandNotice CommandCode = 0x4 // 下发签名 CommandGet CommandCode = 0x5 // 获取消息 )
type IdWorker ¶
type IdWorker struct {
// contains filtered or unexported fields
}
func (*IdWorker) InitIdWorker ¶
type NoticeData ¶
type NoticeRetry ¶
type Servers ¶
type Servers struct { Addr string // 地址 默认0.0.0.0 Port int // 端口 Conn *net.UDPConn // S端的UDP连接对象 CMap map[string]map[string]*ClientConnectObj // 存放客户端连接信息 map:name -> map:ipaddr -> obj PutHandle ServersPutFunc // PUT类型方法 GetHandle ServersGetFunc // GET类型方法 // contains filtered or unexported fields }
func NewServers ¶
func NewServers(addr string, port int, conf ...ServersConf) (*Servers, error)
func (*Servers) ClientDiscard ¶
func (*Servers) DefaultConnectCode ¶
func (s *Servers) DefaultConnectCode()
func (*Servers) DefaultSecretKey ¶
func (s *Servers) DefaultSecretKey()
func (*Servers) DefaultServersName ¶
func (s *Servers) DefaultServersName()
func (*Servers) GetAtIPTimeOut ¶
func (*Servers) GetAtNameTimeOut ¶
func (*Servers) GetClientAllName ¶
func (*Servers) GetClientConn ¶
func (s *Servers) GetClientConn(name string) (map[string]*ClientConnectObj, bool)
func (*Servers) GetClientConnFromIP ¶
func (*Servers) GetHandleFunc ¶
func (*Servers) GetServersName ¶
func (*Servers) NoticeAll ¶
func (s *Servers) NoticeAll(label string, data []byte, retryConf *NoticeRetry)
func (*Servers) OnLineTable ¶
func (s *Servers) OnLineTable() map[string]*ClientConnInfo
OnLineTable 获取当前客户端连接情况
func (*Servers) PutHandleFunc ¶
func (s *Servers) PutHandleFunc(label string, f func(s *Servers, c *ClientInfo, body []byte))
func (*Servers) SetConnectCode ¶
func (*Servers) SetNoticeRetry ¶
func (s *Servers) SetNoticeRetry(maxRetry, retryTimer int) *NoticeRetry
SetNoticeRetry retryTimer 单位ms
func (*Servers) SetSecretKey ¶
func (*Servers) SetServersName ¶
type ServersConf ¶
type ServersConf struct { Name string // servers端的名称 ConnectCode string // 连接code 是静态的由server端配发 SecretKey string // 数据传输加密解密秘钥 8个字节 }
func SetServersConf ¶
func SetServersConf(serversName, connectCode, secretKey string) ServersConf
type ServersPutFunc ¶
type ServersPutFunc map[string]func(s *Servers, c *ClientInfo, data []byte)
Source Files ¶
Click to show internal directories.
Click to hide internal directories.