Blame view

cmd/test/service/TestClient.go 2.81 KB
495e9142   zhangqijia   fix: 增加DisConnect...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  package service
  
  import (
  	"fmt"
  	"github.com/golang/protobuf/proto"
  	"pro2d/cmd/test/action"
  	"pro2d/common"
  	"pro2d/common/components"
  	"pro2d/common/logger"
  	"pro2d/models"
  	"pro2d/pb"
  	"sync/atomic"
  )
  
  type TestClient struct {
  	components.IConnector
  
  	Token              string
  	RoleTest           *models.RoleTestModel
  	lastHeartCheckTime int64
  }
  
  func NewTestClient() *TestClient {
  	options := []components.ConnectorOption{
  		components.WithCtorCount(common.GlobalConf.TestClient.Count),
  		components.WithCtorSplitter(components.NewPBSplitter(nil)),
  	}
  
  	client := components.NewConnector(common.GlobalConf.TestClient.Ip, common.GlobalConf.TestClient.Port, options...)
  	if err := client.Connect(); err != nil {
  		logger.Error(err)
  		return nil
  	}
  	t := &TestClient{IConnector: client}
  
  	client.GetConn().SetConnectionCallback(t.OnConn)
  	client.GetConn().SetMessageCallback(t.OnMessage)
  	client.GetConn().SetCloseCallback(t.OnClose)
  	client.GetConn().SetTimerCallback(t.OnTimer)
  	client.GetConn().Start()
  	return t
  }
  
  func (t *TestClient) OnConn(conn components.IConnection) {
  }
  
  func (t *TestClient) OnLoginQuery(msg components.IMessage) {
  	errCode := msg.GetHeader().GetErrCode()
  	if errCode != 0 {
  		logger.Error("Login errcode: %d", errCode)
  
  		if errCode == 2 {
f8ce769e   zhangqijia   fix: rpc = rar + ...
53
  			req := &pb.CreateRar{
495e9142   zhangqijia   fix: 增加DisConnect...
54
55
56
57
  				Token:  t.Token,
  				Device: "test-create",
  			}
  			r, _ := proto.Marshal(req)
cdef950e   zhangqijia   fix: 服务器bug, 当未登录...
58
  			t.GetConn().SendSuccess(uint32(pb.ProtoCode_CreateRpc), r, 0)
495e9142   zhangqijia   fix: 增加DisConnect...
59
60
61
62
  			return
  		}
  	}
  
f8ce769e   zhangqijia   fix: rpc = rar + ...
63
  	req := pb.LoginRsp{}
495e9142   zhangqijia   fix: 增加DisConnect...
64
65
66
67
68
69
70
71
72
73
74
75
76
  	if err := proto.Unmarshal(msg.GetData(), &req); err != nil {
  		logger.Error("loginRpc err: %v", err)
  		return
  	}
  
  	t.RoleTest = models.NewRoleTestModel(req)
  }
  
  func (t *TestClient) OnMessage(msg components.IMessage) {
  	atomic.StoreInt64(&t.lastHeartCheckTime, common.Timex())
  	logger.Debug("rsp errCode:%d, protocolID: %d", msg.GetHeader().GetErrCode(), msg.GetHeader().GetMsgID())
  
  	//login rsp
f8ce769e   zhangqijia   fix: rpc = rar + ...
77
  	if msg.GetHeader().GetMsgID() == uint32(pb.ProtoCode_LoginRpc) {
495e9142   zhangqijia   fix: 增加DisConnect...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  		t.OnLoginQuery(msg)
  		return
  	}
  
  	errCode := msg.GetHeader().GetErrCode()
  	if errCode != 0 {
  		logger.Error("protocolID: %d, errCode %d", msg.GetHeader().GetMsgID(), errCode)
  		return
  	}
  
  	//get handler by msgid
  	md, ok := action.GetTestActionMap()[msg.GetHeader().GetMsgID()]
  	if !ok {
  		fmt.Errorf("cmd: %d, handler is nil", msg.GetHeader().GetMsgID())
  		return
  	}
  
  	//调用协议号对应的逻辑函数
  	f := md.(func(role *models.RoleTestModel, msg components.IMessage))
  	f(t.RoleTest, msg)
  }
  
  func (t *TestClient) OnClose(conn components.IConnection) {
2ea16684   zhangqijia   fix: update
101
  	conn.Stop()
495e9142   zhangqijia   fix: 增加DisConnect...
102
103
104
105
106
107
108
109
  }
  func (t *TestClient) OnTimer(conn components.IConnection) {
  }
  
  func (t *TestClient) Login(token string) {
  	t.Token = token
  	head := &components.PBHead{
  		Length:   0,
cdef950e   zhangqijia   fix: 服务器bug, 当未登录...
110
  		Cmd:      uint32(2),
495e9142   zhangqijia   fix: 增加DisConnect...
111
112
113
114
115
116
117
118
  		ErrCode:  0,
  		PreField: 0,
  	}
  	loginReq := &pb.LoginReq{
  		Token:  token,
  		Device: "test-login",
  	}
  	l, _ := proto.Marshal(loginReq)
cdef950e   zhangqijia   fix: 服务器bug, 当未登录...
119
  	t.Send(head.Cmd, l, 0)
495e9142   zhangqijia   fix: 增加DisConnect...
120
  }