]> git.r.bdr.sh - rbdr/mobius/commitdiff
Refactor notifyOthers
authorJeff Halter <redacted>
Tue, 21 Jun 2022 21:03:31 +0000 (14:03 -0700)
committerJeff Halter <redacted>
Tue, 21 Jun 2022 21:03:31 +0000 (14:03 -0700)
hotline/client_conn.go
hotline/server.go
hotline/transaction_handlers.go

index a3c5258c48b9963ea292c1c9fd10a719ad3c2f31..997b9c921a50966cd368f27b3cf48739cc41c16a 100644 (file)
@@ -172,7 +172,9 @@ func (cc *ClientConn) Disconnect() {
 
        delete(cc.Server.Clients, binary.BigEndian.Uint16(*cc.ID))
 
-       cc.notifyOthers(*NewTransaction(tranNotifyDeleteUser, nil, NewField(fieldUserID, *cc.ID)))
+       for _, t := range cc.notifyOthers(*NewTransaction(tranNotifyDeleteUser, nil, NewField(fieldUserID, *cc.ID))) {
+               cc.Server.outbox <- t
+       }
 
        if err := cc.Connection.Close(); err != nil {
                cc.Server.Logger.Errorw("error closing client connection", "RemoteAddr", cc.RemoteAddr)
@@ -180,13 +182,14 @@ func (cc *ClientConn) Disconnect() {
 }
 
 // notifyOthers sends transaction t to other clients connected to the server
-func (cc *ClientConn) notifyOthers(t Transaction) {
+func (cc *ClientConn) notifyOthers(t Transaction) (trans []Transaction) {
        for _, c := range sortedClients(cc.Server.Clients) {
                if c.ID != cc.ID && c.Agreed {
                        t.clientID = c.ID
-                       cc.Server.outbox <- t
+                       trans = append(trans, t)
                }
        }
+       return trans
 }
 
 // NewReply returns a reply Transaction with fields for the ClientConn
index a91e1c817b5f0b2656d0c22704f8544704626eb3..11ec21d1b9625501cb72e6d9e2d9b5458fe813aa 100644 (file)
@@ -521,7 +521,7 @@ func dontPanic(logger *zap.SugaredLogger) {
 }
 
 // handleNewConnection takes a new net.Conn and performs the initial login sequence
-func (s *Server) handleNewConnection(ctx context.Context, conn net.Conn, remoteAddr string) error {
+func (s *Server) handleNewConnection(ctx context.Context, conn io.ReadWriteCloser, remoteAddr string) error {
        defer dontPanic(s.Logger)
 
        if err := Handshake(conn); err != nil {
@@ -604,10 +604,9 @@ func (s *Server) handleNewConnection(ctx context.Context, conn net.Conn, remoteA
        // Used simplified hotline v1.2.3 login flow for clients that do not send login info in tranAgreed
        if *c.Version == nil || bytes.Equal(*c.Version, nostalgiaVersion) {
                c.Agreed = true
-
                c.logger = c.logger.With("name", string(c.UserName))
 
-               c.notifyOthers(
+               for _, t := range c.notifyOthers(
                        *NewTransaction(
                                tranNotifyChangeUser, nil,
                                NewField(fieldUserName, c.UserName),
@@ -615,7 +614,9 @@ func (s *Server) handleNewConnection(ctx context.Context, conn net.Conn, remoteA
                                NewField(fieldUserIconID, *c.Icon),
                                NewField(fieldUserFlags, *c.Flags),
                        ),
-               )
+               ) {
+                       c.Server.outbox <- t
+               }
        }
 
        c.Server.Stats.LoginCount += 1
@@ -644,7 +645,7 @@ func (s *Server) handleNewConnection(ctx context.Context, conn net.Conn, remoteA
                // iterate over all the transactions that were parsed from the byte slice and handle them
                for _, t := range transactions {
                        if err := c.handleTransaction(&t); err != nil {
-                               c.Server.Logger.Errorw("Error handling transaction", "err", err)
+                               c.logger.Errorw("Error handling transaction", "err", err)
                        }
                }
        }
index 57639674c445695cfd6c4b9f0ab83a73c78c9c99..cad33700d26d52730342534d01170da9c98b342a 100644 (file)
@@ -964,7 +964,7 @@ func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err er
                cc.AutoReply = []byte{}
        }
 
-       cc.notifyOthers(
+       for _, t := range cc.notifyOthers(
                *NewTransaction(
                        tranNotifyChangeUser, nil,
                        NewField(fieldUserName, cc.UserName),
@@ -972,7 +972,9 @@ func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err er
                        NewField(fieldUserIconID, *cc.Icon),
                        NewField(fieldUserFlags, *cc.Flags),
                ),
-       )
+       ) {
+               cc.Server.outbox <- t
+       }
 
        res = append(res, cc.NewReply(t))