From 9b4af1d2ca4b1a46e62313813976abdda91d197e Mon Sep 17 00:00:00 2001 From: lactyy <92302002+lactyy@users.noreply.github.com> Date: Fri, 20 Sep 2024 11:06:34 +0900 Subject: [PATCH] nethernet: Update to 1.21.30 --- dial.go | 36 ++++++++++++++++++++++++------------ discovery/listener_test.go | 4 ++-- discovery/server_data.go | 5 +++++ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/dial.go b/dial.go index 0b91bb7..1b68f67 100644 --- a/dial.go +++ b/dial.go @@ -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] @@ -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: @@ -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) @@ -305,22 +308,32 @@ 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() @@ -328,8 +341,7 @@ func (d *dialerNotifier) NotifyError(err error) { } func (d *dialerNotifier) close() { - d.once.Do(func() { - close(d.signals) - close(d.errs) - }) + close(d.signals) + close(d.errs) + close(d.closed) } diff --git a/discovery/listener_test.go b/discovery/listener_test.go index 332aec9..a898f78 100644 --- a/discovery/listener_test.go +++ b/discovery/listener_test.go @@ -24,7 +24,7 @@ func TestListen(t *testing.T) { } }) d.ServerData(&ServerData{ - Version: 0x2, + Version: 0x3, ServerName: "Da1z981", LevelName: "LAN のデバッグ", GameType: 2, @@ -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) } diff --git a/discovery/server_data.go b/discovery/server_data.go index e7eff42..771976b 100644 --- a/discovery/server_data.go +++ b/discovery/server_data.go @@ -14,6 +14,7 @@ type ServerData struct { PlayerCount int32 MaxPlayerCount int32 IsEditorWorld bool + Hardcore bool TransportLayer int32 } @@ -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 @@ -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) }