本文整理匯總了Golang中github.com/giskook/gotcp.Conn類的典型用法代碼示例。如果您正苦於以下問題:Golang Conn類的具體用法?Golang Conn怎麽用?Golang Conn使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Conn類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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:
}
}
}
}
示例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)
}
}
}
示例3: OnConnect
func (this *Callback) OnConnect(c *gotcp.Conn) bool {
conn := NewConn(c)
c.PutExtraData(conn)
return true
}
示例4: OnConnect
func (this *DasCallback) OnConnect(c *gotcp.Conn) bool {
addr := c.GetRawConn().RemoteAddr()
c.PutExtraData(addr)
fmt.Println("OnConnect:", addr)
return true
}
示例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:
}
}
}
}
示例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)
})
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
}
}
}
示例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)
})
}
示例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
}
示例15: OnClose
func (this *TelnetCallback) OnClose(c *gotcp.Conn) {
fmt.Println("OnClose:", c.GetExtraData())
}