From: Jeff Halter Date: Tue, 21 Jun 2022 21:03:31 +0000 (-0700) Subject: Refactor notifyOthers X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/commitdiff_plain/21581958c38861f3a62ef7c27d0a8a6f4db2a1f8?hp=0fcfa5d54b166559c0ca31932a71a7eabb79c72c Refactor notifyOthers --- diff --git a/hotline/client_conn.go b/hotline/client_conn.go index a3c5258..997b9c9 100644 --- a/hotline/client_conn.go +++ b/hotline/client_conn.go @@ -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 diff --git a/hotline/server.go b/hotline/server.go index a91e1c8..11ec21d 100644 --- a/hotline/server.go +++ b/hotline/server.go @@ -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) } } } diff --git a/hotline/transaction_handlers.go b/hotline/transaction_handlers.go index 5763967..cad3370 100644 --- a/hotline/transaction_handlers.go +++ b/hotline/transaction_handlers.go @@ -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))