1
0
Fork 0
forked from External/ergo

idle/quit timeout

This commit is contained in:
Jeremy Latt 2014-02-09 12:13:09 -08:00
parent 8a90634c0a
commit 133b91b9f0
4 changed files with 47 additions and 8 deletions

View file

@ -8,7 +8,6 @@ import (
)
type Client struct {
atime time.Time
away bool
channels ChannelSet
conn net.Conn
@ -22,6 +21,8 @@ type Client struct {
server *Server
serverPass bool
username string
idleTimer *time.Timer
quitTimer *time.Timer
}
func NewClient(server *Server, conn net.Conn) *Client {
@ -40,9 +41,38 @@ func NewClient(server *Server, conn net.Conn) *Client {
go client.readConn(read)
go client.writeConn(write, replies)
client.Touch()
return client
}
func (client *Client) Touch() {
if client.quitTimer != nil {
client.quitTimer.Stop()
}
if client.idleTimer == nil {
client.idleTimer = time.AfterFunc(IDLE_TIMEOUT, client.Idle)
} else {
client.idleTimer.Reset(IDLE_TIMEOUT)
}
}
func (client *Client) Idle() {
if client.quitTimer == nil {
client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.Quit)
} else {
client.quitTimer.Reset(QUIT_TIMEOUT)
}
client.Reply(RplPing(client.server, client))
}
func (client *Client) Quit() {
msg := &QuitCommand{
message: "connection timeout",
}
msg.SetClient(client)
client.server.commands <- msg
}
func (c *Client) readConn(recv <-chan string) {
for str := range recv {
m, err := ParseCommand(str)