Change the way the pinger works.
Fix syning threads before reconnections Formatting
This commit is contained in:
parent
d53de8bdbd
commit
c8f78ebf45
@ -51,6 +51,7 @@ Commands
|
|||||||
ircobj.Password = "[server password]"
|
ircobj.Password = "[server password]"
|
||||||
ircobj.Connect("irc.someserver.com:6667") //Connect to server
|
ircobj.Connect("irc.someserver.com:6667") //Connect to server
|
||||||
ircobj.Sendraw("<string>") //sends string to server. Adds \r\n
|
ircobj.Sendraw("<string>") //sends string to server. Adds \r\n
|
||||||
|
ircobj.Sendraw("<formatstring>", ...) //sends formatted string to server.n
|
||||||
ircobj.Join("#channel [password]")
|
ircobj.Join("#channel [password]")
|
||||||
ircobj.Privmsg("#channel", "msg")
|
ircobj.Privmsg("#channel", "msg")
|
||||||
ircobj.Privmsg("nickname", "msg")
|
ircobj.Privmsg("nickname", "msg")
|
||||||
|
40
irc.go
40
irc.go
@ -6,25 +6,23 @@ package irc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"crypto/tls"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VERSION = "cleanirc v1.0"
|
VERSION = "go-ircevent v2.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
var error_ bool
|
|
||||||
|
|
||||||
func (irc *Connection) readLoop() {
|
func (irc *Connection) readLoop() {
|
||||||
br := bufio.NewReader(irc.socket)
|
br := bufio.NewReader(irc.socket)
|
||||||
|
|
||||||
for !irc.reconnecting {
|
for {
|
||||||
msg, err := br.ReadString('\n')
|
msg, err := br.ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
irc.Error <- err
|
irc.Error <- err
|
||||||
@ -71,11 +69,10 @@ func (irc *Connection) readLoop() {
|
|||||||
|
|
||||||
func (irc *Connection) writeLoop() {
|
func (irc *Connection) writeLoop() {
|
||||||
b, ok := <-irc.pwrite
|
b, ok := <-irc.pwrite
|
||||||
for !irc.reconnecting && ok {
|
for ok {
|
||||||
if b == "" || irc.socket == nil {
|
if b == "" || irc.socket == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := irc.socket.Write([]byte(b))
|
_, err := irc.socket.Write([]byte(b))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
irc.log.Printf("%s\n", err)
|
irc.log.Printf("%s\n", err)
|
||||||
@ -90,16 +87,16 @@ func (irc *Connection) writeLoop() {
|
|||||||
|
|
||||||
//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 (irc *Connection) pingLoop() {
|
func (irc *Connection) pingLoop() {
|
||||||
irc.ticker = time.Tick(1 * time.Minute) //Tick every minute.
|
irc.ticker = time.NewTicker(1 * time.Minute) //Tick every minute.
|
||||||
irc.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes.
|
irc.ticker2 = time.NewTicker(15 * time.Minute) //Tick every 15 minutes.
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-irc.ticker:
|
case <-irc.ticker.C:
|
||||||
//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(irc.lastMessage) >= (4 * time.Minute) {
|
if time.Since(irc.lastMessage) >= (4 * time.Minute) {
|
||||||
irc.SendRawf("PING %d", time.Now().UnixNano())
|
irc.SendRawf("PING %d", time.Now().UnixNano())
|
||||||
}
|
}
|
||||||
case <-irc.ticker2:
|
case <-irc.ticker2.C:
|
||||||
//Ping every 15 minutes.
|
//Ping every 15 minutes.
|
||||||
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.
|
||||||
@ -107,8 +104,13 @@ func (irc *Connection) pingLoop() {
|
|||||||
irc.nickcurrent = irc.nick
|
irc.nickcurrent = irc.nick
|
||||||
irc.SendRawf("NICK %s", irc.nick)
|
irc.SendRawf("NICK %s", irc.nick)
|
||||||
}
|
}
|
||||||
|
case <-irc.endping:
|
||||||
|
irc.ticker.Stop()
|
||||||
|
irc.ticker2.Stop()
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
irc.syncpinger <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (irc *Connection) Cycle() {
|
func (irc *Connection) Cycle() {
|
||||||
@ -153,8 +155,15 @@ func (irc *Connection) GetNick() string {
|
|||||||
func (irc *Connection) Reconnect() error {
|
func (irc *Connection) Reconnect() error {
|
||||||
close(irc.pwrite)
|
close(irc.pwrite)
|
||||||
close(irc.pread)
|
close(irc.pread)
|
||||||
|
irc.endping <- true
|
||||||
|
irc.log.Printf("Syncing Threads\n")
|
||||||
|
irc.log.Printf("Syncing Reader\n")
|
||||||
<-irc.syncreader
|
<-irc.syncreader
|
||||||
|
irc.log.Printf("Syncing Writer\n")
|
||||||
<-irc.syncwriter
|
<-irc.syncwriter
|
||||||
|
irc.log.Printf("Syncing Pinger\n")
|
||||||
|
<-irc.syncpinger
|
||||||
|
irc.log.Printf("Syncing Threads Done\n")
|
||||||
for {
|
for {
|
||||||
irc.log.Printf("Reconnecting to %s\n", irc.server)
|
irc.log.Printf("Reconnecting to %s\n", irc.server)
|
||||||
var err error
|
var err error
|
||||||
@ -164,7 +173,6 @@ func (irc *Connection) Reconnect() error {
|
|||||||
}
|
}
|
||||||
irc.log.Printf("Error: %s\n", err)
|
irc.log.Printf("Error: %s\n", err)
|
||||||
}
|
}
|
||||||
error_ = false
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,18 +183,17 @@ func (irc *Connection) Loop() {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
irc.log.Printf("Error: %s\n", e)
|
irc.log.Printf("Error: %s\n", e)
|
||||||
error_ = true
|
|
||||||
irc.Reconnect()
|
irc.Reconnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
close(irc.pwrite)
|
close(irc.pwrite)
|
||||||
close(irc.pread)
|
close(irc.pread)
|
||||||
|
irc.endping <- true
|
||||||
<-irc.syncreader
|
<-irc.syncreader
|
||||||
<-irc.syncwriter
|
<-irc.syncwriter
|
||||||
|
<-irc.syncpinger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (irc *Connection) Connect(server string) error {
|
func (irc *Connection) Connect(server string) error {
|
||||||
irc.server = server
|
irc.server = server
|
||||||
var err error
|
var err error
|
||||||
@ -209,7 +216,8 @@ func (irc *Connection) postConnect() error {
|
|||||||
irc.Error = make(chan error, 10)
|
irc.Error = make(chan error, 10)
|
||||||
irc.syncreader = make(chan bool)
|
irc.syncreader = make(chan bool)
|
||||||
irc.syncwriter = make(chan bool)
|
irc.syncwriter = make(chan bool)
|
||||||
|
irc.syncpinger = make(chan bool)
|
||||||
|
irc.endping = make(chan bool)
|
||||||
go irc.readLoop()
|
go irc.readLoop()
|
||||||
go irc.writeLoop()
|
go irc.writeLoop()
|
||||||
go irc.pingLoop()
|
go irc.pingLoop()
|
||||||
|
@ -20,8 +20,8 @@ type Connection struct {
|
|||||||
|
|
||||||
socket net.Conn
|
socket net.Conn
|
||||||
pread, pwrite chan string
|
pread, pwrite chan string
|
||||||
syncreader, syncwriter chan bool
|
syncreader, syncwriter, syncpinger chan bool
|
||||||
reconnecting bool
|
endping chan bool
|
||||||
|
|
||||||
nick string //The nickname we want.
|
nick string //The nickname we want.
|
||||||
nickcurrent string //The nickname we currently have.
|
nickcurrent string //The nickname we currently have.
|
||||||
@ -31,8 +31,7 @@ type Connection struct {
|
|||||||
events map[string][]func(*Event)
|
events map[string][]func(*Event)
|
||||||
|
|
||||||
lastMessage time.Time
|
lastMessage time.Time
|
||||||
ticker <-chan time.Time
|
ticker, ticker2 *time.Ticker
|
||||||
ticker2 <-chan time.Time
|
|
||||||
|
|
||||||
VerboseCallbackHandler bool
|
VerboseCallbackHandler bool
|
||||||
log *log.Logger
|
log *log.Logger
|
||||||
|
Loading…
Reference in New Issue
Block a user