Commit fee11bff674b0bfb9535ccc11a886cdaac37c2df
1 parent
11abbdea
客户端无法使用grpc热更,不用grpc,定义包头使用长连接
Showing
15 changed files
with
264 additions
and
598 deletions
Show diff stats
actions/AccountAction.go deleted
... | ... | @@ -1,59 +0,0 @@ |
1 | -package actions | |
2 | - | |
3 | -import ( | |
4 | - "context" | |
5 | - "fmt" | |
6 | - "pro2d/components/jwt" | |
7 | - "pro2d/conf" | |
8 | - "pro2d/models" | |
9 | - "pro2d/protos/pb" | |
10 | - "pro2d/utils" | |
11 | -) | |
12 | - | |
13 | -func (s *LoginServer) RegisterHandler(ctx context.Context, in *pb.Register) (*pb.RegisterRsp, error) { | |
14 | - ok, account := models.AccountExistByPhone(in.Phone) | |
15 | - if !ok { | |
16 | - account.Phone = in.Phone | |
17 | - account.Password = utils.Md5V(in.Password) | |
18 | - account.Uid = conf.SnowFlack.NextValStr() | |
19 | - account.Create() | |
20 | - }else { | |
21 | - return nil, fmt.Errorf("1") | |
22 | - } | |
23 | - | |
24 | - return &pb.RegisterRsp{ | |
25 | - Code: 0, | |
26 | - }, nil | |
27 | -} | |
28 | - | |
29 | -func (s *LoginServer) CreateTokenHandler(ctx context.Context, in *pb.Account) (*pb.CreateTokenRsp, error) { | |
30 | - m := models.NewAccount(in.Phone) | |
31 | - if err := m.Load(); err != nil { | |
32 | - return &pb.CreateTokenRsp{ | |
33 | - Code: 1, | |
34 | - }, nil | |
35 | - } | |
36 | - | |
37 | - if m.Password != utils.Md5V(in.Password) { | |
38 | - return &pb.CreateTokenRsp{ | |
39 | - Code: 2, | |
40 | - }, nil | |
41 | - } | |
42 | - | |
43 | - serverInfo := s.EtcdClient.GetByPrefix(conf.GlobalConf.GameConf.Name) | |
44 | - var gameInfo []*pb.ServiceInfo | |
45 | - for k, v := range serverInfo { | |
46 | - gameInfo = append(gameInfo, &pb.ServiceInfo{ | |
47 | - Id: k, | |
48 | - Name: conf.GlobalConf.GameConf.Name, | |
49 | - Address: v, | |
50 | - }) | |
51 | - } | |
52 | - | |
53 | - return &pb.CreateTokenRsp{ | |
54 | - Code: 0, | |
55 | - Uid: m.Uid, | |
56 | - Token: jwt.CreateToken(m.Account.Uid), | |
57 | - GameService: gameInfo, | |
58 | - }, nil | |
59 | -} |
actions/RoleAction.go deleted
... | ... | @@ -1,55 +0,0 @@ |
1 | -package actions | |
2 | - | |
3 | -import ( | |
4 | - "context" | |
5 | - "errors" | |
6 | - "google.golang.org/grpc/metadata" | |
7 | - "google.golang.org/protobuf/types/known/emptypb" | |
8 | - "pro2d/conf" | |
9 | - "pro2d/models" | |
10 | - "pro2d/protos/pb" | |
11 | - "pro2d/utils" | |
12 | -) | |
13 | - | |
14 | -func (s *GameServer) HeartBeatHandler(ctx context.Context, empty *emptypb.Empty) (*pb.HeartRsp, error) { | |
15 | - utils.Sugar.Debugf("HeartBeatHandler被调用!!!") | |
16 | - //获取元数据信息 | |
17 | - _,ok := metadata.FromIncomingContext(ctx) | |
18 | - if !ok { | |
19 | - return nil,errors.New("未传输token") | |
20 | - } | |
21 | - | |
22 | - return &pb.HeartRsp{ | |
23 | - Code: 0, | |
24 | - }, nil | |
25 | -} | |
26 | - | |
27 | -func (s *GameServer) CreateRoleHandler(ctx context.Context, in *pb.LoginReq) (*pb.RoleRsp, error) { | |
28 | - uid := ctx.Value("uid").(string) | |
29 | - ok, role := models.RoleExistByUid(uid) | |
30 | - if !ok { | |
31 | - role = models.NewRole(conf.SnowFlack.NextVal()) | |
32 | - role.Role.Device = in.Device | |
33 | - role.Role.Uid = uid | |
34 | - role.Create() | |
35 | - } | |
36 | - return &pb.RoleRsp{ | |
37 | - Code: 0, | |
38 | - Role: role.Role, | |
39 | - }, nil | |
40 | -} | |
41 | - | |
42 | -func (s *GameServer) LoginHandler(ctx context.Context, in *pb.LoginReq) (*pb.RoleRsp, error) { | |
43 | - uid := ctx.Value("uid").(string) | |
44 | - ok, role := models.RoleExistByUid(uid) | |
45 | - if !ok { | |
46 | - return &pb.RoleRsp{ | |
47 | - Code: 1, | |
48 | - }, nil | |
49 | - } | |
50 | - return &pb.RoleRsp{ | |
51 | - Code: 0, | |
52 | - Role: role.Role, | |
53 | - Hero: models.GetHeros(role.Heros), | |
54 | - }, nil | |
55 | -} |
actions/basic.go deleted
... | ... | @@ -1,44 +0,0 @@ |
1 | -package actions | |
2 | - | |
3 | -import ( | |
4 | - "fmt" | |
5 | - "google.golang.org/grpc" | |
6 | - "net" | |
7 | - "pro2d/components/db" | |
8 | - "pro2d/components/etcd" | |
9 | - "pro2d/conf" | |
10 | - "pro2d/utils" | |
11 | -) | |
12 | - | |
13 | -type BasicServer struct { | |
14 | - SConf *conf.SConf | |
15 | - EtcdClient *etcd.EtcdClient | |
16 | - GrpcServer *grpc.Server | |
17 | -} | |
18 | - | |
19 | -func NewServer() *BasicServer { | |
20 | - return &BasicServer{} | |
21 | -} | |
22 | - | |
23 | -func (b *BasicServer) Start(sConf *conf.SConf) (net.Listener, error) { | |
24 | - b.SConf = sConf | |
25 | - | |
26 | - //初始化数据库 | |
27 | - db.MongoDatabase = db.MongoClient.Database(sConf.DBName) | |
28 | - //初始化etcd | |
29 | - b.EtcdClient = etcd.NewEtcdClient(conf.GlobalConf.Etcd) | |
30 | - b.EtcdClient.PutWithLeasePrefix(sConf.Name, sConf.ID, fmt.Sprintf("%s:%d", sConf.IP, sConf.Port), 10) | |
31 | - | |
32 | - listing := fmt.Sprintf(":%d", sConf.Port) | |
33 | - lis, err := net.Listen("tcp", listing) | |
34 | - if err != nil { | |
35 | - return nil, err | |
36 | - } | |
37 | - return lis, err | |
38 | -} | |
39 | - | |
40 | -func (b *BasicServer) Stop() { | |
41 | - utils.Sugar.Debugf("平滑关闭服务") | |
42 | - b.EtcdClient.Close() | |
43 | - b.GrpcServer.GracefulStop() | |
44 | -} |
actions/server.go deleted
... | ... | @@ -1,137 +0,0 @@ |
1 | -package actions | |
2 | - | |
3 | -import ( | |
4 | - "context" | |
5 | - "fmt" | |
6 | - "google.golang.org/grpc" | |
7 | - "google.golang.org/grpc/credentials" | |
8 | - "google.golang.org/grpc/reflection" | |
9 | - "pro2d/components/jwt" | |
10 | - "pro2d/conf" | |
11 | - "pro2d/models" | |
12 | - "pro2d/protos/pb" | |
13 | - "pro2d/utils" | |
14 | -) | |
15 | - | |
16 | -type LoginServer struct{ | |
17 | - pb.UnsafeLoginServer | |
18 | - *BasicServer | |
19 | -} | |
20 | - | |
21 | -func NewAccountServer() *LoginServer { | |
22 | - return &LoginServer{ | |
23 | - BasicServer: NewServer(), | |
24 | - } | |
25 | -} | |
26 | - | |
27 | -//拦截器 | |
28 | -func AccountServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, | |
29 | - handler grpc.UnaryHandler) (interface{}, error) { | |
30 | - | |
31 | - utils.Sugar.Debugf("gRPC method: %s, %v", info.FullMethod, req) | |
32 | - resp, err := handler(ctx, req) | |
33 | - return resp, err | |
34 | -} | |
35 | - | |
36 | -func (s *LoginServer)Start() error { | |
37 | - lis, err := s.BasicServer.Start(conf.GlobalConf.AccountConf) | |
38 | - if err != nil { | |
39 | - return err | |
40 | - } | |
41 | - | |
42 | - models.InitAccountServerModels() | |
43 | - | |
44 | - var opts []grpc.ServerOption | |
45 | - | |
46 | - if conf.GlobalConf.TLS.Status { | |
47 | - //TLS | |
48 | - creds, err := credentials.NewServerTLSFromFile("keys/server.pem", "keys/server.key") | |
49 | - if err != nil { | |
50 | - utils.Sugar.Errorf("Failed to generate credentials %v", err) | |
51 | - return err | |
52 | - } | |
53 | - opts = append(opts, grpc.Creds(creds)) | |
54 | - } | |
55 | - | |
56 | - //拦截器 | |
57 | - opts = append(opts, grpc.UnaryInterceptor(AccountServerInterceptor)) | |
58 | - | |
59 | - //new一个grpc | |
60 | - s.GrpcServer = grpc.NewServer(opts...) | |
61 | - | |
62 | - pb.RegisterLoginServer(s.GrpcServer, s) | |
63 | - reflection.Register(s.GrpcServer) //在给定的gRPC服务器上注册服务器反射服务 | |
64 | - | |
65 | - // Serve方法在lis上接受传入连接,为每个连接创建一个ServerTransport和server的goroutine。 | |
66 | - // 该goroutine读取gRPC请求,然后调用已注册的处理程序来响应它们。 | |
67 | - utils.Sugar.Debugf("Start LoginServer listening on %d with TLS", conf.GlobalConf.AccountConf.Port) | |
68 | - | |
69 | - return s.GrpcServer.Serve(lis) | |
70 | -} | |
71 | - | |
72 | -func (s *LoginServer)Stop() { | |
73 | - s.BasicServer.Stop() | |
74 | -} | |
75 | - | |
76 | -type GameServer struct{ | |
77 | - pb.UnimplementedGameServer | |
78 | - *BasicServer | |
79 | -} | |
80 | - | |
81 | -func NewGameServer() *GameServer { | |
82 | - return &GameServer{ | |
83 | - BasicServer: NewServer(), | |
84 | - } | |
85 | -} | |
86 | -//拦截器 | |
87 | -func GameServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, | |
88 | - handler grpc.UnaryHandler) (interface{}, error) { | |
89 | - | |
90 | - //utils.Sugar.Debugf("gRPC method: %s, %v", info.FullMethod, req) | |
91 | - uid := jwt.CheckAuth(ctx) | |
92 | - if uid == ""{ | |
93 | - return nil, fmt.Errorf("token error") | |
94 | - } | |
95 | - context.WithValue(ctx, "uid", uid) | |
96 | - resp, err := handler(ctx, req) | |
97 | - return resp, err | |
98 | -} | |
99 | - | |
100 | -func (s *GameServer)Start() error { | |
101 | - lis, err := s.BasicServer.Start(conf.GlobalConf.GameConf) | |
102 | - if err != nil { | |
103 | - return err | |
104 | - } | |
105 | - | |
106 | - models.InitGameServerModels() | |
107 | - | |
108 | - var opts []grpc.ServerOption | |
109 | - //TLS | |
110 | - if conf.GlobalConf.TLS.Status { | |
111 | - //TLS | |
112 | - creds, err := credentials.NewServerTLSFromFile("keys/server.pem", "keys/server.key") | |
113 | - if err != nil { | |
114 | - utils.Sugar.Errorf("Failed to generate credentials %v", err) | |
115 | - return err | |
116 | - } | |
117 | - opts = append(opts, grpc.Creds(creds)) | |
118 | - } | |
119 | - | |
120 | - //拦截器 | |
121 | - opts = append(opts, grpc.UnaryInterceptor(GameServerInterceptor)) | |
122 | - | |
123 | - //new一个grpc | |
124 | - s.GrpcServer = grpc.NewServer(opts...) | |
125 | - | |
126 | - pb.RegisterGameServer(s.GrpcServer, s) | |
127 | - reflection.Register(s.GrpcServer) //在给定的gRPC服务器上注册服务器反射服务 | |
128 | - | |
129 | - // Serve方法在lis上接受传入连接,为每个连接创建一个ServerTransport和server的goroutine。 | |
130 | - // 该goroutine读取gRPC请求,然后调用已注册的处理程序来响应它们。 | |
131 | - utils.Sugar.Debugf("Start GameServer listening on %d with TLS", conf.GlobalConf.GameConf.Port) | |
132 | - return s.GrpcServer.Serve(lis) | |
133 | -} | |
134 | - | |
135 | -func (s *GameServer)Stop() { | |
136 | - s.BasicServer.Stop() | |
137 | -} | |
138 | 0 | \ No newline at end of file |
cmd/game.go deleted
... | ... | @@ -1,29 +0,0 @@ |
1 | -package main | |
2 | - | |
3 | -import ( | |
4 | - "os" | |
5 | - "os/signal" | |
6 | - "pro2d/actions" | |
7 | - "pro2d/utils" | |
8 | - "syscall" | |
9 | -) | |
10 | - | |
11 | -func main() { | |
12 | - err := make(chan error) | |
13 | - stopChan := make(chan os.Signal) | |
14 | - signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) | |
15 | - | |
16 | - server := actions.NewGameServer() | |
17 | - go func() { | |
18 | - err <- server.Start() | |
19 | - }() | |
20 | - | |
21 | - | |
22 | - select { | |
23 | - case e := <- err: | |
24 | - utils.Sugar.Errorf("game server error: %v", e) | |
25 | - case <-stopChan: | |
26 | - // 平滑关闭服务 | |
27 | - server.Stop() | |
28 | - } | |
29 | -} |
cmd/account.go renamed to cmd/server.go
... | ... | @@ -3,27 +3,26 @@ package main |
3 | 3 | import ( |
4 | 4 | "os" |
5 | 5 | "os/signal" |
6 | - "pro2d/actions" | |
6 | + "pro2d/components/net" | |
7 | + "pro2d/conf" | |
7 | 8 | "pro2d/utils" |
8 | 9 | "syscall" |
9 | 10 | ) |
10 | 11 | |
11 | -func main() { | |
12 | +func main() { | |
12 | 13 | err := make(chan error) |
13 | 14 | stopChan := make(chan os.Signal) |
14 | 15 | signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) |
15 | 16 | |
16 | - server := actions.NewAccountServer() | |
17 | + s := net.NewServer(conf.GlobalConf.GameConf) | |
17 | 18 | go func() { |
18 | - err <- server.Start() | |
19 | + err <- s.Start() | |
19 | 20 | }() |
20 | 21 | |
21 | - | |
22 | 22 | select { |
23 | 23 | case e := <- err: |
24 | 24 | utils.Sugar.Errorf("game server error: %v", e) |
25 | 25 | case <-stopChan: |
26 | - // 平滑关闭服务 | |
27 | - server.Stop() | |
26 | + s.Stop() | |
28 | 27 | } |
29 | 28 | } | ... | ... |
... | ... | @@ -0,0 +1,105 @@ |
1 | +package net | |
2 | + | |
3 | +import ( | |
4 | + "bufio" | |
5 | + "fmt" | |
6 | + "net" | |
7 | +) | |
8 | + | |
9 | +type Head struct { | |
10 | + Length int32 | |
11 | + Cmd int32 | |
12 | + ErrCode int32 | |
13 | + PreField int32 | |
14 | +} | |
15 | + | |
16 | + | |
17 | +type Connection struct { | |
18 | + net.Conn | |
19 | + Id int | |
20 | + Server *Server | |
21 | + | |
22 | + scanner *bufio.Scanner | |
23 | + writer *bufio.Writer | |
24 | + | |
25 | + WBuffer chan []byte | |
26 | + RBuffer chan *MsgPkg | |
27 | + | |
28 | + Quit chan *Connection | |
29 | +} | |
30 | + | |
31 | +type MsgPkg struct { | |
32 | + Head Head | |
33 | + Body []byte | |
34 | + Conn *Connection | |
35 | +} | |
36 | + | |
37 | +func NewConn(id int, conn net.Conn, s *Server) *Connection { | |
38 | + return &Connection{ | |
39 | + Id: id, | |
40 | + Conn: conn, | |
41 | + Server: s, | |
42 | + | |
43 | + scanner: bufio.NewScanner(conn), | |
44 | + writer: bufio.NewWriter(conn), | |
45 | + WBuffer: make(chan []byte), | |
46 | + RBuffer: make(chan *MsgPkg), | |
47 | + Quit: make(chan *Connection), | |
48 | + } | |
49 | +} | |
50 | + | |
51 | +func (c *Connection) write() { | |
52 | + defer c.Quiting() | |
53 | + | |
54 | + for msg := range c.WBuffer { | |
55 | + if _, err := c.writer.Write(msg); err != nil { | |
56 | + fmt.Println("write fail err: " + err.Error()) | |
57 | + return | |
58 | + } | |
59 | + if err := c.writer.Flush(); err != nil { | |
60 | + fmt.Println("write Flush fail err: " + err.Error()) | |
61 | + return | |
62 | + } | |
63 | + } | |
64 | +} | |
65 | + | |
66 | +func (c *Connection) read() { | |
67 | + defer c.Quiting() | |
68 | + for { | |
69 | + c.scanner.Split(ParseMsg) | |
70 | + | |
71 | + for c.scanner.Scan() { | |
72 | + req, err := DecodeMsg(c.scanner.Bytes()) | |
73 | + if err != nil { | |
74 | + return | |
75 | + } | |
76 | + | |
77 | + req.Conn = c | |
78 | + c.Server.OnRecv(req) | |
79 | + } | |
80 | + | |
81 | + if err := c.scanner.Err(); err != nil { | |
82 | + fmt.Printf("scanner.err: %s\n", err.Error()) | |
83 | + c.Quiting() | |
84 | + return | |
85 | + } | |
86 | + } | |
87 | +} | |
88 | + | |
89 | +func (c *Connection) Start() { | |
90 | + go c.write() | |
91 | + go c.read() | |
92 | +} | |
93 | + | |
94 | +func (c *Connection) Stop() { | |
95 | + close(c.RBuffer) | |
96 | + close(c.WBuffer) | |
97 | + c.Conn.Close() | |
98 | +} | |
99 | + | |
100 | +func (c *Connection) Quiting() { | |
101 | + c.Server.OnClose(c) | |
102 | +} | |
103 | + | |
104 | +func (c *Connection) SendMsg(){ | |
105 | +} | |
0 | 106 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,43 @@ |
1 | +package net | |
2 | + | |
3 | +import ( | |
4 | + "bytes" | |
5 | + "encoding/binary" | |
6 | + "fmt" | |
7 | + "pro2d/common" | |
8 | +) | |
9 | + | |
10 | +func ParseMsg (data []byte, atEOF bool) (advance int, token []byte, err error) { | |
11 | + // 表示我们已经扫描到结尾了 | |
12 | + if atEOF && len(data) == 0 { | |
13 | + return 0, nil, nil | |
14 | + } | |
15 | + if !atEOF && len(data) >= common.HEADLEN { //4字节数据包长度 4字节指令 | |
16 | + length := int32(0) | |
17 | + binary.Read(bytes.NewReader(data[0:4]), binary.BigEndian, &length) | |
18 | + if length <= 0 { | |
19 | + return 0, nil, fmt.Errorf("length is 0") | |
20 | + } | |
21 | + if int(length) <= len(data) { | |
22 | + return int(length) , data[:int(length)], nil | |
23 | + } | |
24 | + return 0 , nil, nil | |
25 | + } | |
26 | + if atEOF { | |
27 | + return len(data), data, nil | |
28 | + } | |
29 | + return 0, nil, nil | |
30 | +} | |
31 | + | |
32 | + | |
33 | +func DecodeMsg(data []byte) (*MsgPkg, error) { | |
34 | + h := Head{} | |
35 | + err := binary.Read(bytes.NewReader(data), binary.BigEndian, &h) | |
36 | + if err != nil { | |
37 | + return nil, err | |
38 | + } | |
39 | + return &MsgPkg{ | |
40 | + Head: h, | |
41 | + Body: data[common.HEADLEN:], | |
42 | + },nil | |
43 | +} | ... | ... |
... | ... | @@ -0,0 +1,56 @@ |
1 | +package net | |
2 | + | |
3 | +import ( | |
4 | + "fmt" | |
5 | + "net" | |
6 | + "pro2d/conf" | |
7 | + "pro2d/utils" | |
8 | + "sync" | |
9 | +) | |
10 | + | |
11 | +type Server struct { | |
12 | + SConf *conf.SConf | |
13 | + Clients *sync.Map | |
14 | + | |
15 | +} | |
16 | + | |
17 | +func NewServer(sConf *conf.SConf) *Server { | |
18 | + return &Server{ | |
19 | + SConf: sConf, | |
20 | + Clients: new(sync.Map), | |
21 | + } | |
22 | +} | |
23 | + | |
24 | +func (s *Server) OnRecv(msg *MsgPkg) { | |
25 | + utils.Sugar.Debugf("cmd: %d, data: %s", msg.Head.Cmd, msg.Body) | |
26 | +} | |
27 | + | |
28 | +func (s *Server) OnClose(conn *Connection) { | |
29 | + s.Clients.Delete(conn.Id) | |
30 | +} | |
31 | + | |
32 | +func (s *Server)Start() error { | |
33 | + port := fmt.Sprintf(":%d", s.SConf.Port) | |
34 | + l, err := net.Listen("tcp", port) | |
35 | + if err != nil { | |
36 | + return err | |
37 | + } | |
38 | + | |
39 | + utils.Sugar.Debugf("listen on %s\n", port) | |
40 | + id := 0 | |
41 | + for { | |
42 | + conn, err := l.Accept() | |
43 | + if err != nil { | |
44 | + return err | |
45 | + } | |
46 | + | |
47 | + id++ | |
48 | + client := NewConn(id, conn, s) | |
49 | + s.Clients.Store(id, client) | |
50 | + go client.Start() | |
51 | + } | |
52 | +} | |
53 | + | |
54 | +func (s *Server)Stop() { | |
55 | +} | |
56 | + | ... | ... |
test/client.go
1 | -//参考:https://jergoo.gitbooks.io/go-grpc-practice-guide/content/chapter2/interceptor.html | |
2 | - | |
3 | 1 | package main |
4 | 2 | |
5 | 3 | import ( |
6 | - "context" | |
7 | - "fmt" | |
8 | - "google.golang.org/grpc" | |
9 | - "google.golang.org/grpc/credentials" | |
10 | - "pro2d/components/jwt" | |
11 | - _ "pro2d/conf" | |
12 | - "pro2d/protos/pb" | |
4 | + "bytes" | |
5 | + "encoding/binary" | |
6 | + "net" | |
7 | + net2 "pro2d/components/net" | |
13 | 8 | "pro2d/utils" |
14 | 9 | ) |
15 | 10 | |
16 | -func Register(c pb.LoginClient, phone, password string) error { | |
17 | - r, err := c.RegisterHandler(context.Background(), &pb.Register{ | |
18 | - Phone: "17683852936", | |
19 | - Password: "123456", | |
20 | - Code: 0, | |
21 | - }) | |
11 | +func main() { | |
22 | 12 | |
23 | - if err != nil { | |
24 | - return err | |
25 | - } | |
26 | - if r.Code != 0 { | |
27 | - return fmt.Errorf("register fail") | |
13 | + head := net2.Head{ | |
14 | + Length: 0, | |
15 | + Cmd: 1, | |
16 | + ErrCode: 0, | |
17 | + PreField: 0, | |
28 | 18 | } |
29 | - utils.Sugar.Debug("register success") | |
30 | - return nil | |
31 | -} | |
32 | 19 | |
33 | -func Login(loginUri, token, uid string) { | |
34 | - var opts []grpc.DialOption | |
35 | - // 指定自定义认证 | |
36 | - opts = append(opts, grpc.WithPerRPCCredentials(&jwt.AuthToken{Token: token})) | |
37 | - if TLS { | |
38 | - // TLS连接 | |
39 | - creds, err := credentials.NewClientTLSFromFile("keys/server.pem", ServerName) | |
40 | - if err != nil { | |
41 | - utils.Sugar.Fatalf("Failed to create TLS credentials %v", err) | |
42 | - return | |
43 | - } | |
44 | - opts = append(opts, grpc.WithTransportCredentials(creds)) | |
45 | 20 | |
46 | - }else{ | |
47 | - opts = append(opts, grpc.WithInsecure()) | |
21 | + b := net2.MsgPkg{ | |
22 | + Head: head, | |
23 | + Body: []byte("hello world"), | |
48 | 24 | } |
49 | - | |
50 | - gameConn, err := grpc.Dial(loginUri, opts...) | |
25 | + head.Length = int32(16 + len(b.Body)) | |
26 | + buf := &bytes.Buffer{} | |
27 | + err := binary.Write(buf, binary.BigEndian, head) | |
51 | 28 | if err != nil { |
52 | - utils.Sugar.Errorf("game conn err: %v", err) | |
29 | + utils.Sugar.Errorf("err: %v, head: %v", err, head) | |
53 | 30 | return |
54 | 31 | } |
55 | - defer gameConn.Close() | |
56 | - | |
57 | - client:= pb.NewGameClient(gameConn) | |
58 | - var role *pb.Role | |
59 | - loginRsp, err := client.LoginHandler(context.Background(), &pb.LoginReq{ | |
60 | - Device: "111111", | |
61 | - }) | |
32 | + utils.Sugar.Debugf("head: %v", head) | |
62 | 33 | |
34 | + err = binary.Write(buf, binary.BigEndian, b.Body) | |
63 | 35 | if err != nil { |
64 | - utils.Sugar.Errorf("login error: %v", err) | |
36 | + utils.Sugar.Errorf("err: %v, msg: %v", err, b.Body) | |
65 | 37 | return |
66 | 38 | } |
67 | - role = loginRsp.Role | |
68 | - | |
69 | - if loginRsp.Code != 0 { | |
70 | - utils.Sugar.Debugf("login fail, role not exist") | |
71 | - createRole, err := client.CreateRoleHandler(context.Background(), &pb.LoginReq{Device: "11111"}) | |
72 | - if err != nil { | |
73 | - utils.Sugar.Errorf("create role err: %v", err) | |
74 | - return | |
75 | - } | |
76 | - utils.Sugar.Debug("create role rsp: ", createRole.Code) | |
77 | - role = createRole.Role | |
78 | - } | |
79 | - | |
80 | - utils.Sugar.Debugf("login successful role: %v", role) | |
81 | -} | |
82 | - | |
83 | -const ( | |
84 | - TLS = true | |
85 | - ServerName = "pro2d" | |
86 | -) | |
87 | - | |
88 | -func main() { | |
89 | - | |
90 | - var opts []grpc.DialOption | |
91 | - if TLS { | |
92 | - // TLS连接 | |
93 | - creds, err := credentials.NewClientTLSFromFile("keys/server.pem", ServerName) | |
94 | - if err != nil { | |
95 | - utils.Sugar.Fatalf("Failed to create TLS credentials %v", err) | |
96 | - return | |
97 | - } | |
98 | - opts = append(opts, grpc.WithTransportCredentials(creds)) | |
99 | - | |
100 | - }else{ | |
101 | - opts = append(opts, grpc.WithInsecure()) | |
102 | - } | |
103 | - | |
104 | - conn, err := grpc.Dial("localhost:8848", opts...) | |
105 | - if err != nil { | |
106 | - utils.Sugar.Errorf("conn err: %v", err) | |
107 | - return | |
108 | - } | |
109 | - defer conn.Close() | |
110 | - c := pb.NewLoginClient(conn) | |
111 | - //err = Register(c,"17683852936", "123456") | |
112 | - //if err != nil { | |
113 | - // utils.Sugar.Errorf("register err: %v", err) | |
114 | - // return | |
115 | - //} | |
116 | - rsp, err := c.CreateTokenHandler(context.Background(), &pb.Account{ | |
117 | - Phone: "17683852936", | |
118 | - Password: "123456", | |
119 | - }) | |
120 | 39 | |
40 | + client, err := net.Dial("tcp", "localhost:8849") | |
121 | 41 | if err != nil { |
122 | - utils.Sugar.Errorf("createtoken err: %v", err) | |
123 | - return | |
124 | - } | |
125 | - | |
126 | - if rsp.Code != 0 { | |
127 | - utils.Sugar.Errorf("createtoken err" ) | |
42 | + utils.Sugar.Error(err) | |
128 | 43 | return |
129 | 44 | } |
130 | - | |
131 | - if len(rsp.GameService) >0 { | |
132 | - Login(rsp.GameService[0].Address, rsp.Token, rsp.Uid) | |
133 | - } | |
45 | + client.Write(buf.Bytes()) | |
46 | + select {} | |
134 | 47 | } |
135 | 48 | \ No newline at end of file | ... | ... |
test/tlsclient.go deleted
... | ... | @@ -1,64 +0,0 @@ |
1 | -package main | |
2 | - | |
3 | -import ( | |
4 | - "context" | |
5 | - "google.golang.org/grpc" | |
6 | - "google.golang.org/grpc/credentials" | |
7 | - "google.golang.org/protobuf/types/known/emptypb" | |
8 | - "log" | |
9 | - "pro2d/components/jwt" | |
10 | - _ "pro2d/conf" | |
11 | - "pro2d/protos/pb" | |
12 | - "pro2d/utils" | |
13 | -) | |
14 | -// AuthToken 自定义认证 客户端使用 | |
15 | -type CustomToken struct { | |
16 | -} | |
17 | - | |
18 | -func (c CustomToken) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) { | |
19 | - return map[string]string{ | |
20 | - "appId": "100", | |
21 | - "appKey": "token", | |
22 | - }, nil | |
23 | -} | |
24 | - | |
25 | -func (c CustomToken) RequireTransportSecurity() bool { | |
26 | - return true | |
27 | -} | |
28 | - | |
29 | -func main() { | |
30 | - var opts []grpc.DialOption | |
31 | - creds, err := credentials.NewClientTLSFromFile("keys/server.pem", "pro2d") | |
32 | - if err != nil { | |
33 | - log.Fatal(err) | |
34 | - return | |
35 | - } | |
36 | - opts = append(opts, grpc.WithTransportCredentials(creds)) | |
37 | - conn, err := grpc.Dial("localhost:8948", opts...) | |
38 | - | |
39 | - helloClient := pb.NewHelloClient(conn) | |
40 | - token, err := helloClient.CreateToken(context.TODO(), &pb.Login{ | |
41 | - Login: "login", | |
42 | - Password: "123456", | |
43 | - }) | |
44 | - if err != nil { | |
45 | - log.Fatal(err) | |
46 | - return | |
47 | - } | |
48 | - utils.Sugar.Debugf("token: %s", token.Token) | |
49 | - | |
50 | - opts = append(opts, grpc.WithPerRPCCredentials(&jwt.AuthToken{Token: token.Token})) | |
51 | - conn2, err := grpc.Dial("localhost:8948",opts...) | |
52 | - if err != nil { | |
53 | - log.Fatal(err) | |
54 | - return | |
55 | - } | |
56 | - | |
57 | - helloClient2 := pb.NewHelloClient(conn2) | |
58 | - rsp, err := helloClient2.SayHello(context.TODO(), &emptypb.Empty{}) | |
59 | - if err != nil { | |
60 | - log.Fatal(err) | |
61 | - } | |
62 | - | |
63 | - log.Printf("sayhello rsp: %v", rsp) | |
64 | -} |
test/tlsserver.go deleted
... | ... | @@ -1,90 +0,0 @@ |
1 | -package main | |
2 | - | |
3 | -import ( | |
4 | - "context" | |
5 | - "fmt" | |
6 | - "google.golang.org/grpc" | |
7 | - "google.golang.org/grpc/codes" | |
8 | - "google.golang.org/grpc/credentials" | |
9 | - "google.golang.org/grpc/metadata" | |
10 | - "google.golang.org/grpc/status" | |
11 | - "google.golang.org/protobuf/types/known/emptypb" | |
12 | - "log" | |
13 | - "net" | |
14 | - "pro2d/components/jwt" | |
15 | - "pro2d/protos/pb" | |
16 | -) | |
17 | - | |
18 | -type Server struct { | |
19 | - pb.UnimplementedHelloServer | |
20 | -} | |
21 | - | |
22 | -func (s *Server) CreateToken(ctx context.Context, in *pb.Login) (*pb.TokenInfo, error) { | |
23 | - if in.Login == "login" && in.Password == "123456" { | |
24 | - return &pb.TokenInfo{Token: jwt.CreateToken(in.Login)}, nil | |
25 | - } | |
26 | - return nil, fmt.Errorf("login error") | |
27 | -} | |
28 | - | |
29 | -//func (s *Server) SayHello(ctx context.Context, empty *emptypb.Empty) (*pb.HelloWorld, error) { | |
30 | -// md, ok := metadata.FromIncomingContext(ctx) | |
31 | -// if !ok { | |
32 | -// return nil, status.Errorf(codes.Unauthenticated,"ErrNoMetadataInContext") | |
33 | -// } | |
34 | -// // md 的类型是 type MD map[string][]string | |
35 | -// token, ok := md["authorization"] | |
36 | -// if !ok || len(token) == 0 { | |
37 | -// return nil, status.Errorf(codes.Unauthenticated,"ErrNoAuthorizationInMetadata") | |
38 | -// } | |
39 | -// login := jwt.ParseToken(token[0]) | |
40 | -// return &pb.HelloWorld{Msg: "Hello world: " + login}, nil | |
41 | -//} | |
42 | - | |
43 | -func (s *Server) SayHello(ctx context.Context, empty *emptypb.Empty) (*pb.HelloWorld, error) { | |
44 | - md, ok := metadata.FromIncomingContext(ctx) | |
45 | - if !ok { | |
46 | - return nil, status.Errorf(codes.Unauthenticated, "无Token认证信息") | |
47 | - } | |
48 | - var ( | |
49 | - appId string | |
50 | - appKey string | |
51 | - ) | |
52 | - if val, ok := md["appid"]; ok { | |
53 | - appId = val[0] | |
54 | - } | |
55 | - | |
56 | - if val, ok := md["appkey"]; ok { | |
57 | - appKey = val[0] | |
58 | - } | |
59 | - | |
60 | - if appId != "100" || appKey != "token" { | |
61 | - return nil, status.Errorf(codes.Unauthenticated, "Token认证信息无效: appid=%s, appkey=%s", appId, appKey) | |
62 | - } | |
63 | - | |
64 | - return &pb.HelloWorld{Msg: "Hello world"}, nil | |
65 | -} | |
66 | -func main() { | |
67 | - // 监听本地端口 | |
68 | - listener, err := net.Listen("tcp", ":8948") | |
69 | - if err != nil { | |
70 | - log.Fatalf("net.Listen err: %v", err) | |
71 | - } | |
72 | - var opts []grpc.ServerOption | |
73 | - // 从输入证书文件和密钥文件为服务端构造TLS凭证 | |
74 | - creds, err := credentials.NewServerTLSFromFile("keys/server.pem", "keys/server.key") | |
75 | - if err != nil { | |
76 | - log.Fatalf("Failed to generate credentials %v", err) | |
77 | - } | |
78 | - opts = append(opts, grpc.Creds(creds)) | |
79 | - // 新建gRPC服务器实例,并开启TLS认证 | |
80 | - grpcServer := grpc.NewServer(opts...) | |
81 | - | |
82 | - // 在gRPC服务器注册我们的服务 | |
83 | - pb.RegisterHelloServer(grpcServer, &Server{}) | |
84 | - log.Println(" net.Listing whth TLS") | |
85 | - //用服务器 Serve() 方法以及我们的端口信息区实现阻塞等待,直到进程被杀死或者 Stop() 被调用 | |
86 | - err = grpcServer.Serve(listener) | |
87 | - if err != nil { | |
88 | - log.Fatalf("grpcServer.Serve err: %v", err) | |
89 | - } | |
90 | -} | |
91 | 0 | \ No newline at end of file |
tools/csvtostruct.go
... | ... | @@ -154,7 +154,7 @@ func (g *Generate)CsvToMem(csvpath, filename string) error { |
154 | 154 | //4行之后 |
155 | 155 | fields := sheetData[1] |
156 | 156 | typs := sheetData[2] //int, string |
157 | - //keys := sheetData[3] //all, s, c | |
157 | + keys := sheetData[3] //all, key, key1, key2 | |
158 | 158 | |
159 | 159 | t := reflect.ValueOf(csvdata.CsvStruct[name]).Type() |
160 | 160 | v := reflect.New(t).Elem() |
... | ... | @@ -175,9 +175,19 @@ func (g *Generate)CsvToMem(csvpath, filename string) error { |
175 | 175 | f.SetString(record[idx]) |
176 | 176 | } |
177 | 177 | |
178 | + switch keys[idx] { | |
179 | + case "all": | |
180 | + case "key": | |
181 | + case "key1": | |
182 | + | |
183 | + case "key2": | |
184 | + default: | |
185 | + | |
186 | + } | |
187 | + | |
178 | 188 | //fmt.Printf("%d, %s: %v, %v\n", idx, firstRuneToUpper(field), v.Field(idx), v.Interface()) |
179 | - csvdata.SetCsv(name, v.Interface()) | |
180 | 189 | } |
190 | + csvdata.SetCsv(name, datam) | |
181 | 191 | } |
182 | 192 | i++ |
183 | 193 | } | ... | ... |