Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
eac2741
Require the correct packet to initialize TCP connection
dimspell Jul 14, 2025
a6cc761
Move TCP probe to the probe/ directory
dimspell Jul 14, 2025
928443e
Remove extra data in host manager
dimspell Jul 14, 2025
2ef5c42
Remove unused code
dimspell Jul 14, 2025
ffa5dcd
Refactor code related to starting the fake host
dimspell Jul 14, 2025
26a1d1b
Handle relay leave room event to close empty rooms
dimspell Jul 14, 2025
bd49274
More logs
dimspell Jul 14, 2025
774c36f
Fix degradation - TCP listener didnt start
dimspell Jul 14, 2025
f85a123
Less logs
dimspell Jul 14, 2025
b795680
Less logs
dimspell Jul 14, 2025
b5f962a
Print error
dimspell Jul 14, 2025
2875818
Less logs
dimspell Jul 14, 2025
2f7242d
This fragment causes a problem with receiving `##user` TCP packet
dimspell Jul 14, 2025
12a0e12
Stop the connection after disconnect
dimspell Jul 14, 2025
11cbe55
Use one function to simplify the disconnection
dimspell Jul 14, 2025
c442949
Spin multiple fake host for testing
dimspell Jul 15, 2025
7701c8c
Better log / Remove unused code
dimspell Jul 17, 2025
63bd836
Better naming / Simplify code
dimspell Jul 17, 2025
366fcfe
Simplify code
dimspell Jul 17, 2025
9a067c1
Use interface to mock
dimspell Jul 17, 2025
25e0c17
Rename parameters
dimspell Jul 17, 2025
fed8a93
Remove unused func
dimspell Jul 17, 2025
a6848e3
Test fake host closing
dimspell Jul 17, 2025
7ed34b0
Less logs
dimspell Jul 17, 2025
2387288
Add some testers
dimspell Jul 17, 2025
b792372
Try to call disconnect from relay
dimspell Jul 17, 2025
e0c29a8
Try again with dial-tcp updated
dimspell Jul 17, 2025
b4e55b9
Less logs
dimspell Jul 17, 2025
3d95ae5
Clear all after 30s
dimspell Jul 17, 2025
2204945
Try to build on linux
dimspell Jul 18, 2025
27fd9a7
Remove linux ci
dimspell Jul 18, 2025
f47d2cd
Disable probe
dimspell Jul 18, 2025
507d0d1
Better logs
dimspell Jul 18, 2025
ab67837
Disable custom handshake
dimspell Jul 18, 2025
54e7df7
Revert
dimspell Jul 18, 2025
5bd275d
Cleanup
dimspell Jul 18, 2025
45171c8
Remove unused
dimspell Jul 18, 2025
ac764cf
Stop just once
dimspell Jul 18, 2025
f9ff8df
Simplify some code
dimspell Jul 20, 2025
19d2846
vibe coding
dimspell Jul 21, 2025
9163929
Put all config parameters into Console struct
dimspell Jul 24, 2025
d995a74
Fix lan acceptance test
dimspell Jul 24, 2025
d044d2a
Remove unused code
dimspell Jul 24, 2025
08a2049
The Big Proxy Rewrite
dimspell Jul 24, 2025
313a3c8
Fix panic occurring in Multiplayer
dimspell Jul 25, 2025
843720b
Rename Multiplayer as RoomService
dimspell Jul 25, 2025
3230046
Refactor Session Manager and move packet creation to packet module
dimspell Jul 25, 2025
2a219ad
Make a use of refactored SessionManager
dimspell Jul 25, 2025
5b5c45d
Refactor / rearrange code
dimspell Jul 25, 2025
cc53bbe
Remove commented code
dimspell Jul 25, 2025
467de4d
Remove packetlogger
dimspell Jul 27, 2025
622504a
Use global logger, do not ignore the packets
dimspell Jul 27, 2025
710e24e
Fix relay listing rooms
dimspell Jul 27, 2025
7bda531
Use table test so the file is shorter and without duplicated code
dimspell Jul 27, 2025
5615ee1
Log error with failure
dimspell Jul 27, 2025
8081812
The first packet - the handshake packet - was not sent
dimspell Jul 27, 2025
da659c7
Send the packets (ignoring who is the author)
dimspell Jul 27, 2025
d2c0b70
Make use of proxy factory
dimspell Jul 25, 2025
d2f61ba
Rename binary name
dimspell Jul 27, 2025
05e2c81
Remove unused markdown file
dimspell Jul 27, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions cmd/event-test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"fmt"
"time"

