Skip to content

Commit

Permalink
nethernet: Update to 1.21.30
Browse files Browse the repository at this point in the history
  • Loading branch information
lactyy committed Sep 20, 2024
1 parent 8274a46 commit 9b4af1d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
36 changes: 24 additions & 12 deletions dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"log/slog"
"math/rand"
"strconv"
"sync"
)

// Dialer encapsulates options for establishing a connection with a NetherNet network through [Dialer.DialContext]
Expand Down Expand Up @@ -276,6 +275,8 @@ func (d Dialer) handleConn(ctx context.Context, conn *Conn, signals <-chan *Sign
select {
case <-ctx.Done():
return
case <-conn.closed:
return
case signal := <-signals:
switch signal.Type {
case SignalTypeCandidate, SignalTypeError:
Expand All @@ -294,8 +295,10 @@ func (d Dialer) notifySignals(networkID uint64, signaling Signaling) (*dialerNot
n := &dialerNotifier{
Dialer: d,

signals: make(chan *Signal),
errs: make(chan error),
signals: make(chan *Signal),
errs: make(chan error),
closed: make(chan struct{}),

networkID: networkID,
}
return n, signaling.Notify(n)
Expand All @@ -305,31 +308,40 @@ func (d Dialer) notifySignals(networkID uint64, signaling Signaling) (*dialerNot
type dialerNotifier struct {
Dialer

signals chan *Signal // Notifies incoming Signal that has the same IDs
errs chan error // Notifies error occurred in Signaling
signals chan *Signal // Notifies incoming Signal that has the same IDs
errs chan error // Notifies error occurred in Signaling
closed chan struct{} // Notifies that dialerNotifier is closed, and ensures that closure occur only once

once sync.Once // Ensures closure only occur once
networkID uint64 // Remote network ID
networkID uint64 // Remote network ID
}

func (d *dialerNotifier) NotifySignal(signal *Signal) {
if signal.ConnectionID != d.ConnectionID || signal.NetworkID != d.networkID {
return
}

d.signals <- signal
}

func (d *dialerNotifier) NotifyError(err error) {
d.errs <- err
select {
case <-d.closed:
return
default:
}

select {
case d.errs <- err:
default:
}

if errors.Is(err, ErrSignalingStopped) {
d.close()
}
}

func (d *dialerNotifier) close() {
d.once.Do(func() {
close(d.signals)
close(d.errs)
})
close(d.signals)
close(d.errs)
close(d.closed)
}
4 changes: 2 additions & 2 deletions discovery/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestListen(t *testing.T) {
}
})
d.ServerData(&ServerData{
Version: 0x2,
Version: 0x3,
ServerName: "Da1z981",
LevelName: "LAN のデバッグ",
GameType: 2,
Expand All @@ -38,7 +38,7 @@ func TestListen(t *testing.T) {
})))

var c nethernet.ListenConfig
l, err := c.Listen(cfg.NetworkID, d)
l, err := c.Listen(d)
if err != nil {
t.Fatalf("error listening: %s", err)
}
Expand Down
5 changes: 5 additions & 0 deletions discovery/server_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type ServerData struct {
PlayerCount int32
MaxPlayerCount int32
IsEditorWorld bool
Hardcore bool
TransportLayer int32
}

Expand All @@ -27,6 +28,7 @@ func (d *ServerData) MarshalBinary() ([]byte, error) {
_ = binary.Write(buf, binary.LittleEndian, d.PlayerCount)
_ = binary.Write(buf, binary.LittleEndian, d.MaxPlayerCount)
_ = binary.Write(buf, binary.LittleEndian, d.IsEditorWorld)
_ = binary.Write(buf, binary.LittleEndian, d.Hardcore)
_ = binary.Write(buf, binary.LittleEndian, d.TransportLayer)

return buf.Bytes(), nil
Expand Down Expand Up @@ -60,6 +62,9 @@ func (d *ServerData) UnmarshalBinary(data []byte) error {
if err := binary.Read(buf, binary.LittleEndian, &d.IsEditorWorld); err != nil {
return fmt.Errorf("read editor world: %w", err)
}
if err := binary.Read(buf, binary.LittleEndian, &d.Hardcore); err != nil {
return fmt.Errorf("read hardcore: %w", err)
}
if err := binary.Read(buf, binary.LittleEndian, &d.TransportLayer); err != nil {
return fmt.Errorf("read transport layer: %w", err)
}
Expand Down

0 comments on commit 9b4af1d

Please sign in to comment.