fusenapi/server/websocket/internal/logic/rendernotifylogic.go
laodaming 3cd976976a fix
2023-11-02 16:00:49 +08:00

126 lines
4.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package logic
//云渲染回调
import (
"context"
"encoding/json"
"fmt"
"fusenapi/server/websocket/internal/svc"
"fusenapi/server/websocket/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/file"
"fusenapi/utils/websocket_data"
"time"
"github.com/zeromicro/go-zero/core/logx"
)
type RenderNotifyLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewRenderNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RenderNotifyLogic {
return &RenderNotifyLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *RenderNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *RenderNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
unityRenderEndTime := time.Now().UTC().UnixMilli()
//解析数据
var info websocket_data.ToUnityIdStruct
if err := json.Unmarshal([]byte(req.TaskId), &info); err != nil {
logx.Error("解析taskId错误")
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "failed to parse param taskId !!!!")
}
//发出去的时间必须是大于服务启动的时间才参与统计
if info.RenderBeginTime > serverStartTime {
//logx.Info("任务时间:", info.RenderBeginTime, "服务器启动时间:", serverStartTime)
//统计unity处理数
decreaseUnityRequestCount(info.UserId, info.GuestId)
if req.Code != 0 {
//统计unity失败处理数
increaseUnityErrorCount(info.UserId, info.GuestId)
}
}
//重新赋值(很重要)
wid := info.Wid
requestId := info.RequestId
unityRenderBeginTime := info.RenderBeginTime
//获取连接
value, wsConnectOk := mapConnPool.Load(wid)
if req.Code == 0 { //渲染成功
//上传文件
var upload = file.Upload{
Ctx: l.ctx,
MysqlConn: l.svcCtx.MysqlConn,
AwsSession: l.svcCtx.AwsSession,
}
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
Source: "unity cloud render",
FileHash: info.TaskId,
FileData: req.Image,
Metadata: "",
UploadBucket: 1,
ApiType: 2,
UserId: info.UserId,
GuestId: info.GuestId,
FileByte: nil,
})
if err != nil {
logx.Error("渲染回调上传文件失败:", err)
return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "failed to upload render resource image")
}
uploadUnityRenderImageTakesTime := time.Now().UTC().UnixMilli() - unityRenderEndTime
if wsConnectOk {
//断言连接
ws, ok := value.(wsConnectItem)
if !ok {
logx.Error("渲染回调断言websocket连接失败")
return resp.SetStatusWithMessage(basic.CodeServiceErr, "渲染回调断言websocket连接失败")
}
//发送到出口
ws.sendRenderResultData(websocket_data.RenderImageRspMsg{
RequestId: requestId,
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")
}
logx.Info("渲染成功但找不到ws连接")
return resp.SetStatusWithMessage(basic.CodeOK, "successbut websocket connect not found")
}
//渲染失败走下面
if wsConnectOk {
//断言连接
ws, ok := value.(wsConnectItem)
if !ok {
logx.Error("渲染回调断言websocket连接失败")
return resp.SetStatusWithMessage(basic.CodeServiceErr, "渲染回调断言websocket连接失败")
}
//发送错误信息给前端
ws.renderErrResponse(requestId, info.TemplateTag, info.TaskId, "unity云渲染错误:"+req.Msg, 0, 0, 0, 0, 0, 0, 0)
logx.Info("渲染失败且发送了失败信息")
} else {
logx.Info("渲染失败且找不到ws连接")
}
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}