Example #1
0
// 多个连接使用同样的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)
		}

	})

}
Example #2
0
// 连接建立,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)
		}

	})

}
Example #3
0
// 业务数据包发送
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))

}