Use append built-in instead of custom function
This commit is contained in:
commit
deae8abc23
@ -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)
|
||||
|
33
irc.go
33
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
|
||||
@ -76,24 +76,40 @@ 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()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
@ -131,12 +147,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 {
|
||||
|
@ -7,30 +7,13 @@ import (
|
||||
"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)) {
|
||||
eventcode = strings.ToUpper(eventcode)
|
||||
if event, ok := irc.events[eventcode]; ok {
|
||||
newevent := make([]func(*IRCEvent), 1)
|
||||
newevent[0] = callback
|
||||
irc.events[eventcode] = AppendCallback(event, newevent)
|
||||
if _, ok := irc.events[eventcode]; ok {
|
||||
irc.events[eventcode] = append(irc.events[eventcode], callback)
|
||||
} else {
|
||||
event = make([]func(*IRCEvent), 1)
|
||||
event[0] = callback
|
||||
irc.events[eventcode] = event
|
||||
irc.events[eventcode] = make([]func(*IRCEvent), 1)
|
||||
irc.events[eventcode][0] = callback
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,21 +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 {
|
||||
|
Loading…
Reference in New Issue
Block a user