Fix a bunch of golangci-lint issues
This commit is contained in:
parent
e4fae94588
commit
9cd116c5e4
9 changed files with 48 additions and 26 deletions
10
main.go
10
main.go
|
@ -10,11 +10,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
listen_addr := ":1883"
|
listenAddr := ":1883"
|
||||||
listener, err := net.Listen("tcp", listen_addr)
|
listener, err := net.Listen("tcp", listenAddr)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Coulde't start a listener on tcp %v. Error: %e", listen_addr, err)
|
log.Fatalf("Coulde't start a listener on tcp %v. Error: %e", listenAddr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var sessions map[string]*session.Session = make(map[string]*session.Session)
|
var sessions map[string]*session.Session = make(map[string]*session.Session)
|
||||||
|
@ -37,8 +37,8 @@ func main() {
|
||||||
select {
|
select {
|
||||||
case con := <-connChan:
|
case con := <-connChan:
|
||||||
handleConnection(con, sessions, removeSessChan)
|
handleConnection(con, sessions, removeSessChan)
|
||||||
case sesId := <- removeSessChan:
|
case sesID := <-removeSessChan:
|
||||||
delete(sessions, sesId)
|
delete(sessions, sesID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,13 @@ func (c controlPacket) write(w io.Writer) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
types.WriteDataWithVarIntLen(buf, data)
|
err = types.WriteDataWithVarIntLen(buf, data)
|
||||||
w.Write(buf.Bytes())
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = w.Write(buf.Bytes())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,7 +74,10 @@ func WriteVariableByteInt(w io.Writer, v uint32) error {
|
||||||
encodedByte = encodedByte | 128
|
encodedByte = encodedByte | 128
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Write([]byte{encodedByte})
|
_,err := w.Write([]byte{encodedByte})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if v == 0 {
|
if v == 0 {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -59,7 +59,10 @@ func (s *Session) Connect(req ConnectionRequest) {
|
||||||
connAck.Properties.SharedSubscriptionAvailable.Value = &false
|
connAck.Properties.SharedSubscriptionAvailable.Value = &false
|
||||||
|
|
||||||
s.Connection = req.Connection
|
s.Connection = req.Connection
|
||||||
s.Connection.sendPacket(connAck)
|
err := s.Connection.sendPacket(connAck)
|
||||||
|
if err != nil {
|
||||||
|
panic("TODO, handle this")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Starts a loop the recieves and responds to packets
|
// Starts a loop the recieves and responds to packets
|
||||||
|
@ -69,7 +72,7 @@ func (s *Session) HandlerLoop() {
|
||||||
select {
|
select {
|
||||||
case packet := <-s.Connection.PacketChannel:
|
case packet := <-s.Connection.PacketChannel:
|
||||||
packet.Visit(s)
|
packet.Visit(s)
|
||||||
case _ = <-s.Connection.ClientDisconnectedChan:
|
case <-s.Connection.ClientDisconnectedChan:
|
||||||
s.onDisconnect()
|
s.onDisconnect()
|
||||||
case c := <-s.ConnecionChannel:
|
case c := <-s.ConnecionChannel:
|
||||||
s.Connect(c)
|
s.Connect(c)
|
||||||
|
@ -77,7 +80,10 @@ func (s *Session) HandlerLoop() {
|
||||||
// TODO implement other qos levels
|
// TODO implement other qos levels
|
||||||
subMessage.QOSLevel = 0
|
subMessage.QOSLevel = 0
|
||||||
subMessage.Dup = false
|
subMessage.Dup = false
|
||||||
s.Connection.sendPacket(subMessage)
|
err := s.Connection.sendPacket(subMessage)
|
||||||
|
if err != nil {
|
||||||
|
panic("TOOO handle this")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,7 +92,7 @@ func (s *Session) HandlerLoop() {
|
||||||
s.Connect(c)
|
s.Connect(c)
|
||||||
// Tail recursion baybeeee
|
// Tail recursion baybeeee
|
||||||
s.HandlerLoop()
|
s.HandlerLoop()
|
||||||
case _ = <- s.expireTimer.C:
|
case <- s.expireTimer.C:
|
||||||
s.expireSession()
|
s.expireSession()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ type Connection struct {
|
||||||
|
|
||||||
func (c *Connection) resetKeepAlive() {
|
func (c *Connection) resetKeepAlive() {
|
||||||
if c.KeepAliveInterval != 0 {
|
if c.KeepAliveInterval != 0 {
|
||||||
|
panic("TODO")
|
||||||
// TODO IMPLEMENT THIS
|
// TODO IMPLEMENT THIS
|
||||||
//s.keepAliveTicker.Reset(s.KeepAliveInterval)
|
//s.keepAliveTicker.Reset(s.KeepAliveInterval)
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ func (s *Session) expireSession() {
|
||||||
|
|
||||||
// newTime is nullable
|
// newTime is nullable
|
||||||
func (s *Session) SetExpireTimer(newTime *uint32) {
|
func (s *Session) SetExpireTimer(newTime *uint32) {
|
||||||
var expiry = uint32(0)
|
expiry := uint32(0)
|
||||||
if newTime != nil {
|
if newTime != nil {
|
||||||
expiry = *newTime
|
expiry = *newTime
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -31,7 +31,10 @@ func (s *Session) VisitPublish(p packets.PublishPacket) {
|
||||||
PacketID: *p.PacketId,
|
PacketID: *p.PacketId,
|
||||||
Reason: reason,
|
Reason: reason,
|
||||||
}
|
}
|
||||||
s.Connection.sendPacket(ack)
|
err := s.Connection.sendPacket(ack)
|
||||||
|
if err != nil {
|
||||||
|
panic("TODO")
|
||||||
|
}
|
||||||
} else if p.QOSLevel == 2 {
|
} else if p.QOSLevel == 2 {
|
||||||
panic("UNIMPLEMENTED QOS level 2")
|
panic("UNIMPLEMENTED QOS level 2")
|
||||||
}
|
}
|
||||||
|
@ -56,24 +59,30 @@ func (s *Session) VisitSubscribe(p packets.SubscribePacket) {
|
||||||
for _, filter := range p.TopicFilters {
|
for _, filter := range p.TopicFilters {
|
||||||
subscription.Subscriptions.Subscribe(filter, s.SubscriptionChannel)
|
subscription.Subscriptions.Subscribe(filter, s.SubscriptionChannel)
|
||||||
}
|
}
|
||||||
s.Connection.sendPacket(packets.SubAckPacket{
|
err := s.Connection.sendPacket(packets.SubAckPacket{
|
||||||
PacketID: p.PacketId,
|
PacketID: p.PacketId,
|
||||||
Reason: packets.SubackReasonGrantedQoSTwo,
|
Reason: packets.SubackReasonGrantedQoSTwo,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic("TODO")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) VisitUnsubscribe(p packets.UnsubscribePacket) {
|
func (s *Session) VisitUnsubscribe(p packets.UnsubscribePacket) {
|
||||||
for _, topic := range p.Topics {
|
for _, topic := range p.Topics {
|
||||||
subscription.Subscriptions.Unsubscribe(topic, s.SubscriptionChannel)
|
subscription.Subscriptions.Unsubscribe(topic, s.SubscriptionChannel)
|
||||||
}
|
}
|
||||||
s.Connection.sendPacket(packets.UnsubAckPacket{
|
err := s.Connection.sendPacket(packets.UnsubAckPacket{
|
||||||
PacketID: p.PacketID,
|
PacketID: p.PacketID,
|
||||||
Reason: packets.UnsubackReasonSuccess,
|
Reason: packets.UnsubackReasonSuccess,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic("TODO")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) VisitPing(p packets.PingreqPacket) {
|
func (s *Session) VisitPing(p packets.PingreqPacket) {
|
||||||
s.Connection.sendPacket(packets.PingrespPacket{})
|
_ = s.Connection.sendPacket(packets.PingrespPacket{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -24,17 +24,14 @@ func genClientID() *string {
|
||||||
return &id
|
return &id
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) Disconnect(code packets.DisconnectReasonCode) error {
|
func (s *Session) Disconnect(code packets.DisconnectReasonCode) {
|
||||||
s.Connection.sendPacket(packets.DisconnectPacket{
|
// If disconnetion fails that means we are already disconnected, great!
|
||||||
|
_ = s.Connection.sendPacket(packets.DisconnectPacket{
|
||||||
ReasonCode: code,
|
ReasonCode: code,
|
||||||
})
|
})
|
||||||
|
_ = s.Connection.close()
|
||||||
|
|
||||||
err := s.Connection.close()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s.onDisconnect()
|
s.onDisconnect()
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ func (s *SubscriptionTreeNode) findNode(fields []string) *SubscriptionTreeNode {
|
||||||
s.NodeLock.RLock()
|
s.NodeLock.RLock()
|
||||||
}
|
}
|
||||||
|
|
||||||
child, _ := s.children[field]
|
child := s.children[field]
|
||||||
s.NodeLock.RUnlock()
|
s.NodeLock.RUnlock()
|
||||||
return child.findNode(fields[1:])
|
return child.findNode(fields[1:])
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue