func (chn *Channel) handleWriteSocket(proto *base.Proto) { if atomic.CompareAndSwapUint32(&chn.valid, 0, 0) { return } cmdID := proto.RD.BaseHD.CommandId & 0x7f if cmdID == base.HEART_CMD { heartHdr := new(base.BaseHeader) heartHdr.CommandId = proto.RD.BaseHD.CommandId msg := heartHdr.Encode() _, err := chn.connSocket.Write(msg.Bytes()) if err != nil { chn.handleError(base.DTerror{"Send Error"}) } return } fmt.Println("handleWriteSocket ", cmdID) msg := proto.EncodeHdr() _, err := chn.connSocket.Write(msg.Bytes()) if err != nil { chn.handleError(base.DTerror{"Send Error"}) } _, dErr := chn.connSocket.Write(proto.BD.Data) if dErr != nil { chn.handleError(base.DTerror{"Send Error"}) } base.PutProto(proto) }
func (chn *Channel) handleReadSocketTask() { var productID uint32 = 1 defer func() { if r := recover(); r != nil { chn.handleError(base.DTerror{"handleReadSocketTask Panic"}) } }() for { proto := base.GetProto() e := proto.ReadBinaryProto(chn.connSocket) if e != nil { chn.handleError(base.DTerror{"handleReadSocketTask"}) break } consumerID := proto.RD.HD.ClientIdent cmdID := proto.RD.BaseHD.CommandId & 0x7f isRequest := proto.RD.BaseHD.CommandId & 0x80 switch cmdID { case base.HEART_CMD: if isRequest == 0 { heartProto := base.GetProto() heartProto.RD.BaseHD.CommandId = base.HEART_CMD | 0x80 chn.Send(heartProto) } else { chn.heartChn <- true } case base.OPEN_RESOURCE_CMD: // 打开资源 if isRequest == 0 { //fmt.Println("打开资源") go chn.handleReqOpenRes(consumerID, proto.BD.Data) } else { //fmt.Println("回应资源") go chn.handleRspRes(proto) } case base.CLOSE_RESOURCE_CMD: // 关闭资源 if isRequest == 0 { go chn.handleReqCloseRes(consumerID, proto.BD.Data) } else { go chn.handleRspRes(proto) } case base.CRTL_RESOURCE_CMD: // 控制资源 case base.REGISTER_RESOURCE: // 注册资源 go chn.handleRegisterRes(productID, proto.BD.Data) productID++ case base.UNREGISTER_RESOURCE: // 注销资源 case base.DATA_STREAM: // 数据流 chn.handleRspRes(proto) default: go chn.handleOtherCMD(proto) } base.PutProto(proto) } //fmt.Println("End Cu Read Task") }