-
Notifications
You must be signed in to change notification settings - Fork 5
/
connector.go
127 lines (108 loc) · 3.24 KB
/
connector.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package services
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"github.com/golang/protobuf/proto"
"github.com/topfreegames/pitaya"
"github.com/topfreegames/pitaya-admin/examples/protos"
pitayaprotos "github.com/topfreegames/pitaya-admin/protos"
"github.com/topfreegames/pitaya/component"
)
// ConnectorRemote is a remote that will receive rpc's
type ConnectorRemote struct {
component.Base
}
// Connector struct
type Connector struct {
component.Base
}
// SessionData struct
type SessionData struct {
Data map[string]interface{}
}
// Response struct
type Response struct {
Code int32
Msg string
}
func reply(code int32, msg string) (*Response, error) {
res := &Response{
Code: code,
Msg: msg,
}
return res, nil
}
// GetSessionData gets the session data
func (c *Connector) GetSessionData(ctx context.Context) (*SessionData, error) {
s := pitaya.GetSessionFromCtx(ctx)
res := &SessionData{
Data: s.GetData(),
}
return res, nil
}
// SetSessionData sets the session data
func (c *Connector) SetSessionData(ctx context.Context, data *SessionData) (*Response, error) {
s := pitaya.GetSessionFromCtx(ctx)
err := s.SetData(data.Data)
if err != nil {
return nil, pitaya.Error(err, "CN-000", map[string]string{"failed": "set data"})
}
return reply(200, "success")
}
// NotifySessionData sets the session data
func (c *Connector) NotifySessionData(ctx context.Context, data *SessionData) {
s := pitaya.GetSessionFromCtx(ctx)
err := s.SetData(data.Data)
if err != nil {
fmt.Println("got error on notify", err)
}
}
// RemoteFunc is a function that will be called remotely
func (c *ConnectorRemote) RemoteFunc(ctx context.Context, msg *protos.RPCMsg) (*protos.RPCRes, error) {
return &protos.RPCRes{
Msg: msg.GetMsg(),
}, nil
}
// RemoteWithImports is a function that will be called remotely with imported proto messages
func (c *ConnectorRemote) RemoteWithImports(ctx context.Context, msg *protos.TestMessage) (*protos.RPCRes, error) {
return &protos.RPCRes{
Msg: msg.Payload.ImportedMessage,
}, nil
}
// Proto is a function that will be called remotely to get the game proto specified by name.doc
func (c *ConnectorRemote) Proto(ctx context.Context, name *pitayaprotos.ProtoName) (*pitayaprotos.ProtoDescriptor, error) {
protoName := name.Name
if strings.HasSuffix(protoName, ".proto") {
return &pitayaprotos.ProtoDescriptor{
Desc: proto.FileDescriptor(protoName),
}, nil
}
protoReflectTypePointer := proto.MessageType(protoName)
protoReflectType := protoReflectTypePointer.Elem()
protoValue := reflect.New(protoReflectType)
descriptorMethod, ok := protoReflectTypePointer.MethodByName("Descriptor")
if !ok {
return nil, errors.New("failed to get proto descriptor")
}
descriptorValue := descriptorMethod.Func.Call([]reflect.Value{protoValue})
protoDescriptor := descriptorValue[0].Bytes()
return &pitayaprotos.ProtoDescriptor{
Desc: protoDescriptor,
}, nil
}
// Docs returns documentation
func (c *ConnectorRemote) Docs(ctx context.Context, flag *pitayaprotos.DocMsg) (*pitayaprotos.Doc, error) {
d, err := pitaya.Documentation(flag.GetGetProtos())
if err != nil {
return nil, err
}
doc, err := json.Marshal(d)
if err != nil {
return nil, err
}
return &pitayaprotos.Doc{Doc: string(doc)}, nil
}