This commit is contained in:
laodaming 2023-08-24 17:23:41 +08:00
parent 190010a6fe
commit 4a41cb77e9
4 changed files with 75 additions and 47 deletions

View File

@ -366,8 +366,8 @@ func (w *wsConnectItem) respondDataFormat(msgType constants.Websocket, data inte
func (w *wsConnectItem) dealwithReciveData(data []byte) {
var parseInfo websocket_data.DataTransferData
if err := json.Unmarshal(data, &parseInfo); err != nil {
w.incomeDataFormatErrResponse("invalid format of income message:" + string(data))
logx.Error("invalid format of websocket message:", err)
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket message:"+string(data)))
return
}
d, _ := json.Marshal(parseInfo.D)

View File

@ -0,0 +1,40 @@
package logic
import "fusenapi/constants"
// 入口数据格式错误
func (w *wsConnectItem) incomeDataFormatErrResponse(data interface{}) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, data))
}
// 渲染错误通知
func (w *wsConnectItem) renderErrResponse(renderId, templateTag, taskId, description string, userId, guestId, templateId, modelId, sizeId int64) {
data := make(map[string]interface{})
data["render_id"] = renderId
data["description"] = description
data["template_tag"] = templateTag
if taskId != "" {
data["task_id"] = taskId
}
if userId >= 0 {
data["user_id"] = userId
}
if guestId >= 0 {
data["guest_id"] = guestId
}
if templateId > 0 {
data["template_id"] = templateId
}
if modelId > 0 {
data["model_id"] = modelId
}
if sizeId > 0 {
data["size_id"] = sizeId
}
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE_ERR, data))
}
// 复用连接错误通知
func (w *wsConnectItem) reuseLastConnErrResponse(data interface{}) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, data))
}

View File

