From 0b332411b6e74f25c9eaa3e975c596a62c30a79a Mon Sep 17 00:00:00 2001 From: tj Date: Fri, 15 Oct 2010 12:44:14 +0200 Subject: [PATCH 1/5] Replace broken Makefile --- example/Makefile | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/example/Makefile b/example/Makefile index cdf104c..05650bc 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,26 +1,13 @@ -all: $(GOARCH) +include $(GOROOT)/src/Make.inc -clean: clean_$(GOARCH) - rm test +OBJS := $(patsubst %.go,%.$O,$(wildcard *.go)) +OUT := $(patsubst %.$O,%,$(OBJS)) -386: - 8g test.go - 8l -o test test.8 +all: $(OBJS) -amd64: - 6g test.go - 6l -o test test.6 - -arm: - 5g test.go - 5l -o test test.5 - -clean_amd64: - rm *.6 - -clean_386: - rm *.8 - -clean_arm: - rm *.5 +%.$O: %.go + $(GC) $< + $(LD) -o $(patsubst %.$O,%,$@) $@ +clean: + rm -f *.$O $(OBJS) $(OUT) From 6f170547d3a61730a6561690b1c8cae8f8b9bebf Mon Sep 17 00:00:00 2001 From: tj Date: Sat, 16 Oct 2010 20:43:59 +0200 Subject: [PATCH 2/5] Fix the pinger/ticker code a bit. Thanks soul9 ;) --- irc.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/irc.go b/irc.go index 9c5d260..ee44014 100644 --- a/irc.go +++ b/irc.go @@ -76,15 +76,17 @@ 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 * 4) //Every 4 minutes - i.ticker2 = time.Tick(1000 * 1000 * 1000 * 60 * 15) //Every 15 minutes + 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. for { select { case <-i.ticker: - if time.Seconds()-i.lastMessage > 60*4 { + //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())) } case <-i.ticker2: + //Ping every 15 minutes. i.SendRaw(fmt.Sprintf("PING %d", time.Nanoseconds())) } } From 8b8321be965831562a0eab30f350e55e7074381d Mon Sep 17 00:00:00 2001 From: tj Date: Mon, 18 Oct 2010 21:34:07 +0200 Subject: [PATCH 3/5] Check for closed channel in writer to avoid throw/crash --- irc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/irc.go b/irc.go index ee44014..75d7661 100644 --- a/irc.go +++ b/irc.go @@ -59,7 +59,7 @@ func reader(irc *IRCConnection) { } func writer(irc *IRCConnection) { - for !error { + for !error && ! closed(irc.pwrite) { b := []byte(<-irc.pwrite) if b == nil || irc.socket == nil { break From 79ac1741ead763666631fb38bd726eaa828769ed Mon Sep 17 00:00:00 2001 From: tj Date: Mon, 18 Oct 2010 21:46:34 +0200 Subject: [PATCH 4/5] Add irccon.Quit() and irccon.Cycle() Quit() Sends QUIT to server and exits the main loop. Cycle() Sends QUIT to server and reconnects. --- irc.go | 21 +++++++++++++++++++-- irc_struct.go | 20 +++++++++++--------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/irc.go b/irc.go index 75d7661..683d98b 100644 --- a/irc.go +++ b/irc.go @@ -59,7 +59,7 @@ func reader(irc *IRCConnection) { } func writer(irc *IRCConnection) { - for !error && ! closed(irc.pwrite) { + for !error && !closed(irc.pwrite) { b := []byte(<-irc.pwrite) if b == nil || irc.socket == nil { break @@ -92,6 +92,16 @@ func pinger(i *IRCConnection) { } } +func (irc *IRCConnection) Cycle() { + irc.SendRaw("QUIT") + irc.Reconnect() +} + +func (irc *IRCConnection) Quit() { + irc.quitting = true + irc.SendRaw("QUIT") +} + func (irc *IRCConnection) Join(channel string) { irc.pwrite <- fmt.Sprintf("JOIN %s\r\n", channel) } @@ -133,12 +143,19 @@ func (i *IRCConnection) Reconnect() os.Error { } func (i *IRCConnection) Loop() { - for { + for !i.quitting { e := <-i.Error + if i.quitting { + break + } fmt.Printf("Error: %s\n", e) error = true i.Reconnect() } + close(i.pwrite) + close(i.pread) + <-i.syncreader + <-i.syncwriter } func (i *IRCConnection) Connect(server string) os.Error { diff --git a/irc_struct.go b/irc_struct.go index e0b9a07..c9fd948 100644 --- a/irc_struct.go +++ b/irc_struct.go @@ -10,20 +10,22 @@ import ( ) type IRCConnection struct { - socket net.Conn - pread, pwrite chan string - Error chan os.Error + socket net.Conn + pread, pwrite chan string + Error chan os.Error syncreader, syncwriter chan bool - nick string - user string - registered bool - server string - Password string - events map[string][]func(*IRCEvent) + nick string + user string + registered bool + server string + Password string + events map[string][]func(*IRCEvent) lastMessage int64 ticker <-chan int64 ticker2 <-chan int64 + + quitting bool } type IRCEvent struct { From 105d6d61d353b1bb56c4cbad2aefd3e103206946 Mon Sep 17 00:00:00 2001 From: tj Date: Tue, 19 Oct 2010 14:12:17 +0200 Subject: [PATCH 5/5] Add part, patch by soul9 --- irc.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/irc.go b/irc.go index 683d98b..fe85763 100644 --- a/irc.go +++ b/irc.go @@ -106,6 +106,10 @@ func (irc *IRCConnection) Join(channel string) { irc.pwrite <- fmt.Sprintf("JOIN %s\r\n", channel) } +func (irc *IRCConnection) Part(channel string) { + irc.pwrite <- fmt.Sprintf("PART %s\r\n", channel) +} + func (irc *IRCConnection) Notice(target, message string) { irc.pwrite <- fmt.Sprintf("NOTICE %s :%s\r\n", target, message) }