From ebffb995c6581724b0b6760bf0c68f0f9d0c4160 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Thu, 12 Dec 2024 06:44:47 +0800 Subject: Use uint32 for SIDs, uint64 for CIDs --- clients.go | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) (limited to 'clients.go') diff --git a/clients.go b/clients.go index 1c3f8e0..abf7283 100644 --- a/clients.go +++ b/clients.go @@ -1,16 +1,14 @@ package main import ( - "crypto/rand" "log/slog" - "math/big" "net" "sync" ) type Client struct { conn *net.Conn - CID string + CID uint64 Nick string Ident string Gecos string @@ -45,7 +43,7 @@ func (client *Client) ClientSource() string { return client.Nick + "!" + client.Ident + "@" + client.Host } -func (client *Client) ServerSource() string { +func (client *Client) ServerSource() uint64 { return client.CID } @@ -64,6 +62,16 @@ func (client *Client) Teardown() { } func NewLocalClient(conn *net.Conn) (*Client, error) { + var cidPart uint32 + { + cidPartCountLock.Lock() + defer cidPartCountLock.Unlock() + if cidPartCount == ^uint32(0) { // UINT32_MAX + return nil, ErrFullClients + } + cidPartCount++ + cidPart = cidPartCount + } client := &Client{ conn: conn, Server: self, @@ -71,24 +79,9 @@ func NewLocalClient(conn *net.Conn) (*Client, error) { Nick: "*", Caps: make(map[string]struct{}), Extra: make(map[string]any), + CID: uint64(self.SID)<<32 | uint64(cidPart), } - for range 10 { - cid_ := []byte(self.SID) - for range 6 { - randint, err := rand.Int(rand.Reader, big.NewInt(26)) - if err != nil { - return nil, err - } - cid_ = append(cid_, byte(65+randint.Uint64())) - } - cid := string(cid_) - _, exists := cidToClient.LoadOrStore(cid, client) - if !exists { - client.CID = cid - return client, nil - } - } - return nil, ErrCIDBusy + return client, nil } func (client *Client) checkRegistration() error { @@ -136,6 +129,8 @@ const ( ) var ( - cidToClient = sync.Map{} - nickToClient = sync.Map{} + cidToClient = sync.Map{} + nickToClient = sync.Map{} + cidPartCount uint32 + cidPartCountLock sync.Mutex ) -- cgit v1.2.3