@ -72,30 +72,15 @@ func (w *wsConnectItem) consumeRenderCache(data []byte) {
logx.Info("消费渲染数据:", string(data))
var renderImageData websocket_data.RenderImageReqMsg
if err := json.Unmarshal(data, &renderImageData); err != nil {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:"+string(data)))
w.incomeDataFormatErrResponse("invalid format of render data:" + string(data))
logx.Error("invalid format of websocket render image message", err)
return
}
if renderImageData.RenderId == "" {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:render_id is empty"))
logx.Error("invalid format of websocket render image message:render_id is empty")
return
}
if renderImageData.RenderData.ProductId <= 0 {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:product_id "))
logx.Error("invalid format of websocket render image message:product_id")
return
}
if renderImageData.RenderData.TemplateTag == "" {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "invalid format of websocket render image message:template_tag "))
logx.Error("invalid format of websocket render image message:template_tag")
return
}
//获取上传最近的logo
userMaterial, err := w.logic.svcCtx.AllModels.FsUserMaterial.FindLatestOne(w.logic.ctx, w.userId, w.guestId)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "failed to get user logo"))
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "failed to get user logo", w.userId, w.guestId, 0, 0, 0)
logx.Error("failed to get user logo")
return
}
@ -103,10 +88,10 @@ func (w *wsConnectItem) consumeRenderCache(data []byte) {
userMaterialDefault, err := w.logic.svcCtx.AllModels.FsUserMaterial.FindOneById(w.logic.ctx, 0)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "default logo is not exists"))
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "default logo is not exists", w.userId, w.guestId, 0, 0, 0)
return
}
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_ERR_DATA_FORMAT, "failed to get default logo"))
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, "", "failed to get default logo", w.userId, w.guestId, 0, 0, 0)
logx.Error("default logo is not exists")
return
}
@ -130,7 +115,7 @@ func (w *wsConnectItem) consumeRenderCache(data []byte) {
resource, err := w.logic.svcCtx.AllModels.FsResource.FindOneById(w.logic.ctx, taskId)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE_ERR, fmt.Sprintf("获取云渲染缓存资源错误 task_id:%s ", taskId)))
w.renderErrResponse(renderImageData.RenderId, renderImageData.RenderData.TemplateTag, taskId, "failed to get render cache", w.userId, w.guestId, 0, 0, 0)
logx.Error("failed to find render resource:", err)
return
}
@ -166,11 +151,11 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re
productFirstSize, err := w.logic.svcCtx.AllModels.FsProductSize.GetProductFirstSize(w.logic.ctx, info.RenderData.ProductId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE_ERR, fmt.Sprintf("没有尺寸 product_id:%d ", info.RenderData.ProductId)))
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "product first size is not exists", w.userId, w.guestId, 0, 0, 0)
logx.Error("product first size is not found")
return err
}
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE_ERR, fmt.Sprintf("获取尺寸错误 product_id:%d ", info.RenderData.ProductId)))
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "failed to get product first size", w.userId, w.guestId, 0, 0, 0)
logx.Error("failed to get product first size:", err)
return err
}
@ -178,11 +163,11 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re
model3dInfo, err := w.logic.svcCtx.AllModels.FsProductModel3d.GetOneBySizeIdTag(w.logic.ctx, productFirstSize.Id, constants.TAG_MODEL, "id")
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE_ERR, fmt.Sprintf("没有模型 product_id:%d , size_id:%d ", info.RenderData.ProductId, productFirstSize.Id)))
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "product model is not exists", w.userId, w.guestId, 0, 0, productFirstSize.Id)
logx.Error("product model is not found")
return err
}
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE_ERR, fmt.Sprintf("获取模型错误 product_id:%d , size_id:%d ", info.RenderData.ProductId, productFirstSize.Id)))
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "failed to get product model", w.userId, w.guestId, 0, 0, productFirstSize.Id)
logx.Error("failed to get product model:", err)
return err
}
@ -190,11 +175,11 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re
productTemplate, err := w.logic.svcCtx.AllModels.FsProductTemplateV2.FindFirstOneByProductIdModelIdTemplateTag(w.logic.ctx, info.RenderData.ProductId, model3dInfo.Id, info.RenderData.TemplateTag)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE_ERR, fmt.Sprintf("模板未找到 product_id:%d , model_id:%d ,template_tag:%s", info.RenderData.ProductId, model3dInfo.Id, info.RenderData.TemplateTag)))
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "product template is not exists", w.userId, w.guestId, 0, model3dInfo.Id, productFirstSize.Id)
logx.Error("template info is not found")
return err
}
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE_ERR, fmt.Sprintf("获取模板错误 product_id:%d , model_id:%d ,template_tag:%s", info.RenderData.ProductId, model3dInfo.Id, info.RenderData.TemplateTag)))
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "failed to get product template", w.userId, w.guestId, 0, model3dInfo.Id, productFirstSize.Id)
logx.Error("failed to get template info:", err)
return err
}
@ -219,7 +204,7 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re
}
res, err := w.logic.svcCtx.Repositories.ImageHandle.LogoCombine(w.logic.ctx, &combineReq)
if err != nil {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE_ERR, fmt.Sprintf("接口合图错误,产品id:%d,模板id%d", info.RenderData.ProductId, productTemplate.Id)))
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "failed to combine image", w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productFirstSize.Id)
logx.Error("合成刀版图失败,合成请求数据:", combineReq, "错误信息:", err)
return err
}
@ -227,7 +212,7 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re
if res != nil && res.ResourceUrl != nil {
combineImage = *res.ResourceUrl
} else {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE_ERR, fmt.Sprintf("接口合图错误,刀版图是空的,产品id:%d", info.RenderData.ProductId)))
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "combine image is empty", w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productFirstSize.Id)
logx.Error("合成刀版图失败,合成的刀版图是空指针:", err)
return err
}
@ -244,10 +229,11 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re
element, err := w.logic.svcCtx.AllModels.FsProductTemplateElement.FindOneByModelId(w.logic.ctx, *productTemplate.ModelId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE_ERR, fmt.Sprintf("无渲染设置信息,产品id:%d ,模板id%d,模型id:%d", info.RenderData.ProductId, productTemplate.Id, *productTemplate.ModelId)))
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "render element is not exists", w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productFirstSize.Id)
logx.Error("element info is not found,model_id = ", *productTemplate.ModelId)
return err
}
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "failed to get render element", w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productFirstSize.Id)
logx.Error("failed to get element list,", err)
return err
}
@ -257,6 +243,8 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re
refletion, err = strconv.Atoi(*element.Refletion)
if err != nil {
logx.Error("err refletion:set default -1")
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "parse element.Refletion from string to number err", w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productFirstSize.Id)
return err
}
}
//组装data数据
@ -264,6 +252,7 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re
if element.Mode != nil && *element.Mode != "" {
if err = json.Unmarshal([]byte(*element.Mode), &mode); err != nil {
logx.Error("faile to parse element mode json:", err)
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "parse element.Mode err", w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productFirstSize.Id)
return err
}
}
@ -336,7 +325,7 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re
postDataBytes, _ := json.Marshal(postData)
_, err = curl.ApiCall(url, "POST", header, bytes.NewReader(postDataBytes), time.Second*10)
if err != nil {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE_ERR, fmt.Sprintf("请求unity接口错误,产品id:%d", info.RenderData.ProductId)))
w.renderErrResponse(info.RenderId, info.RenderData.TemplateTag, taskId, "request unity api err", w.userId, w.guestId, productTemplate.Id, model3dInfo.Id, productFirstSize.Id)
logx.Error("failed to send data to unity")
return err
}

