89 lines
2.0 KiB
Go
89 lines
2.0 KiB
Go
|
package logic
|
|||
|
|
|||
|
import (
|
|||
|
"context"
|
|||
|
"crypto/md5"
|
|||
|
"encoding/hex"
|
|||
|
"encoding/json"
|
|||
|
"fmt"
|
|||
|
"github.com/zeromicro/go-zero/core/logx"
|
|||
|
"net"
|
|||
|
"time"
|
|||
|
)
|
|||
|
|
|||
|
var (
|
|||
|
//取消unity僵尸任务控制通道
|
|||
|
cancelUnityCtlChan = make(chan cancelUnityCtlChanItem, 1000)
|
|||
|
cancelRenderContextPanicMsg any = "cancel_render_context_panic_msg"
|
|||
|
)
|
|||
|
|
|||
|
// 控制通道元素
|
|||
|
type cancelUnityCtlChanItem struct {
|
|||
|
Wid string `json:"wid"` //ws的唯一id
|
|||
|
DeadlineTime int64 `json:"deadline_time"` //截断时间
|
|||
|
Sign string `json:"sign"` //有效签名
|
|||
|
}
|
|||
|
|
|||
|
// 取消渲染抛出的异常
|
|||
|
func cancelRenderPanic() {
|
|||
|
panic(cancelRenderContextPanicMsg)
|
|||
|
}
|
|||
|
|
|||
|
// 判断是否是取消渲染的异常
|
|||
|
func isCancelRenderPanic(err any) bool {
|
|||
|
return err == cancelRenderContextPanicMsg
|
|||
|
}
|
|||
|
|
|||
|
// 发送取消上下文消息给unity
|
|||
|
func sendCancelRenderMsgToUnity(wid string, deadlineTime int64) {
|
|||
|
h := md5.New()
|
|||
|
h.Write([]byte(fmt.Sprintf("%s_%d", wid, deadlineTime)))
|
|||
|
data := cancelUnityCtlChanItem{
|
|||
|
Wid: wid,
|
|||
|
DeadlineTime: deadlineTime,
|
|||
|
Sign: hex.EncodeToString(h.Sum(nil)),
|
|||
|
}
|
|||
|
select {
|
|||
|
case cancelUnityCtlChan <- data:
|
|||
|
case <-time.After(time.Millisecond * 200):
|
|||
|
logx.Error("sendCancelRenderMsgToUnity数据超时丢弃")
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 拨号udp
|
|||
|
func DialUdp(ctx context.Context) error {
|
|||
|
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{IP: net.ParseIP("localhost"), Port: 9501})
|
|||
|
if err != nil {
|
|||
|
return err
|
|||
|
}
|
|||
|
go ConsumeCancelUnityChanMessage(ctx, conn)
|
|||
|
return nil
|
|||
|
}
|
|||
|
|
|||
|
// 消费数据
|
|||
|
func ConsumeCancelUnityChanMessage(ctx context.Context, conn *net.UDPConn) {
|
|||
|
defer func() {
|
|||
|
if err := recover(); err != nil {
|
|||
|
logx.Error("ConsumeCancelUnityChanMessage 异常:", err)
|
|||
|
}
|
|||
|
}()
|
|||
|
go func() {
|
|||
|
select {
|
|||
|
case <-ctx.Done():
|
|||
|
panic(any("ConsumeCancelUnityChanMessage ctx deadline"))
|
|||
|
}
|
|||
|
}()
|
|||
|
defer conn.Close()
|
|||
|
for {
|
|||
|
select {
|
|||
|
case data := <-cancelUnityCtlChan:
|
|||
|
d, _ := json.Marshal(data)
|
|||
|
_, err := conn.Write(d)
|
|||
|
if err != nil {
|
|||
|
logx.Error("发送udp包失败:", err)
|
|||
|
continue
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|