/
types.go
100 lines (86 loc) · 2.9 KB
/
types.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
/*************************************************************************
* Copyright 2021 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package websocketRouter
import (
"encoding/json"
"errors"
"time"
"github.com/gorilla/websocket"
)
const (
defaultBufferSize int = 1024
defaultMsgDepth int = 32
defaultByteBufferSize int = 1024
defaultSubProtoCount int = 4
errThreshHold int = 3 //how many sequential errors can we consume before exit
subProtoNegotiationDeadline time.Duration = time.Millisecond * 2000
)
var (
ErrSubProtocolsRequested = errors.New("No Subprotocols Requested")
ErrSubProtoClosed = errors.New("SubProtoConn closed")
ErrSubProtoNotFound = errors.New("Subprotocol not found")
ErrServerClosed = errors.New("SubProtoServer closed")
ErrAlreadyRunning = errors.New("Already Running")
ACK_RESP = SubProtocolsResp{`ACK`}
ERR_RESP = SubProtocolsResp{`ERROR`}
)
type subProtoSendMsg struct {
Type string `json:"type"`
Data interface{} `json:"data"`
}
type subProtoRecvMsg struct {
Type string `json:"type"`
Data json.RawMessage `json:"data"`
}
// UnkProtoMsg represents a subprotocol packet with an uknown type and unknown structure.
// This type is useful for handling erroneous messages.
type UnkProtoMsg struct {
Type string
Data json.RawMessage
}
// SubProtocolsReq is used to request websocket subprotocol channels during intialization.
type SubProtocolsReq struct {
Subs []string //the string IDs of subprotocols to be used
}
// SubProtocolsResp defines the structure that a websocket router uses when responding to a request to initalize subprotocols.
type SubProtocolsResp struct {
Resp string
}
type subParentRouter interface {
writeProtoJSON(proto string, obj interface{}) error
}
// readDeadLine is a simple wrapper around a websocket connection that allows for recieving an object with a timeout.
func readDeadLine(conn *websocket.Conn, dur time.Duration, obj interface{}) error {
var err error
err = conn.SetReadDeadline(time.Now().Add(dur))
if err != nil {
return err
}
//always remove the read deadline
defer conn.SetReadDeadline(time.Time{})
err = conn.ReadJSON(obj)
if err != nil {
return err
}
return nil
}
// writeDeadLine is a simple wrapper around a websocket connection that allows for sending an object with a timeout.
func writeDeadLine(conn *websocket.Conn, dur time.Duration, obj interface{}) error {
var err error
err = conn.SetWriteDeadline(time.Now().Add(dur))
if err != nil {
return err
}
//always remove the read deadline
defer conn.SetWriteDeadline(time.Time{})
err = conn.WriteJSON(obj)
if err != nil {
return err
}
return nil
}