Skip to content
Snippets Groups Projects
Unverified Commit 4603af9c authored by Wolfgang Welz's avatar Wolfgang Welz
Browse files

assure conn is not nil

parent 1c3f0760
No related branches found
No related tags found
No related merge requests found
package client
import (
"errors"
"net"
"sync"
"time"
......@@ -96,28 +97,43 @@ func (c *connector) Stop() {
}
func (c *connector) new() {
c.mu.Lock()
defer c.mu.Unlock()
select {
case _ = <-c.closing:
return
default:
c.mu.Lock()
defer c.mu.Unlock()
conn, err := net.Dial("tcp", config.Node.GetString(CfgServerAddress))
c.conn = nil
tcpConn, err := net.Dial("tcp", config.Node.GetString(CfgServerAddress))
if err != nil {
time.AfterFunc(1*time.Minute, c.new)
log.Warn(err)
return
}
c.conn = network.NewManagedConnection(conn)
c.conn = network.NewManagedConnection(tcpConn)
c.conn.Events.Close.Attach(events.NewClosure(c.new))
}
}
func (c *connector) Close() (err error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.conn != nil {
err = c.conn.Close()
}
return
}
func (c *connector) Write(b []byte) (int, error) {
// TODO: check that start was called
// TODO: check that Stop was not called
c.mu.Lock()
defer c.mu.Unlock()
if c.conn == nil {
return 0, errors.New("no connection established")
}
return c.conn.Write(b)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment