Пример #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 on_posup(c *gotcp.Conn, p *ShaPacket) {
	log.Println("posupin")
	posup_pkg := p.Packet.(*protocol.PosUpPacket)
	if posup_pkg.WifiCount > 2 {
		posup_pkg.LocationTime = time.Now().Format("060102150405")
		sql := fmt.Sprintf("INSERT INTO t_posup_log(id, imme,location_time,datainfo,accesstype) VALUES ('%s',to_timestamp('%s','YYMMDDhh24miss'),'%s','%s')",
			posup_pkg.IMEI, posup_pkg.LocationTime, posup_pkg.Wifi, 1)
		log.Println("heihei", sql)
		GetServer().Dbsrv.Insert(sql)
		c.AsyncWritePacket(p, time.Second)
	} else if posup_pkg.GPSFlag != "" {
		c.AsyncWritePacket(p, time.Second)
		log.Println("long", posup_pkg.Longitude)
		if posup_pkg.Longitude != "" {
			log.Println("-----tag 2")
			posup_pkg.LocationTime = time.Now().Format("060102150405")
			sql := fmt.Sprintf("INSERT INTO t_posup_log(imme,location_time,glat,glong) VALUES ('%s',to_timestamp('%s','YYMMDDhh24miss'),'%s','%s')",
				posup_pkg.IMEI, posup_pkg.LocationTime, posup_pkg.Latitude, posup_pkg.Longitude)
			log.Println(sql)
			GetServer().Dbsrv.Insert(sql)
		} else if posup_pkg.WifiCount > 2 {
			log.Println("-----tag 3")
			posup_pkg.LocationTime = time.Now().Format("060102150405")
			sql := fmt.Sprintf("INSERT INTO t_posup_log(imme,location_time,datainfo,accesstype) VALUES ('%s',to_timestamp('%s','YYMMDDhh24miss'),'%s','%s')",
				posup_pkg.IMEI, posup_pkg.LocationTime, posup_pkg.Wifi, 1)
			log.Println("heihei", sql)
			GetServer().Dbsrv.Insert(sql)
		}
	}
}
Пример #3
0
func (this *Callback) OnConnect(c *gotcp.Conn) bool {
	conn := NewConn(c)

	c.PutExtraData(conn)

	return true
}
Пример #4
0
func (this *DasCallback) OnConnect(c *gotcp.Conn) bool {
	addr := c.GetRawConn().RemoteAddr()
	c.PutExtraData(addr)
	fmt.Println("OnConnect:", addr)

	return true
}
Пример #5
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:
			}
		}
	}

}
Пример #6
0
func on_warnup(c *gotcp.Conn, p *ShaPacket) {
	c.AsyncWritePacket(p, time.Second)

	warnup_pkg := p.Packet.(*protocol.WarnUpPacket)
	sql := fmt.Sprintf("INSERT INTO t_warnup_log(imme,warnstyle,warn_time) VALUES ('%s','%s',to_timestamp('%s','YYMMDDhh24miss'))",
		warnup_pkg.IMEI, warnup_pkg.WarnStyle, time.Now().Format("060102150405"))
	log.Println(sql)
	GetServer().Dbsrv.Insert(sql)

	time.AfterFunc(1*time.Second, func() {
		locate_pkg := protocol.Parse_Locate(warnup_pkg.Encryption, warnup_pkg.IMEI, warnup_pkg.SerialNumber)
		c.AsyncWritePacket(locate_pkg, time.Second)
	})

}
Пример #7
0
func (this *Callback) OnConnect(c *gotcp.Conn) bool {
	heartbeat := GetConfiguration().GetServerConnCheckInterval()
	readlimit := GetConfiguration().GetServerReadLimit()
	writelimit := GetConfiguration().GetServerWriteLimit()
	config := &ConnConfig{
		HeartBeat:  uint8(heartbeat),
		ReadLimit:  int64(readlimit),
		WriteLimit: int64(writelimit),
	}
	conn := NewConn(c, config)

	c.PutExtraData(conn)

	//NewConns().Add(conn)
	conn.Do()

	return true
}
Пример #8
0
func (this *Callback) OnConnect(c *gotcp.Conn) bool {
	checkinterval := GetConfiguration().GetServerConnCheckInterval()
	readlimit := GetConfiguration().GetServerReadLimit()
	writelimit := GetConfiguration().GetServerWriteLimit()
	config := &ConnConfig{
		ConnCheckInterval: uint16(checkinterval),
		ReadLimit:         uint16(readlimit),
		WriteLimit:        uint16(writelimit),
	}
	conn := NewConn(c, config)

	c.PutExtraData(conn)

	conn.Do()
	NewConns().Add(conn)

	return true
}
Пример #9
0
func (this *Callback) OnMessage(c *gotcp.Conn, p gotcp.Packet) bool {
	shaPacket := p.(*ShaPacket)
	log.Println("onmessage packettype", shaPacket.Type)
	switch shaPacket.Type {
	case protocol.Login:
		on_login(c, shaPacket)
	case protocol.HeartBeat:
		c.AsyncWritePacket(shaPacket, time.Second)
	case protocol.PosUp:
		on_posup(c, shaPacket)
	case protocol.Echo:
		c.AsyncWritePacket(shaPacket, time.Second)
	case protocol.WarnUp:
		on_warnup(c, shaPacket)
	}

	return true
}
Пример #10
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
}
Пример #11
0
func (this *Callback) OnMessage(c *gotcp.Conn, p gotcp.Packet) bool {
	bedPacket := p.(*BedPacket)
	switch bedPacket.Type {
	case Login:
		c.AsyncWritePacket(bedPacket, time.Second)
	case HeartBeat:
		c.AsyncWritePacket(bedPacket, time.Second)
	case AppControlFeedback:
		GetServer().GetProducer().Send(GetServer().GetTopic(), p.Serialize())
	case HandleControlFeedback:
		GetServer().GetProducer().Send(GetServer().GetTopic(), p.Serialize())
	case AppPottyFeedback:
		GetServer().GetProducer().Send(GetServer().GetTopic(), p.Serialize())
	case HandlePottyFeedback:
		GetServer().GetProducer().Send(GetServer().GetTopic(), p.Serialize())
	case AfterPotty:
		GetServer().GetProducer().Send(GetServer().GetTopic(), p.Serialize())
	case AppBedReset:
		GetServer().GetProducer().Send(GetServer().GetTopic(), p.Serialize())
	}

	return true
}
Пример #12
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
			}
		}
	}
}
Пример #13
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)
	})
}
Пример #14
0
func (this *TelnetCallback) OnMessage(c *gotcp.Conn, p gotcp.Packet) bool {
	packet := p.(*TelnetPacket)
	command := packet.GetData()
	commandType := packet.GetType()

	switch commandType {
	case "echo":
		c.AsyncWritePacket(NewTelnetPacket("echo", command), 0)
	case "login":
		c.AsyncWritePacket(NewTelnetPacket("login", []byte(string(command)+" has login")), 0)
	case "quit":
		return false
	default:
		c.AsyncWritePacket(NewTelnetPacket("unknow", []byte("unknow command")), 0)
	}

	return true
}
Пример #15
0
func (this *TelnetCallback) OnClose(c *gotcp.Conn) {
	fmt.Println("OnClose:", c.GetExtraData())
}
Пример #16
0
func (this *Callback) OnClose(c *gotcp.Conn) {
	conn := c.GetExtraData().(*Conn)
	conn.Close()
}
Пример #17
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
			}
		}
	}
}
Пример #18
0
func (this *Callback) OnClose(c *gotcp.Conn) {
	conn := c.GetExtraData().(*Conn)
	conn.Close()
	NewConns().Remove(conn.GetBedID())
	NewBedHub().Remove(conn.GetBedID())
}
Пример #19
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
		}
	}

}
Пример #20
0
func (this *DasCallback) OnMessage(c *gotcp.Conn, p gotcp.Packet) bool {
	// 0xBA command feedback 0xBB heartbeat 0xBC login
	// 0xBA cmdtype(1-8) status(0/1) serialid
	daspacket := p.(*DasPacket)
	command := daspacket.GetData()
	commandtype := daspacket.GetType()
	fmt.Println("----onmessage ", command)
	switch commandtype {
	case 0xBA:
		var result []byte
		var cmdop byte
		cmdop = command[0]*10 + command[1]
		result = append(result, cmdop)
		result = append(result, c.GetMac()...)
		result = append(result, command[3:7]...)
		result = append(result, command[2])
		c.Send(c.GetTopic(), result)

		fmt.Printf("-----recv should up the result%x \n", result)
	case 0xBB:
		c.SetTimeFlag(time.Now().Unix())
		c.AsyncWritePacket(NewDasPacket(0xAB, command), time.Second)
	case 0xBC:
		c.SetID(getMac(command), c.GetIndex())
		c.AsyncWritePacket(NewDasPacket(0xAC, command), time.Second)
	default:
		gktoolkit.Trace()
	}

	return true
}
Пример #21
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:
			}
		}
	}

}
Пример #22
0
func (this *Callback) OnMessage(c *gotcp.Conn, p gotcp.Packet) bool {
	echoPacket := p.(*echo.EchoPacket)
	fmt.Printf("OnMessage:[%v] [%v]\n", echoPacket.GetLength(), string(echoPacket.GetBody()))
	c.AsyncWritePacket(echo.NewEchoPacket(echoPacket.Serialize(), true), time.Second)
	return true
}