5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-23 03:31:37 +00:00

Update vendor (irc)

This commit is contained in:
Wim 2017-01-24 21:24:57 +01:00
parent 991183e514
commit 09b243d8c2
4 changed files with 26 additions and 33 deletions

View File

@ -74,7 +74,9 @@ func (irc *Connection) readLoop() {
irc.Log.Printf("<-- %s\n", strings.TrimSpace(msg)) irc.Log.Printf("<-- %s\n", strings.TrimSpace(msg))
} }
irc.Lock()
irc.lastMessage = time.Now() irc.lastMessage = time.Now()
irc.Unlock()
event, err := parseToEvent(msg) event, err := parseToEvent(msg)
event.Connection = irc event.Connection = irc
if err == nil { if err == nil {
@ -171,10 +173,12 @@ func (irc *Connection) pingLoop() {
//Ping at the ping frequency //Ping at the ping frequency
irc.SendRawf("PING %d", time.Now().UnixNano()) irc.SendRawf("PING %d", time.Now().UnixNano())
//Try to recapture nickname if it's not as configured. //Try to recapture nickname if it's not as configured.
irc.Lock()
if irc.nick != irc.nickcurrent { if irc.nick != irc.nickcurrent {
irc.nickcurrent = irc.nick irc.nickcurrent = irc.nick
irc.SendRawf("NICK %s", irc.nick) irc.SendRawf("NICK %s", irc.nick)
} }
irc.Unlock()
case <-irc.end: case <-irc.end:
ticker.Stop() ticker.Stop()
ticker2.Stop() ticker2.Stop()
@ -183,13 +187,21 @@ func (irc *Connection) pingLoop() {
} }
} }
func (irc *Connection) isQuitting() bool {
irc.Lock()
defer irc.Unlock()
return irc.quit
}
// Main loop to control the connection. // Main loop to control the connection.
func (irc *Connection) Loop() { func (irc *Connection) Loop() {
errChan := irc.ErrorChan() errChan := irc.ErrorChan()
for !irc.quit { for !irc.isQuitting() {
err := <-errChan err := <-errChan
close(irc.end)
irc.Wait()
for !irc.isQuitting() {
irc.Log.Printf("Error, disconnected: %s\n", err) irc.Log.Printf("Error, disconnected: %s\n", err)
for !irc.quit {
if err = irc.Reconnect(); err != nil { if err = irc.Reconnect(); err != nil {
irc.Log.Printf("Error while reconnecting: %s\n", err) irc.Log.Printf("Error while reconnecting: %s\n", err)
time.Sleep(60 * time.Second) time.Sleep(60 * time.Second)
@ -211,8 +223,10 @@ func (irc *Connection) Quit() {
} }
irc.SendRaw(quit) irc.SendRaw(quit)
irc.Lock()
irc.stopped = true irc.stopped = true
irc.quit = true irc.quit = true
irc.Unlock()
} }
// Use the connection to join a given channel. // Use the connection to join a given channel.
@ -341,37 +355,14 @@ func (irc *Connection) Connected() bool {
// A disconnect sends all buffered messages (if possible), // A disconnect sends all buffered messages (if possible),
// stops all goroutines and then closes the socket. // stops all goroutines and then closes the socket.
func (irc *Connection) Disconnect() { func (irc *Connection) Disconnect() {
for event := range irc.events {
irc.ClearCallback(event)
}
if irc.end != nil {
close(irc.end)
}
irc.end = nil
if irc.pwrite != nil {
close(irc.pwrite)
}
irc.Wait()
if irc.socket != nil { if irc.socket != nil {
irc.socket.Close() irc.socket.Close()
} }
irc.socket = nil
irc.ErrorChan() <- ErrDisconnected irc.ErrorChan() <- ErrDisconnected
} }
// Reconnect to a server using the current connection. // Reconnect to a server using the current connection.
func (irc *Connection) Reconnect() error { func (irc *Connection) Reconnect() error {
if irc.end != nil {
close(irc.end)
}
irc.end = nil
irc.Wait() //make sure that wait group is cleared ensuring that all spawned goroutines have completed
irc.end = make(chan struct{}) irc.end = make(chan struct{})
return irc.Connect(irc.Server) return irc.Connect(irc.Server)
} }
@ -427,7 +418,7 @@ func (irc *Connection) Connect(server string) error {
} }
irc.stopped = false irc.stopped = false
//irc.Log.Printf("Connected to %s (%s)\n", irc.Server, irc.socket.RemoteAddr()) irc.Log.Printf("Connected to %s (%s)\n", irc.Server, irc.socket.RemoteAddr())
irc.pwrite = make(chan string, 10) irc.pwrite = make(chan string, 10)
irc.Error = make(chan error, 2) irc.Error = make(chan error, 2)

View File

@ -136,9 +136,8 @@ func (irc *Connection) RunCallbacks(event *Event) {
func (irc *Connection) setupCallbacks() { func (irc *Connection) setupCallbacks() {
irc.events = make(map[string]map[int]func(*Event)) irc.events = make(map[string]map[int]func(*Event))
//Handle error events. This has to be called in a new thred to allow //Handle error events.
//readLoop to exit irc.AddCallback("ERROR", func(e *Event) { irc.Disconnect() })
irc.AddCallback("ERROR", func(e *Event) { go irc.Disconnect() })
//Handle ping events //Handle ping events
irc.AddCallback("PING", func(e *Event) { irc.SendRaw("PONG :" + e.Message()) }) irc.AddCallback("PING", func(e *Event) { irc.SendRaw("PONG :" + e.Message()) })
@ -201,7 +200,7 @@ func (irc *Connection) setupCallbacks() {
ns, _ := strconv.ParseInt(e.Message(), 10, 64) ns, _ := strconv.ParseInt(e.Message(), 10, 64)
delta := time.Duration(time.Now().UnixNano() - ns) delta := time.Duration(time.Now().UnixNano() - ns)
if irc.Debug { if irc.Debug {
irc.Log.Printf("Lag: %vs\n", delta) irc.Log.Printf("Lag: %.3f s\n", delta.Seconds())
} }
}) })
@ -216,6 +215,8 @@ func (irc *Connection) setupCallbacks() {
// 1: RPL_WELCOME "Welcome to the Internet Relay Network <nick>!<user>@<host>" // 1: RPL_WELCOME "Welcome to the Internet Relay Network <nick>!<user>@<host>"
// Set irc.nickcurrent to the actually used nick in this connection. // Set irc.nickcurrent to the actually used nick in this connection.
irc.AddCallback("001", func(e *Event) { irc.AddCallback("001", func(e *Event) {
irc.Lock()
irc.nickcurrent = e.Arguments[0] irc.nickcurrent = e.Arguments[0]
irc.Unlock()
}) })
} }

View File

@ -13,6 +13,7 @@ import (
) )
type Connection struct { type Connection struct {
sync.Mutex
sync.WaitGroup sync.WaitGroup
Debug bool Debug bool
Error chan error Error chan error
@ -46,7 +47,7 @@ type Connection struct {
Log *log.Logger Log *log.Logger
stopped bool stopped bool
quit bool quit bool //User called Quit, do not reconnect.
} }
// A struct to represent an event. // A struct to represent an event.

2
vendor/manifest vendored
View File

@ -186,7 +186,7 @@
"importpath": "github.com/thoj/go-ircevent", "importpath": "github.com/thoj/go-ircevent",
"repository": "https://github.com/thoj/go-ircevent", "repository": "https://github.com/thoj/go-ircevent",
"vcs": "git", "vcs": "git",
"revision": "98c1902dd2097f38142384167e60206ba26f1585", "revision": "1b0acb5f2f1b615cfbd4b9f91abb14cb39a18769",
"branch": "master", "branch": "master",
"notests": true "notests": true
}, },