Beispiel #1
0
func (cu *CUClient) closeSource(clientID uint32, id string) {

	var err error
	proto := new(base.Proto)
	proto.RD.BaseHD.CommandId = base.CLOSE_RESOURCE_CMD
	proto.RD.HD.ClientIdent = clientID
	proto.RD.HD.ContextType = base.CONTEXT_JSON
	proto.RD.HD.TransferType = base.TRANSFER_RAW

	resourceJson := base.CloseResParamJson{id}

	requestJson := base.RequestJson{"", "ns", "close", resourceJson}
	b, err := json.Marshal(requestJson)
	if err != nil {
		cu.handleError(err)
		return
	}

	proto.EncodeBody(b)
	result := proto.EncodeHdr()
	n, err := cu.connSocket.Write(result.Bytes())
	if err != nil {
		cu.handleError(err)
		return
	}
	n = n

	_, bErr := cu.connSocket.Write(proto.BD.Data)
	if err != nil {
		cu.handleError(bErr)
	}
	return
}
Beispiel #2
0
func (pu *PUDevice) closeSource(clientID uint32) error {
	defer func() {
		if r := recover(); r != nil {
			pu.handleError(base.DTerror{"closeSource Panic"})
		}
	}()
	atomic.CompareAndSwapUint32(&pu.SendFlag, 1, 0)

	proto := new(base.Proto)
	proto.RD.BaseHD.CommandId = 0x80 | base.CLOSE_RESOURCE_CMD
	proto.RD.HD.ClientIdent = clientID
	proto.RD.HD.TransferType = base.TRANSFER_RAW
	proto.RD.HD.ContextType = base.CONTEXT_JSON
	responseJson := base.ResponseJson{"ns", "close", "", "ok"}
	b, err := json.Marshal(responseJson)
	if err != nil {
		pu.handleError(err)
		return err
	}

	proto.EncodeBody(b)
	pu.rwChan <- proto

	stat.GetLocalStatistInst().CloseRes()
	return err
}
Beispiel #3
0
func (cu *CUClient) openSource(clientID uint32, id string) error {

	proto := new(base.Proto)
	proto.RD.BaseHD.CommandId = base.OPEN_RESOURCE_CMD
	proto.RD.HD.ClientIdent = clientID
	proto.RD.HD.ContextType = base.CONTEXT_JSON
	proto.RD.HD.TransferType = base.TRANSFER_RAW

	resourceJson := base.OpenResParamJson{id, 10}

	requestJson := base.RequestJson{cu.token, "ns", "open", resourceJson}
	b, err := json.Marshal(requestJson)
	if err != nil {
		cu.handleError(err)
		return err
	}

	proto.EncodeBody(b)
	result := proto.EncodeHdr()
	n, err := cu.connSocket.Write(result.Bytes())
	if err != nil {
		cu.handleError(err)
		return err
	}
	n = n

	_, bErr := cu.connSocket.Write(proto.BD.Data)
	if err != nil {
		cu.handleError(bErr)
		return err
	}
	go cu.timerClear()
	return err
}
Beispiel #4
0
func (pu *PUDevice) openSource(clientID uint32) error {

	proto := new(base.Proto)
	proto.RD.BaseHD.CommandId = 0x80 | base.OPEN_RESOURCE_CMD
	proto.RD.HD.ClientIdent = clientID
	proto.RD.HD.TransferType = base.TRANSFER_RAW
	proto.RD.HD.ContextType = base.CONTEXT_JSON

	//resourceJson := command.ResourceDes{id}

	//res, e := json.Marshal(resourceJson)
	//if e != nil {
	//	return e
	//}
	atomic.CompareAndSwapUint32(&pu.SendFlag, 0, 1)

	responseJson := base.ResponseJson{"ns", "open", "", "ok"}
	b, err := json.Marshal(responseJson)
	if err != nil {
		pu.handleError(err)
		return err
	}

	proto.EncodeBody(b)

	pu.rwChan <- proto

	go pu.handleSource(clientID)
	stat.GetLocalStatistInst().OpenRes()
	return err
}
Beispiel #5
0
func (chn *Channel) handleOtherCMD(proto *base.Proto) {

	cmdID := proto.RD.BaseHD.CommandId
	mylog.GetErrorLogger().Println("handleOtherCMD", cmdID)
	error := base.NOSUPPORT501
	responseJson := base.ResponseJson{"", "", "", error}
	b, err := json.Marshal(responseJson)
	if err != nil {
		mylog.GetErrorLogger().Println("handleOtherCMD : ", err)
	}

	proto.EncodeBody(b)
	chn.Send(proto)
}
Beispiel #6
0
func (pu *PUDevice) handleSource(clientID uint32) {

	defer func() {
		if r := recover(); r != nil {
			pu.handleError(base.DTerror{"handleSource Panic"})
		}
	}()
	file := new(record.RecordFile)
	resourceFile := new(bytes.Buffer)
	resourceFile.WriteString(oneEnvParam.ResourceDir)
	resourceFile.WriteString("\\")
	resourceFile.WriteString(oneEnvParam.ResourceFile)
	fmt.Println("Open File ", resourceFile)
	if b := file.Open("C:\\1.hmv"); !b {
		return
	}
	defer file.Close()
	file.GetHeader()
	for {
		if !atomic.CompareAndSwapUint32(&pu.SendFlag, 1, 1) {
			break
		}
		if atomic.CompareAndSwapUint32(&pu.Valid, 0, 0) {
			break
		}

		proto := new(base.Proto)
		proto.RD.BaseHD.CommandId = 0x80 | base.DATA_STREAM
		proto.RD.HD.TransferType = base.TRANSFER_RAW
		proto.RD.HD.ContextType = base.CONTEXT_VIDEO
		proto.RD.HD.ClientIdent = clientID

		b := file.GetNextFrame()
		if b == nil {
			file.Seek()
			continue
		}
		proto.EncodeBody(b)
		pu.rwChan <- proto

		stat.GetLocalStatistInst().SendData(uint64(len(proto.BD.Data)))

		time.Sleep(100 * time.Millisecond)

	}
}
Beispiel #7
0
func (pu *PUDevice) Register() {
	// 注册资源
	proto := new(base.Proto)
	proto.RD.BaseHD.CommandId = base.REGISTER_RESOURCE
	proto.RD.HD.TransferType = base.TRANSFER_RAW
	proto.RD.HD.ContextType = base.CONTEXT_JSON

	regsterJson := base.RegisterResParamJson{pu.SN, 1, false}
	requestJson := base.RequestJson{"", "ns", "register", regsterJson}

	b, err := json.Marshal(requestJson)
	if err != nil {
		pu.handleError(err)
		return
	}

	proto.EncodeBody(b)
	pu.rwChan <- proto

}
Beispiel #8
0
func (chn *Channel) handleReqOpenRes(clientID uint32, data []byte) {

	defer func() {
		if r := recover(); r != nil {
			chn.handleError(base.DTerror{"handleReqOpenResr Panic"})
		}
	}()

	var responseErr string = ""
	var resourcer *Resourcer = nil
	var timeOut uint32 = 20 // 默认超时20s

	request, token, err := chn.getReqParam(clientID, data[4:])
	if err != nil {
		chn.sendProto(clientID,
			base.OPEN_RESOURCE_CMD,
			"", err.Error(),
			"huamail.defualt")
		return
	}

	//获取资源
	param := request.Param.(map[string]interface{})
	openResParam := base.OpenResParamJson{param["resourceid"].(string),
		uint32(param["timeout"].(float64))}

	if chn.CheckResIsOpen(clientID) {
		chn.sendProto(clientID,
			base.OPEN_RESOURCE_CMD,
			"", base.OPEN303,
			"huamail.defualt")
		return
	}

	mylog.GetErrorLogger().Println("Chn Open Resource ID ", openResParam.ID)

	if openResParam.TimeOut < 120 { //当超时时间大于120s,设置超时为默认

		timeOut = openResParam.TimeOut
	}

	for delayWait := uint32(0); delayWait < timeOut; delayWait++ {

		resourcer = GetResourcerByID(openResParam.ID)
		if resourcer != nil {
			break
		}

		time.Sleep(1 * time.Second)
	}

	if resourcer == nil {
		mylog.GetErrorLogger().Println("No Found Source ", openResParam.ID)

		chn.sendProto(clientID, base.OPEN_RESOURCE_CMD, "", base.NOFOUNF404, request.Ns)
		ReleaseResourcer(resourcer)

		return
	}

	clientDataID := new(ResourceClient)
	clientDataID.ClientInf = chn
	clientDataID.ClientID = clientID

	_, responseErr = resourcer.Open(clientDataID, request, token)
	responseJson := base.ResponseJson{request.Ns, request.Method, "", responseErr}
	b, err := json.Marshal(responseJson)
	if err != nil {
		mylog.GetErrorLogger().Println("handleOpenResource : ", err)
		ReleaseResourcer(resourcer)
		return
	}

	proto := new(base.Proto)
	proto.RD.BaseHD.CommandId = 0x80 | base.OPEN_RESOURCE_CMD
	proto.RD.HD.ClientIdent = clientID

	proto.EncodeBody(b)

	if !chn.Send(proto) {
		ReleaseResourcer(resourcer)
		return
	}

	if !strings.EqualFold(responseErr, base.OK) {
		ReleaseResourcer(resourcer)
		return
	}

	{
		chn.resMutex.Lock()
		defer chn.resMutex.Unlock()
		chn.openRes[clientID] = resourcer
	}
	stat.GetLocalStatistInst().OpenRes()

}