-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
119 lines (106 loc) · 4.02 KB
/
Copy pathtypes.go
File metadata and controls
119 lines (106 loc) · 4.02 KB
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
package duplex
import (
"sync"
"time"
peer "github.com/cloudlink-delta/peerjs-go"
"github.com/goccy/go-json"
"github.com/rs/zerolog"
)
type Listener func(*RxPacket)
type OpcodeMatcher struct {
Opcodes []string
Callback func(*RxPacket)
}
// Peer is a representation of a peer connection for a duplex instance.
type Peer struct {
Parent *Instance // Pointer to the parent instance that created this peer
Lock *sync.Mutex // Lock for thread safety
KeyStore map[string]any // Map of key-value pairs of any type
KeyLock *sync.Mutex
OpcodeMatchers map[*Peer]*OpcodeMatcher // Map of key-value pairs to listen to specific opcodes from specific peers.
Listeners map[string]Listener // Map of key-value pairs to listeners.
Features []string // List of features advertised by this peer
IsInitiator bool // True if this peer initiated the connection
IsBridge bool // True if this peer is a bridge
IsRelay bool // True if this peer is a relay
IsDiscovery bool // True if this peer is a discovery
Done chan bool // Channel to signal connection closure
RTT int64 // Round-trip time (in milliseconds)
GiveNameRemapper func() string
Logger zerolog.Logger
*peer.DataConnection // Pointer to the peer data connection
}
// Instance is a representation of a duplex instance.
type Instance struct {
Name string
Pinger bool
PingInterval time.Duration
Handler *peer.Peer
Close chan bool
Done chan bool
RetryCounter int
MaxRetries int
Peers Peers
OnCreate func()
AfterNegotiation func(*Peer)
OnOpen func(*Peer)
OnClose func(*Peer)
CustomHandlersRequiredFeatures map[string][]string
CustomHandlers map[string]func(*Peer, *RxPacket)
RemappedHandlersRequiredFeatures map[string][]string
RemappedHandlers map[string]func(*Peer, *RxPacket)
IsBridge bool
IsRelay bool
IsDiscovery bool
OnBridgeConnected func(*Peer)
OnRelayConnected func(*Peer)
OnDiscoveryConnected func(*Peer)
isReconnecting bool
mu sync.Mutex
active_time_start time.Time
peerjs_config *peer.Options
Logger *zerolog.Logger
}
type PeerState struct {
Uptime string `json:"uptime"`
ConnectionState bool `json:"connection_state"`
}
type Packet struct {
Opcode string `json:"opcode"`
Origin string `json:"origin,omitempty"`
Target string `json:"target,omitempty"`
TTL int `json:"ttl,omitempty"`
Id string `json:"id,omitempty"`
Method string `json:"method,omitempty"`
Listener string `json:"listener,omitempty"`
}
type RxPacket struct {
Packet
Payload json.RawMessage `json:"payload,omitempty"`
}
func (p *RxPacket) String() string {
resp, _ := json.Marshal(p)
return string(resp)
}
type TxPacket struct {
Packet
Payload any `json:"payload,omitempty"`
}
func (p *TxPacket) String() string {
resp, _ := json.Marshal(p)
return string(resp)
}
type NegotiationArgs struct {
Version VersionArgs `json:"version"`
SpecVersion int `json:"spec_version"`
Plugins []string `json:"plugins"`
IsBridge bool `json:"is_bridge"`
IsRelay bool `json:"is_relay"`
IsDiscovery bool `json:"is_discovery"`
}
type VersionArgs struct {
Type string `json:"type"`
Major int `json:"major"`
Minor int `json:"minor"`
Patch int `json:"patch"`
}