Esempio n. 1
0
func main() {
	tcpAddr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:8989")
	checkError(err)
	conn, err := net.DialTCP("tcp", nil, tcpAddr)
	checkError(err)

	var msgId uint16
	msgId = 1
	reqObj := new(protos.CSGetuserReq)
	reqObj.Uid = proto.Int32(101)

	reqBytes, err := proto.Marshal(reqObj)
	checkError(err)

	var reqBuff []byte = make([]byte, 4+len(reqBytes))
	binary.BigEndian.PutUint16(reqBuff[0:2], uint16(len(reqBuff)))
	binary.BigEndian.PutUint16(reqBuff[2:4], msgId)
	copy(reqBuff[4:], reqBytes)
	// write
	conn.Write(reqBuff)

	// read
	p, err := protocol.ReadPacket(conn)
	checkError(err)
	rspObjt := new(protos.CSGetuserRsp)
	body := p.GetBody()
	proto.Unmarshal(body, rspObjt)
	fmt.Println(rspObjt)
	conn.Close()
}
Esempio n. 2
0
func CSGetuserReq(reqBytes []byte) ([]byte, error) {
	reqObj := new(protos.CSGetuserReq)
	err := proto.Unmarshal(reqBytes, reqObj)
	if err != nil {
		return nil, err
	}
	uid := reqObj.GetUid()

	rspObj := new(protos.CSGetuserRsp)
	rspObj.Uid = proto.Int32(uid)
	rspObj.Name = proto.String("aison")
	rspObj.Age = proto.Int32(30)
	rspObj.City = proto.String("shenzhen")
	buffer, err := proto.Marshal(rspObj)
	if err != nil {
		return nil, err
	}
	return buffer, nil
}