-
Notifications
You must be signed in to change notification settings - Fork 28
/
agentToGame.go
141 lines (125 loc) · 3.06 KB
/
agentToGame.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package game
import (
"github.com/daniel840829/gameServer/game/session"
. "github.com/daniel840829/gameServer/msg"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
// io"
"time"
)
/*
type AgentToGameServer interface {
// SessionManager
AquireGameRoom(context.Context, *GameCreation) (*PemKey, error)
}
*/
type ATGServer struct {
}
type CTGServer struct {
}
func (g *CTGServer) TimeCalibrate(c context.Context, e *Empty) (*TimeStamp, error) {
log.Debug("Calibration")
return &TimeStamp{Value: int64(time.Now().UnixNano() / 1000000)}, nil
}
func (g *ATGServer) AquireGameRoom(c context.Context, gc *GameCreation) (*PemKey, error) {
err := session.RoomManager.CreateGame(gc)
return &PemKey{SSL: "HI"}, err
}
func (g *ATGServer) DeletGameRoom(c context.Context, info *RoomInfo) (*Success, error) {
err := session.RoomManager.DeleteRoom(info)
if err != nil {
return &Success{}, err
}
return &Success{Ok: true}, nil
}
func (g *CTGServer) PlayerInput(stream ClientToGame_PlayerInputServer) error {
log.Debug("input")
session := GetSesionFromContext(stream.Context())
if session == nil {
return status.Errorf(codes.NotFound, "Session Not Found!")
}
LOOP2:
for {
select {
default:
input, err := stream.Recv()
if HandleRPCError(session, err) {
break LOOP2
}
session.State.HandleInput(input)
}
}
return nil
}
func (g *CTGServer) UpdateGameFrame(e *Empty, stream ClientToGame_UpdateGameFrameServer) error {
log.Debug("Frame")
session := GetSesionFromContext(stream.Context())
if session == nil {
return status.Errorf(codes.NotFound, "Session Not Found!")
}
msgch := session.GetMsgChan("GameFrame")
if msgch == nil {
return status.Errorf(codes.Internal, "GameFrame MsgChan Not Found!")
}
session.State.StartGame()
LOOP:
for {
select {
case <-msgch.StopSignal:
break LOOP
case msg := <-msgch.DataCh:
err := stream.Send(msg.(*GameFrame))
if HandleRPCError(session, err) {
break LOOP
}
}
}
return nil
}
func (g *CTGServer) Pipe(ClientToGame_PipeServer) error {
return nil
}
func GetSesionFromContext(c context.Context) *session.Session {
md, ok := metadata.FromIncomingContext(c)
if !ok {
return nil
}
s := session.Manager.GetSession(md)
return s
}
func HandleRPCError(s *session.Session, e error) (IfEndStream bool) {
if e == nil {
return false
}
st, _ := status.FromError(e)
log.Warn(st.Message())
switch st.Code() {
case codes.Canceled:
IfEndStream = ReconnectError(s)
case codes.Unavailable:
IfEndStream = ReconnectError(s)
default:
IfEndStream = RecordError(s, e)
}
return
}
func IgnoreError(s *session.Session) (IfEndStream bool) {
return true
}
func RecordError(s *session.Session, e error) (IfEndStream bool) {
log.Warn("RPCError:", e)
EndConnection(s)
return true
}
func ReconnectError(s *session.Session) (IfEndStream bool) {
log.Warn("Wait to reconnect")
s.State.WaitReconnect()
return true
}
func EndConnection(s *session.Session) (IfEndStream bool) {
s.State.EndConnection()
return true
}