Merge pull request #38 from ecnahc515/custom_handling_disconnect
Custom handling disconnect
This commit is contained in:
commit
33fb824b4f
24
irc.go
24
irc.go
@ -35,11 +35,15 @@ const (
|
|||||||
VERSION = "go-ircevent v2.1"
|
VERSION = "go-ircevent v2.1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrDisconnected = errors.New("Disconnect Called")
|
||||||
|
|
||||||
// Read data from a connection. To be used as a goroutine.
|
// Read data from a connection. To be used as a goroutine.
|
||||||
func (irc *Connection) readLoop() {
|
func (irc *Connection) readLoop() {
|
||||||
defer irc.Done()
|
defer irc.Done()
|
||||||
br := bufio.NewReaderSize(irc.socket, 512)
|
br := bufio.NewReaderSize(irc.socket, 512)
|
||||||
|
|
||||||
|
errChan := irc.ErrorChan()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-irc.end:
|
case <-irc.end:
|
||||||
@ -61,7 +65,7 @@ func (irc *Connection) readLoop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
irc.Error <- err
|
errChan <- err
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,6 +107,7 @@ func (irc *Connection) readLoop() {
|
|||||||
// Loop to write to a connection. To be used as a goroutine.
|
// Loop to write to a connection. To be used as a goroutine.
|
||||||
func (irc *Connection) writeLoop() {
|
func (irc *Connection) writeLoop() {
|
||||||
defer irc.Done()
|
defer irc.Done()
|
||||||
|
errChan := irc.ErrorChan()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-irc.end:
|
case <-irc.end:
|
||||||
@ -127,7 +132,7 @@ func (irc *Connection) writeLoop() {
|
|||||||
irc.socket.SetWriteDeadline(zero)
|
irc.socket.SetWriteDeadline(zero)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
irc.Error <- err
|
errChan <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -166,8 +171,9 @@ func (irc *Connection) pingLoop() {
|
|||||||
|
|
||||||
// Main loop to control the connection.
|
// Main loop to control the connection.
|
||||||
func (irc *Connection) Loop() {
|
func (irc *Connection) Loop() {
|
||||||
|
errChan := irc.ErrorChan()
|
||||||
for !irc.stopped {
|
for !irc.stopped {
|
||||||
err := <-irc.Error
|
err := <-errChan
|
||||||
if irc.stopped {
|
if irc.stopped {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -276,12 +282,19 @@ func (irc *Connection) Mode(target string, modestring ...string) {
|
|||||||
irc.SendRawf("MODE %s", target)
|
irc.SendRawf("MODE %s", target)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (irc *Connection) ErrorChan() chan error {
|
||||||
|
return irc.Error
|
||||||
|
}
|
||||||
|
|
||||||
// A disconnect sends all buffered messages (if possible),
|
// A disconnect sends all buffered messages (if possible),
|
||||||
// stops all goroutines and then closes the socket.
|
// stops all goroutines and then closes the socket.
|
||||||
func (irc *Connection) Disconnect() {
|
func (irc *Connection) Disconnect() {
|
||||||
|
for event := range irc.events {
|
||||||
|
irc.ClearCallback(event)
|
||||||
|
}
|
||||||
|
|
||||||
close(irc.end)
|
close(irc.end)
|
||||||
close(irc.pwrite)
|
close(irc.pwrite)
|
||||||
close(irc.pread)
|
|
||||||
|
|
||||||
irc.Wait()
|
irc.Wait()
|
||||||
irc.socket.Close()
|
irc.socket.Close()
|
||||||
@ -290,7 +303,7 @@ func (irc *Connection) Disconnect() {
|
|||||||
irc.netsock.Close()
|
irc.netsock.Close()
|
||||||
irc.netsock = nil
|
irc.netsock = nil
|
||||||
}
|
}
|
||||||
irc.Error <- errors.New("Disconnect Called")
|
irc.ErrorChan() <- ErrDisconnected
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reconnect to a server using the current connection.
|
// Reconnect to a server using the current connection.
|
||||||
@ -350,7 +363,6 @@ func (irc *Connection) Connect(server string) error {
|
|||||||
}
|
}
|
||||||
irc.Log.Printf("Connected to %s (%s)\n", irc.server, irc.socket.RemoteAddr())
|
irc.Log.Printf("Connected to %s (%s)\n", irc.server, irc.socket.RemoteAddr())
|
||||||
|
|
||||||
irc.pread = make(chan string, 10)
|
|
||||||
irc.pwrite = make(chan string, 10)
|
irc.pwrite = make(chan string, 10)
|
||||||
irc.Error = make(chan error, 2)
|
irc.Error = make(chan error, 2)
|
||||||
irc.Add(3)
|
irc.Add(3)
|
||||||
|
@ -26,7 +26,7 @@ type Connection struct {
|
|||||||
|
|
||||||
socket net.Conn
|
socket net.Conn
|
||||||
netsock net.Conn
|
netsock net.Conn
|
||||||
pread, pwrite chan string
|
pwrite chan string
|
||||||
end chan struct{}
|
end chan struct{}
|
||||||
|
|
||||||
nick string //The nickname we want.
|
nick string //The nickname we want.
|
||||||
|
Loading…
Reference in New Issue
Block a user