mirror of
https://github.com/gissleh/irc.git
synced 2026-10-17 00:52:54 +00:00
fixed client.HasQuit and made internal disconnects not setting that flag.
This commit is contained in:
@@ -187,12 +187,13 @@ func (client *Client) Ready() bool {
|
|||||||
return client.ready
|
return client.ready
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasQuit returns true if the client had manually quit.
|
// HasQuit returns true if the client had manually quit. It should be checked before
|
||||||
|
// performing any reconnection logic.
|
||||||
func (client *Client) HasQuit() bool {
|
func (client *Client) HasQuit() bool {
|
||||||
client.mutex.RLock()
|
client.mutex.RLock()
|
||||||
defer client.mutex.RUnlock()
|
defer client.mutex.RUnlock()
|
||||||
|
|
||||||
return client.ready
|
return client.quit
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) State() ClientState {
|
func (client *Client) State() ClientState {
|
||||||
@@ -234,7 +235,7 @@ func (client *Client) Connect(addr string, ssl bool) (err error) {
|
|||||||
var conn net.Conn
|
var conn net.Conn
|
||||||
|
|
||||||
if client.Connected() {
|
if client.Connected() {
|
||||||
_ = client.Disconnect()
|
_ = client.Disconnect(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
client.isupport.Reset()
|
client.isupport.Reset()
|
||||||
@@ -306,17 +307,21 @@ func (client *Client) Connect(addr string, ssl bool) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Disconnect disconnects from the server. It will either return the
|
// Disconnect disconnects from the server. It will either return the
|
||||||
// close error, or ErrNoConnection if there is no connection
|
// close error, or ErrNoConnection if there is no connection. If
|
||||||
func (client *Client) Disconnect() error {
|
// markAsQuit is specified, HasQuit will return true until the next
|
||||||
|
// connections.
|
||||||
|
func (client *Client) Disconnect(markAsQuit bool) error {
|
||||||
client.mutex.Lock()
|
client.mutex.Lock()
|
||||||
defer client.mutex.Unlock()
|
defer client.mutex.Unlock()
|
||||||
|
|
||||||
|
if markAsQuit {
|
||||||
|
client.quit = true
|
||||||
|
}
|
||||||
|
|
||||||
if client.conn == nil {
|
if client.conn == nil {
|
||||||
return ErrNoConnection
|
return ErrNoConnection
|
||||||
}
|
}
|
||||||
|
|
||||||
client.quit = true
|
|
||||||
|
|
||||||
return client.conn.Close()
|
return client.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -348,7 +353,7 @@ func (client *Client) Send(line string) error {
|
|||||||
_, err := conn.Write([]byte(line))
|
_, err := conn.Write([]byte(line))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
client.EmitNonBlocking(NewErrorEvent("write", err.Error()))
|
client.EmitNonBlocking(NewErrorEvent("write", err.Error()))
|
||||||
_ = client.Disconnect()
|
_ = client.Disconnect(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
@@ -521,9 +526,12 @@ func (client *Client) SetValue(key string, value interface{}) {
|
|||||||
// Destroy destroys the client, which will lead to a disconnect. Cancelling the
|
// Destroy destroys the client, which will lead to a disconnect. Cancelling the
|
||||||
// parent context will do the same.
|
// parent context will do the same.
|
||||||
func (client *Client) Destroy() {
|
func (client *Client) Destroy() {
|
||||||
_ = client.Disconnect()
|
_ = client.Disconnect(false)
|
||||||
client.cancel()
|
client.cancel()
|
||||||
close(client.sends)
|
close(client.sends)
|
||||||
|
|
||||||
|
client.Emit(NewEvent("client", "destroy"))
|
||||||
|
|
||||||
close(client.events)
|
close(client.events)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -781,14 +789,7 @@ end:
|
|||||||
|
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
|
|
||||||
_ = client.Disconnect()
|
_ = client.Disconnect(false)
|
||||||
|
|
||||||
event := NewEvent("client", "destroy")
|
|
||||||
event.ctx, event.cancel = context.WithCancel(client.ctx)
|
|
||||||
|
|
||||||
client.handleEvent(&event)
|
|
||||||
|
|
||||||
event.cancel()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) handleSendLoop() {
|
func (client *Client) handleSendLoop() {
|
||||||
|
|||||||
+1
-1
@@ -355,7 +355,7 @@ func TestClient(t *testing.T) {
|
|||||||
t.Fatal("Listen:", err)
|
t.Fatal("Listen:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := client.Disconnect(); err != irc.ErrNoConnection {
|
if err := client.Disconnect(false); err != irc.ErrNoConnection {
|
||||||
t.Errorf("It should fail to disconnect, got: %s", err)
|
t.Errorf("It should fail to disconnect, got: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ import (
|
|||||||
"github.com/gissleh/irc/handlers"
|
"github.com/gissleh/irc/handlers"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/gissleh/irc"
|
"github.com/gissleh/irc"
|
||||||
)
|
)
|
||||||
@@ -95,6 +97,10 @@ func main() {
|
|||||||
target = client.Target(event.Arg(1), event.Arg(2))
|
target = client.Target(event.Arg(1), event.Arg(2))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if event.Name() == "client.disconnect" {
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
j, err := json.MarshalIndent(event, "", " ")
|
j, err := json.MarshalIndent(event, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -103,6 +109,15 @@ func main() {
|
|||||||
fmt.Println(string(j))
|
fmt.Println(string(j))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
exitSignal := make(chan os.Signal)
|
||||||
|
signal.Notify(exitSignal, os.Interrupt, os.Kill, syscall.SIGTERM)
|
||||||
|
|
||||||
|
<-exitSignal
|
||||||
|
|
||||||
|
client.Quit("Goodnight.")
|
||||||
|
}()
|
||||||
|
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
for {
|
for {
|
||||||
line, err := reader.ReadString('\n')
|
line, err := reader.ReadString('\n')
|
||||||
|
|||||||
Reference in New Issue
Block a user