]> git.r.bdr.sh - rbdr/mobius/commitdiff
Simplify user idle check
authorJeff Halter <redacted>
Fri, 27 May 2022 17:44:52 +0000 (10:44 -0700)
committerJeff Halter <redacted>
Fri, 27 May 2022 17:44:52 +0000 (10:44 -0700)
hotline/client_conn.go
hotline/client_conn_test.go
hotline/server.go
hotline/transaction_handlers.go

index 8e9a6147c5e3d864d8b2a26378dcdd096ffc7b14..b43a6ff65a371dcca85a169404ae98b5346f5b72 100644 (file)
@@ -31,7 +31,7 @@ type ClientConn struct {
        Flags      *[]byte
        UserName   []byte
        Account    *Account
        Flags      *[]byte
        UserName   []byte
        Account    *Account
-       IdleTime   *int
+       IdleTime   int
        Server     *Server
        Version    *[]byte
        Idle       bool
        Server     *Server
        Version    *[]byte
        Idle       bool
@@ -103,28 +103,28 @@ func (cc *ClientConn) handleTransaction(transaction *Transaction) error {
        cc.Server.mux.Lock()
        defer cc.Server.mux.Unlock()
 
        cc.Server.mux.Lock()
        defer cc.Server.mux.Unlock()
 
-       // if user was idle and this is a non-keepalive transaction
-       if *cc.IdleTime > userIdleSeconds && requestNum != tranKeepAlive {
-               flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*cc.Flags)))
-               flagBitmap.SetBit(flagBitmap, userFlagAway, 0)
-               binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
-               cc.Idle = false
-               //*cc.IdleTime = 0
-
-               cc.sendAll(
-                       tranNotifyChangeUser,
-                       NewField(fieldUserID, *cc.ID),
-                       NewField(fieldUserFlags, *cc.Flags),
-                       NewField(fieldUserName, cc.UserName),
-                       NewField(fieldUserIconID, *cc.Icon),
-               )
-
-               //return nil
+       if requestNum != tranKeepAlive {
+               // reset the user idle timer
+               cc.IdleTime = 0
+
+               // if user was previously idle, mark as not idle and notify other connected clients that
+               // the user is no longer away
+               if cc.Idle {
+                       flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*cc.Flags)))
+                       flagBitmap.SetBit(flagBitmap, userFlagAway, 0)
+                       binary.BigEndian.PutUint16(*cc.Flags, uint16(flagBitmap.Int64()))
+                       cc.Idle = false
+
+                       cc.sendAll(
+                               tranNotifyChangeUser,
+                               NewField(fieldUserID, *cc.ID),
+                               NewField(fieldUserFlags, *cc.Flags),
+                               NewField(fieldUserName, cc.UserName),
+                               NewField(fieldUserIconID, *cc.Icon),
+                       )
+               }
        }
 
        }
 
-       // TODO: Don't we need to skip this if requestNum == tranKeepalive ??
-       *cc.IdleTime = 0
-
        return nil
 }
 
        return nil
 }
 
index 6e3464f30f0bca55803a74b8d60cbedbb4bc61c0..2176185876a6a4a10f475df754e31a1b27ed6aaa 100644 (file)
@@ -13,7 +13,7 @@ func TestClientConn_handleTransaction(t *testing.T) {
                Flags      *[]byte
                UserName   []byte
                Account    *Account
                Flags      *[]byte
                UserName   []byte
                Account    *Account
-               IdleTime   *int
+               IdleTime   int
                Server     *Server
                Version    *[]byte
                Idle       bool
                Server     *Server
                Version    *[]byte
                Idle       bool
@@ -39,7 +39,6 @@ func TestClientConn_handleTransaction(t *testing.T) {
                                Flags:      tt.fields.Flags,
                                UserName:   tt.fields.UserName,
                                Account:    tt.fields.Account,
                                Flags:      tt.fields.Flags,
                                UserName:   tt.fields.UserName,
                                Account:    tt.fields.Account,
-                               IdleTime:   tt.fields.IdleTime,
                                Server:     tt.fields.Server,
                                Version:    tt.fields.Version,
                                Idle:       tt.fields.Idle,
                                Server:     tt.fields.Server,
                                Version:    tt.fields.Version,
                                Idle:       tt.fields.Idle,
index 61cd7fa0a7e82fae0fb4f63df7b534695c23efc7..c5a29ba9ea19173b3499be0ab3e1ade4d7e49f95 100644 (file)
@@ -278,8 +278,8 @@ func (s *Server) keepaliveHandler() {
                s.mux.Lock()
 
                for _, c := range s.Clients {
                s.mux.Lock()
 
                for _, c := range s.Clients {
-                       *c.IdleTime += idleCheckInterval
-                       if *c.IdleTime > userIdleSeconds && !c.Idle {
+                       c.IdleTime += idleCheckInterval
+                       if c.IdleTime > userIdleSeconds && !c.Idle {
                                c.Idle = true
 
                                flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*c.Flags)))
                                c.Idle = true
 
                                flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*c.Flags)))
@@ -327,7 +327,6 @@ func (s *Server) NewClientConn(conn net.Conn) *ClientConn {
                Connection: conn,
                Server:     s,
                Version:    &[]byte{},
                Connection: conn,
                Server:     s,
                Version:    &[]byte{},
-               IdleTime:   new(int),
                AutoReply:  &[]byte{},
                Transfers:  make(map[int][]*FileTransfer),
                Agreed:     false,
                AutoReply:  &[]byte{},
                Transfers:  make(map[int][]*FileTransfer),
                Agreed:     false,
@@ -335,8 +334,6 @@ func (s *Server) NewClientConn(conn net.Conn) *ClientConn {
        *s.NextGuestID++
        ID := *s.NextGuestID
 
        *s.NextGuestID++
        ID := *s.NextGuestID
 
-       *clientConn.IdleTime = 0
-
        binary.BigEndian.PutUint16(*clientConn.ID, ID)
        s.Clients[ID] = clientConn
 
        binary.BigEndian.PutUint16(*clientConn.ID, ID)
        s.Clients[ID] = clientConn
 
index ade024c4a3803c397e293c1c8606fa270311ad34..5aa6984a727c8be82df7193c876adf6c83e2881c 100644 (file)
@@ -1399,9 +1399,9 @@ func HandleSetClientUserInfo(cc *ClientConn, t *Transaction) (res []Transaction,
        return res, err
 }
 
        return res, err
 }
 
-// HandleKeepAlive response to keepalive transactions with an empty reply
-// HL 1.9.2 Client sends keepalive msg every 3 minutes
-// HL 1.2.3 Client doesn't send keepalives
+// HandleKeepAlive responds to keepalive transactions with an empty reply
+// HL 1.9.2 Client sends keepalive msg every 3 minutes
+// HL 1.2.3 Client doesn't send keepalives
 func HandleKeepAlive(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
        res = append(res, cc.NewReply(t))
 
 func HandleKeepAlive(cc *ClientConn, t *Transaction) (res []Transaction, err error) {
        res = append(res, cc.NewReply(t))