From 9f9c6497e8a87b6164302267deec0b37f31d4d16 Mon Sep 17 00:00:00 2001 From: singchia Date: Thu, 16 May 2024 17:17:30 +0800 Subject: [PATCH] conn: adjust GetClientID function's signature, add wantedID field --- conn/conn_server.go | 2 +- conn/delegate.go | 2 +- conn/example/server/server.go | 10 +++++----- delegate/delegate.go | 2 +- examples/chatroom/server/main.go | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/conn/conn_server.go b/conn/conn_server.go index 2681c98..fab1f51 100644 --- a/conn/conn_server.go +++ b/conn/conn_server.go @@ -291,7 +291,7 @@ func (sc *ServerConn) handleInConnPacket(pkt *packet.ConnPacket) iodefine.IORet sc.meta = pkt.ConnData.Meta if pkt.ClientIDAcquire() { if sc.dlgt != nil { - sc.clientID, err = sc.dlgt.GetClientID(sc.meta) + sc.clientID, err = sc.dlgt.GetClientID(pkt.ClientID, sc.meta) } if sc.clientID == 0 && err == nil { // if delegate returns 0 meaning use a ID by inner diff --git a/conn/delegate.go b/conn/delegate.go index 96ee00b..d49df7d 100644 --- a/conn/delegate.go +++ b/conn/delegate.go @@ -14,5 +14,5 @@ type ClientConnDelegate interface { type ServerConnDelegate interface { Delegate Heartbeat(delegate.ConnDescriber) error - GetClientID(meta []byte) (uint64, error) + GetClientID(wantedID uint64, meta []byte) (uint64, error) } diff --git a/conn/example/server/server.go b/conn/example/server/server.go index 1695f20..4d93bbc 100644 --- a/conn/example/server/server.go +++ b/conn/example/server/server.go @@ -34,12 +34,12 @@ func newServer() *server { func (s *server) ConnOnline(_ delegate.ConnDescriber) error { return nil } func (s *server) ConnOffline(_ delegate.ConnDescriber) error { return nil } func (s *server) Heartbeat(_ delegate.ConnDescriber) error { return nil } -func (s *server) GetClientID(meta []byte) (uint64, error) { +func (s *server) GetClientID(_ uint64, meta []byte) (uint64, error) { return s.idFactory.GetID(), nil } func forceFree() { - for _ = range time.Tick(30 * time.Second) { + for range time.Tick(30 * time.Second) { debug.FreeOSMemory() } } @@ -90,7 +90,7 @@ func main() { } rc, ok := clients.Load(clientID) if !ok { - log.Error("clientID not found '%d'\n", clientID) + log.Errorf("clientID not found '%d'\n", clientID) continue } rc.(*conn.ServerConn).Close() @@ -127,7 +127,7 @@ func main() { for { netconn, err := ln.Accept() if err != nil { - log.Error("accept err: %s", err) + log.Errorf("accept err: %s", err) break } go func(netconn net.Conn) { @@ -135,7 +135,7 @@ func main() { conn.OptionServerConnPacketFactory(pf), conn.OptionServerConnDelegate(server)) if err != nil { - log.Error("new recvconn err: %s", err) + log.Errorf("new recvconn err: %s", err) return } clients.Store(rc.ClientID(), rc) diff --git a/delegate/delegate.go b/delegate/delegate.go index 4d5a93d..b34547e 100644 --- a/delegate/delegate.go +++ b/delegate/delegate.go @@ -25,7 +25,7 @@ type ServerConnDelegate interface { ClientConnDelegate Heartbeat(ConnDescriber) error // requirements - GetClientID(meta []byte) (uint64, error) + GetClientID(wantedID uint64, meta []byte) (uint64, error) } // dialogue layer delegation diff --git a/examples/chatroom/server/main.go b/examples/chatroom/server/main.go index 759f3a4..0ca013d 100644 --- a/examples/chatroom/server/main.go +++ b/examples/chatroom/server/main.go @@ -41,7 +41,7 @@ func NewChatRoom() *ChatRoom { } } -func (room *ChatRoom) GetClientID(meta []byte) (uint64, error) { +func (room *ChatRoom) GetClientID(_ uint64, meta []byte) (uint64, error) { room.mtx.RLock() defer room.mtx.RUnlock()