From d13ed6d79af61db6333ad5cc3200d96b6410d064 Mon Sep 17 00:00:00 2001
From: gongshangguo <gongshangguo@admin.com>
Date: 星期三, 02 三月 2022 10:17:46 +0800
Subject: [PATCH] close

---
 client/client.go |   21 +++++++++++++++++----
 1 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/client/client.go b/client/client.go
index 96bfe94..c289f02 100644
--- a/client/client.go
+++ b/client/client.go
@@ -6,6 +6,7 @@
 	"bufio"
 	"encoding/binary"
 	"encoding/json"
+	"errors"
 	uuid "github.com/satori/go.uuid"
 	"go.uber.org/zap"
 	"io"
@@ -239,6 +240,12 @@
 			c.Logger.Error("Write Body Error:", err)
 		}
 	}()
+
+	if c.IsClosed() {
+		errMsg := "Can not write msg into closed chain"
+		c.Logger.Warn(errMsg, zap.Any("msg",body))
+		return errors.New(errMsg)
+	}
 	body.Receiver = aiot.RECEIVER_TO_SAAS
 	msgData, err := json.Marshal(body)
 	if err != nil {
@@ -431,6 +438,11 @@
 	return c.deviceId
 }
 
+// 鑾峰彇杩炴帴鐘舵��
+func (c *Client) GetState() State {
+	return c.state
+}
+
 // 鍒ゆ柇杩炴帴鏄惁鍏抽棴
 func (c *Client) IsClosed() bool {
 	return c.state == StateDisconnected
@@ -448,7 +460,7 @@
 
 // 鍏抽棴TCP
 func (c *Client) Close() {
-	c.Logger.Debug("Closing connect", zap.String("addr", c.addr))
+	c.Logger.Debug("Closing connect...", zap.String("addr", c.addr))
 	c.closeLock.Lock()
 	defer c.closeLock.Unlock()
 	// 鍏抽棴閫氶亾
@@ -457,8 +469,9 @@
 		if c.IsConnected() {
 			c.clientCallback.OnClose(c)
 		}
-
-		// 璁剧疆杩炴帴灞炴��
-		c.SetState(StateDisconnected)
 	}
+
+	// 璁剧疆杩炴帴灞炴��
+	c.SetState(StateDisconnected)
+	c.Logger.Debug("Connect closed...", zap.String("addr", c.addr))
 }

--
Gitblit v1.8.0