// Heart 测试 func tstHeart() { tstfun := "tstHeart" util.LogInfo("<<<<<<%s TEST", tstfun) syn := &pushproto.Talk{ Type: pushproto.Talk_HEART.Enum(), } data, err := proto.Marshal(syn) if err != nil { util.LogError("%s ERROR:proto marshal error:%s", tstfun, err) return } sb := util.Packdata(data) tstErr(tstfun, sb, 1, func(pb *pushproto.Talk) { pb_type := pb.GetType() if pb_type == pushproto.Talk_HEART { util.LogInfo(">>>>>>%s PASS", tstfun) } else { util.LogError("%s ERROR", tstfun) } }) }
func (self *Client) proto(data []byte) { fun := "Client.proto" pb := &pushproto.Talk{} err := proto.Unmarshal(data, pb) if err != nil { slog.Warnf("%s unmarshaling error: %s", fun, err) self.errNotifyCLOSED("package unmarshaling error") return } slog.Debugf("%s recv proto: %s", fun, pb) pb_type := pb.GetType() if pb_type == pushproto.Talk_SYN { self.recvSYN(pb) } else if pb_type == pushproto.Talk_ECHO { self.Send(util.Packdata(data)) } else if pb_type == pushproto.Talk_HEART { self.sendHEART() } else if pb_type == pushproto.Talk_ACK { self.recvACK(pb) } }
// Echo 测试 func tstEcho() { tstfun := "tstEcho" util.LogInfo("<<<<<<%s TEST", tstfun) syn := &pushproto.Talk{ Type: pushproto.Talk_ECHO.Enum(), Extdata: []byte("JUST ECHO"), } data, err := proto.Marshal(syn) if err != nil { util.LogError("%s ERROR:proto marshal error:%s", tstfun, err) return } sb := util.Packdata(data) tstErr(tstfun, sb, 1, func(pb *pushproto.Talk) { pb_type := pb.GetType() if pb_type == pushproto.Talk_ECHO { util.LogInfo(">>>>>>%s PASS: %s", tstfun, string(pb.GetExtdata())) } else { util.LogError("%s ERROR", tstfun) } }) }
// 连接建立,clientid获取 func tstSyn() { tstfun := "tstSyn" util.LogInfo("<<<<<<%s TEST", tstfun) syn := &pushproto.Talk{ Type: pushproto.Talk_SYN.Enum(), Appid: proto.String("shawn"), Installid: proto.String("1cf52f542ec2f6d1e96879bd6f5243da3baa42e4"), Auth: proto.String("F**k"), Clienttype: proto.String("Android"), Clientver: proto.String("1.0.0"), } data, err := proto.Marshal(syn) if err != nil { util.LogError("%s ERROR:syn proto marshal error:%s", tstfun, err) return } sb := util.Packdata(data) tstErr(tstfun, sb, 1, func(pb *pushproto.Talk) { pb_type := pb.GetType() if pb_type == pushproto.Talk_SYNACK { util.LogInfo(">>>>>>%s PASS: client_id:%s", tstfun, pb.GetClientid()) } else { util.LogError("%s ERROR", tstfun) } }) }
func (self *Client) SendBussiness(ziptype int32, datatype int32, data []byte) (uint64, string) { fun := "Client.SendBussiness" msgid, err := self.manager.Msgid() if err != nil { slog.Errorf("%s get msgid error:%s", fun, err) return 0, self.remoteaddr } buss := &pushproto.Talk{ Type: pushproto.Talk_BUSSINESS.Enum(), Msgid: proto.Uint64(msgid), Ziptype: proto.Int32(ziptype), Datatype: proto.Int32(datatype), Bussdata: data, } spb, err := proto.Marshal(buss) if err != nil { slog.Errorf("%s marshaling error: ", fun, err) return 0, self.remoteaddr } p := util.Packdata(spb) self.sendBussRetry(msgid, p) slog.Infof("%s client:%s send msgid:%d", fun, self, msgid) self.Send(p) return msgid, self.remoteaddr }
// 多个连接使用同样的clientid,老的被剔除 func tstDupClient() { tstfun := "tstDupClient" util.LogInfo("<<<<<<%s TEST", tstfun) syn := &pushproto.Talk{ Type: pushproto.Talk_SYN.Enum(), Appid: proto.String("shawn"), Installid: proto.String("1cf52f542ec2f6d1e96879bd6f5243da3baa42e4"), Auth: proto.String("F**k"), Clienttype: proto.String("Android"), Clientver: proto.String("1.0.0"), } data, err := proto.Marshal(syn) if err != nil { util.LogError("%s ERROR:syn proto marshal error:%s", tstfun, err) return } sb := util.Packdata(data) first_conn_read := 0 go tstErr(tstfun, sb, 10, func(pb *pushproto.Talk) { pb_type := pb.GetType() if first_conn_read == 0 { if pb_type == pushproto.Talk_SYNACK { util.LogInfo("%s First Conn: client_id:%s", tstfun, pb.GetClientid()) } else { util.LogError("%s First Conn ERROR", tstfun) return } first_conn_read += 1 } else { if pb_type == pushproto.Talk_ERR { util.LogInfo(">>>>>>%s First Conn PASS: msg:%s", tstfun, pb.GetExtdata()) } else { util.LogError("%s First Conn ERROR", tstfun) return } } }) time.Sleep(1000 * 1000 * 1000 * 5) tstErr(tstfun, sb, 1, func(pb *pushproto.Talk) { pb_type := pb.GetType() if pb_type == pushproto.Talk_SYNACK { util.LogInfo(">>>>>>%s Second Conn PASS: client_id:%s", tstfun, pb.GetClientid()) } else { util.LogError("%s Second Conn ERROR", tstfun) } }) }
func (self *Client) sendHEART() { synack := &pushproto.Talk{ Type: pushproto.Talk_HEART.Enum(), } data, _ := proto.Marshal(synack) self.Send(util.Packdata(data)) }
func (self *Client) sendSYNACK(client_id string) { synack := &pushproto.Talk{ Type: pushproto.Talk_SYNACK.Enum(), Clientid: proto.String(client_id), } data, _ := proto.Marshal(synack) self.Send(util.Packdata(data)) }
func (self *Client) errNotifyCLOSED(errmsg string) { fun := "Client.errNotifyCLOSED" //slog.Debug("errmsg:%s", errmsg) errpb := &pushproto.Talk{ Type: pushproto.Talk_ERR.Enum(), Extdata: []byte(errmsg), } slog.Debugf("%s errmsg:%s", fun, errpb) data, _ := proto.Marshal(errpb) self.SendClose(util.Packdata(data)) }
// 长度为1数据包 func tstErrOneSizePack() { tstfun := "tstErrOneSizePack" util.LogInfo("<<<<<<%s TEST", tstfun) sb := util.Packdata([]byte("1")) tstErr(tstfun, sb, 1, func(pb *pushproto.Talk) { pb_type := pb.GetType() if pb_type == pushproto.Talk_ERR { util.LogInfo(">>>>>>%s PASS: msg:%s", tstfun, pb.GetExtdata()) } else { util.LogError("%s ERROR", tstfun) } }) }
// 业务数据包发送 func tstBussinessSend(ackDelay int) { tstfun := "tstBussinessSend" util.LogInfo("<<<<<<%s TEST", tstfun) syn := &pushproto.Talk{ Type: pushproto.Talk_SYN.Enum(), Appid: proto.String("shawn"), Installid: proto.String("1cf52f542ec2f6d1e96879bd6f5243da3baa42e4"), Auth: proto.String("F**k"), Clienttype: proto.String("Android"), Clientver: proto.String("1.0.0"), } data, err := proto.Marshal(syn) if err != nil { util.LogError("%s ERROR:syn proto marshal error:%s", tstfun, err) return } sb := util.Packdata(data) first_conn_read := 0 clientid := "" conn, err := connect() if err != nil { util.LogError("%s ERROR:create connection error:%s", tstfun, err) return } defer conn.Close() go tstErrConn(conn, tstfun, sb, 1000, func(pb *pushproto.Talk) { pb_type := pb.GetType() if first_conn_read == 0 { if pb_type == pushproto.Talk_SYNACK { clientid = pb.GetClientid() util.LogInfo("%s Conn: client_id:%s", tstfun, pb.GetClientid()) } else { util.LogError("%s Conn ERROR", tstfun) return } first_conn_read += 1 } else { first_conn_read += 1 if pb_type == pushproto.Talk_BUSSINESS { util.LogInfo(">>>>>>%s Recv PASS readtimes:%d", tstfun, first_conn_read) if ackDelay+1 == first_conn_read { ack := &pushproto.Talk{ Type: pushproto.Talk_ACK.Enum(), Ackmsgid: proto.Uint64(pb.GetMsgid()), } data, err := proto.Marshal(ack) if err != nil { util.LogError("%s ERROR:syn proto marshal error:%s", tstfun, err) return } sb2 := util.Packdata(data) ln, err := conn.Write(sb2) if ln != len(sb2) || err != nil { util.LogError("%s ERROR:send error:%s", tstfun, err) return } } } else { util.LogError("%s Recv ERROR", tstfun) return } } }) // waiting for connection util.LogInfo("%s waiting for connection", tstfun) time.Sleep(1000 * 1000 * 1000 * 1) btst := &pushproto.Talk{ Type: pushproto.Talk_ECHO.Enum(), Extdata: []byte("BUSSESS TEST"), } bd, err := proto.Marshal(btst) if err != nil { util.LogError("%s ERROR:proto marshal error:%s", tstfun, err) return } client := &http.Client{} url := fmt.Sprintf("http://localhost:9091/push/%s/0/1", clientid) reqest, _ := http.NewRequest("POST", url, bytes.NewReader(bd)) reqest.Header.Set("Connection", "Keep-Alive") response, _ := client.Do(reqest) if response.StatusCode == 200 { body, err := ioutil.ReadAll(response.Body) if err != nil { util.LogError("%s Push return ERROR %s", tstfun, err) return } util.LogInfo("%s Push return %s", tstfun, body) } else { util.LogError("%s Push ERROR", tstfun) return } //time.Sleep(time.Second * time.Duration(10 * (ackDelay+1))) time.Sleep(time.Second * time.Duration(3)) }