Beispiel #1
0
func (r *Resourcer) broadcastData(proto *base.Proto) {

	r.mutex.Lock()
	defer r.mutex.Unlock()

	if !atomic.CompareAndSwapUint32(&r.valid, 1, 1) {
		return
	}

	tmpClientDataChn := make([]*ResourceClient, 0)
	for _, chn := range r.ClientDataChn {

		sendLen := uint64(proto.RD.HD.BodyLen)
		stat.GetLocalStatistInst().RecvData(sendLen)
		sendP := base.GetProto()
		sendP.RD = proto.RD
		sendP.RD.BaseHD.CommandId |= 0x80
		sendP.BD = proto.BD
		sendP.RD.HD.ClientIdent = chn.ClientID

		if chn.ClientInf.Send(sendP) {
			tmpClientDataChn = append(tmpClientDataChn, chn)
			stat.GetLocalStatistInst().SendData(sendLen)
		}
	}
	r.ClientDataChn = tmpClientDataChn
}
Beispiel #2
0
func main() {

	// 减少内存分配

	parseArg()
	devCount := oneEnvParam.ResourceCount
	stat.GetLocalStatistInst().Init("pu_llog", "logic.dat", int32(devCount))
	go stat.GetLocalStatistInst().Start()
	mylog.GetErrorLogger().Init("pu_elog", "error.log")

	go stat.StartMonitorTask("pu_mlog", "monitor.dat")

	for i := oneEnvParam.BeginID; i <= devCount; i++ {

		sn := new(bytes.Buffer)
		fmt.Fprintf(sn, "%v", strconv.Itoa(int(i)))
		var pu *PUDevice = new(PUDevice)
		pu.run(sn.String())
	}

	for {
		time.Sleep(10 * time.Minute)
	}

}
Beispiel #3
0
// 内部访问接口
// 错误处理
func (chn *Channel) handleError(err error) {

	defer func() {
		if r := recover(); r != nil {
			mylog.GetErrorLogger().Println("Channel handleError Panic")
		}
	}()

	if err != nil {
		mylog.GetErrorLogger().Println("handleError", err.Error())
	}

	if atomic.CompareAndSwapUint32(&chn.valid, 0, 0) {
		return
	}

	atomic.CompareAndSwapUint32(&chn.valid, 1, 0)

	stat.GetLocalStatistInst().Off()

	func() {
		chn.resMutex.Lock()
		defer chn.resMutex.Unlock()
		for _, r := range chn.openRes {
			stat.GetLocalStatistInst().CloseRes()
			mylog.GetErrorLogger().Println(" release chn res ", r.GetID())
			clientDataID := new(ResourceClient)
			clientDataID.ClientInf = chn
			r.Close(clientDataID, "", true)
			ReleaseResourcer(r)
		}

		for _, v := range chn.registerRes {

			v.Unregister()
			ReleaseResourcer(v)
		}
	}()

	func() {

		fmt.Println("Chn Close")
		chn.chnMutex.Lock()
		defer chn.chnMutex.Unlock()
		chn.connSocket.Close()
		close(chn.rwChn)
		close(chn.notifyChn)
	}()

}
Beispiel #4
0
func (pu *PUDevice) handleError(err error) {
	mylog.GetErrorLogger().Println(err)
	if atomic.CompareAndSwapUint32(&pu.Valid, 0, 0) {
		return
	}

	atomic.CompareAndSwapUint32(&pu.Valid, 1, 0)
	pu.connSocket.Close()
	stat.GetLocalStatistInst().Off()
	if atomic.CompareAndSwapUint32(&pu.SendFlag, 1, 1) {
		stat.GetLocalStatistInst().CloseRes()
	}
	close(pu.rwChan)
	go pu.ReRun(pu.SN)
}
Beispiel #5
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 #6
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 #7
0
func (chn *Channel) handleReqCloseRes(clientID uint32, data []byte) {

	mylog.GetErrorLogger().Println("handleReqCloseRes", string(data[4:]))
	defer func() {
		if r := recover(); r != nil {
			chn.sendProto(clientID,
				base.CLOSE_RESOURCE_CMD,
				"", base.PROTOERR400,
				"huamail.defualt")
			chn.handleError(base.DTerror{"handleReqCloseRes Panic"})
		}
	}()

	var responseResult, responseErr string = "", base.NOFOUNF404

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

	func() {
		chn.resMutex.Lock()
		defer chn.resMutex.Unlock()
		resourcer, ok := chn.openRes[clientID]

		if ok {
			mylog.GetErrorLogger().Println("Chn Close Resource ID ", resourcer.GetID())
			clientDataID := new(ResourceClient)
			clientDataID.ClientInf = chn
			clientDataID.ClientID = clientID
			responseResult, responseErr = resourcer.Close(clientDataID, "", false)
			ReleaseResourcer(resourcer)
			delete(chn.openRes, clientID)
		}
	}()

	proto := base.GetProto()
	proto.RD.BaseHD.CommandId = 0x80 | base.CLOSE_RESOURCE_CMD
	proto.RD.HD.ClientIdent = clientID

	responseJson := base.ResponseJson{request.Ns, request.Method, responseResult, responseErr}
	b, err := json.Marshal(responseJson)
	if err != nil {
		mylog.GetErrorLogger().Println("handleCloseResource : ", err)
	}
	proto.EncodeBody(b)

	chn.Send(proto)
	stat.GetLocalStatistInst().CloseRes()
}
Beispiel #8
0
func (pu *PUDevice) readTask() {
	defer func() {
		if r := recover(); r != nil {
			mylog.GetErrorLogger().Println("readTask panic")
			return
		}
	}()
	for {
		proto := new(base.Proto)

		err := proto.ReadBinaryProto(pu.connSocket)
		if err != nil {
			pu.handleError(err)

			break
		}

		cmdID := proto.RD.BaseHD.CommandId & 0x7f

		if cmdID == base.HEART_CMD {
			mylog.GetErrorLogger().Println("Recv And Send Heart", proto.RD.BaseHD.CommandId)
			heartProto := new(base.Proto)
			heartProto.RD.BaseHD.CommandId = 0x80 | cmdID
			pu.rwChan <- heartProto

		} else {
			mylog.GetErrorLogger().Println(string(proto.BD.Data[4:]))
			if cmdID == base.OPEN_RESOURCE_CMD {
				go pu.openSource(proto.RD.HD.ClientIdent)
			} else if cmdID == base.CLOSE_RESOURCE_CMD {
				go pu.closeSource(proto.RD.HD.ClientIdent)
			} else if cmdID == base.REGISTER_RESOURCE {
				var reponseJson base.ResponseJson
				resErr := json.Unmarshal(proto.BD.Data[4:], &reponseJson)
				if resErr != nil {
					pu.handleError(resErr)
					return
				}

				if reponseJson.Error != base.OK {
					pu.handleError(base.DTerror{"Resoure Fail"})
					fmt.Println(reponseJson.Error)
					return
				}
				stat.GetLocalStatistInst().RegisterRes()
			}
		}

	}
}
Beispiel #9
0
func main() {

	fmt.Println("DataTransfer Running!")

	server.ParseArg()

	right := base.VIDEO_RIGHT | base.AUDIO_RIGHT
	fmt.Println(right)
	var PKS1024 string = "s5DrLk2RE355BcO7FZ49xNdfAQtwKsMaEJa8yOnX7IjkRiOnmOoXCiCsRl0vpe23eZ7SWUW47DvQ1UxEpkGsFv/LOeOxMh06oXeH0zqlRPCw074q0s+IfRtcbdqGLvzvCz8ZAaoQrEIfwq2hm+7ueWKjWecqqQlQ5cu+dg3gd80="
	var E string = "AQAB"
	base.InitRsaBig(PKS1024, E)

	stat.GetLocalStatistInst().Init("dt_llog", "logic.dat", 0)
	mylog.GetErrorLogger().Init("dt_elog", "error.log")
	go stat.StartMonitorTask("dt_mlog", "monitor.dat")
	go stat.GetLocalStatistInst().Start()
	runtime.GOMAXPROCS(runtime.NumCPU())
	server.Run()

	exitChn := make(chan bool, 1)
	exit := <-exitChn
	fmt.Println("DataTransfer Running Over!", exit)
}
Beispiel #10
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 #11
0
func (pu *PUDevice) run(sn string) {
	pu.SN = sn
	connSocket, err := net.Dial("tcp", oneEnvParam.Url)
	if err != nil {
		fmt.Println("Connect ", err)
		go pu.ReRun(sn)
		return
	}
	pu.rwChan = make(chan *base.Proto, 24)

	stat.GetLocalStatistInst().On()

	pu.SendFlag = 0
	pu.Valid = 1
	pu.connSocket = connSocket
	go pu.Register()
	go pu.handleReadChnTask()
	go pu.readTask()

}
Beispiel #12
0
func (chn *Channel) Run(connSocket net.Conn) error {

	chn.rwChn = make(chan *base.Proto, 24)
	chn.notifyChn = make(chan notifyEvent, 1)
	chn.heartChn = make(chan bool, 1)

	chn.openRes = make(map[uint32]*Resourcer)
	chn.registerRes = make(map[string]*Resourcer)

	chn.connSocket = connSocket
	chn.valid = 1 // CU有效

	stat.GetLocalStatistInst().On()

	go chn.timerSendHeartTask()
	go chn.handleReadChnTask()
	go chn.handleReadSocketTask()
	go chn.handleNotifyTask()

	return nil
}
Beispiel #13
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()

}