3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

keep track of whether sockets are closed

This commit is contained in:
Jeremy Latt 2014-04-15 09:07:25 -07:00
parent 6c10add399
commit 4ce4ba31c5
2 changed files with 17 additions and 5 deletions

View File

@ -264,11 +264,8 @@ func (client *Client) ChangeNickname(nickname Name) {
} }
} }
func (client *Client) Reply(reply string, args ...interface{}) { func (client *Client) Reply(reply string) error {
if len(args) > 0 { return client.socket.Write(reply)
reply = fmt.Sprintf(reply, args...)
}
client.socket.Write(reply)
} }
func (client *Client) Quit(message Text) { func (client *Client) Quit(message Text) {

View File

@ -12,6 +12,7 @@ const (
) )
type Socket struct { type Socket struct {
closed bool
conn net.Conn conn net.Conn
scanner *bufio.Scanner scanner *bufio.Scanner
writer *bufio.Writer writer *bufio.Writer
@ -30,11 +31,20 @@ func (socket *Socket) String() string {
} }
func (socket *Socket) Close() { func (socket *Socket) Close() {
if socket.closed {
return
}
socket.closed = true
socket.conn.Close() socket.conn.Close()
Log.debug.Printf("%s closed", socket) Log.debug.Printf("%s closed", socket)
} }
func (socket *Socket) Read() (line string, err error) { func (socket *Socket) Read() (line string, err error) {
if socket.closed {
err = io.EOF
return
}
for socket.scanner.Scan() { for socket.scanner.Scan() {
line = socket.scanner.Text() line = socket.scanner.Text()
if len(line) == 0 { if len(line) == 0 {
@ -53,6 +63,11 @@ func (socket *Socket) Read() (line string, err error) {
} }
func (socket *Socket) Write(line string) (err error) { func (socket *Socket) Write(line string) (err error) {
if socket.closed {
err = io.EOF
return
}
if _, err = socket.writer.WriteString(line); socket.isError(err, W) { if _, err = socket.writer.WriteString(line); socket.isError(err, W) {
return return
} }