fix
This commit is contained in:
parent
2ce9776255
commit
6c36817750
|
@ -0,0 +1,33 @@
|
||||||
|
package logic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fusenapi/constants"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 消息分发工厂
|
||||||
|
type allocationProcessorFactory interface {
|
||||||
|
allocationMessage(data []byte)
|
||||||
|
}
|
||||||
|
|
||||||
|
var mapAllocationProcessor = make(map[constants.Websocket]allocationProcessorFactory)
|
||||||
|
|
||||||
|
func (w *wsConnectItem) newAllocationProcessor(msgType constants.Websocket) allocationProcessorFactory {
|
||||||
|
if obj, ok := mapAllocationProcessor[msgType]; ok {
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
var obj allocationProcessorFactory
|
||||||
|
switch msgType {
|
||||||
|
//图片渲染
|
||||||
|
case constants.WEBSOCKET_RENDER_IMAGE:
|
||||||
|
obj = &renderProcesser{w}
|
||||||
|
//刷新重连请求恢复上次连接的标识
|
||||||
|
case constants.WEBSOCKET_REQUEST_REUSE_LAST_CONNECT:
|
||||||
|
obj = &reuseConnProcesser{w}
|
||||||
|
default:
|
||||||
|
|
||||||
|
}
|
||||||
|
if obj != nil {
|
||||||
|
mapAllocationProcessor[msgType] = obj
|
||||||
|
}
|
||||||
|
return obj
|
||||||
|
}
|
|
@ -391,15 +391,8 @@ func (w *wsConnectItem) allocationProcessing(data []byte) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
d, _ := json.Marshal(parseInfo.D)
|
d, _ := json.Marshal(parseInfo.D)
|
||||||
//分消息类型给到不同逻辑处理,可扩展
|
//获取工厂实例
|
||||||
switch parseInfo.T {
|
processor := w.newAllocationProcessor(parseInfo.T)
|
||||||
//图片渲染
|
//执行工厂方法
|
||||||
case constants.WEBSOCKET_RENDER_IMAGE:
|
processor.allocationMessage(d)
|
||||||
w.sendToRenderChan(d)
|
|
||||||
//刷新重连请求恢复上次连接的标识
|
|
||||||
case constants.WEBSOCKET_REQUEST_REUSE_LAST_CONNECT:
|
|
||||||
w.reuseLastConnect(d)
|
|
||||||
default:
|
|
||||||
logx.Error("未知消息类型:uid:", w.userId, "gid:", w.guestId, "data:", string(data))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,11 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 渲染处理器
|
||||||
|
type renderProcesser struct {
|
||||||
|
w *wsConnectItem
|
||||||
|
}
|
||||||
|
|
||||||
// 云渲染属性
|
// 云渲染属性
|
||||||
type extendRenderProperty struct {
|
type extendRenderProperty struct {
|
||||||
renderImageTask map[string]*renderTask //需要渲染的图片任务 key是taskId val 是renderId
|
renderImageTask map[string]*renderTask //需要渲染的图片任务 key是taskId val 是renderId
|
||||||
|
@ -44,12 +49,11 @@ type renderTask struct {
|
||||||
uploadUnityRenderImageTakesTime int64 //上传unity渲染结果图时间
|
uploadUnityRenderImageTakesTime int64 //上传unity渲染结果图时间
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送到渲染缓冲队列
|
func (r *renderProcesser) allocationMessage(data []byte) {
|
||||||
func (w *wsConnectItem) sendToRenderChan(data []byte) {
|
|
||||||
select {
|
select {
|
||||||
case <-w.closeChan: //已经关闭
|
case <-r.w.closeChan: //已经关闭
|
||||||
return
|
return
|
||||||
case w.extendRenderProperty.renderChan <- data: //发入到缓冲队列
|
case r.w.extendRenderProperty.renderChan <- data: //发入到缓冲队列
|
||||||
return
|
return
|
||||||
case <-time.After(time.Second * 3): //三秒没进入缓冲队列就丢弃
|
case <-time.After(time.Second * 3): //三秒没进入缓冲队列就丢弃
|
||||||
return
|
return
|
||||||
|
|
|
@ -9,32 +9,36 @@ import (
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 刷新重连请求恢复上次连接的标识
|
// 复用连接处理器
|
||||||
func (w *wsConnectItem) reuseLastConnect(data []byte) {
|
type reuseConnProcesser struct {
|
||||||
|
w *wsConnectItem
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *reuseConnProcesser) allocationMessage(data []byte) {
|
||||||
logx.Info("收到请求恢复上次连接标识数据:", string(data))
|
logx.Info("收到请求恢复上次连接标识数据:", string(data))
|
||||||
var wid string
|
var wid string
|
||||||
if err := json.Unmarshal(data, &wid); err != nil {
|
if err := json.Unmarshal(data, &wid); err != nil {
|
||||||
logx.Error(" invalid format of wid :", wid)
|
logx.Error(" invalid format of wid :", wid)
|
||||||
w.incomeDataFormatErrResponse("invalid format of wid")
|
r.w.incomeDataFormatErrResponse("invalid format of wid")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//解密
|
//解密
|
||||||
decryptionWid, err := encryption_decryption.CBCDecrypt(wid)
|
decryptionWid, err := encryption_decryption.CBCDecrypt(wid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.reuseLastConnErrResponse("invalid wid")
|
r.w.reuseLastConnErrResponse("invalid wid")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
lendecryptionWid := len(decryptionWid)
|
lendecryptionWid := len(decryptionWid)
|
||||||
//合成client后缀,不是同个后缀的不能复用
|
//合成client后缀,不是同个后缀的不能复用
|
||||||
userPart := getUserJoinPart(w.userId, w.guestId, w.userAgent)
|
userPart := getUserJoinPart(r.w.userId, r.w.guestId, r.w.userAgent)
|
||||||
lenUserPart := len(userPart)
|
lenUserPart := len(userPart)
|
||||||
if lendecryptionWid <= lenUserPart {
|
if lendecryptionWid <= lenUserPart {
|
||||||
w.reuseLastConnErrResponse("length of client id is to short")
|
r.w.reuseLastConnErrResponse("length of client id is to short")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//尾部不同不能复用
|
//尾部不同不能复用
|
||||||
if decryptionWid[lendecryptionWid-lenUserPart:] != userPart {
|
if decryptionWid[lendecryptionWid-lenUserPart:] != userPart {
|
||||||
w.reuseLastConnErrResponse("the client id is not belong to you before")
|
r.w.reuseLastConnErrResponse("the client id is not belong to you before")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//存在是不能给他申请重新绑定
|
//存在是不能给他申请重新绑定
|
||||||
|
@ -44,20 +48,20 @@ func (w *wsConnectItem) reuseLastConnect(data []byte) {
|
||||||
logx.Error("连接断言失败")
|
logx.Error("连接断言失败")
|
||||||
}
|
}
|
||||||
//是当前自己占用(无需处理)
|
//是当前自己占用(无需处理)
|
||||||
if obj.uniqueId == w.uniqueId {
|
if obj.uniqueId == r.w.uniqueId {
|
||||||
rsp := w.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, wid)
|
rsp := r.w.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, wid)
|
||||||
w.sendToOutChan(rsp)
|
r.w.sendToOutChan(rsp)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
w.reuseLastConnErrResponse("the wid is used by other people")
|
r.w.reuseLastConnErrResponse("the wid is used by other people")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//重新绑定
|
//重新绑定
|
||||||
w.uniqueId = wid
|
r.w.uniqueId = wid
|
||||||
mapConnPool.Store(wid, *w)
|
mapConnPool.Store(wid, *r.w)
|
||||||
rsp := w.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, wid)
|
rsp := r.w.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, wid)
|
||||||
w.sendToOutChan(rsp)
|
r.w.sendToOutChan(rsp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user