3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 11:59:40 +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{}) {
if len(args) > 0 {
reply = fmt.Sprintf(reply, args...)
}
client.socket.Write(reply)
func (client *Client) Reply(reply string) error {
return client.socket.Write(reply)
}
func (client *Client) Quit(message Text) {

View File

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