-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ddf9db
commit 268b156
Showing
3 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,47 @@ | ||
package proxy | ||
|
||
import ( | ||
"bytes" | ||
"go.minekube.com/gate/pkg/edition/java/proto/packet/config" | ||
"go.minekube.com/gate/pkg/edition/java/proto/packet/plugin" | ||
"go.minekube.com/gate/pkg/edition/java/proto/util" | ||
"go.minekube.com/gate/pkg/internal/oncetrue" | ||
) | ||
|
||
type clientConfigSessionHandler struct { | ||
player *connectedPlayer | ||
brandChannel string | ||
|
||
configSwitchDone oncetrue.OnceWhenTrue | ||
} | ||
|
||
// handleBackendFinishUpdate handles the backend finishing the config stage. | ||
func (h *clientConfigSessionHandler) handleBackendFinishUpdate( | ||
serverConn *serverConnection, | ||
p *config.FinishedUpdate, | ||
onConfigSwitch func(), | ||
) { | ||
smc, ok := serverConn.ensureConnected() | ||
if ok { | ||
brand := serverConn.player.ClientBrand() | ||
if brand == "" && h.brandChannel != "" { | ||
buf := new(bytes.Buffer) | ||
_ = util.WriteString(buf, brand) | ||
|
||
brandPacket := &plugin.Message{ | ||
Channel: h.brandChannel, | ||
Data: buf.Bytes(), | ||
} | ||
_ = smc.WritePacket(brandPacket) | ||
} | ||
err := smc.WritePacket(p) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
if err := h.player.WritePacket(p); err != nil { | ||
return | ||
} | ||
|
||
h.configSwitchDone.DoWhenTrue(onConfigSwitch) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package oncetrue | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type OnceWhenTrue struct { | ||
condition bool | ||
onTrue func() | ||
called bool | ||
mu sync.Mutex | ||
} | ||
|
||
func NewOnceWhenTrue() *OnceWhenTrue { | ||
return &OnceWhenTrue{} | ||
} | ||
|
||
func (o *OnceWhenTrue) DoWhenTrue(onTrue func()) { | ||
o.mu.Lock() | ||
defer o.mu.Unlock() | ||
|
||
o.onTrue = onTrue | ||
|
||
// If condition is true and onTrue hasn't been called, call it | ||
if o.condition && !o.called { | ||
o.onTrue() | ||
o.called = true | ||
} | ||
} | ||
|
||
func (o *OnceWhenTrue) SetTrue() { | ||
o.mu.Lock() | ||
defer o.mu.Unlock() | ||
|
||
o.condition = true | ||
|
||
// If onTrue is set and hasn't been called, call it | ||
if o.onTrue != nil && !o.called { | ||
o.onTrue() | ||
o.called = true | ||
} | ||
} |