View File

@ -12,33 +12,33 @@ import (
// 刷新重连请求恢复上次连接的标识
func (w *wsConnectItem) reuseLastConnect(data []byte) {
logx.Info("收到请求恢复上次连接标识数据:", string(data))
var clientId string
if err := json.Unmarshal(data, &clientId); err != nil {
logx.Error(" invalid format of client id :", clientId)
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "invalid format of client id"))
var wid string
if err := json.Unmarshal(data, &wid); err != nil {
logx.Error(" invalid format of wid :", wid)
w.incomeDataFormatErrResponse("invalid format of wid")
return
}
//解密
decryptionClientId, err := encryption_decryption.CBCDecrypt(clientId)
decryptionWid, err := encryption_decryption.CBCDecrypt(wid)
if err != nil {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "invalid client id"))
w.reuseLastConnErrResponse("invalid wid")
return
}
lendecryptionClientId := len(decryptionClientId)
lendecryptionWid := len(decryptionWid)
//合成client后缀,不是同个后缀的不能复用
userPart := getUserJoinPart(w.userId, w.guestId)
lenUserPart := len(userPart)
if lendecryptionClientId <= lenUserPart {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "length of client id is to short"))
if lendecryptionWid <= lenUserPart {
w.reuseLastConnErrResponse("length of client id is to short")
return
}
//尾部不同不能复用
if decryptionClientId[lendecryptionClientId-lenUserPart:] != userPart {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "the client id is not belong you before"))
if decryptionWid[lendecryptionWid-lenUserPart:] != userPart {
w.reuseLastConnErrResponse("the client id is not belong you before")
return
}
//存在是不能给他申请重新绑定
if v, ok := mapConnPool.Load(clientId); ok {
if v, ok := mapConnPool.Load(wid); ok {
obj, ok := v.(wsConnectItem)
if !ok {
logx.Error("连接断言失败")
@ -47,14 +47,13 @@ func (w *wsConnectItem) reuseLastConnect(data []byte) {
if obj.uniqueId == w.uniqueId {
return
} else {
rsp := w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "id has bound by other connect ")
w.sendToOutChan(rsp)
w.reuseLastConnErrResponse("the wid is used by other people")
return
}
}
//重新绑定
w.uniqueId = clientId
rsp := w.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, clientId)
w.uniqueId = wid
rsp := w.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, wid)
w.sendToOutChan(rsp)
return
}