56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package session
|
|
|
|
import (
|
|
"spark-server/internal/pkg/i18n"
|
|
"spark-server/internal/transport/tcp/protocol"
|
|
)
|
|
|
|
func (m *memManager) Get(sessionID string) (Session, bool) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
s, ok := m.byID[sessionID]
|
|
return s, ok
|
|
}
|
|
|
|
func (m *memManager) kickSameDevice(incoming Session) {
|
|
userMap := m.byUser[incoming.UserID()]
|
|
if userMap == nil {
|
|
return
|
|
}
|
|
for _, existing := range userMap {
|
|
if existing.DeviceID() != incoming.DeviceID() || existing.ID() == incoming.ID() {
|
|
continue
|
|
}
|
|
reason := i18n.T(i18n.Parse(existing.Language()), i18n.NotifyKickReplaced, nil)
|
|
_ = existing.Send(&Packet{
|
|
Command: protocol.CmdKick,
|
|
Body: protocol.EncodeKick(existing.RequestID(), reason),
|
|
})
|
|
_ = existing.Close()
|
|
delete(m.byID, existing.ID())
|
|
delete(userMap, existing.ID())
|
|
}
|
|
}
|
|
|
|
func (m *memManager) KickDevice(userID int64, deviceID uint64) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
userMap := m.byUser[userID]
|
|
if userMap == nil {
|
|
return
|
|
}
|
|
for id, existing := range userMap {
|
|
if existing.DeviceID() != deviceID {
|
|
continue
|
|
}
|
|
reason := i18n.T(i18n.Parse(existing.Language()), i18n.NotifyKickLogout, nil)
|
|
_ = existing.Send(&Packet{
|
|
Command: protocol.CmdKick,
|
|
Body: protocol.EncodeKick(existing.RequestID(), reason),
|
|
})
|
|
_ = existing.Close()
|
|
delete(m.byID, id)
|
|
delete(userMap, id)
|
|
}
|
|
}
|