Skip to content

Commit

Permalink
Merge pull request #10 from mjard/master
Browse files Browse the repository at this point in the history
Fixes against weekly.2012-02-22
  • Loading branch information
thoj committed Feb 25, 2012
2 parents 4e661a3 + a025638 commit 67c1c92
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
12 changes: 6 additions & 6 deletions irc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func reader(irc *IRCConnection) {
irc.Error <- err
break
}
irc.lastMessage = time.Seconds()
irc.lastMessage = time.Now()
msg = msg[0 : len(msg)-2] //Remove \r\n
event := &IRCEvent{Raw: msg}
if msg[0] == ':' {
Expand Down Expand Up @@ -75,18 +75,18 @@ func writer(irc *IRCConnection) {

//Pings the server if we have not recived any messages for 5 minutes
func pinger(i *IRCConnection) {
i.ticker = time.Tick(1000 * 1000 * 1000 * 60 * 1) //Tick every minute.
i.ticker2 = time.Tick(1000 * 1000 * 1000 * 60 * 15) //Tick every 15 minutes.
i.ticker = time.Tick(1 * time.Minute) //Tick every minute.
i.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes.
for {
select {
case <-i.ticker:
//Ping if we haven't recived anything from the server within 4 minutes
if time.Seconds()-i.lastMessage >= 60*4 {
i.SendRaw(fmt.Sprintf("PING %d", time.Nanoseconds()))
if time.Since(i.lastMessage) >= (4 * time.Minute) {
i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
}
case <-i.ticker2:
//Ping every 15 minutes.
i.SendRaw(fmt.Sprintf("PING %d", time.Nanoseconds()))
i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
//Try to recapture nickname if it's not as configured.
if i.nick != i.nickcurrent {
i.nickcurrent = i.nick
Expand Down
11 changes: 6 additions & 5 deletions irc_callback.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package irc

import (
"strings"
"fmt"
"time"
"strconv"
"strings"
"time"
)

func (irc *IRCConnection) AddCallback(eventcode string, callback func(*IRCEvent)) {
Expand Down Expand Up @@ -80,7 +80,7 @@ func (irc *IRCConnection) setupCallbacks() {
})

irc.AddCallback("CTCP_TIME", func(e *IRCEvent) {
ltime := time.LocalTime()
ltime := time.Now()
irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01TIME %s\x01", e.Nick, ltime.String()))
})

Expand All @@ -101,8 +101,9 @@ func (irc *IRCConnection) setupCallbacks() {
})

irc.AddCallback("PONG", func(e *IRCEvent) {
ns, _ := strconv.Atoi64(e.Message)
fmt.Printf("Lag: %fs\n", float32((time.Nanoseconds()-ns))/1000/1000/1000)
ns, _ := strconv.ParseInt(e.Message, 10, 64)
delta := time.Duration(time.Now().UnixNano() - ns)
fmt.Printf("Lag: %vs\n", delta)
})

irc.AddCallback("NICK", func(e *IRCEvent) {
Expand Down
11 changes: 7 additions & 4 deletions irc_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package irc

import "net"
import (
"net"
"time"
)

type IRCConnection struct {
socket net.Conn
Expand All @@ -19,9 +22,9 @@ type IRCConnection struct {
Password string
events map[string][]func(*IRCEvent)

lastMessage int64
ticker <-chan int64
ticker2 <-chan int64
lastMessage time.Time
ticker <-chan time.Time
ticker2 <-chan time.Time

VerboseCallbackHandler bool

Expand Down

0 comments on commit 67c1c92

Please sign in to comment.