Use append built-in instead of custom function

This commit is contained in:
tj 2010-11-19 19:26:27 +01:00
commit deae8abc23
4 changed files with 52 additions and 58 deletions

View File

@ -1,26 +1,13 @@
all: $(GOARCH) include $(GOROOT)/src/Make.inc
clean: clean_$(GOARCH) OBJS := $(patsubst %.go,%.$O,$(wildcard *.go))
rm test OUT := $(patsubst %.$O,%,$(OBJS))
386: all: $(OBJS)
8g test.go
8l -o test test.8
amd64: %.$O: %.go
6g test.go $(GC) $<
6l -o test test.6 $(LD) -o $(patsubst %.$O,%,$@) $@
arm:
5g test.go
5l -o test test.5
clean_amd64:
rm *.6
clean_386:
rm *.8
clean_arm:
rm *.5
clean:
rm -f *.$O $(OBJS) $(OUT)

33
irc.go
View File

@ -59,7 +59,7 @@ func reader(irc *IRCConnection) {
} }
func writer(irc *IRCConnection) { func writer(irc *IRCConnection) {
for !error { for !error && !closed(irc.pwrite) {
b := []byte(<-irc.pwrite) b := []byte(<-irc.pwrite)
if b == nil || irc.socket == nil { if b == nil || irc.socket == nil {
break break
@ -76,24 +76,40 @@ func writer(irc *IRCConnection) {
//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 pinger(i *IRCConnection) { func pinger(i *IRCConnection) {
i.ticker = time.Tick(1000 * 1000 * 1000 * 60 * 4) //Every 4 minutes i.ticker = time.Tick(1000 * 1000 * 1000 * 60 * 1) //Tick every minute.
i.ticker2 = time.Tick(1000 * 1000 * 1000 * 60 * 15) //Every 15 minutes i.ticker2 = time.Tick(1000 * 1000 * 1000 * 60 * 15) //Tick every 15 minutes.
for { for {
select { select {
case <-i.ticker: 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())) i.SendRaw(fmt.Sprintf("PING %d", time.Nanoseconds()))
} }
case <-i.ticker2: case <-i.ticker2:
//Ping every 15 minutes.
i.SendRaw(fmt.Sprintf("PING %d", time.Nanoseconds())) i.SendRaw(fmt.Sprintf("PING %d", time.Nanoseconds()))
} }
} }
} }
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) { func (irc *IRCConnection) Join(channel string) {
irc.pwrite <- fmt.Sprintf("JOIN %s\r\n", channel) 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) { func (irc *IRCConnection) Notice(target, message string) {
irc.pwrite <- fmt.Sprintf("NOTICE %s :%s\r\n", target, message) irc.pwrite <- fmt.Sprintf("NOTICE %s :%s\r\n", target, message)
} }
@ -131,12 +147,19 @@ func (i *IRCConnection) Reconnect() os.Error {
} }
func (i *IRCConnection) Loop() { func (i *IRCConnection) Loop() {
for { for !i.quitting {
e := <-i.Error e := <-i.Error
if i.quitting {
break
}
fmt.Printf("Error: %s\n", e) fmt.Printf("Error: %s\n", e)
error = true error = true
i.Reconnect() i.Reconnect()
} }
close(i.pwrite)
close(i.pread)
<-i.syncreader
<-i.syncwriter
} }
func (i *IRCConnection) Connect(server string) os.Error { func (i *IRCConnection) Connect(server string) os.Error {

View File

@ -7,30 +7,13 @@ import (
"strconv" "strconv"
) )
func AppendCallback(slice, data []func(*IRCEvent)) []func(*IRCEvent) {
l := len(slice)
if l+len(data) > cap(slice) {
newSlice := make([]func(*IRCEvent), (l+len(data))*2)
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0 : l+len(data)]
for i, c := range data {
slice[l+i] = c
}
return slice
}
func (irc *IRCConnection) AddCallback(eventcode string, callback func(*IRCEvent)) { func (irc *IRCConnection) AddCallback(eventcode string, callback func(*IRCEvent)) {
eventcode = strings.ToUpper(eventcode) eventcode = strings.ToUpper(eventcode)
if event, ok := irc.events[eventcode]; ok { if _, ok := irc.events[eventcode]; ok {
newevent := make([]func(*IRCEvent), 1) irc.events[eventcode] = append(irc.events[eventcode], callback)
newevent[0] = callback
irc.events[eventcode] = AppendCallback(event, newevent)
} else { } else {
event = make([]func(*IRCEvent), 1) irc.events[eventcode] = make([]func(*IRCEvent), 1)
event[0] = callback irc.events[eventcode][0] = callback
irc.events[eventcode] = event
} }
} }

View File

@ -19,12 +19,13 @@ type IRCConnection struct {
registered bool registered bool
server string server string
Password string Password string
events map[string][]func(*IRCEvent) events map[string][]func(*IRCEvent)
lastMessage int64 lastMessage int64
ticker <-chan int64 ticker <-chan int64
ticker2 <-chan int64 ticker2 <-chan int64
quitting bool
} }
type IRCEvent struct { type IRCEvent struct {