Цей коміт міститься в:
tj
2010-09-29 09:00:16 +02:00
джерело 998e1c3050 3d0ffc5d06
коміт c64043c75b
4 змінених файлів з 19 додано та 8 видалено
+1 -1
Переглянути файл
@@ -1,4 +1,4 @@
include $(GOROOT)/src/Make.$(GOARCH) include $(GOROOT)/src/Make.inc
TARG=irc TARG=irc
GOFILES=irc.go irc_struct.go irc_callback.go GOFILES=irc.go irc_struct.go irc_callback.go
+16 -5
Переглянути файл
@@ -17,14 +17,16 @@ const (
VERSION = "GolangBOT v1.0" VERSION = "GolangBOT v1.0"
) )
var error bool
func reader(irc *IRCConnection) { func reader(irc *IRCConnection) {
br := bufio.NewReader(irc.socket) br := bufio.NewReader(irc.socket)
for { for !error {
msg, err := br.ReadString('\n') msg, err := br.ReadString('\n')
if err != nil { if err != nil {
irc.Error <- err irc.Error <- err
return break
} }
irc.lastMessage = time.Seconds() irc.lastMessage = time.Seconds()
msg = msg[0 : len(msg)-2] //Remove \r\n msg = msg[0 : len(msg)-2] //Remove \r\n
@@ -53,10 +55,11 @@ func reader(irc *IRCConnection) {
} }
irc.RunCallbacks(event) irc.RunCallbacks(event)
} }
irc.syncreader <- true
} }
func writer(irc *IRCConnection) { func writer(irc *IRCConnection) {
for { for !error {
b := []byte(<-irc.pwrite) b := []byte(<-irc.pwrite)
if b == nil || irc.socket == nil { if b == nil || irc.socket == nil {
return return
@@ -65,9 +68,10 @@ func writer(irc *IRCConnection) {
if err != nil { if err != nil {
fmt.Printf("%s\n", err) fmt.Printf("%s\n", err)
irc.Error <- err irc.Error <- err
return break
} }
} }
irc.syncwriter <- true
} }
//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
@@ -104,6 +108,8 @@ func (irc *IRCConnection) SendRaw(message string) {
} }
func (i *IRCConnection) Reconnect() os.Error { func (i *IRCConnection) Reconnect() os.Error {
<-i.syncreader
<-i.syncwriter
for { for {
fmt.Printf("Reconnecting to %s\n", i.server) fmt.Printf("Reconnecting to %s\n", i.server)
var err os.Error var err os.Error
@@ -113,6 +119,7 @@ func (i *IRCConnection) Reconnect() os.Error {
} }
fmt.Printf("Error: %s\n", err) fmt.Printf("Error: %s\n", err)
} }
error = false
fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr()) fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr())
go reader(i) go reader(i)
go writer(i) go writer(i)
@@ -123,7 +130,9 @@ func (i *IRCConnection) Reconnect() os.Error {
func (i *IRCConnection) Loop() { func (i *IRCConnection) Loop() {
for { for {
<-i.Error e := <-i.Error
fmt.Printf("Error: %s\n", e)
error = true
i.Reconnect() i.Reconnect()
} }
} }
@@ -140,6 +149,8 @@ func (i *IRCConnection) Connect(server string) os.Error {
i.pread = make(chan string, 100) i.pread = make(chan string, 100)
i.pwrite = make(chan string, 100) i.pwrite = make(chan string, 100)
i.Error = make(chan os.Error, 10) i.Error = make(chan os.Error, 10)
i.syncreader = make(chan bool)
i.syncwriter = make(chan bool)
go reader(i) go reader(i)
go writer(i) go writer(i)
go pinger(i) go pinger(i)
+1 -1
Переглянути файл
@@ -79,7 +79,7 @@ func (irc *IRCConnection) setupCallbacks() {
}) })
irc.AddCallback("CTCP_TIME", func(e *IRCEvent) { irc.AddCallback("CTCP_TIME", func(e *IRCEvent) {
ltime := time.LocalTime(); ltime := time.LocalTime()
irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01TIME %s\x01", e.Nick, ltime.String())) irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01TIME %s\x01", e.Nick, ltime.String()))
}) })
+1 -1
Переглянути файл
@@ -13,12 +13,12 @@ type IRCConnection struct {
socket net.Conn socket net.Conn
pread, pwrite chan string pread, pwrite chan string
Error chan os.Error Error chan os.Error
syncreader, syncwriter chan bool
nick string nick string
user string user string
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