Пример #1
0
func (this *MattressProtocol) ReadPacket(c *gotcp.Conn) (gotcp.Packet, error) {
	smconn := c.GetExtraData().(*Conn)
	buffer := smconn.GetBuffer()

	conn := c.GetRawConn()
	for {
		data := make([]byte, 2048)
		readLengh, err := conn.Read(data)
		log.Printf("%X\n", data[0:readLengh])

		if err != nil {
			return nil, err
		}

		if readLengh == 0 {
			return nil, gotcp.ErrConnClosing
		} else {
			buffer.Write(data[0:readLengh])
			cmdid, pkglen := CheckProtocol(buffer)
			//		log.Printf("recv box cmd %d \n", cmdid)

			pkgbyte := make([]byte, pkglen)
			buffer.Read(pkgbyte)
			switch cmdid {
			case ReportStatus:
				return ParseReportStatus(pkgbyte), nil
			case HalfPack:
			case Illegal:
			}
		}
	}
}
Пример #2
0
func (this *ShaProtocol) ReadPacket(c *gotcp.Conn) (gotcp.Packet, error) {
	smconn := c.GetExtraData().(*Conn)
	smconn.UpdateReadflag()

	buffer := smconn.GetBuffer()

	conn := c.GetRawConn()
	for {
		data := make([]byte, 2048)
		readLengh, err := conn.Read(data)

		if err != nil {
			return nil, err
		}

		if readLengh == 0 {
			return nil, gotcp.ErrConnClosing
		} else {
			buffer.Write(data[0:readLengh])
			cmdid, pkglen := CheckProtocol(buffer)

			pkgbyte := make([]byte, pkglen)
			buffer.Read(pkgbyte)
			switch cmdid {
			case Login:
				pkg := ParseLogin(pkgbyte, smconn)
				return NewShaPacket(Login, pkg), nil
			case HeartBeat:
				pkg := ParseHeart(pkgbyte)
				return NewShaPacket(HeartBeat, pkg), nil
			case SendDeviceList:
				pkg := ParseDeviceList(pkgbyte, smconn)
				return NewShaPacket(SendDeviceList, pkg), nil
			case OperateFeedback:
				pkg := ParseFeedback(pkgbyte)
				return NewShaPacket(OperateFeedback, pkg), nil
			case Warn:
				pkg := ParseWarn(pkgbyte)
				return NewShaPacket(Warn, pkg), nil
			case AddDelDevice:
				pkg := ParseAddDelDevice(pkgbyte)
				return NewShaPacket(AddDelDevice, pkg), nil
			case SetDevicenameFeedback:
				pkg := ParseFeedbackSetDevicename(pkgbyte)
				return NewShaPacket(SetDevicenameFeedback, pkg), nil
			case DelDeviceFeedback:
				pkg := ParseFeedbackDelDevice(pkgbyte)
				return NewShaPacket(DelDeviceFeedback, pkg), nil

			case Illegal:
			case HalfPack:
			}
		}
	}

}
Пример #3
0
func on_login(c *gotcp.Conn, p *ShaPacket) {
	conn := c.GetExtraData().(*Conn)
	conn.Status = ConnSuccess
	loginPkg := p.Packet.(*protocol.LoginPacket)
	conn.IMEI = loginPkg.IMEI
	conn.ID, _ = strconv.ParseUint(loginPkg.IMEI, 10, 64)
	NewConns().SetID(conn.ID, conn.index)
	c.AsyncWritePacket(p, time.Second)
	time.AfterFunc(1*time.Second, func() {
		set_interval_pkg := protocol.Parse_Set_Interval(loginPkg.Encryption, loginPkg.IMEI, loginPkg.SerialNumber)
		c.AsyncWritePacket(set_interval_pkg, time.Second)
	})
}
Пример #4
0
func (this *TelnetCallback) OnClose(c *gotcp.Conn) {
	fmt.Println("OnClose:", c.GetExtraData())
}
Пример #5
0
func (this *Callback) OnClose(c *gotcp.Conn) {
	conn := c.GetExtraData().(*Conn)
	conn.Close()
}
Пример #6
0
func (this *Callback) OnClose(c *gotcp.Conn) {
	conn := c.GetExtraData().(*Conn)
	conn.Close()
	NewConns().Remove(conn.GetBedID())
	NewBedHub().Remove(conn.GetBedID())
}
Пример #7
0
func (this *BedProtocol) ReadPacket(c *gotcp.Conn) (gotcp.Packet, error) {
	smconn := c.GetExtraData().(*Conn)

	buffer := smconn.GetBuffer()

	conn := c.GetRawConn()
	for {
		data := make([]byte, 2048)
		readLengh, err := conn.Read(data)
		log.Printf("recv %x", data[0:readLengh])

		if err != nil {
			return nil, err
		}

		if readLengh == 0 {
			return nil, gotcp.ErrConnClosing
		} else {
			buffer.Write(data[0:readLengh])
			cmdid, pkglen := CheckProtocol(buffer)
			log.Println(cmdid)
			log.Println(NewConns().Check(0))
			if cmdid != Login && !NewConns().Check(smconn.GetBedID()) {
				return nil, ErrNotLogin
			}

			smconn.UpdateReadflag()

			pkgbyte := make([]byte, pkglen)
			buffer.Read(pkgbyte)
			switch cmdid {
			case Login:
				pkg := ParseLogin(pkgbyte, smconn)
				return NewBedPacket(Login, pkg), nil
			case HeartBeat:
				pkg := ParseHeart(pkgbyte)
				return NewBedPacket(HeartBeat, pkg), nil
			case AppControlFeedback:
				pkg := ParseAppControlFeedback(pkgbyte, smconn, AppControlFeedback)
				return NewBedPacket(AppControlFeedback, pkg), nil
			case HandleControlFeedback:
				bedpkg := ParseHandleControlFeedback(pkgbyte)
				smconn.SendToBed(bedpkg)
				pkg := ParseAppControlFeedback(pkgbyte, smconn, HandleControlFeedback)
				return NewBedPacket(HandleControlFeedback, pkg), nil
			case AppPottyFeedback:
				pkg := ParsePottyFeedback(pkgbyte, smconn, AppPottyFeedback)
				return NewBedPacket(AppPottyFeedback, pkg), nil
			case HandlePottyFeedback:
				bedpkg := ParseHandlePottyFeedback(pkgbyte)
				smconn.SendToBed(bedpkg)
				pkg := ParsePottyFeedback(pkgbyte, smconn, HandlePottyFeedback)
				return NewBedPacket(HandlePottyFeedback, pkg), nil
			case AfterPotty:
				bedpkg := ParseAfterPottyTobedFeedback(pkgbyte)
				smconn.SendToBed(bedpkg)
				pkg := ParseAfterPottyFeedback(pkgbyte, smconn)
				return NewBedPacket(AfterPotty, pkg), nil
			case AppBedReset:
				pkg := ParseAppControlFeedback(pkgbyte, smconn, AppBedReset)
				return NewBedPacket(AppBedReset, pkg), nil

			case Illegal:
			case HalfPack:
			}
		}
	}

}
Пример #8
0
func (this *ShaProtocol) ReadPacket(c *gotcp.Conn) (gotcp.Packet, error) {
	smconn := c.GetExtraData().(*Conn)
	var once sync.Once
	once.Do(smconn.UpdateReadflag)

	buffer := smconn.GetBuffer()

	conn := c.GetRawConn()
	for {
		if smconn.ReadMore {
			data := make([]byte, 2048)
			readLengh, err := conn.Read(data)
			log.Printf("<IN>    %x\n", data[0:readLengh])
			if err != nil {
				return nil, err
			}

			if readLengh == 0 {
				return nil, gotcp.ErrConnClosing
			}
			buffer.Write(data[0:readLengh])
		}

		cmdid, pkglen := protocol.CheckProtocol(buffer)

		pkgbyte := make([]byte, pkglen)
		buffer.Read(pkgbyte)
		switch cmdid {
		case protocol.Login:
			pkg := protocol.ParseLogin(pkgbyte)
			smconn.ReadMore = false
			return NewShaPacket(protocol.Login, pkg), nil
		case protocol.HeartBeat:
			pkg := protocol.ParseHeart(pkgbyte, smconn.ID)
			smconn.ReadMore = false
			return NewShaPacket(protocol.HeartBeat, pkg), nil
		case protocol.Add_Del_Device:
			pkg := protocol.Parse_Add_Del_Device(pkgbyte, smconn.ID)
			smconn.ReadMore = false
			return NewShaPacket(protocol.Add_Del_Device, pkg), nil
		case protocol.Notification:
			pkg := protocol.Parse_Notification(pkgbyte, smconn.ID)
			smconn.ReadMore = false
			return NewShaPacket(protocol.Notification, pkg), nil
		case protocol.Feedback_SetName:
			pkg := protocol.Parse_Feedback_SetName(pkgbyte, smconn.ID)
			smconn.ReadMore = false
			return NewShaPacket(protocol.Feedback_SetName, pkg), nil
		case protocol.Feedback_Del_Device:
			pkg := protocol.Parse_Feedback_Del_Device(pkgbyte, smconn.ID)
			smconn.ReadMore = false
			return NewShaPacket(protocol.Feedback_Del_Device, pkg), nil
		case protocol.Feedback_Query_Attr:
			pkg := protocol.Parse_Feedback_Query_Attr(pkgbyte, smconn.ID)
			smconn.ReadMore = false
			return NewShaPacket(protocol.Feedback_Query_Attr, pkg), nil
		case protocol.Feedback_Depolyment:
			pkg := protocol.Parse_Feedback_Deployment(pkgbyte, smconn.ID)
			smconn.ReadMore = false
			return NewShaPacket(protocol.Feedback_Depolyment, pkg), nil
		case protocol.Feedback_OnOff:
			pkg := protocol.Parse_Feedback_Onoff(pkgbyte, smconn.ID)
			smconn.ReadMore = false
			return NewShaPacket(protocol.Feedback_OnOff, pkg), nil

		case protocol.Feedback_Level_Control:
			pkg := protocol.Parse_Feedback_Level_Control(pkgbyte, smconn.ID)
			smconn.ReadMore = false
			return NewShaPacket(protocol.Feedback_Level_Control, pkg), nil

		case protocol.Illegal:
			smconn.ReadMore = true
		case protocol.HalfPack:
			smconn.ReadMore = true
		}
	}

}