-
Notifications
You must be signed in to change notification settings - Fork 85
/
server_control_not_connected_handler.go
145 lines (124 loc) · 4.1 KB
/
server_control_not_connected_handler.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
142
143
144
145
//
// Copyright (c) 2018- yutopp ([email protected])
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
//
package rtmp
import (
"github.com/pkg/errors"
"github.com/yutopp/go-rtmp/internal"
"github.com/yutopp/go-rtmp/message"
)
var _ stateHandler = (*serverControlNotConnectedHandler)(nil)
// serverControlNotConnectedHandler Handle control messages from a client which has not send connect at server side.
//
// transitions:
// | "connect" -> controlStreamStateConnected
// | _ -> self
type serverControlNotConnectedHandler struct {
sh *streamHandler
}
func (h *serverControlNotConnectedHandler) onMessage(
chunkStreamID int,
timestamp uint32,
msg message.Message,
) error {
return internal.ErrPassThroughMsg
}
func (h *serverControlNotConnectedHandler) onData(
chunkStreamID int,
timestamp uint32,
dataMsg *message.DataMessage,
body interface{},
) error {
return internal.ErrPassThroughMsg
}
func (h *serverControlNotConnectedHandler) onCommand(
chunkStreamID int,
timestamp uint32,
cmdMsg *message.CommandMessage,
body interface{},
) (err error) {
l := h.sh.Logger()
switch cmd := body.(type) {
case *message.NetConnectionConnect:
l.Info("Connect")
defer func() {
if err != nil {
result := h.newConnectErrorResult()
l.Infof("Connect(Error): ResponseBody = %#v, Err = %+v", result, err)
if err1 := h.sh.stream.ReplyConnect(chunkStreamID, timestamp, result); err1 != nil {
err = errors.Wrapf(err, "Failed to reply response: Err = %+v", err1)
}
}
}()
if err := h.sh.stream.userHandler().OnConnect(timestamp, cmd); err != nil {
return err
}
l.Infof("Set win ack size: Size = %+v", h.sh.stream.streamer().SelfState().AckWindowSize())
if err := h.sh.stream.WriteWinAckSize(ctrlMsgChunkStreamID, timestamp, &message.WinAckSize{
Size: h.sh.stream.streamer().SelfState().AckWindowSize(),
}); err != nil {
return err
}
l.Infof("Set peer bandwidth: Size = %+v, Limit = %+v",
h.sh.stream.streamer().SelfState().BandwidthWindowSize(),
h.sh.stream.streamer().SelfState().BandwidthLimitType(),
)
if err := h.sh.stream.WriteSetPeerBandwidth(ctrlMsgChunkStreamID, timestamp, &message.SetPeerBandwidth{
Size: h.sh.stream.streamer().SelfState().BandwidthWindowSize(),
Limit: h.sh.stream.streamer().SelfState().BandwidthLimitType(),
}); err != nil {
return err
}
l.Infof("Stream Begin: ID = %d", h.sh.stream.streamID)
if err := h.sh.stream.WriteUserCtrl(ctrlMsgChunkStreamID, timestamp, &message.UserCtrl{
Event: &message.UserCtrlEventStreamBegin{
StreamID: h.sh.stream.streamID,
},
}); err != nil {
return err
}
result := h.newConnectSuccessResult()
l.Infof("Connect: ResponseBody = %#v", result)
if err := h.sh.stream.ReplyConnect(chunkStreamID, timestamp, result); err != nil {
return err
}
l.Info("Connected")
h.sh.ChangeState(streamStateServerConnected)
return nil
default:
return internal.ErrPassThroughMsg
}
}
func (h *serverControlNotConnectedHandler) newConnectSuccessResult() *message.NetConnectionConnectResult {
rPreset := h.sh.stream.conn.config.RPreset
if rPreset == nil {
rPreset = defaultResponsePreset
}
return &message.NetConnectionConnectResult{
Properties: rPreset.GetServerConnectResultProperties(),
Information: message.NetConnectionConnectResultInformation{
Level: "status",
Code: message.NetConnectionConnectCodeSuccess,
Description: "Connection succeeded.",
Data: rPreset.GetServerConnectResultData(),
},
}
}
func (h *serverControlNotConnectedHandler) newConnectErrorResult() *message.NetConnectionConnectResult {
rPreset := h.sh.stream.conn.config.RPreset
if rPreset == nil {
rPreset = defaultResponsePreset
}
return &message.NetConnectionConnectResult{
Properties: rPreset.GetServerConnectResultProperties(),
Information: message.NetConnectionConnectResultInformation{
Level: "error",
Code: message.NetConnectionConnectCodeFailed,
Description: "Connection failed.",
Data: rPreset.GetServerConnectResultData(),
},
}
}