-
Notifications
You must be signed in to change notification settings - Fork 0
/
def.go
129 lines (115 loc) · 3.27 KB
/
def.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
/**
* Copyright © 2024, Staufi Tech - Switzerland
* All rights reserved.
*
* ________________________ ___ _ ________________ _ ____
* / _____ _ ____________/ / __|_| /_______________ | | ___/
* ( (____ _| |_ _____ _ _ _| |__ _ | |_____ ____| |_|_
* \____ (_ _|____ | | | (_ __) | | | ___ |/ ___) _ \
* _____) )| |_/ ___ | |_| | | | | | | | ____( (___| | | |
* (______/ \__)_____|____/ |_| |_| |_|_____)\____)_| |_|
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package websocket
import (
"unsafe"
log "github.com/ChrIgiSta/go-utils/logger"
"github.com/gorilla/websocket"
)
type Message struct {
MessageType int
Data []byte
ClientId int
}
type Events interface {
OnReceive(msg Message)
OnDisconnect(id int)
OnConnect(id int)
OnFailure(exited bool, err error)
}
func getIdFromConn(conn *websocket.Conn) int {
return int(uintptr(unsafe.Pointer(conn)))
}
type EventType int
const (
Connect EventType = 1
Disconnect EventType = 0
Failure EventType = -1
FailureWithExit EventType = -2
)
type Event struct {
Err error
Type EventType
Id int
}
type EventsToChannel struct {
messageChannel chan<- Message
eventChannel chan<- Event
}
func NewEventsToChannel(messageChannel chan<- Message,
eventChannel chan<- Event) *EventsToChannel {
return &EventsToChannel{
messageChannel: messageChannel,
eventChannel: eventChannel,
}
}
func (t *EventsToChannel) OnReceive(msg Message) {
_ = log.Debug("Evnt2Channel", "onReceive: %v", msg)
if t.messageChannel != nil {
t.messageChannel <- msg
} else {
_ = log.Error("Evnt2Channel", "message channel is nil")
}
}
func (t *EventsToChannel) OnDisconnect(id int) {
_ = log.Debug("Evnt2Channel", "onDisconnect: %v", id)
if t.eventChannel != nil {
t.eventChannel <- Event{
Err: nil,
Type: Disconnect,
Id: id,
}
} else {
_ = log.Error("Evnt2Channel", "event channel is nil")
}
}
func (t *EventsToChannel) OnConnect(id int) {
_ = log.Debug("Evnt2Channel", "onConnect: %v", id)
if t.eventChannel != nil {
t.eventChannel <- Event{
Err: nil,
Type: Connect,
Id: id,
}
} else {
_ = log.Error("Evnt2Channel", "event channel is nil")
}
}
func (t *EventsToChannel) OnFailure(exited bool, err error) {
_ = log.Debug("Evnt2Channel", "onFailure: %v", err)
fType := Failure
if exited {
fType = FailureWithExit
}
if t.eventChannel != nil {
t.eventChannel <- Event{
Err: err,
Type: fType,
Id: -1,
}
} else {
_ = log.Error("Evnt2Channel", "event channel is nil")
}
}