"github.com/kelindar/event"
)

// Various event types
const EventA = 0x01

// Event type for testing purposes
type Event struct {
Data string
}

// Type returns the event type
func (ev Event) Type() uint32 {
return EventA
}

// newEventA creates a new instance of an event
func newEventA(data string) Event {
return Event{Data: data}
}

func main() {
bus := event.NewDispatcher()
// bus.Close()

// Subcribe to event A, and automatically unsubscribe at the end
defer event.SubscribeTo(bus, EventA, func(e Event) {
println("(consumer 1)", e.Data)
})()

// Subcribe to event A, and automatically unsubscribe at the end
unsub := event.SubscribeTo(bus, EventA, func(e Event) {
println("(consumer 2)", e.Data)
})

// Publish few events

time.AfterFunc(time.Second*5, func() {
unsub()
})

go func() {
// after := time.After(time.Second * 5)

tk := time.NewTicker(time.Second)
// defer tk.Stop()

for range tk.C {
fmt.Println("publishing event 4")
event.Publish(bus, newEventA("event 4"))
}
}()

event.Publish(bus, newEventA("event 1"))
event.Publish(bus, newEventA("event 2"))
event.Publish(bus, newEventA("event 3"))

time.Sleep(50 * time.Second)
}
78 changes: 78 additions & 0 deletions cmd/listener-check-tcp/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"context"
"fmt"
"log"
"log/slog"
"net"
"os"
"time"

"github.com/dimspell/gladiator/internal/app/logger"
"github.com/dimspell/gladiator/internal/backend/redirect"
"github.com/dimspell/gladiator/probe"
)

func main() {
logger.SetColoredLogger(os.Stderr, slog.LevelDebug, false)

host, port := "127.0.0.1", "21370"

l, err := redirect.NewListenerTCP(host, port, func(p []byte) (err error) {
log.Printf("Received on TCP %s", p)
return nil
})
if err != nil {
log.Fatalf("listener start error: %v", err)
}
defer func() {
if err := l.Close(); err != nil {
log.Fatalf("close error: %v", err)
return
}
}()

// lctx, cancel := context.WithCancel(context.Background())

lctx := context.Background()

// go func() {
// time.Sleep(3 * time.Second)
// cancel()
// }()

go func() {
if err := l.Run(lctx); err != nil {
log.Printf("run error: %v", err)
return
}
}()

errProbe := probe.StartProbeTCP(context.Background(), net.JoinHostPort(host, port), func() {
log.Println("Closing probe 1....")
})
if errProbe != nil {
log.Fatalf("probe error: %v", err)
}

go func() {
time.Sleep(1 * time.Second)
ticker := time.NewTicker(2 * time.Second)

for now := range ticker.C {
fmt.Println("Alive", l.Alive(now, 5*time.Second), now.Format(time.TimeOnly))
}
}()

time.Sleep(100 * time.Second)

// errProbe2 := probe.StartProbeTCP(context.Background(), net.JoinHostPort(host, port), func() {
// log.Println("Closing probe 2....")
// })
// if errProbe2 != nil {
// log.Fatalf("probe2 error: %v", err)
// }

select {}
}
25 changes: 6 additions & 19 deletions cmd/p2p-host/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import (
"os"
"time"

"connectrpc.com/connect"
multiv1 "github.com/dimspell/gladiator/gen/multi/v1"
"github.com/dimspell/gladiator/gen/multi/v1/multiv1connect"
"github.com/dimspell/gladiator/internal/app/logger"
"github.com/dimspell/gladiator/internal/app/logger/logging"
"github.com/dimspell/gladiator/internal/backend/bsession"
"github.com/dimspell/gladiator/internal/backend/proxy"
"github.com/dimspell/gladiator/internal/backend/proxy/p2p"
"github.com/dimspell/gladiator/internal/backend/redirect"
"github.com/dimspell/gladiator/internal/model"
)

