fusenapi/server/websocket/internal/logic/ws_render_image_logic.go

70 lines
2.0 KiB
Go
Raw Normal View History

2023-07-26 06:35:29 +00:00
package logic
import (
"encoding/json"
2023-07-27 09:33:50 +00:00
"fusenapi/constants"
2023-07-26 06:35:29 +00:00
"fusenapi/server/websocket/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
2023-07-26 09:06:53 +00:00
// 云渲染属性
type renderProperty struct {
renderImageTask map[string]struct{} //需要渲染的图片任务
renderImageTaskCtlChan chan renderImageControlChanItem //渲染任务新增移除的控制通道
}
// 渲染任务新增移除的控制通道的数据
type renderImageControlChanItem struct {
Option int // 0删除 1添加
Key string //map的key
}
2023-07-26 06:35:29 +00:00
// 渲染请求数据处理发送云渲染服务处理
func (w *wsConnectItem) SendToCloudRender(data []byte) {
2023-07-27 07:46:07 +00:00
var renderImageData types.RenderImageReqMsg
2023-07-26 06:35:29 +00:00
if err := json.Unmarshal(data, &renderImageData); err != nil {
2023-07-27 09:33:50 +00:00
select {
case <-w.closeChan:
return
case w.outChan <- w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message"):
logx.Error("invalid format of websocket render image message", err)
return
}
2023-07-26 06:35:29 +00:00
}
logx.Info("收到请求云渲染图片数据:", renderImageData)
2023-07-26 07:05:53 +00:00
//把需要渲染的图片任务加进去
2023-07-27 07:46:07 +00:00
for _, productId := range renderImageData.ProductIds {
select {
case <-w.closeChan: //连接关闭了
return
default:
//加入渲染任务
2023-07-27 08:13:06 +00:00
key := w.getRenderImageMapKey(productId, renderImageData.TemplateTagId, renderImageData.AlgorithmVersion)
2023-07-27 07:46:07 +00:00
w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{
Option: 1, //0删除 1添加
Key: key,
2023-07-26 08:23:38 +00:00
}
2023-07-27 07:46:07 +00:00
// TODO 数据发送给云渲染服务器
2023-07-26 07:01:59 +00:00
}
2023-07-26 06:35:29 +00:00
}
}
2023-07-26 07:15:37 +00:00
// 操作连接中渲染任务的增加/删除
func (w *wsConnectItem) operationRenderTask() {
for {
select {
case <-w.closeChan:
return
2023-07-26 09:06:53 +00:00
case data := <-w.renderProperty.renderImageTaskCtlChan:
2023-07-26 07:15:37 +00:00
switch data.Option {
case 0: //删除任务
2023-07-26 09:06:53 +00:00
delete(w.renderProperty.renderImageTask, data.Key)
2023-07-26 07:15:37 +00:00
case 1: //新增任务
2023-07-26 09:06:53 +00:00
w.renderProperty.renderImageTask[data.Key] = struct{}{}
2023-07-26 07:15:37 +00:00
default:
}
}
}
}