Skip to content

Commit eef4a08

Browse files
authored
Merge pull request #47 from dscsnu/channel-proto
update to json streams
2 parents c77ba90 + 7b37537 commit eef4a08

File tree

4 files changed

+36
-78
lines changed

4 files changed

+36
-78
lines changed

backend/internal/channel/channel.go

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (c *ChannelPool) GetChannel(teamId string) *Channel {
3939
}
4040

4141
type Channel struct {
42-
Recv <-chan protocol.Packet
42+
Recv chan protocol.Packet
4343
BroadcastClients struct {
4444
bc map[chan<- protocol.Packet]struct{}
4545
mut sync.Mutex
@@ -48,7 +48,7 @@ type Channel struct {
4848

4949
func NewChannel() *Channel {
5050

51-
return &Channel{Recv: make(<-chan protocol.Packet), BroadcastClients: struct {
51+
return &Channel{Recv: make(chan protocol.Packet), BroadcastClients: struct {
5252
bc map[chan<- protocol.Packet]struct{}
5353
mut sync.Mutex
5454
}{
@@ -66,6 +66,12 @@ func (c *Channel) AddMember(memberChannel chan<- protocol.Packet) {
6666

6767
}
6868

69+
func (c *Channel) Broadcast(packet protocol.Packet) {
70+
71+
c.Recv <- packet
72+
73+
}
74+
6975
func (c *Channel) Start() {
7076

7177
for packet := range c.Recv {
@@ -76,40 +82,27 @@ func (c *Channel) Start() {
7682

7783
}
7884

85+
c.BroadcastClients.mut.Lock()
86+
for client := range c.BroadcastClients.bc {
87+
88+
client <- packet
89+
}
90+
c.BroadcastClients.mut.Unlock()
91+
7992
}
8093

8194
}
8295

96+
// change backend state with packets
8397
func (c *Channel) handlePacket(packet protocol.Packet) bool {
8498

8599
switch packet.Type {
86100

87-
case protocol.PacketTypeChannelState:
88-
89-
//channelStateMessage, err := protocol.DecodeChannelStateMessage(packet.Message)
90-
91-
case protocol.PacketTypeBackground:
92-
backgroundMessage, err := protocol.DecodeBackgroundMessage(packet.Message)
93-
if err != nil {
101+
case "BackgroundMessage":
94102

95-
return true
96-
97-
}
98-
99-
if backgroundMessage.MsgContext == protocol.JoinBackgroundMessageContext {
100-
101-
c.BroadcastClients.mut.Lock()
102-
for broadcastClient := range c.BroadcastClients.bc {
103-
104-
broadcastClient <- packet
105-
106-
}
107-
c.BroadcastClients.mut.Unlock()
108-
109-
}
103+
case "ChannelStateMessage":
110104

111-
case protocol.PacketTypeGame:
112-
//gameMessage, err := protocol.DecodeGameMessage(packet.Message)
105+
case "GameMessage":
113106

114107
}
115108

backend/internal/controllers/controllers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package controllers
22

33
import (
44
"context"
5+
"encoding/json"
56
"labyrinth/internal/controllers/middleware"
67
"labyrinth/internal/protocol"
78
"labyrinth/internal/router"
@@ -81,7 +82,7 @@ func TeamChannelEventHandler(rtr *router.Router) http.HandlerFunc {
8182

8283
for eventMessage := range listenerChannel {
8384

84-
if _, err = w.Write(eventMessage.Message); err != nil {
85+
if err = json.NewEncoder(w).Encode(eventMessage); err != nil {
8586

8687
rtr.Logger.Debug("http stream write failed")
8788

backend/internal/controllers/team.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package controllers
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"labyrinth/internal/channel"
8+
"labyrinth/internal/protocol"
79
"labyrinth/internal/router"
810
"labyrinth/internal/types"
911
"log/slog"
@@ -144,6 +146,9 @@ func TeamUpdateHandler(rtr *router.Router) http.HandlerFunc {
144146
rtr.Logger.Error("internal error while getting team", "error", err.Error())
145147
}
146148

149+
teamChannel := rtr.State.ChanPool.GetChannel(team.ID)
150+
teamChannel.Broadcast(protocol.Packet{Type: "BackgroundMessage", BackgroundMessage: protocol.BackgroundMessage{Relay: fmt.Sprintf("teamId:%s -> %s joined the team", team.ID, profile.Email), MsgContext: "channel_creation"}})
151+
147152
if err := json.NewEncoder(w).Encode(team); err != nil {
148153
http.Error(w, "error encoding response", http.StatusInternalServerError)
149154
}
Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,21 @@
11
package protocol
22

3-
import (
4-
"encoding/binary"
5-
)
6-
7-
type PacketType int
8-
9-
const (
10-
PacketTypeBackground PacketType = iota
11-
PacketTypeGame
12-
PacketTypeChannelState
13-
)
14-
153
type Packet struct {
16-
Type PacketType
17-
Message []byte
4+
Type string `json:"type"`
5+
BackgroundMessage `json:"backgroundMessage"`
6+
ChannelStateMessage `json:"channelStateMessage"`
7+
GameMessage `json:"gameMessage"`
188
}
199

20-
type BackgroundMessageContext int
21-
22-
const (
23-
JoinBackgroundMessageContext BackgroundMessageContext = iota
24-
LeaveBackgroundMessageContext
25-
)
26-
2710
type BackgroundMessage struct {
28-
Message [128]byte
29-
MsgContext BackgroundMessageContext
11+
Relay string `json:"message"`
12+
MsgContext string `json:"msgcontext"`
3013
}
31-
32-
func DecodeBackgroundMessage(msg []byte) (BackgroundMessage, error) {
33-
34-
backgroundMessage := BackgroundMessage{}
35-
_, err := binary.Decode(msg, binary.LittleEndian, &backgroundMessage)
36-
37-
return backgroundMessage, err
38-
}
39-
4014
type ChannelStateMessage struct {
41-
Open bool
15+
Relay string `json:"message"`
16+
MsgContext string `json:"msgcontext"`
4217
}
43-
44-
func DecodeChannelStateMessage(msg []byte) (ChannelStateMessage, error) {
45-
46-
channelStateMessage := ChannelStateMessage{}
47-
_, err := binary.Decode(msg, binary.LittleEndian, &channelStateMessage)
48-
49-
return channelStateMessage, err
50-
}
51-
5218
type GameMessage struct {
53-
54-
// game message fields
55-
56-
}
57-
58-
func DecodeGameMessage(msg []byte) (GameMessage, error) {
59-
gameMessage := GameMessage{}
60-
_, err := binary.Decode(msg, binary.LittleEndian, &gameMessage)
61-
return gameMessage, err
19+
Relay string `json:"message"`
20+
MsgContext string `json:"msgcontext"`
6221
}

0 commit comments

Comments
 (0)