Example #1
0
func (this *TelnetCallback) OnConnect(c *gotcp.Conn) bool {
	addr := c.GetRawConn().RemoteAddr()
	c.PutExtraData(addr)
	fmt.Println("OnConnect:", addr)
	c.AsyncWritePacket(NewTelnetPacket("unknow", []byte("Welcome to this Telnet Server")), 0)
	return true
}
Example #2
0
func (this *DasCallback) OnConnect(c *gotcp.Conn) bool {
	addr := c.GetRawConn().RemoteAddr()
	c.PutExtraData(addr)
	fmt.Println("OnConnect:", addr)

	return true
}
Example #3
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:
			}
		}
	}
}
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:
			}
		}
	}

}
Example #5
0
func (this *DasProtocol) ReadPacket(goconn *gotcp.Conn) (gotcp.Packet, error) {
	conn := goconn.GetRawConn()
	for {
		data := make([]byte, 1024)
		readLengh, err := conn.Read(data)

		if err != nil { // EOF, or worse
			return nil, err
		}

		if readLengh == 0 { // Connection maybe cloased by the client
			return nil, gotcp.ErrConnClosing
		} else {
			goconn.GetRecvBytes().Write(data[:readLengh])
			fmt.Println(goconn.GetRecvBytes().Bytes())
			if goconn.GetRecvBytes().Bytes()[0] == 0xBA ||
				goconn.GetRecvBytes().Bytes()[0] == 0xBB ||
				goconn.GetRecvBytes().Bytes()[0] == 0xBC {
				if goconn.GetRecvBytes().Len() >= 8 {
					cmdtype, _ := goconn.GetRecvBytes().ReadByte()
					if cmdtype == 0xBA {
						result := goconn.GetRecvBytes().Next(6) // cmdtype + result + serialid
						goconn.GetRecvBytes().Next(1)
						return NewDasPacket(0xBA, result), nil
					} else if cmdtype == 0xBB {
						mac := goconn.GetRecvBytes().Next(6)
						goconn.GetRecvBytes().Next(1)
						return NewDasPacket(0xBB, mac), nil
					} else if cmdtype == 0xBC {
						mac := goconn.GetRecvBytes().Next(6)
						goconn.GetRecvBytes().Next(1)
						return NewDasPacket(0xBC, mac), nil
					}
				} else {
					return nil, gotcp.ErrReadHalf
				}
			} else {
				goconn.GetRecvBytes().Reset()
				return nil, gotcp.ErrReadHalf
			}
		}
	}
}
Example #6
0
func (this *NsqProtocol) ReadPacket(goconn *gotcp.Conn) (gotcp.Packet, error) {

	conn := goconn.GetRawConn()
	fullBuf := bytes.NewBuffer([]byte{})
	fmt.Println("Read packet")
	for {
		data := make([]byte, 1024)
		readLengh, err := conn.Read(data)

		if err != nil { // EOF, or worse
			return nil, err
		}

		if readLengh == 0 { // Connection maybe cloased by the client
			return nil, gotcp.ErrConnClosing
		} else {
			fullBuf.Write(data[:readLengh])
			cmdtype, err := fullBuf.ReadByte()
			if err != nil {
				return nil, err
			}
			if cmdtype == 0xBA {
				result := fullBuf.Next(7)
				//			end := fullBuf.Next(1)
				return NewDasPacket(0xBA, result), nil
			} else if cmdtype == 0xBB {
				mac := fullBuf.Next(6)
				//		end := fullBuf.Next(1)
				return NewDasPacket(0xBB, mac), nil
			} else if cmdtype == 0xBC {
				mac := fullBuf.Next(6)
				//	end := fullBuf.Next(1)
				return NewDasPacket(0xBC, mac), nil
			}
		}
	}
}
Example #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:
			}
		}
	}

}
Example #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
		}
	}

}