From 62679b1288072670235025a46ffa4ad3622cbac4 Mon Sep 17 00:00:00 2001 From: alsm Date: Mon, 28 Jan 2019 13:20:19 +0000 Subject: [PATCH] Struct alignment --- packets/auth.go | 2 +- packets/connack.go | 4 +- packets/connect.go | 22 ++++---- packets/disconnect.go | 2 +- packets/packets.go | 10 ++-- packets/properties.go | 56 +++++++++---------- packets/puback.go | 2 +- packets/pubcomp.go | 2 +- packets/publish.go | 10 ++-- packets/pubrec.go | 2 +- packets/pubrel.go | 2 +- packets/suback.go | 2 +- packets/subscribe.go | 4 +- packets/unsuback.go | 4 +- packets/unsubscribe.go | 4 +- paho/client.go | 118 ++++++++++++++++++++--------------------- paho/cp_auth.go | 8 +-- paho/cp_connack.go | 22 ++++---- paho/cp_connect.go | 14 ++--- paho/cp_disconnect.go | 4 +- paho/cp_publish.go | 8 +-- paho/cp_pubresp.go | 2 +- paho/cp_suback.go | 2 +- paho/cp_subscribe.go | 4 +- paho/message_ids.go | 2 +- paho/pinger.go | 12 ++--- paho/router.go | 10 +--- 27 files changed, 164 insertions(+), 170 deletions(-) diff --git a/packets/auth.go b/packets/auth.go index 2f6f404..827313c 100644 --- a/packets/auth.go +++ b/packets/auth.go @@ -8,8 +8,8 @@ import ( // Auth is the Variable Header definition for a Auth control packet type Auth struct { - ReasonCode byte Properties *Properties + ReasonCode byte } // Unpack is the implementation of the interface required function for a packet diff --git a/packets/connack.go b/packets/connack.go index 8851d08..4eb30c5 100644 --- a/packets/connack.go +++ b/packets/connack.go @@ -8,9 +8,9 @@ import ( // Connack is the Variable Header definition for a connack control packet type Connack struct { - SessionPresent bool - ReasonCode byte Properties *Properties + ReasonCode byte + SessionPresent bool } //Unpack is the implementation of the interface required function for a packet diff --git a/packets/connect.go b/packets/connect.go index 0dd2a22..06bc010 100644 --- a/packets/connect.go +++ b/packets/connect.go @@ -8,22 +8,22 @@ import ( // Connect is the Variable Header definition for a connect control packet type Connect struct { - PasswordFlag bool - UsernameFlag bool - ProtocolName string - ProtocolVersion byte - WillTopic string - WillRetain bool - WillQOS byte - WillFlag bool WillMessage []byte - CleanStart bool - Username string Password []byte - KeepAlive uint16 + Username string + ProtocolName string ClientID string + WillTopic string Properties *Properties WillProperties *Properties + KeepAlive uint16 + ProtocolVersion byte + WillQOS byte + PasswordFlag bool + UsernameFlag bool + WillRetain bool + WillFlag bool + CleanStart bool } // PackFlags takes the Connect flags and packs them into the single byte diff --git a/packets/disconnect.go b/packets/disconnect.go index b019571..203a3bc 100644 --- a/packets/disconnect.go +++ b/packets/disconnect.go @@ -8,8 +8,8 @@ import ( // Disconnect is the Variable Header definition for a Disconnect control packet type Disconnect struct { - ReasonCode byte Properties *Properties + ReasonCode byte } // DisconnectNormalDisconnection, etc are the list of valid disconnection reason codes. diff --git a/packets/packets.go b/packets/packets.go index 78ae534..bb4fda6 100644 --- a/packets/packets.go +++ b/packets/packets.go @@ -42,15 +42,15 @@ type ( // FixedHeader is the definition of a control packet fixed header FixedHeader struct { - Flags byte - Type PacketType remainingLength int + Type PacketType + Flags byte } // ControlPacket is the definition of a control packet ControlPacket struct { - FixedHeader Content Packet + FixedHeader } ) @@ -154,7 +154,7 @@ func ReadPacket(r io.Reader) (*ControlPacket, error) { } cp := NewControlPacket(PacketType(t[0] >> 4)) if cp == nil { - return nil, fmt.Errorf("Invalid packet type requested, %d", t[0]>>4) + return nil, fmt.Errorf("invalid packet type requested, %d", t[0]>>4) } cp.Flags = t[0] & 0xF if cp.Type == PUBLISH { @@ -177,7 +177,7 @@ func ReadPacket(r io.Reader) (*ControlPacket, error) { return nil, err } if n != int64(cp.remainingLength) { - return nil, fmt.Errorf("Failed to read packet, expected %d bytes, read %d", cp.remainingLength, n) + return nil, fmt.Errorf("failed to read packet, expected %d bytes, read %d", cp.remainingLength, n) } err = cp.Content.Unpack(&content) if err != nil { diff --git a/packets/properties.go b/packets/properties.go index 7dfaa6f..01d2e1d 100644 --- a/packets/properties.go +++ b/packets/properties.go @@ -10,32 +10,32 @@ import ( // MQTT packet properties const ( PropPayloadFormat byte = 1 - PropMessageExpiry = 2 - PropContentType = 3 - PropResponseTopic = 8 - PropCorrelationData = 9 - PropSubscriptionIdentifier = 11 - PropSessionExpiryInterval = 17 - PropAssignedClientID = 18 - PropServerKeepAlive = 19 - PropAuthMethod = 21 - PropAuthData = 22 - PropRequestProblemInfo = 23 - PropWillDelayInterval = 24 - PropRequestResponseInfo = 25 - PropResponseInfo = 26 - PropServerReference = 28 - PropReasonString = 31 - PropReceiveMaximum = 33 - PropTopicAliasMaximum = 34 - PropTopicAlias = 35 - PropMaximumQOS = 36 - PropRetainAvailable = 37 - PropUser = 38 - PropMaximumPacketSize = 39 - PropWildcardSubAvailable = 40 - PropSubIDAvailable = 41 - PropSharedSubAvailable = 42 + PropMessageExpiry byte = 2 + PropContentType byte = 3 + PropResponseTopic byte = 8 + PropCorrelationData byte = 9 + PropSubscriptionIdentifier byte = 11 + PropSessionExpiryInterval byte = 17 + PropAssignedClientID byte = 18 + PropServerKeepAlive byte = 19 + PropAuthMethod byte = 21 + PropAuthData byte = 22 + PropRequestProblemInfo byte = 23 + PropWillDelayInterval byte = 24 + PropRequestResponseInfo byte = 25 + PropResponseInfo byte = 26 + PropServerReference byte = 28 + PropReasonString byte = 31 + PropReceiveMaximum byte = 33 + PropTopicAliasMaximum byte = 34 + PropTopicAlias byte = 35 + PropMaximumQOS byte = 36 + PropRetainAvailable byte = 37 + PropUser byte = 38 + PropMaximumPacketSize byte = 39 + PropWildcardSubAvailable byte = 40 + PropSubIDAvailable byte = 41 + PropSharedSubAvailable byte = 42 ) // Properties is a struct representing the all the described properties @@ -323,7 +323,7 @@ func (i *Properties) Unpack(r *bytes.Buffer, p PacketType) error { break } if !ValidateID(p, PropType) { - return fmt.Errorf("Invalid Prop type %d for packet %d", PropType, p) + return fmt.Errorf("invalid Prop type %d for packet %d", PropType, p) } switch PropType { case PropPayloadFormat: @@ -493,7 +493,7 @@ func (i *Properties) Unpack(r *bytes.Buffer, p PacketType) error { } i.SharedSubAvailable = &ss default: - return fmt.Errorf("Unknown Prop type %d", PropType) + return fmt.Errorf("unknown Prop type %d", PropType) } } diff --git a/packets/puback.go b/packets/puback.go index 53f671a..bbd6aa4 100644 --- a/packets/puback.go +++ b/packets/puback.go @@ -8,9 +8,9 @@ import ( // Puback is the Variable Header definition for a Puback control packet type Puback struct { + Properties *Properties PacketID uint16 ReasonCode byte - Properties *Properties } // PubackSuccess, etc are the list of valid puback reason codes. diff --git a/packets/pubcomp.go b/packets/pubcomp.go index ef4340a..4d9ae09 100644 --- a/packets/pubcomp.go +++ b/packets/pubcomp.go @@ -8,9 +8,9 @@ import ( // Pubcomp is the Variable Header definition for a Pubcomp control packet type Pubcomp struct { + Properties *Properties PacketID uint16 ReasonCode byte - Properties *Properties } // PubcompSuccess, etc are the list of valid pubcomp reason codes. diff --git a/packets/publish.go b/packets/publish.go index 4851678..ef2d58c 100644 --- a/packets/publish.go +++ b/packets/publish.go @@ -9,13 +9,13 @@ import ( // Publish is the Variable Header definition for a publish control packet type Publish struct { - Duplicate bool - QoS byte - Retain bool + Payload []byte Topic string - PacketID uint16 Properties *Properties - Payload []byte + PacketID uint16 + QoS byte + Duplicate bool + Retain bool } //Unpack is the implementation of the interface required function for a packet diff --git a/packets/pubrec.go b/packets/pubrec.go index 9228b45..fcaa81b 100644 --- a/packets/pubrec.go +++ b/packets/pubrec.go @@ -8,9 +8,9 @@ import ( // Pubrec is the Variable Header definition for a Pubrec control packet type Pubrec struct { + Properties *Properties PacketID uint16 ReasonCode byte - Properties *Properties } // PubrecSuccess, etc are the list of valid Pubrec reason codes diff --git a/packets/pubrel.go b/packets/pubrel.go index d0d662a..11016ba 100644 --- a/packets/pubrel.go +++ b/packets/pubrel.go @@ -8,9 +8,9 @@ import ( // Pubrel is the Variable Header definition for a Pubrel control packet type Pubrel struct { + Properties *Properties PacketID uint16 ReasonCode byte - Properties *Properties } //Unpack is the implementation of the interface required function for a packet diff --git a/packets/suback.go b/packets/suback.go index 56dea5a..21ef499 100644 --- a/packets/suback.go +++ b/packets/suback.go @@ -8,9 +8,9 @@ import ( // Suback is the Variable Header definition for a Suback control packet type Suback struct { - PacketID uint16 Properties *Properties Reasons []byte + PacketID uint16 } // SubackGrantedQoS0, etc are the list of valid suback reason codes. diff --git a/packets/subscribe.go b/packets/subscribe.go index b9ab11c..992b85e 100644 --- a/packets/subscribe.go +++ b/packets/subscribe.go @@ -8,17 +8,17 @@ import ( // Subscribe is the Variable Header definition for a Subscribe control packet type Subscribe struct { - PacketID uint16 Properties *Properties Subscriptions map[string]SubOptions + PacketID uint16 } // SubOptions is the struct representing the options for a subscription type SubOptions struct { QoS byte + RetainHandling byte NoLocal bool RetainAsPublished bool - RetainHandling byte } // Pack is the implementation of the interface required function for a packet diff --git a/packets/unsuback.go b/packets/unsuback.go index 7d044bb..9001dd1 100644 --- a/packets/unsuback.go +++ b/packets/unsuback.go @@ -8,9 +8,9 @@ import ( // Unsuback is the Variable Header definition for a Unsuback control packet type Unsuback struct { - PacketID uint16 - Properties *Properties Reasons []byte + Properties *Properties + PacketID uint16 } // UnsubackSuccess, etc are the list of valid unsuback reason codes. diff --git a/packets/unsubscribe.go b/packets/unsubscribe.go index fbac62b..471203e 100644 --- a/packets/unsubscribe.go +++ b/packets/unsubscribe.go @@ -8,9 +8,9 @@ import ( // Unsubscribe is the Variable Header definition for a Unsubscribe control packet type Unsubscribe struct { - PacketID uint16 - Properties *Properties Topics []string + Properties *Properties + PacketID uint16 } // Unpack is the implementation of the interface required function for a packet diff --git a/paho/client.go b/paho/client.go index a496ba1..ad1d747 100644 --- a/paho/client.go +++ b/paho/client.go @@ -43,10 +43,10 @@ type ( // be set by the server in the Connack and that the client needs to be // aware of for future subscribes/publishes CommsProperties struct { - ReceiveMaximum uint16 - MaximumQoS byte MaximumPacketSize uint32 + ReceiveMaximum uint16 TopicAliasMaximum uint16 + MaximumQoS byte RetainAvailable bool WildcardSubAvailable bool SubIDAvailable bool @@ -54,8 +54,8 @@ type ( } caContext struct { - Return chan *packets.Connack Context context.Context + Return chan *packets.Connack } ) @@ -110,7 +110,7 @@ func NewClient() *Client { // along with an error indicating the reason for the failure to connect. func (c *Client) Connect(ctx context.Context, cp *Connect) (*Connack, error) { if c.Conn == nil { - return nil, fmt.Errorf("Client connection is nil") + return nil, fmt.Errorf("client connection is nil") } debug.Println("Connecting") @@ -143,7 +143,7 @@ func (c *Client) Connect(ctx context.Context, cp *Connect) (*Connack, error) { connCtx, cf := context.WithTimeout(ctx, c.PacketTimeout) defer cf() - c.caCtx = &caContext{make(chan *packets.Connack, 1), connCtx} + c.caCtx = &caContext{connCtx, make(chan *packets.Connack, 1)} defer func() { c.caCtx = nil }() @@ -153,17 +153,17 @@ func (c *Client) Connect(ctx context.Context, cp *Connect) (*Connack, error) { ccp.ProtocolName = "MQTT" ccp.ProtocolVersion = 5 - debug.Println("Sending CONNECT") + debug.Println("sending CONNECT") if _, err := ccp.WriteTo(c.Conn); err != nil { return nil, err } - debug.Println("Waiting for CONNACK") + debug.Println("waiting for CONNACK") var cap *packets.Connack select { case <-connCtx.Done(): if e := connCtx.Err(); e == context.DeadlineExceeded { - debug.Println("Timeout waiting for CONNACK") + debug.Println("timeout waiting for CONNACK") return nil, e } case cap = <-c.caCtx.Return: @@ -173,11 +173,11 @@ func (c *Client) Connect(ctx context.Context, cp *Connect) (*Connack, error) { if ca.ReasonCode >= 0x80 { var reason string - debug.Println("Received an error code in Connack:", ca.ReasonCode) + debug.Println("received an error code in Connack:", ca.ReasonCode) if ca.Properties != nil { reason = ca.Properties.ReasonString } - return ca, fmt.Errorf("Failed to connect to server: %s", reason) + return ca, fmt.Errorf("failed to connect to server: %s", reason) } if ca.Properties != nil { @@ -322,7 +322,7 @@ func (c *Client) Incoming() { if c.raCtx != nil { c.raCtx.Return <- *recv } - c.Error(fmt.Errorf("Received server initiated disconnect")) + c.Error(fmt.Errorf("received server initiated disconnect")) } } } @@ -352,16 +352,16 @@ func (c *Client) Error(e error) { // server until either a successful Auth packet is passed back, or a Disconnect // is received. func (c *Client) Authenticate(ctx context.Context, a *Auth) (*AuthResponse, error) { - debug.Println("Client initiated reauthentication") + debug.Println("client initiated reauthentication") c.Lock() defer c.Unlock() - c.raCtx = &CPContext{make(chan packets.ControlPacket, 1), ctx} + c.raCtx = &CPContext{ctx, make(chan packets.ControlPacket, 1)} defer func() { c.raCtx = nil }() - debug.Println("Sending AUTH") + debug.Println("sending AUTH") if _, err := a.Packet().WriteTo(c.Conn); err != nil { return nil, err } @@ -370,7 +370,7 @@ func (c *Client) Authenticate(ctx context.Context, a *Auth) (*AuthResponse, erro select { case <-ctx.Done(): if e := ctx.Err(); e == context.DeadlineExceeded { - debug.Println("Timeout waiting for Auth to complete") + debug.Println("timeout waiting for Auth to complete") return nil, e } case rp = <-c.raCtx.Return: @@ -385,7 +385,7 @@ func (c *Client) Authenticate(ctx context.Context, a *Auth) (*AuthResponse, erro return AuthResponseFromPacketDisconnect(rp.Content.(*packets.Disconnect)), nil } - return nil, fmt.Errorf("Error with Auth, didn't receive Auth or Disconnect") + return nil, fmt.Errorf("error with Auth, didn't receive Auth or Disconnect") } // Subscribe is used to send a Subscription request to the MQTT server. @@ -395,50 +395,50 @@ func (c *Client) Authenticate(ctx context.Context, a *Auth) (*AuthResponse, erro func (c *Client) Subscribe(ctx context.Context, s *Subscribe) (*Suback, error) { if !c.serverProps.WildcardSubAvailable { for t := range s.Subscriptions { - if strings.IndexAny(t, "#+") > -1 { + if strings.ContainsAny(t, "#+") { // Using a wildcard in a subscription when not supported - return nil, fmt.Errorf("Cannot subscribe to %s, server does not support wildcards", t) + return nil, fmt.Errorf("cannot subscribe to %s, server does not support wildcards", t) } } } if !c.serverProps.SubIDAvailable && s.Properties != nil && s.Properties.SubscriptionIdentifier != nil { - return nil, fmt.Errorf("Cannot send subscribe with subID set, server does not support subID") + return nil, fmt.Errorf("cannot send subscribe with subID set, server does not support subID") } if !c.serverProps.SharedSubAvailable { for t := range s.Subscriptions { if strings.HasPrefix(t, "$share") { - return nil, fmt.Errorf("Cannont subscribe to %s, server does not support shared subscriptions", t) + return nil, fmt.Errorf("cannont subscribe to %s, server does not support shared subscriptions", t) } } } - debug.Printf("Subscribing to %+v", s.Subscriptions) + debug.Printf("subscribing to %+v", s.Subscriptions) subCtx, cf := context.WithTimeout(ctx, c.PacketTimeout) defer cf() - cpCtx := &CPContext{make(chan packets.ControlPacket, 1), subCtx} + cpCtx := &CPContext{subCtx, make(chan packets.ControlPacket, 1)} sp := s.Packet() sp.PacketID = c.MIDs.Request(cpCtx) - debug.Println("Sending SUBSCRIBE") + debug.Println("sending SUBSCRIBE") if _, err := sp.WriteTo(c.Conn); err != nil { return nil, err } - debug.Println("Waiting for SUBACK") + debug.Println("waiting for SUBACK") var sap packets.ControlPacket select { case <-subCtx.Done(): if e := subCtx.Err(); e == context.DeadlineExceeded { - debug.Println("Timeout waiting for SUBACK") + debug.Println("timeout waiting for SUBACK") return nil, e } case sap = <-cpCtx.Return: } if sap.Type != packets.SUBACK { - return nil, fmt.Errorf("Received %d instead of Suback", sap.Type) + return nil, fmt.Errorf("received %d instead of Suback", sap.Type) } debug.Println("Received SUBACK") @@ -447,17 +447,17 @@ func (c *Client) Subscribe(ctx context.Context, s *Subscribe) (*Suback, error) { case len(sa.Reasons) == 1: if sa.Reasons[0] >= 0x80 { var reason string - debug.Println("Received an error code in Suback:", sa.Reasons[0]) + debug.Println("received an error code in Suback:", sa.Reasons[0]) if sa.Properties != nil { reason = sa.Properties.ReasonString } - return sa, fmt.Errorf("Failed to subscribe to topic: %s", reason) + return sa, fmt.Errorf("failed to subscribe to topic: %s", reason) } default: for _, code := range sa.Reasons { if code >= 0x80 { - debug.Println("Received an error code in Suback:", code) - return sa, fmt.Errorf("At least one requested subscription failed") + debug.Println("received an error code in Suback:", code) + return sa, fmt.Errorf("at least one requested subscription failed") } } } @@ -470,51 +470,51 @@ func (c *Client) Subscribe(ctx context.Context, s *Subscribe) (*Suback, error) { // a response Unsuback, or for the timeout to fire. Any response Unsuback // is returned from the function, along with any errors. func (c *Client) Unsubscribe(ctx context.Context, u *Unsubscribe) (*Unsuback, error) { - debug.Printf("Unsubscribing from %+v", u.Topics) + debug.Printf("unsubscribing from %+v", u.Topics) unsubCtx, cf := context.WithTimeout(ctx, c.PacketTimeout) defer cf() - cpCtx := &CPContext{make(chan packets.ControlPacket, 1), unsubCtx} + cpCtx := &CPContext{unsubCtx, make(chan packets.ControlPacket, 1)} up := u.Packet() up.PacketID = c.MIDs.Request(cpCtx) - debug.Println("Sending UNSUBSCRIBE") + debug.Println("sending UNSUBSCRIBE") if _, err := up.WriteTo(c.Conn); err != nil { return nil, err } - debug.Println("Waiting for UNSUBACK") + debug.Println("waiting for UNSUBACK") var uap packets.ControlPacket select { case <-unsubCtx.Done(): if e := unsubCtx.Err(); e == context.DeadlineExceeded { - debug.Println("Timeout waiting for UNSUBACK") + debug.Println("timeout waiting for UNSUBACK") return nil, e } case uap = <-cpCtx.Return: } if uap.Type != packets.UNSUBACK { - return nil, fmt.Errorf("Received %d instead of Unsuback", uap.Type) + return nil, fmt.Errorf("received %d instead of Unsuback", uap.Type) } - debug.Println("Received SUBACK") + debug.Println("received SUBACK") ua := UnsubackFromPacketUnsuback(uap.Content.(*packets.Unsuback)) switch { case len(ua.Reasons) == 1: if ua.Reasons[0] >= 0x80 { var reason string - debug.Println("Received an error code in Unsuback:", ua.Reasons[0]) + debug.Println("received an error code in Unsuback:", ua.Reasons[0]) if ua.Properties != nil { reason = ua.Properties.ReasonString } - return ua, fmt.Errorf("Failed to unsubscribe from topic: %s", reason) + return ua, fmt.Errorf("failed to unsubscribe from topic: %s", reason) } default: for _, code := range ua.Reasons { if code >= 0x80 { - debug.Println("Received an error code in Suback:", code) - return ua, fmt.Errorf("At least one requested unsubscribe failed") + debug.Println("received an error code in Suback:", code) + return ua, fmt.Errorf("at least one requested unsubscribe failed") } } } @@ -528,24 +528,24 @@ func (c *Client) Unsubscribe(ctx context.Context, u *Unsubscribe) (*Unsuback, er // Any response message is returned from the function, along with any errors. func (c *Client) Publish(ctx context.Context, p *Publish) (*PublishResponse, error) { if p.QoS > c.serverProps.MaximumQoS { - return nil, fmt.Errorf("Cannot send Publish with QoS %d, server maximum QoS is %d", p.QoS, c.serverProps.MaximumQoS) + return nil, fmt.Errorf("cannot send Publish with QoS %d, server maximum QoS is %d", p.QoS, c.serverProps.MaximumQoS) } if p.Properties != nil && p.Properties.TopicAlias != nil { if c.serverProps.TopicAliasMaximum > 0 && *p.Properties.TopicAlias > c.serverProps.TopicAliasMaximum { - return nil, fmt.Errorf("Cannot send publish with TopicAlias %d, server topic alias maximum is %d", *p.Properties.TopicAlias, c.serverProps.TopicAliasMaximum) + return nil, fmt.Errorf("cannot send publish with TopicAlias %d, server topic alias maximum is %d", *p.Properties.TopicAlias, c.serverProps.TopicAliasMaximum) } } if !c.serverProps.RetainAvailable && p.Retain { - return nil, fmt.Errorf("Cannot send Publish with retain flag set, server does not support retained messages") + return nil, fmt.Errorf("cannot send Publish with retain flag set, server does not support retained messages") } - debug.Printf("Sending message to %s", p.Topic) + debug.Printf("sending message to %s", p.Topic) pb := p.Packet() switch p.QoS { case 0: - debug.Println("Sending QoS0 message") + debug.Println("sending QoS0 message") if _, err := pb.WriteTo(c.Conn); err != nil { return nil, err } @@ -558,13 +558,13 @@ func (c *Client) Publish(ctx context.Context, p *Publish) (*PublishResponse, err } func (c *Client) publishQoS12(ctx context.Context, pb *packets.Publish) (*PublishResponse, error) { - debug.Println("Sending QoS12 message") + debug.Println("sending QoS12 message") pubCtx, cf := context.WithTimeout(ctx, c.PacketTimeout) defer cf() if err := c.serverInflight.Acquire(pubCtx, 1); err != nil { return nil, err } - cpCtx := &CPContext{make(chan packets.ControlPacket, 1), pubCtx} + cpCtx := &CPContext{pubCtx, make(chan packets.ControlPacket, 1)} pb.PacketID = c.MIDs.Request(cpCtx) if _, err := pb.WriteTo(c.Conn); err != nil { @@ -575,7 +575,7 @@ func (c *Client) publishQoS12(ctx context.Context, pb *packets.Publish) (*Publis select { case <-pubCtx.Done(): if e := pubCtx.Err(); e == context.DeadlineExceeded { - debug.Println("Timeout waiting for Publish response") + debug.Println("timeout waiting for Publish response") return nil, e } case resp = <-cpCtx.Return: @@ -584,36 +584,36 @@ func (c *Client) publishQoS12(ctx context.Context, pb *packets.Publish) (*Publis switch pb.QoS { case 1: if resp.Type != packets.PUBACK { - return nil, fmt.Errorf("Received %d instead of PUBACK", resp.Type) + return nil, fmt.Errorf("received %d instead of PUBACK", resp.Type) } - debug.Println("Received PUBACK for", pb.PacketID) + debug.Println("received PUBACK for", pb.PacketID) c.serverInflight.Release(1) pr := PublishResponseFromPuback(resp.Content.(*packets.Puback)) if pr.ReasonCode >= 0x80 { - debug.Println("Received an error code in Puback:", pr.ReasonCode) - return pr, fmt.Errorf("Error publishing: %s", resp.Content.(*packets.Puback).Reason()) + debug.Println("received an error code in Puback:", pr.ReasonCode) + return pr, fmt.Errorf("error publishing: %s", resp.Content.(*packets.Puback).Reason()) } return pr, nil case 2: switch resp.Type { case packets.PUBCOMP: - debug.Println("Received PUBCOMP for", pb.PacketID) + debug.Println("received PUBCOMP for", pb.PacketID) c.serverInflight.Release(1) pr := PublishResponseFromPubcomp(resp.Content.(*packets.Pubcomp)) return pr, nil case packets.PUBREC: - debug.Printf("Received PUBREC for %s (must have errored)", pb.PacketID) + debug.Printf("received PUBREC for %s (must have errored)", pb.PacketID) c.serverInflight.Release(1) pr := PublishResponseFromPubrec(resp.Content.(*packets.Pubrec)) return pr, nil default: - return nil, fmt.Errorf("Received %d instead of PUBCOMP", resp.Type) + return nil, fmt.Errorf("received %d instead of PUBCOMP", resp.Type) } } - debug.Println("Ended up with a non QoS1/2 message:", pb.QoS) - return nil, fmt.Errorf("Ended up with a non QoS1/2 message: %d", pb.QoS) + debug.Println("ended up with a non QoS1/2 message:", pb.QoS) + return nil, fmt.Errorf("ended up with a non QoS1/2 message: %d", pb.QoS) } // Disconnect is used to send a Disconnect packet to the MQTT server @@ -621,7 +621,7 @@ func (c *Client) publishQoS12(ctx context.Context, pb *packets.Publish) (*Publis // (and if it does this function returns any error) the network connection // is closed. func (c *Client) Disconnect(d *Disconnect) error { - debug.Println("Disconnecting") + debug.Println("disconnecting") c.Lock() defer c.Unlock() defer c.Conn.Close() diff --git a/paho/cp_auth.go b/paho/cp_auth.go index 3884a86..5ae86ff 100644 --- a/paho/cp_auth.go +++ b/paho/cp_auth.go @@ -5,15 +5,15 @@ import "github.com/eclipse/paho.golang/packets" type ( // Auth is a representation of the MQTT Auth packet Auth struct { - ReasonCode byte Properties *AuthProperties + ReasonCode byte } // AuthProperties is a struct of the properties that can be set // for a Auth packet AuthProperties struct { - AuthMethod string AuthData []byte + AuthMethod string ReasonString string User map[string]string } @@ -60,9 +60,9 @@ func (a *Auth) Packet() *packets.Auth { // AuthResponse is a represenation of the response to an Auth // packet type AuthResponse struct { - Success bool - ReasonCode byte Properties *AuthProperties + ReasonCode byte + Success bool } // AuthResponseFromPacketAuth takes a packets library Auth and diff --git a/paho/cp_connack.go b/paho/cp_connack.go index fb5b361..432b177 100644 --- a/paho/cp_connack.go +++ b/paho/cp_connack.go @@ -5,30 +5,30 @@ import "github.com/eclipse/paho.golang/packets" type ( // Connack is a representation of the MQTT Connack packet Connack struct { - SessionPresent bool - ReasonCode byte Properties *ConnackProperties + ReasonCode byte + SessionPresent bool } // ConnackProperties is a struct of the properties that can be set // for a Connack packet ConnackProperties struct { - AssignedClientID string - ServerKeepAlive *uint16 - WildcardSubAvailable bool - SubIDAvailable bool - SharedSubAvailable bool - RetainAvailable bool - ResponseInfo string - AuthMethod string AuthData []byte + AuthMethod string + ResponseInfo string ServerReference string ReasonString string + AssignedClientID string + MaximumPacketSize *uint32 ReceiveMaximum *uint16 TopicAliasMaximum *uint16 + ServerKeepAlive *uint16 MaximumQoS *byte - MaximumPacketSize *uint32 User map[string]string + WildcardSubAvailable bool + SubIDAvailable bool + SharedSubAvailable bool + RetainAvailable bool } ) diff --git a/paho/cp_connect.go b/paho/cp_connect.go index a8a13a8..58d88e2 100644 --- a/paho/cp_connect.go +++ b/paho/cp_connect.go @@ -5,14 +5,14 @@ import "github.com/eclipse/paho.golang/packets" type ( // Connect is a representation of the MQTT Connect packet Connect struct { - Username string Password []byte + Username string ClientID string - CleanStart bool - KeepAlive uint16 Properties *ConnectProperties WillMessage *WillMessage WillProperties *WillProperties + KeepAlive uint16 + CleanStart bool UsernameFlag bool PasswordFlag bool } @@ -20,17 +20,17 @@ type ( // ConnectProperties is a struct of the properties that can be set // for a Connect packet ConnectProperties struct { - SessionExpiryInterval *uint32 - AuthMethod string AuthData []byte + AuthMethod string + SessionExpiryInterval *uint32 WillDelayInterval *uint32 - RequestProblemInfo bool - RequestResponseInfo bool ReceiveMaximum *uint16 TopicAliasMaximum *uint16 MaximumQOS *byte MaximumPacketSize *uint32 User map[string]string + RequestProblemInfo bool + RequestResponseInfo bool } ) diff --git a/paho/cp_disconnect.go b/paho/cp_disconnect.go index 527bc68..74b3afa 100644 --- a/paho/cp_disconnect.go +++ b/paho/cp_disconnect.go @@ -5,16 +5,16 @@ import "github.com/eclipse/paho.golang/packets" type ( // Disconnect is a representation of the MQTT Disconnect packet Disconnect struct { - ReasonCode byte Properties *DisconnectProperties + ReasonCode byte } // DisconnectProperties is a struct of the properties that can be set // for a Disconnect packet DisconnectProperties struct { - SessionExpiryInterval *uint32 ServerReference string ReasonString string + SessionExpiryInterval *uint32 User map[string]string } ) diff --git a/paho/cp_publish.go b/paho/cp_publish.go index e6c4748..a964d52 100644 --- a/paho/cp_publish.go +++ b/paho/cp_publish.go @@ -20,13 +20,13 @@ type ( // PublishProperties is a struct of the properties that can be set // for a Publish packet PublishProperties struct { - PayloadFormat *byte - MessageExpiry *uint32 + CorrelationData []byte ContentType string ResponseTopic string - CorrelationData []byte - TopicAlias *uint16 + PayloadFormat *byte + MessageExpiry *uint32 SubscriptionIdentifier *uint32 + TopicAlias *uint16 User map[string]string } ) diff --git a/paho/cp_pubresp.go b/paho/cp_pubresp.go index 9499c0b..b49983c 100644 --- a/paho/cp_pubresp.go +++ b/paho/cp_pubresp.go @@ -6,8 +6,8 @@ type ( // PublishResponse is a generic representation of a response // to a QoS1 or QoS2 Publish PublishResponse struct { - ReasonCode byte Properties *PublishResponseProperties + ReasonCode byte } // PublishResponseProperties is the properties associated with diff --git a/paho/cp_suback.go b/paho/cp_suback.go index ed7ad74..7c88652 100644 --- a/paho/cp_suback.go +++ b/paho/cp_suback.go @@ -5,8 +5,8 @@ import "github.com/eclipse/paho.golang/packets" type ( // Suback is a representation of an MQTT suback packet Suback struct { - Reasons []byte Properties *SubackProperties + Reasons []byte } // SubackProperties is a struct of the properties that can be set diff --git a/paho/cp_subscribe.go b/paho/cp_subscribe.go index 29673ab..d067ce9 100644 --- a/paho/cp_subscribe.go +++ b/paho/cp_subscribe.go @@ -5,16 +5,16 @@ import "github.com/eclipse/paho.golang/packets" type ( // Subscribe is a representation of a MQTT subscribe packet Subscribe struct { - Subscriptions map[string]SubscribeOptions Properties *SubscribeProperties + Subscriptions map[string]SubscribeOptions } // SubscribeOptions is the struct representing the options for a subscription SubscribeOptions struct { QoS byte + RetainHandling byte NoLocal bool RetainAsPublished bool - RetainHandling byte } ) diff --git a/paho/message_ids.go b/paho/message_ids.go index 4e77c69..4c3d8c0 100644 --- a/paho/message_ids.go +++ b/paho/message_ids.go @@ -33,8 +33,8 @@ type MIDService interface { // The response packet is send down the Return channel and the // Context is used to track timeouts. type CPContext struct { - Return chan packets.ControlPacket Context context.Context + Return chan packets.ControlPacket } // MIDs is the default MIDService provided by this library. diff --git a/paho/pinger.go b/paho/pinger.go index f750c6e..4886b5b 100644 --- a/paho/pinger.go +++ b/paho/pinger.go @@ -32,11 +32,11 @@ type Pinger interface { // PingHandler is the library provided default Pinger type PingHandler struct { - stop chan struct{} - conn net.Conn lastPing time.Time - pingOutstanding int32 + conn net.Conn + stop chan struct{} pingFailHandler PingFailHandler + pingOutstanding int32 } // DefaultPingerWithCustomFailHandler returns an instance of the @@ -61,12 +61,12 @@ func (p *PingHandler) Start(c net.Conn, pt time.Duration) { debug.Println("pingHandler stopped") return case <-checkTicker.C: - if atomic.LoadInt32(&p.pingOutstanding) > 0 && time.Now().Sub(p.lastPing) > (pt+pt>>1) { - p.pingFailHandler(fmt.Errorf("Ping resp timed out")) + if atomic.LoadInt32(&p.pingOutstanding) > 0 && time.Since(p.lastPing) > (pt+pt>>1) { + p.pingFailHandler(fmt.Errorf("ping resp timed out")) //ping outstanding and not reset in 1.5 times ping timer return } - if time.Now().Sub(p.lastPing) >= pt { + if time.Since(p.lastPing) >= pt { //time to send a ping if _, err := packets.NewControlPacket(packets.PINGREQ).WriteTo(p.conn); err != nil { debug.Println("pingHandler sending ping request") diff --git a/paho/router.go b/paho/router.go index b8d305a..737ad2b 100644 --- a/paho/router.go +++ b/paho/router.go @@ -99,17 +99,11 @@ func match(route, topic string) bool { func matchDeep(route []string, topic []string) bool { if len(route) == 0 { - if len(topic) == 0 { - return true - } - return false + return len(topic) == 0 } if len(topic) == 0 { - if route[0] == "#" { - return true - } - return false + return route[0] == "#" } if route[0] == "#" {