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.
notify/server.go

370 lines
8.6 KiB
Go

5 years ago
// Package notify is a package which provide common tcp/udp/unix socket service
5 years ago
package notify
import (
5 years ago
"context"
4 years ago
"errors"
"fmt"
"math/rand"
5 years ago
"net"
5 years ago
"strings"
5 years ago
"time"
"b612.me/starainrt"
)
5 years ago
var builder *starainrt.StarQueue
5 years ago
5 years ago
func init() {
builder = starainrt.NewQueue()
}
// StarNotifyS 为Server端
type StarNotifyS struct {
// Queue 是用来处理收发信息的简单消息队列
Queue *starainrt.StarQueue
// FuncLists 记录了被通知项所记录的函数
5 years ago
FuncLists map[string]func(SMsg) string
defaultFunc func(SMsg) string
4 years ago
Connected func(SMsg) string
5 years ago
stopSign context.Context
cancel context.CancelFunc
connPool map[string]net.Conn
4 years ago
lockPool map[string]SMsg
5 years ago
udpPool map[string]*net.UDPAddr
isUDP bool
4 years ago
// Stop 停止信 号
Stop chan int
5 years ago
// UDPConn UDP监听
UDPConn *net.UDPConn
5 years ago
// Online 当前链接是否处于活跃状态
Online bool
5 years ago
// ReadDeadline tcp/unix中读超时设置,udp请直接调用UDPConn
ReadDeadline time.Time
// WriteDeadline tcp/unix中写超时设置,udp请直接调用UDPConn
WriteDeadline time.Time
// Deadline tcp/unix中超时设置,udp请直接调用UDPConn
Deadline time.Time
5 years ago
}
5 years ago
5 years ago
// SMsg 指明当前服务端被通知的关键字
type SMsg struct {
Conn net.Conn
Key string
Value string
5 years ago
UDP *net.UDPAddr
uconn *net.UDPConn
4 years ago
mode string
wait chan int
}
// GetConnPool 获取所有Client端信息
func (star *StarNotifyS) GetConnPool() []SMsg {
var result []SMsg
for _, v := range star.connPool {
result = append(result, SMsg{Conn: v, mode: "pa"})
}
for _, v := range star.udpPool {
result = append(result, SMsg{UDP: v, uconn: star.UDPConn, mode: "pa0"})
}
return result
}
func (nmsg *SMsg) addSlash(name string) string {
var key []byte
for _, v := range []byte(name) {
if v == byte(124) || v == byte(92) {
key = append(key, byte(92))
}
key = append(key, v)
}
return string(key)
5 years ago
}
5 years ago
// Reply 用于向client端回复数据
func (nmsg *SMsg) Reply(msg string) error {
5 years ago
var err error
if nmsg.uconn == nil {
4 years ago
_, err = nmsg.Conn.Write(builder.BuildMessage([]byte(nmsg.mode + "||" + nmsg.addSlash(nmsg.Key) + "||" + msg)))
5 years ago
} else {
4 years ago
_, err = nmsg.uconn.WriteToUDP(builder.BuildMessage([]byte(nmsg.mode+"||"+nmsg.addSlash(nmsg.Key)+"||"+msg)), nmsg.UDP)
5 years ago
}
5 years ago
return err
}
// Send 用于向client端发送key-value数据
func (nmsg *SMsg) Send(key, value string) error {
5 years ago
var err error
if nmsg.uconn == nil {
4 years ago
_, err = nmsg.Conn.Write(builder.BuildMessage([]byte("pa||" + nmsg.addSlash(key) + "||" + value)))
5 years ago
} else {
4 years ago
_, err = nmsg.uconn.WriteToUDP(builder.BuildMessage([]byte("pa||"+nmsg.addSlash(key)+"||"+value)), nmsg.UDP)
5 years ago
}
5 years ago
return err
}
4 years ago
// SendWait 用于向client端发送key-value数据并等待
func (star *StarNotifyS) SendWait(source SMsg, key, value string, tmout time.Duration) (SMsg, error) {
var err error
var tmceed <-chan time.Time
rand.Seed(time.Now().UnixNano())
mode := "sr" + fmt.Sprintf("%05d", rand.Intn(99999))
if source.uconn == nil {
_, err = source.Conn.Write(builder.BuildMessage([]byte(mode + "||" + source.addSlash(key) + "||" + value)))
} else {
_, err = source.uconn.WriteToUDP(builder.BuildMessage([]byte(mode+"||"+source.addSlash(key)+"||"+value)), source.UDP)
}
if err != nil {
return SMsg{}, err
}
if int64(tmout) > 0 {
tmceed = time.After(tmout)
}
source.wait = make(chan int, 2)
star.lockPool[mode] = source
select {
case <-source.wait:
res := star.lockPool[mode]
delete(star.lockPool, mode)
return res, nil
case <-tmceed:
return SMsg{}, errors.New("Time Exceed")
}
}
5 years ago
func (star *StarNotifyS) starinits() {
5 years ago
star.stopSign, star.cancel = context.WithCancel(context.Background())
5 years ago
star.Queue = starainrt.NewQueue()
5 years ago
star.udpPool = make(map[string]*net.UDPAddr)
5 years ago
star.FuncLists = make(map[string]func(SMsg) string)
star.connPool = make(map[string]net.Conn)
4 years ago
star.lockPool = make(map[string]SMsg)
star.Stop = make(chan int, 5)
5 years ago
star.Online = false
5 years ago
star.Queue.RestoreDuration(time.Second * 2)
5 years ago
}
5 years ago
// NewNotifyS 开启一个新的Server端通知
5 years ago
func NewNotifyS(netype, value string) (*StarNotifyS, error) {
5 years ago
if netype[0:3] != "udp" {
return notudps(netype, value)
}
return doudps(netype, value)
}
func doudps(netype, value string) (*StarNotifyS, error) {
5 years ago
var star StarNotifyS
star.starinits()
5 years ago
star.isUDP = true
udpaddr, err := net.ResolveUDPAddr(netype, value)
if err != nil {
return nil, err
}
star.UDPConn, err = net.ListenUDP(netype, udpaddr)
if err != nil {
return nil, err
}
go star.notify()
5 years ago
go func() {
5 years ago
<-star.stopSign.Done()
5 years ago
for k, v := range star.udpPool {
5 years ago
star.UDPConn.WriteToUDP(star.Queue.BuildMessage([]byte("b612ryzstop")), v)
5 years ago
delete(star.connPool, k)
}
star.UDPConn.Close()
star.Online = false
return
}()
5 years ago
go func() {
for {
5 years ago
buf := make([]byte, 8192)
n, addr, err := star.UDPConn.ReadFromUDP(buf)
if n != 0 {
star.Queue.ParseMessage(buf[0:n], addr)
4 years ago
if _, ok := star.udpPool[addr.String()]; !ok {
if star.Connected != nil {
go star.Connected(SMsg{UDP: addr, uconn: star.UDPConn})
}
}
5 years ago
star.udpPool[addr.String()] = addr
}
if err != nil {
continue
5 years ago
}
}
}()
5 years ago
star.Online = true
5 years ago
return &star, nil
}
func notudps(netype, value string) (*StarNotifyS, error) {
var star StarNotifyS
star.starinits()
star.isUDP = false
5 years ago
listener, err := net.Listen(netype, value)
5 years ago
if err != nil {
return nil, err
}
go star.notify()
5 years ago
go func() {
5 years ago
<-star.stopSign.Done()
5 years ago
for k, v := range star.connPool {
v.Close()
delete(star.connPool, k)
}
listener.Close()
star.Online = false
return
}()
5 years ago
go func() {
for {
conn, err := listener.Accept()
if err != nil {
5 years ago
select {
5 years ago
case <-star.stopSign.Done():
5 years ago
listener.Close()
5 years ago
return
5 years ago
default:
continue
}
}
5 years ago
if !star.ReadDeadline.IsZero() {
conn.SetReadDeadline(star.ReadDeadline)
}
if !star.WriteDeadline.IsZero() {
conn.SetWriteDeadline(star.WriteDeadline)
}
if !star.Deadline.IsZero() {
conn.SetDeadline(star.Deadline)
}
5 years ago
go func(conn net.Conn) {
for {
buf := make([]byte, 8192)
n, err := conn.Read(buf)
if n != 0 {
star.Queue.ParseMessage(buf[0:n], conn)
}
if err != nil {
conn.Close()
delete(star.connPool, conn.RemoteAddr().String())
break
}
}
}(conn)
star.connPool[conn.RemoteAddr().String()] = conn
4 years ago
if star.Connected != nil {
go star.Connected(SMsg{Conn: conn})
}
5 years ago
}
}()
5 years ago
star.Online = true
5 years ago
return &star, nil
5 years ago
}
// SetNotify 用于设置通知关键词的调用函数
5 years ago
func (star *StarNotifyS) SetNotify(name string, data func(SMsg) string) {
5 years ago
star.FuncLists[name] = data
5 years ago
}
5 years ago
// SetDefaultNotify 用于设置默认关键词的调用函数
4 years ago
func (star *StarNotifyS) SetDefaultNotify(data func(SMsg) string) {
5 years ago
star.defaultFunc = data
5 years ago
}
4 years ago
func (star *StarNotifyS) trim(name string) string {
var slash bool = false
var key []byte
for _, v := range []byte(name) {
if v == byte(92) && !slash {
slash = true
continue
}
slash = false
key = append(key, v)
}
return string(key)
}
5 years ago
func (star *StarNotifyS) notify() {
5 years ago
for {
select {
5 years ago
case <-star.stopSign.Done():
5 years ago
return
5 years ago
default:
}
5 years ago
data, err := star.Queue.RestoreOne()
5 years ago
if err != nil {
time.Sleep(time.Millisecond * 20)
continue
}
4 years ago
mode, key, value := star.analyseData(string(data.Msg))
5 years ago
var rmsg SMsg
if !star.isUDP {
4 years ago
rmsg = SMsg{data.Conn.(net.Conn), key, value, nil, nil, mode, nil}
5 years ago
} else {
4 years ago
rmsg = SMsg{nil, key, value, data.Conn.(*net.UDPAddr), star.UDPConn, mode, nil}
5 years ago
if key == "b612ryzstop" {
delete(star.udpPool, rmsg.UDP.String())
continue
}
}
4 years ago
if mode[0:2] != "sr" {
go func() {
if msg, ok := star.FuncLists[key]; ok {
sdata := msg(rmsg)
5 years ago
if sdata == "" {
return
}
rmsg.Reply(sdata)
4 years ago
} else {
if star.defaultFunc != nil {
sdata := star.defaultFunc(rmsg)
if sdata == "" {
return
}
rmsg.Reply(sdata)
}
5 years ago
}
4 years ago
}()
} else {
if sa, ok := star.lockPool[mode]; ok {
rmsg.wait = sa.wait
star.lockPool[mode] = rmsg
star.lockPool[mode].wait <- 1
} else {
go func() {
if msg, ok := star.FuncLists[key]; ok {
sdata := msg(rmsg)
if sdata == "" {
return
}
rmsg.Reply(sdata)
} else {
if star.defaultFunc != nil {
sdata := star.defaultFunc(rmsg)
if sdata == "" {
return
}
rmsg.Reply(sdata)
}
}
}()
5 years ago
}
4 years ago
}
5 years ago
}
}
4 years ago
func (star *StarNotifyS) analyseData(msg string) (mode, key, value string) {
slice := strings.SplitN(msg, "||", 3)
return slice[0], star.trim(slice[1]), slice[2]
5 years ago
}
5 years ago
// ServerStop 用于终止Server端运行
5 years ago
func (star *StarNotifyS) ServerStop() {
5 years ago
star.cancel()
4 years ago
star.Stop <- 1
star.Stop <- 1
star.Stop <- 1
5 years ago
}