This commit is contained in:
zhouyuyan
2017-09-12 09:15:08 +08:00
parent 5601632a33
commit 96277996f0
3 changed files with 36 additions and 6 deletions
+1
View File
@@ -342,6 +342,7 @@ func (b *Broker) connectRouter(url, remoteID string) {
route: route, route: route,
info: info, info: info,
} }
c.init()
b.remotes.Store(cid, c) b.remotes.Store(cid, c)
c.SendConnect() c.SendConnect()
c.SendInfo() c.SendInfo()
+29 -5
View File
@@ -33,6 +33,8 @@ type client struct {
conn net.Conn conn net.Conn
info info info info
route *route route *route
status int
smu sync.RWMutex
subs map[string]*subscription subs map[string]*subscription
rsubs map[string]*subInfo rsubs map[string]*subInfo
} }
@@ -69,6 +71,9 @@ var (
) )
func (c *client) init() { func (c *client) init() {
c.smu.Lock()
defer c.smu.Unlock()
c.status = Connected
typ := c.typ typ := c.typ
if typ == ROUTER { if typ == ROUTER {
c.rsubs = make(map[string]*subInfo) c.rsubs = make(map[string]*subInfo)
@@ -162,8 +167,11 @@ func ProcessMessage(msg *Message) {
} }
func (c *client) ProcessPublish(packet *packets.PublishPacket) { func (c *client) ProcessPublish(packet *packets.PublishPacket) {
topic := packet.TopicName if c.status == Disconnected {
return
}
topic := packet.TopicName
if !c.CheckTopicAuth(PUB, topic) { if !c.CheckTopicAuth(PUB, topic) {
log.Error("Pub Topics Auth failed, ", topic) log.Error("Pub Topics Auth failed, ", topic)
return return
@@ -198,6 +206,9 @@ func (c *client) ProcessPublish(packet *packets.PublishPacket) {
} }
func (c *client) ProcessPublishMessage(packet *packets.PublishPacket) { func (c *client) ProcessPublishMessage(packet *packets.PublishPacket) {
if c.status == Disconnected {
return
}
b := c.broker b := c.broker
if b == nil { if b == nil {
@@ -282,6 +293,10 @@ func getQueueSubscribeNum(qsubs []*subscription) int {
} }
func (c *client) ProcessSubscribe(packet *packets.SubscribePacket) { func (c *client) ProcessSubscribe(packet *packets.SubscribePacket) {
if c.status == Disconnected {
return
}
b := c.broker b := c.broker
if b == nil { if b == nil {
return return
@@ -375,6 +390,9 @@ func (c *client) ProcessSubscribe(packet *packets.SubscribePacket) {
} }
func (c *client) ProcessUnSubscribe(packet *packets.UnsubscribePacket) { func (c *client) ProcessUnSubscribe(packet *packets.UnsubscribePacket) {
if c.status == Disconnected {
return
}
b := c.broker b := c.broker
if b == nil { if b == nil {
return return
@@ -437,6 +455,9 @@ func (c *client) unsubscribe(sub *subscription) {
} }
func (c *client) ProcessPing() { func (c *client) ProcessPing() {
if c.status == Disconnected {
return
}
resp := packets.NewControlPacket(packets.Pingresp).(*packets.PingrespPacket) resp := packets.NewControlPacket(packets.Pingresp).(*packets.PingrespPacket)
err := c.WriterPacket(resp) err := c.WriterPacket(resp)
if err != nil { if err != nil {
@@ -446,6 +467,13 @@ func (c *client) ProcessPing() {
} }
func (c *client) Close() { func (c *client) Close() {
c.smu.Lock()
c.status = Disconnected
if c.conn != nil {
c.conn.Close()
c.conn = nil
}
c.smu.Unlock()
b := c.broker b := c.broker
subs := c.subs subs := c.subs
if b != nil { if b != nil {
@@ -463,10 +491,6 @@ func (c *client) Close() {
b.PublishMessage(c.info.willMsg) b.PublishMessage(c.info.willMsg)
} }
} }
if c.conn != nil {
c.conn.Close()
c.conn = nil
}
} }
func (c *client) WriterPacket(packet packets.ControlPacket) error { func (c *client) WriterPacket(packet packets.ControlPacket) error {
+6 -1
View File
@@ -11,6 +11,9 @@ import (
) )
func (c *client) SendInfo() { func (c *client) SendInfo() {
if c.status == Disconnected {
return
}
url := c.info.localIP + ":" + c.broker.config.Cluster.Port url := c.info.localIP + ":" + c.broker.config.Cluster.Port
infoMsg := NewInfo(c.broker.id, url, false) infoMsg := NewInfo(c.broker.id, url, false)
@@ -37,7 +40,9 @@ func (c *client) StartPing() {
} }
func (c *client) SendConnect() { func (c *client) SendConnect() {
if c.status == Disconnected {
return
}
m := packets.NewControlPacket(packets.Connect).(*packets.ConnectPacket) m := packets.NewControlPacket(packets.Connect).(*packets.ConnectPacket)
m.CleanSession = true m.CleanSession = true