retry failed ICE for 3 times

Mehrdad Mirsamie 2023-08-14 13:55:26 +03:30
parent 2366c32298
commit d4281c3265
1 changed files with 78 additions and 5 deletions

View File

@ -3,13 +3,15 @@ package controllers
import (
"bytes"
"encoding/json"
"github.com/gin-gonic/gin"
"fmt"
"io"
"net/http"
"time"
"github.com/gin-gonic/gin"
"sourcecode.social/greatape/goldgorilla/models"
"sourcecode.social/greatape/goldgorilla/models/dto"
"sourcecode.social/greatape/goldgorilla/repositories"
"time"
)
type RoomController struct {
@ -43,20 +45,91 @@ func (c *RoomController) CreatePeer(ctx *gin.Context) {
c.helper.Response(ctx, struct{}{}, http.StatusNoContent)
}
func (c *RoomController) AddICECandidate(ctx *gin.Context) {
func (c *RoomController) RetryAddICECandidate(ctx *gin.Context, tries int) {
time.Sleep(1 * time.Second)
tries++
fmt.Println("----RetryAddICECandidate----")
var reqModel dto.AddPeerICECandidateReqModel
badReqSt := 400
if err := ctx.ShouldBindJSON(&reqModel); c.helper.HandleIfErr(ctx, err, &badReqSt) {
err1 := ctx.ShouldBindJSON(&reqModel)
// if err1 != nil && tries < 3 {
// // var reqGlobal interface{}
// // err2 := ctx.ShouldBindJSON(&reqGlobal)
// // fmt.Println("Err2: ", err2)
// // fmt.Println("reqGlobal: ", reqGlobal)
// c.RetryAddICECandidate(ctx, tries)
// return
// }
if err1 != nil {
c.helper.HandleIfErr(ctx, err1, &badReqSt)
fmt.Println("Err1: ", err1)
return
}
// if err := ctx.ShouldBindJSON(&reqModel); c.helper.HandleIfErr(ctx, err, &badReqSt) {
// fmt.Println("Err1: ", err)
// time.Sleep(1 * time.Second)
// c.RetryAddICECandidate(ctx, tries)
// return
// }
if !reqModel.Validate() {
c.helper.ResponseUnprocessableEntity(ctx)
fmt.Println("Validate")
return
}
err := c.repo.AddPeerIceCandidate(reqModel.RoomId, reqModel.ID, reqModel.ICECandidate)
if c.helper.HandleIfErr(ctx, err, nil) {
if err != nil && tries < 3 {
c.RetryAddICECandidate(ctx, tries)
return
}
if err != nil {
c.helper.HandleIfErr(ctx, err, &badReqSt)
fmt.Println("Err: ", err)
return
}
// if c.helper.HandleIfErr(ctx, err, nil) {
// fmt.Println("Err2: ", err)
// time.Sleep(1 * time.Second)
// c.RetryAddICECandidate(ctx, tries)
// return
// }
c.helper.Response(ctx, struct{}{}, http.StatusNoContent)
}
func (c *RoomController) AddICECandidate(ctx *gin.Context) {
fmt.Println("----AddICECandidate----")
// bd, _ := io.ReadAll(ctx.Request.Body)
// fmt.Println("bd: ", string(bd))
var reqModel dto.AddPeerICECandidateReqModel
// badReqSt := 400
err1 := ctx.ShouldBindJSON(&reqModel)
if err1 != nil {
// c.RetryAddICECandidate(ctx, 0)
return
}
// if err := ctx.ShouldBindJSON(&reqModel); c.helper.HandleIfErr(ctx, err, &badReqSt) {
// fmt.Println("Err1: ", err)
// time.Sleep(1 * time.Second)
// c.AddICECandidate(ctx)
// return
// }
if !reqModel.Validate() {
c.helper.ResponseUnprocessableEntity(ctx)
fmt.Println("Validate")
return
}
err := c.repo.AddPeerIceCandidate(reqModel.RoomId, reqModel.ID, reqModel.ICECandidate)
if err != nil {
c.RetryAddICECandidate(ctx, 0)
return
}
// if c.helper.HandleIfErr(ctx, err, nil) {
// fmt.Println("Err2: ", err)
// time.Sleep(1 * time.Second)
// c.AddICECandidate(ctx)
// return
// }
c.helper.Response(ctx, struct{}{}, http.StatusNoContent)
}