Expand Down Expand Up @@ -50,9 +48,9 @@ func main() {
Username: meName,
}
p2pProxy := p2p.ProxyP2P{}
px := p2pProxy.Create(session).(*p2p.PeerToPeer)
px.NewUDPRedirect = redirect.NewNoop
px.NewTCPRedirect = redirect.NewLineReader
px := p2pProxy.Create(session, gm).(*p2p.PeerToPeer)
// px.NewUDPRedirect = redirect.NewNoop
// px.NewTCPRedirect = redirect.NewLineReader

if err := session.ConnectOverWebsocket(ctx, user1, fmt.Sprintf("ws://%s/lobby", consoleUri)); err != nil {
slog.Error("failed to connect over websocket", logging.Error(err))
Expand Down Expand Up @@ -80,26 +78,15 @@ func main() {
}
}()

game, err := gm.CreateGame(ctx, connect.NewRequest(&multiv1.CreateGameRequest{
GameName: roomId,
Password: "",
MapId: multiv1.GameMap_AbandonedRealm,
HostUserId: meUserId,
HostIpAddress: "127.0.1.2", // Not used for P2P traffic
}))
if err != nil {
slog.Error("failed to create game over console", logging.Error(err))
return
}
slog.Info("created game over console")
params := proxy.CreateParams{GameID: roomId, Password: "", MapId: multiv1.GameMap_AbandonedRealm}

if _, err := px.CreateRoom(proxy.CreateParams{GameID: game.Msg.Game.GameId}); err != nil {
if err := px.CreateRoom(ctx, params); err != nil {
slog.Error("failed to create room over proxy", logging.Error(err))
return
}
slog.Info("created room over proxy")

if err := px.HostRoom(ctx, proxy.HostParams{GameID: game.Msg.Game.GameId}); err != nil {
if err := px.SetRoomReady(ctx, params); err != nil {
slog.Error("failed to host room over proxy", logging.Error(err))
return
}
Expand Down
65 changes: 6 additions & 59 deletions cmd/p2p-join/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
"os"
"time"

"connectrpc.com/connect"
multiv1 "github.com/dimspell/gladiator/gen/multi/v1"
"github.com/dimspell/gladiator/gen/multi/v1/multiv1connect"
"github.com/dimspell/gladiator/internal/app/logger"
"github.com/dimspell/gladiator/internal/app/logger/logging"
"github.com/dimspell/gladiator/internal/backend/bsession"
"github.com/dimspell/gladiator/internal/backend/proxy"
"github.com/dimspell/gladiator/internal/backend/proxy/p2p"
"github.com/dimspell/gladiator/internal/backend/redirect"
"github.com/dimspell/gladiator/internal/model"
)

Expand Down Expand Up @@ -57,9 +54,9 @@
UserId: meUserId,
Username: meName,
}
px := p2p.NewPeerToPeer(session)
px.NewUDPRedirect = redirect.NewNoop
px.NewTCPRedirect = redirect.NewLineReader
px := p2p.NewPeerToPeer(session, gm)

Check failure on line 57 in cmd/p2p-join/main.go

View workflow job for this annotation

GitHub Actions / test

not enough arguments in call to p2p.NewPeerToPeer
// px.NewUDPRedirect = redirect.NewNoop
// px.NewTCPRedirect = redirect.NewLineReader

if err := session.ConnectOverWebsocket(ctx, user2, fmt.Sprintf("ws://%s/lobby", consoleUri)); err != nil {
slog.Error("failed to connect over websocket", logging.Error(err))
Expand Down Expand Up @@ -87,66 +84,16 @@
}
}()

game, err := gm.GetGame(ctx, connect.NewRequest(&multiv1.GetGameRequest{
GameRoomId: roomId,
}))
if err != nil {
slog.Error("failed to get game", logging.Error(err))
return
}
slog.Info("got game", "game", game.Msg.Game, "players", game.Msg.Players)

