diff options
author | Runxi Yu <me@runxiyu.org> | 2024-12-12 06:44:47 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2024-12-12 06:44:47 +0800 |
commit | ebffb995c6581724b0b6760bf0c68f0f9d0c4160 (patch) | |
tree | aa9f232f988130a5d8dda9fc1fbd13cf8fc187a2 /clients.go | |
parent | CAP LS: Take version as integer (diff) | |
download | meseircd-ebffb995c6581724b0b6760bf0c68f0f9d0c4160.tar.gz meseircd-ebffb995c6581724b0b6760bf0c68f0f9d0c4160.tar.zst meseircd-ebffb995c6581724b0b6760bf0c68f0f9d0c4160.zip |
Use uint32 for SIDs, uint64 for CIDs
Diffstat (limited to 'clients.go')
-rw-r--r-- | clients.go | 41 |
1 files changed, 18 insertions, 23 deletions
@@ -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 ) |