mirror of
https://github.com/mochi-mqtt/server.git
synced 2025-10-17 13:40:51 +08:00
8bit align struct fields
This commit is contained in:
@@ -15,27 +15,25 @@ var (
|
||||
ErrInsufficientBytes = errors.New("Insufficient bytes to return")
|
||||
)
|
||||
|
||||
// buffer contains core values and methods to be included in a reader or writer.
|
||||
type Buffer struct {
|
||||
Mu sync.RWMutex // the buffer needs it's own mutex to work properly.
|
||||
ID string // the identifier of the buffer. This is used in debug output.
|
||||
size int // the size of the buffer.
|
||||
mask int // a bitmask of the buffer size (size-1).
|
||||
block int // the size of the R/W block.
|
||||
buf []byte // the bytes buffer.
|
||||
tmp []byte // a temporary buffer.
|
||||
_ int32 // align the next fields to an 8-byte boundary for atomic access.
|
||||
Mu sync.RWMutex // the buffer needs its own mutex to work properly.
|
||||
ID string // the identifier of the buffer. This is used in debug output.
|
||||
head int64 // the current position in the sequence - a forever increasing index.
|
||||
tail int64 // the committed position in the sequence - a forever increasing index.
|
||||
rcond *sync.Cond // the sync condition for the buffer reader.
|
||||
wcond *sync.Cond // the sync condition for the buffer writer.
|
||||
size int // the size of the buffer.
|
||||
mask int // a bitmask of the buffer size (size-1).
|
||||
block int // the size of the R/W block.
|
||||
done uint32 // indicates that the buffer is closed.
|
||||
State uint32 // indicates whether the buffer is reading from (1) or writing to (2).
|
||||
}
|
||||
|
||||
// NewBuffer returns a new instance of buffer. You should call NewReader or
|
||||
// NewWriter instead of this function.
|
||||
func NewBuffer(size, block int) Buffer {
|
||||
func NewBuffer(size, block int) *Buffer {
|
||||
if size == 0 {
|
||||
size = DefaultBufferSize
|
||||
}
|
||||
|
@@ -84,30 +84,30 @@ func (cl *Clients) GetByListener(id string) []*Client {
|
||||
|
||||
// Client contains information about a client known by the broker.
|
||||
type Client struct {
|
||||
sync.RWMutex
|
||||
State State // the operational state of the client.
|
||||
LWT LWT // the last will and testament for the client.
|
||||
Inflight *Inflight // a map of in-flight qos messages.
|
||||
sync.RWMutex // mutex
|
||||
Username []byte // the username the client authenticated with.
|
||||
AC auth.Controller // an auth controller inherited from the listener.
|
||||
Listener string // the id of the listener the client is connected to.
|
||||
ID string // the client id.
|
||||
conn net.Conn // the net.Conn used to establish the connection.
|
||||
r *circ.Reader // a reader for reading incoming bytes.
|
||||
w *circ.Writer // a writer for writing outgoing bytes.
|
||||
ID string // the client id.
|
||||
AC auth.Controller // an auth controller inherited from the listener.
|
||||
Subscriptions topics.Subscriptions // a map of the subscription filters a client maintains.
|
||||
Listener string // the id of the listener the client is connected to.
|
||||
Inflight Inflight // a map of in-flight qos messages.
|
||||
Username []byte // the username the client authenticated with.
|
||||
systemInfo *system.Info // pointers to server system info.
|
||||
packetID uint32 // the current highest packetID.
|
||||
keepalive uint16 // the number of seconds the connection can wait.
|
||||
cleanSession bool // indicates if the client expects a clean-session.
|
||||
packetID uint32 // the current highest packetID.
|
||||
LWT LWT // the last will and testament for the client.
|
||||
State State // the operational state of the client.
|
||||
systemInfo *system.Info // pointers to server system info.
|
||||
}
|
||||
|
||||
// State tracks the state of the client.
|
||||
type State struct {
|
||||
Done uint32 // atomic counter which indicates that the client has closed.
|
||||
started *sync.WaitGroup // tracks the goroutines which have been started.
|
||||
endedW *sync.WaitGroup // tracks when the writer has ended.
|
||||
endedR *sync.WaitGroup // tracks when the reader has ended.
|
||||
Done uint32 // atomic counter which indicates that the client has closed.
|
||||
endOnce sync.Once // only end once.
|
||||
}
|
||||
|
||||
@@ -454,8 +454,8 @@ func (cl *Client) WritePacket(pk packets.Packet) (n int, err error) {
|
||||
|
||||
// LWT contains the last will and testament details for a client connection.
|
||||
type LWT struct {
|
||||
Topic string // the topic the will message shall be sent to.
|
||||
Message []byte // the message that shall be sent when the client disconnects.
|
||||
Topic string // the topic the will message shall be sent to.
|
||||
Qos byte // the quality of service desired.
|
||||
Retain bool // indicates whether the will message should be retained
|
||||
}
|
||||
|
@@ -6,11 +6,11 @@ import (
|
||||
|
||||
// FixedHeader contains the values of the fixed header portion of the MQTT packet.
|
||||
type FixedHeader struct {
|
||||
Type byte // the type of the packet (PUBLISH, SUBSCRIBE, etc) from bits 7 - 4 (byte 1).
|
||||
Dup bool // indicates if the packet was already sent at an earlier time.
|
||||
Qos byte // indicates the quality of service expected.
|
||||
Retain bool // whether the message should be retained.
|
||||
Remaining int // the number of remaining bytes in the payload.
|
||||
Type byte // the type of the packet (PUBLISH, SUBSCRIBE, etc) from bits 7 - 4 (byte 1).
|
||||
Qos byte // indicates the quality of service expected.
|
||||
Dup bool // indicates if the packet was already sent at an earlier time.
|
||||
Retain bool // whether the message should be retained.
|
||||
}
|
||||
|
||||
// Encode encodes the FixedHeader and returns a bytes buffer.
|
||||
|
@@ -76,44 +76,31 @@ var (
|
||||
// packet structs, this is a single concrete packet type to cover all packet
|
||||
// types, which allows us to take advantage of various compiler optimizations.
|
||||
type Packet struct {
|
||||
FixedHeader FixedHeader
|
||||
|
||||
PacketID uint16
|
||||
|
||||
// Connect
|
||||
FixedHeader FixedHeader
|
||||
AllowClients []string // For use with OnMessage event hook.
|
||||
Topics []string
|
||||
ReturnCodes []byte
|
||||
ProtocolName []byte
|
||||
Qoss []byte
|
||||
Payload []byte
|
||||
Username []byte
|
||||
Password []byte
|
||||
WillMessage []byte
|
||||
ClientIdentifier string
|
||||
TopicName string
|
||||
WillTopic string
|
||||
PacketID uint16
|
||||
Keepalive uint16
|
||||
ReturnCode byte
|
||||
ProtocolVersion byte
|
||||
WillQos byte
|
||||
ReservedBit byte
|
||||
CleanSession bool
|
||||
WillFlag bool
|
||||
WillQos byte
|
||||
WillRetain bool
|
||||
UsernameFlag bool
|
||||
PasswordFlag bool
|
||||
ReservedBit byte
|
||||
Keepalive uint16
|
||||
ClientIdentifier string
|
||||
WillTopic string
|
||||
WillMessage []byte
|
||||
Username []byte
|
||||
Password []byte
|
||||
|
||||
// Connack
|
||||
SessionPresent bool
|
||||
ReturnCode byte
|
||||
|
||||
// Publish
|
||||
TopicName string
|
||||
Payload []byte
|
||||
|
||||
// Subscribe, Unsubscribe
|
||||
Topics []string
|
||||
Qoss []byte
|
||||
|
||||
// If AllowClients set, only deliver to clients in the client allow list.
|
||||
// For use with the OnMessage event hook.
|
||||
AllowClients []string
|
||||
|
||||
ReturnCodes []byte // Suback
|
||||
SessionPresent bool
|
||||
}
|
||||
|
||||
// ConnectEncode encodes a connect packet.
|
||||
|
@@ -175,12 +175,12 @@ func (x *Index) Messages(filter string) []packets.Packet {
|
||||
|
||||
// Leaf is a child node on the tree.
|
||||
type Leaf struct {
|
||||
Message packets.Packet // a message which has been retained for a specific topic.
|
||||
Key string // the key that was used to create the leaf.
|
||||
Filter string // the path of the topic filter being matched.
|
||||
Parent *Leaf // a pointer to the parent node for the leaf.
|
||||
Leaves map[string]*Leaf // a map of child nodes, keyed on particle id.
|
||||
Clients map[string]byte // a map of client ids subscribed to the topic.
|
||||
Filter string // the path of the topic filter being matched.
|
||||
Message packets.Packet // a message which has been retained for a specific topic.
|
||||
}
|
||||
|
||||
// scanSubscribers recursively steps through a branch of leaves finding clients who
|
||||
|
@@ -18,9 +18,9 @@ import (
|
||||
type HTTPStats struct {
|
||||
sync.RWMutex
|
||||
id string // the internal id of the listener.
|
||||
address string // the network address to bind to.
|
||||
config *Config // configuration values for the listener.
|
||||
system *system.Info // pointers to the server data.
|
||||
address string // the network address to bind to.
|
||||
listen *http.Server // the http server.
|
||||
end uint32 // ensure the close methods are only called once.}
|
||||
}
|
||||
|
@@ -38,10 +38,10 @@ type Listener interface {
|
||||
|
||||
// Listeners contains the network listeners for the broker.
|
||||
type Listeners struct {
|
||||
sync.RWMutex
|
||||
wg sync.WaitGroup // a waitgroup that waits for all listeners to finish.
|
||||
internal map[string]Listener // a map of active listeners.
|
||||
system *system.Info // pointers to system info.
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
// New returns a new instance of Listeners.
|
||||
|
@@ -22,11 +22,11 @@ func MockEstablisher(id string, c net.Conn, ac auth.Controller) error {
|
||||
type MockListener struct {
|
||||
sync.RWMutex
|
||||
id string // the id of the listener.
|
||||
Config *Config // configuration for the listener.
|
||||
address string // the network address the listener binds to.
|
||||
Listening bool // indiciate the listener is listening.
|
||||
Serving bool // indicate the listener is serving.
|
||||
Config *Config // configuration for the listener.
|
||||
done chan bool // indicate the listener is done.
|
||||
Serving bool // indicate the listener is serving.
|
||||
Listening bool // indiciate the listener is listening.
|
||||
ErrListen bool // throw an error on listen.
|
||||
}
|
||||
|
||||
|
@@ -14,10 +14,10 @@ import (
|
||||
type TCP struct {
|
||||
sync.RWMutex
|
||||
id string // the internal id of the listener.
|
||||
config *Config // configuration values for the listener.
|
||||
protocol string // the TCP protocol to use.
|
||||
address string // the network address to bind to.
|
||||
listen net.Listener // a net.Listener which will listen for new clients.
|
||||
config *Config // configuration values for the listener.
|
||||
end uint32 // ensure the close methods are only called once.
|
||||
}
|
||||
|
||||
|
@@ -31,11 +31,11 @@ var (
|
||||
type Websocket struct {
|
||||
sync.RWMutex
|
||||
id string // the internal id of the listener.
|
||||
config *Config // configuration values for the listener.
|
||||
address string // the network address to bind to.
|
||||
config *Config // configuration values for the listener.
|
||||
listen *http.Server // an http server for serving websocket connections.
|
||||
end uint32 // ensure the close methods are only called once.
|
||||
establish EstablishFunc // the server's establish conection handler.
|
||||
end uint32 // ensure the close methods are only called once.
|
||||
}
|
||||
|
||||
// wsConn is a websocket connection which satisfies the net.Conn interface.
|
||||
|
@@ -53,50 +53,50 @@ type Subscription struct {
|
||||
|
||||
// Message contains the details of a retained or inflight message.
|
||||
type Message struct {
|
||||
ID string // the storage key.
|
||||
T string // the type of the stored data.
|
||||
Client string // the id of the client who sent the message (if inflight).
|
||||
FixedHeader FixedHeader // the header properties of the message.
|
||||
PacketID uint16 // the unique id of the packet (if inflight).
|
||||
TopicName string // the topic the message was sent to (if retained).
|
||||
Payload []byte // the message payload (if retained).
|
||||
FixedHeader FixedHeader // the header properties of the message.
|
||||
T string // the type of the stored data.
|
||||
ID string // the storage key.
|
||||
Client string // the id of the client who sent the message (if inflight).
|
||||
TopicName string // the topic the message was sent to (if retained).
|
||||
Sent int64 // the last time the message was sent (for retries) in unixtime (if inflight).
|
||||
Resends int // the number of times the message was attempted to be sent (if inflight).
|
||||
PacketID uint16 // the unique id of the packet (if inflight).
|
||||
}
|
||||
|
||||
// FixedHeader contains the fixed header properties of a message.
|
||||
type FixedHeader struct {
|
||||
Type byte // the type of the packet (PUBLISH, SUBSCRIBE, etc) from bits 7 - 4 (byte 1).
|
||||
Dup bool // indicates if the packet was already sent at an earlier time.
|
||||
Qos byte // indicates the quality of service expected.
|
||||
Retain bool // whether the message should be retained.
|
||||
Remaining int // the number of remaining bytes in the payload.
|
||||
Type byte // the type of the packet (PUBLISH, SUBSCRIBE, etc) from bits 7 - 4 (byte 1).
|
||||
Qos byte // indicates the quality of service expected.
|
||||
Dup bool // indicates if the packet was already sent at an earlier time.
|
||||
Retain bool // whether the message should be retained.
|
||||
}
|
||||
|
||||
// Client contains client data that can be persistently stored.
|
||||
type Client struct {
|
||||
LWT LWT // the last-will-and-testament message for the client.
|
||||
Username []byte // the username the client authenticated with.
|
||||
ID string // the storage key.
|
||||
ClientID string // the id of the client.
|
||||
T string // the type of the stored data.
|
||||
Listener string // the last known listener id for the client
|
||||
Username []byte // the username the client authenticated with.
|
||||
LWT LWT // the last-will-and-testament message for the client.
|
||||
}
|
||||
|
||||
// LWT contains details about a clients LWT payload.
|
||||
type LWT struct {
|
||||
Topic string // the topic the will message shall be sent to.
|
||||
Message []byte // the message that shall be sent when the client disconnects.
|
||||
Topic string // the topic the will message shall be sent to.
|
||||
Qos byte // the quality of service desired.
|
||||
Retain bool // indicates whether the will message should be retained
|
||||
}
|
||||
|
||||
// MockStore is a mock storage backend for testing.
|
||||
type MockStore struct {
|
||||
Fail map[string]bool // issue errors for different methods.
|
||||
FailOpen bool // error on open.
|
||||
Closed bool // indicate mock store is closed.
|
||||
Opened bool // indicate mock store is open.
|
||||
Fail map[string]bool // issue errors for different methods.
|
||||
}
|
||||
|
||||
// Open opens the storage instance.
|
||||
|
Reference in New Issue
Block a user