From 91639436843a757e0a35a04c2523438e956ce3da Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sat, 5 Oct 2024 10:15:43 +0200 Subject: [PATCH] Add isConnected flag to track active connection state Introduced the isConnected boolean flag in the Client struct to clearly indicate whether there is an active connection. Updated relevant methods to set this flag accordingly, ensuring consistent state management across the Client's lifecycle. --- smtp/smtp.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/smtp/smtp.go b/smtp/smtp.go index ce163a2..444b203 100644 --- a/smtp/smtp.go +++ b/smtp/smtp.go @@ -67,6 +67,9 @@ type Client struct { // helloError is the error from the hello helloError error + // isConnected indicates if the Client has an active connection + isConnected bool + // localName is the name to use in HELO/EHLO localName string // the name to use in HELO/EHLO @@ -113,6 +116,7 @@ func NewClient(conn net.Conn, host string) (*Client, error) { } c := &Client{Text: text, conn: conn, serverName: host, localName: "localhost"} _, c.tls = conn.(*tls.Conn) + c.isConnected = true return c, nil } @@ -121,6 +125,7 @@ func NewClient(conn net.Conn, host string) (*Client, error) { func (c *Client) Close() error { c.mutex.Lock() err := c.Text.Close() + c.isConnected = false c.mutex.Unlock() return err } @@ -516,8 +521,7 @@ func (c *Client) Quit() error { } c.mutex.Lock() err = c.Text.Close() - c.Text = nil - c.conn = nil + c.isConnected = false c.mutex.Unlock() return err @@ -558,11 +562,12 @@ func (c *Client) SetDSNRcptNotifyOption(d string) { // Returns true if the `conn` field is not nil, indicating an active connection. func (c *Client) HasConnection() bool { c.mutex.RLock() - conn := c.conn + isConn := c.isConnected c.mutex.RUnlock() - return conn != nil + return isConn } +// UpdateDeadline sets a new deadline on the SMTP connection with the specified timeout duration. func (c *Client) UpdateDeadline(timeout time.Duration) error { c.mutex.Lock() if err := c.conn.SetDeadline(time.Now().Add(timeout)); err != nil {