• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang gotcp.Conn类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/giskook/gotcp.Conn的典型用法代码示例。如果您正苦于以下问题:Golang Conn类的具体用法?Golang Conn怎么用?Golang Conn使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Conn类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: ReadPacket

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:
			}
		}
	}
}
开发者ID:GisKook,项目名称:mattress,代码行数:32,代码来源:protocol.go


示例2: on_posup

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)
		}
	}
}
开发者ID:GisKook,项目名称:watch_xixun,代码行数:30,代码来源:eventhandler.go


示例3: OnConnect

func (this *Callback) OnConnect(c *gotcp.Conn) bool {
	conn := NewConn(c)

	c.PutExtraData(conn)

	return true
}
开发者ID:GisKook,项目名称:mattress,代码行数:7,代码来源:conn.go


示例4: OnConnect

func (this *DasCallback) OnConnect(c *gotcp.Conn) bool {
	addr := c.GetRawConn().RemoteAddr()
	c.PutExtraData(addr)
	fmt.Println("OnConnect:", addr)

	return true
}
开发者ID:GisKook,项目名称:gobed,代码行数:7,代码来源:dasProtocol.go


示例5: ReadPacket

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:
			}
		}
	}

}
开发者ID:huoyan108,项目名称:smarthome-access,代码行数:56,代码来源:protocolsha.go


示例6: on_warnup

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)
	})

}
开发者ID:GisKook,项目名称:watch_xixun,代码行数:15,代码来源:eventhandler.go


示例7: OnConnect

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
}
开发者ID:GisKook,项目名称:bed,代码行数:18,代码来源:conn.go


示例8: OnConnect

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
}
开发者ID:GisKook,项目名称:watch_xixun,代码行数:18,代码来源:eventhandler.go


示例9: OnMessage

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
}
开发者ID:GisKook,项目名称:watch_xixun,代码行数:18,代码来源:eventhandler.go


示例10: OnConnect

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
}
开发者ID:GisKook,项目名称:gobed,代码行数:7,代码来源:telnetProtocol.go


示例11: OnMessage

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
}
开发者ID:GisKook,项目名称:bed,代码行数:23,代码来源:conn.go


示例12: ReadPacket

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
			}
		}
	}
}
开发者ID:GisKook,项目名称:gobed,代码行数:37,代码来源:nsqProtocol.go


示例13: on_login

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)
	})
}
开发者ID:GisKook,项目名称:watch_xixun,代码行数:13,代码来源:eventhandler.go


示例14: OnMessage

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
}
开发者ID:GisKook,项目名称:gobed,代码行数:18,代码来源:telnetProtocol.go


示例15: OnClose

func (this *TelnetCallback) OnClose(c *gotcp.Conn) {
	fmt.Println("OnClose:", c.GetExtraData())
}
开发者ID:GisKook,项目名称:gobed,代码行数:3,代码来源:telnetProtocol.go


示例16: OnClose

func (this *Callback) OnClose(c *gotcp.Conn) {
	conn := c.GetExtraData().(*Conn)
	conn.Close()
}
开发者ID:GisKook,项目名称:mattress,代码行数:4,代码来源:conn.go


示例17: ReadPacket

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
			}
		}
	}
}
开发者ID:GisKook,项目名称:gobed,代码行数:43,代码来源:dasProtocol.go


示例18: OnClose

func (this *Callback) OnClose(c *gotcp.Conn) {
	conn := c.GetExtraData().(*Conn)
	conn.Close()
	NewConns().Remove(conn.GetBedID())
	NewBedHub().Remove(conn.GetBedID())
}
开发者ID:GisKook,项目名称:bed,代码行数:6,代码来源:conn.go


示例19: ReadPacket

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

}
开发者ID:GisKook,项目名称:smarthome-access,代码行数:78,代码来源:protocolsha.go


示例20: OnMessage

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
}
开发者ID:GisKook,项目名称:gobed,代码行数:31,代码来源:dasProtocol.go



注:本文中的github.com/giskook/gotcp.Conn类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang utils.Command类代码示例发布时间:2022-05-23
下一篇:
Golang models.RedisLogger类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap