This commit is contained in:
laodaming 2023-09-13 10:42:33 +08:00
parent 06b1ad1678
commit 7e8f445169
3 changed files with 29 additions and 16 deletions

View File

@ -165,7 +165,7 @@ func (l *DataTransferLogic) DataTransfer(w http.ResponseWriter, r *http.Request)
//消费入口数据
go ws.consumeInChanData()
//操作连接中渲染任务的增加/删除
go ws.operationRenderTask()
//go ws.operationRenderTask()
//消费渲染缓冲队列
go ws.consumeRenderImageData()
//心跳
@ -194,7 +194,7 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.Use
userId: userInfo.UserId,
guestId: userInfo.GuestId,
extendRenderProperty: extendRenderProperty{
renderImageTask: make(map[string]*renderTask),
//renderImageTask: make(map[string]*renderTask),
renderImageTaskCtlChan: make(chan renderImageControlChanItem, renderImageTaskCtlChanLen),
renderChan: make(chan []byte, renderChanLen),
renderConsumeTickTime: 1, //默认1纳秒后面需要根据不同用户不同触发速度

View File

@ -3,11 +3,14 @@ package logic
//云渲染回调
import (
"context"
"fmt"
"fusenapi/server/websocket/internal/svc"
"fusenapi/server/websocket/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/file"
"fusenapi/utils/websocket_data"
"strconv"
"strings"
"time"
@ -51,7 +54,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *a
unityRenderEndTime := time.Now().UTC().UnixMilli()
//解析出真的taskId跟wid
splitSlice := strings.Split(req.TaskId, " ")
if len(splitSlice) != 3 {
if len(splitSlice) != 4 {
logx.Error("渲染回调参数错误task_id拆分得不到合理结果")
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param task_id")
}
@ -59,6 +62,11 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *a
req.TaskId = splitSlice[0]
wid := splitSlice[1]
renderId := splitSlice[2]
unityRenderBeginTime, err := strconv.ParseInt(splitSlice[3], 10, 64)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "failed to parse send to unity time")
}
//存base64打印测试
/* f, _ := os.Create("b.txt")
defer f.Close()
@ -92,10 +100,15 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *a
logx.Error("渲染回调断言websocket连接失败")
return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "渲染回调断言websocket连接失败")
}
//记录收到unity渲染结果时间以及上传渲染图耗时时间
ws.modifyRenderTaskProperty(req.TaskId, renderTask{unityRenderEndTime: unityRenderEndTime, uploadUnityRenderImageTakesTime: uploadUnityRenderImageTakesTime})
//发送处理并删除任务
ws.deleteRenderTask(req.TaskId, renderId, uploadRes.ResourceUrl)
//发送到出口
ws.sendRenderResultData(websocket_data.RenderImageRspMsg{
RenderId: renderId,
Image: uploadRes.ResourceUrl,
RenderProcessTime: websocket_data.RenderProcessTime{
UnityRenderTakesTime: fmt.Sprintf("%dms", unityRenderEndTime-unityRenderBeginTime),
UploadUnityRenderImageTakesTime: fmt.Sprintf("%dms", uploadUnityRenderImageTakesTime),
},
})
}
logx.Info("渲染回调成功,渲染结果图片为:", uploadRes.ResourceUrl)
return resp.SetStatusWithMessage(basic.CodeOK, "success")

View File

@ -32,7 +32,7 @@ type renderProcessor struct {
// 云渲染属性
type extendRenderProperty struct {
renderImageTask map[string]*renderTask //需要渲染的图片任务 key是taskId val 是renderId
//renderImageTask map[string]*renderTask //需要渲染的图片任务 key是taskId val 是renderId
renderImageTaskCtlChan chan renderImageControlChanItem //渲染任务新增/回调结果移除任务/更新渲染耗时属性的控制通道由于任务map无法读写并发
renderChan chan []byte //渲染消息入口的缓冲队列
renderConsumeTickTime time.Duration //消费渲染消息时钟间隔(纳秒),用于后期控制不同类型用户渲染速度限制
@ -238,7 +238,7 @@ func (w *wsConnectItem) renderImage(data []byte) {
}
//###########################################
//把需要渲染的图片任务加进去
w.createRenderTask(taskId, renderImageData.RenderId)
//w.createRenderTask(taskId, renderImageData.RenderId)
//组装数据
if err = w.assembleRenderDataToUnity(taskId, combineImage, renderImageData, productTemplate, model3dInfo, element, productSize); err != nil {
logx.Error("组装数据失败:", err)
@ -359,7 +359,7 @@ func (w *wsConnectItem) assembleRenderDataToUnity(taskId string, combineImage st
if element.Base != nil && *element.Base != "" {
tempData = append(tempData, map[string]interface{}{
"name": "model",
"data": "0," + combineImage + "," + *element.Base,
"data": "0," + combineImage + "," + *element.Base + " " + fmt.Sprintf("%d", time.Now().UTC().UnixMilli()),
"type": "other",
"layer": "0",
"is_update": 1,
@ -423,7 +423,7 @@ func (w *wsConnectItem) assembleRenderDataToUnity(taskId string, combineImage st
"render_data": sendData,
}
postDataBytes, _ := json.Marshal(postData)
unityRenderBeginTime := time.Now().UTC().UnixMilli()
//unityRenderBeginTime := time.Now().UTC().UnixMilli()
_, err = curl.ApiCall(url, "POST", header, bytes.NewReader(postDataBytes), time.Second*10)
if err != nil {
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "请求unity接口失败", info.RenderData.ProductId, w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productSize.Id, *productTemplate.ElementModelId)
@ -431,7 +431,7 @@ func (w *wsConnectItem) assembleRenderDataToUnity(taskId string, combineImage st
return err
}
//记录发送到unity时间
w.modifyRenderTaskProperty(taskId, renderTask{unityRenderBeginTime: unityRenderBeginTime})
//w.modifyRenderTaskProperty(taskId, renderTask{unityRenderBeginTime: unityRenderBeginTime})
//发送运行阶段消息
w.sendRenderDataToUnityStepResponseMessage(info.RenderId)
logx.Info("发送到unity成功,刀版图:", combineImage /*, " 请求unity的数据:", string(postDataBytes)*/)
@ -476,7 +476,7 @@ func (w *wsConnectItem) sendRenderResultData(data websocket_data.RenderImageRspM
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, data))
}
// 增加渲染任务
/*// 增加渲染任务
func (w *wsConnectItem) createRenderTask(taskId, renderId string) {
if taskId == "" {
logx.Error("task_id不能为空")
@ -543,7 +543,7 @@ func (w *wsConnectItem) modifyRenderTaskProperty(taskId string, property renderT
case <-time.After(time.Second * 3):
return
}
}
}*/
// 组装渲染任务id
func (w *wsConnectItem) genRenderTaskId(combineImage string, renderImageData websocket_data.RenderImageReqMsg, model3dInfo *gmodel.FsProductModel3d, productTemplate *gmodel.FsProductTemplateV2, element *gmodel.FsProductTemplateElement) string {
@ -573,7 +573,7 @@ func (w *wsConnectItem) genRenderTaskId(combineImage string, renderImageData web
}
// 处理渲染任务的增加/删除/修改耗时属性任务map不能读写并发所以放在chan里面串行执行
func (w *wsConnectItem) operationRenderTask() {
/*func (w *wsConnectItem) operationRenderTask() {
defer func() {
if err := recover(); err != nil {
logx.Error("operation render task panic:", err)
@ -645,4 +645,4 @@ func (w *wsConnectItem) operationRenderTask() {
}
}
}
}
}*/