Merge changes from other fork
This commit is contained in:
parent
be3afcc796
commit
e05c098519
52
irc.go
52
irc.go
@ -21,7 +21,7 @@ const (
|
|||||||
|
|
||||||
var error_ bool
|
var error_ bool
|
||||||
|
|
||||||
func readLoop(irc *Connection) {
|
func (irc *Connection) readLoop() {
|
||||||
br := bufio.NewReader(irc.socket)
|
br := bufio.NewReader(irc.socket)
|
||||||
|
|
||||||
for !irc.reconnecting {
|
for !irc.reconnecting {
|
||||||
@ -69,7 +69,7 @@ func readLoop(irc *Connection) {
|
|||||||
irc.syncreader <-true
|
irc.syncreader <-true
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeLoop(irc *Connection) {
|
func (irc *Connection) writeLoop() {
|
||||||
b, ok := <-irc.pwrite
|
b, ok := <-irc.pwrite
|
||||||
for !irc.reconnecting && ok {
|
for !irc.reconnecting && ok {
|
||||||
if b == "" || irc.socket == nil {
|
if b == "" || irc.socket == nil {
|
||||||
@ -89,23 +89,23 @@ func writeLoop(irc *Connection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Pings the server if we have not recived any messages for 5 minutes
|
//Pings the server if we have not recived any messages for 5 minutes
|
||||||
func pingLoop(i *Connection) {
|
func (irc *Connection) pingLoop() {
|
||||||
i.ticker = time.Tick(1 * time.Minute) //Tick every minute.
|
irc.ticker = time.Tick(1 * time.Minute) //Tick every minute.
|
||||||
i.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes.
|
irc.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes.
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-i.ticker:
|
case <-irc.ticker:
|
||||||
//Ping if we haven't recived anything from the server within 4 minutes
|
//Ping if we haven't recived anything from the server within 4 minutes
|
||||||
if time.Since(i.lastMessage) >= (4 * time.Minute) {
|
if time.Since(irc.lastMessage) >= (4 * time.Minute) {
|
||||||
i.SendRawf("PING %d", time.Now().UnixNano())
|
irc.SendRawf("PING %d", time.Now().UnixNano())
|
||||||
}
|
}
|
||||||
case <-irc.ticker2:
|
case <-irc.ticker2:
|
||||||
//Ping every 15 minutes.
|
//Ping every 15 minutes.
|
||||||
i.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.
|
||||||
if i.nick != i.nickcurrent {
|
if irc.nick != irc.nickcurrent {
|
||||||
i.nickcurrent = i.nick
|
irc.nickcurrent = irc.nick
|
||||||
i.SendRawf("NICK %s", i.nick)
|
irc.SendRawf("NICK %s", irc.nick)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -150,33 +150,33 @@ func (irc *Connection) GetNick() string {
|
|||||||
return irc.nickcurrent
|
return irc.nickcurrent
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Connection) Reconnect() error {
|
func (irc *Connection) Reconnect() error {
|
||||||
close(i.pwrite)
|
close(irc.pwrite)
|
||||||
close(i.pread)
|
close(irc.pread)
|
||||||
<-i.syncreader
|
<-irc.syncreader
|
||||||
<-i.syncwriter
|
<-irc.syncwriter
|
||||||
for {
|
for {
|
||||||
i.log.Printf("Reconnecting to %s\n", i.server)
|
irc.log.Printf("Reconnecting to %s\n", irc.server)
|
||||||
var err error
|
var err error
|
||||||
irc.Connect(irc.server)
|
irc.Connect(irc.server)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
i.log.Printf("Error: %s\n", err)
|
irc.log.Printf("Error: %s\n", err)
|
||||||
}
|
}
|
||||||
error_ = false
|
error_ = false
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Connection) Loop() {
|
func (irc *Connection) Loop() {
|
||||||
for !i.quitting {
|
for !irc.quitting {
|
||||||
e := <-i.Error
|
e := <-irc.Error
|
||||||
if i.quitting {
|
if irc.quitting {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
i.log.Printf("Error: %s\n", e)
|
irc.log.Printf("Error: %s\n", e)
|
||||||
error_ = true
|
error_ = true
|
||||||
i.Reconnect()
|
irc.Reconnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
close(irc.pwrite)
|
close(irc.pwrite)
|
||||||
@ -189,8 +189,8 @@ func (i *Connection) Loop() {
|
|||||||
|
|
||||||
func (irc *Connection) Connect(server string) error {
|
func (irc *Connection) Connect(server string) error {
|
||||||
irc.server = server
|
irc.server = server
|
||||||
irc.log.Printf("Connecting to %s\n", i.server)
|
|
||||||
var err error
|
var err error
|
||||||
|
irc.log.Printf("Connecting to %s\n", irc.server)
|
||||||
if irc.UseSSL {
|
if irc.UseSSL {
|
||||||
irc.socket, err = tls.Dial("tcp", irc.server, irc.SSLConfig)
|
irc.socket, err = tls.Dial("tcp", irc.server, irc.SSLConfig)
|
||||||
} else {
|
} else {
|
||||||
|
@ -28,7 +28,7 @@ type Connection struct {
|
|||||||
user string
|
user string
|
||||||
registered bool
|
registered bool
|
||||||
server string
|
server string
|
||||||
events map[string][]func(*IRCEvent)
|
events map[string][]func(*Event)
|
||||||
|
|
||||||
lastMessage time.Time
|
lastMessage time.Time
|
||||||
ticker <-chan time.Time
|
ticker <-chan time.Time
|
||||||
|
Loading…
Reference in New Issue
Block a user