if err := px.SelectGame(proxy.GameData{
Game: game.Msg.Game,
Players: game.Msg.Players,
}); err != nil {
if _, _, err := px.GetGame(ctx, roomId); err != nil {
slog.Error("failed to select a game", logging.Error(err))
return
}

addr, err := px.GetPlayerAddr(proxy.GetPlayerAddrParams{
GameID: roomId,
UserID: otherUserId,
IPAddress: "127.0.1.2",
HostUserID: fmt.Sprintf("%d", otherUserId),
})
if err != nil {
slog.Error("failed to get player address", logging.Error(err))
return
}
slog.Info("got player address", "address", addr)

join, err := gm.JoinGame(ctx, connect.NewRequest(&multiv1.JoinGameRequest{
UserId: meUserId,
GameRoomId: roomId,
IpAddress: "127.0.0.1",
}))
if err != nil {
if _, err := px.JoinGame(ctx, roomId, ""); err != nil {
slog.Error("failed to join game", logging.Error(err))
return
}
slog.Info("joined game", "players", join.Msg.Players)

if _, err := px.Join(ctx, proxy.JoinParams{
HostUserID: otherUserId,
GameID: roomId,
HostUserIP: "127.0.1.2",
}); err != nil {
slog.Error("failed to join game", logging.Error(err))
return
}

addr2, err := px.ConnectToPlayer(ctx, proxy.GetPlayerAddrParams{
GameID: roomId,
UserID: otherUserId,
IPAddress: "127.0.1.2",
HostUserID: fmt.Sprintf("%d", otherUserId),
})
if err != nil {
slog.Error("failed to get player address", logging.Error(err))
return
}
slog.Info("connected to player", "address", addr2)
slog.Info("joined game")

select {}
}
33 changes: 13 additions & 20 deletions cmd/relay-host/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"os"
"time"

"connectrpc.com/connect"
multiv1 "github.com/dimspell/gladiator/gen/multi/v1"
"github.com/dimspell/gladiator/gen/multi/v1/multiv1connect"
"github.com/dimspell/gladiator/internal/app/logger"
Expand All @@ -25,6 +24,9 @@ import (
func main() {
logger.SetColoredLogger(os.Stderr, slog.LevelDebug, false)

consoleUri := fmt.Sprintf("%s://%s/grpc", "http", "localhost:2137")
gameClient := multiv1connect.NewGameServiceClient(&http.Client{Timeout: 10 * time.Second}, consoleUri)

px := &relay.ProxyRelay{
RelayServerAddr: "localhost:9999",
}
Expand All @@ -34,7 +36,7 @@ func main() {
session.Username = "knight"
session.CharacterID = 1
session.ClassType = model.ClassTypeKnight
proxyClient := px.Create(session).(*relay.Relay)
proxyClient := px.Create(session, gameClient).(*relay.Relay)
session.Proxy = proxyClient

ctx := context.TODO()
Expand Down Expand Up @@ -72,38 +74,29 @@ func main() {

var err error

_, err = session.Proxy.CreateRoom(proxy.CreateParams{
GameID: roomID,
})
if err != nil {
slog.Error("CreateRoom", logging.Error(err))
return
params := proxy.CreateParams{
GameID: roomID,
MapId: multiv1.GameMap_FrozenLabyrinth,
Password: "",
}

consoleUri := fmt.Sprintf("%s://%s/grpc", "http", "localhost:2137")
gameClient := multiv1connect.NewGameServiceClient(&http.Client{Timeout: 10 * time.Second}, consoleUri)
if _, err := gameClient.CreateGame(ctx, connect.NewRequest(&multiv1.CreateGameRequest{
GameName: roomID,
Password: "",
MapId: multiv1.GameMap(1),
HostUserId: session.UserID,
HostIpAddress: "127.0.0.1",
})); err != nil {
slog.Error("CreateGame", logging.Error(err))
err = session.Proxy.CreateRoom(ctx, params)
if err != nil {
slog.Error("CreateRoom", logging.Error(err))
return
}

// startFakeBackendServer(ctx)

err = session.Proxy.HostRoom(ctx, proxy.HostParams{GameID: roomID})
err = session.Proxy.SetRoomReady(ctx, params)
if err != nil {
slog.Error("HostRoom", logging.Error(err))
return
}

r := chi.NewRouter()
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
v := proxyClient.Debug()
v := proxyClient

doc, err := json.MarshalIndent(v, "", " ")
if err != nil {
Expand Down
Loading
Loading