3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00

get rid of goroutines on client socket

This commit is contained in:
Jeremy Latt 2014-02-19 18:13:35 -08:00
parent 95f267ea4f
commit 6265b70622
2 changed files with 38 additions and 67 deletions

View File

@ -59,7 +59,11 @@ func NewClient(server *Server, conn net.Conn) *Client {
// //
func (client *Client) readCommands() { func (client *Client) readCommands() {
for line := range client.socket.Read() { for {
line, err := client.socket.Read()
if err != nil {
break
}
msg, err := ParseCommand(line) msg, err := ParseCommand(line)
if err != nil { if err != nil {
switch err { switch err {

View File

@ -13,27 +13,19 @@ const (
) )
type Socket struct { type Socket struct {
closed bool
conn net.Conn conn net.Conn
done chan bool
reader *bufio.Reader reader *bufio.Reader
receive chan string
send chan string
writer *bufio.Writer writer *bufio.Writer
} }
func NewSocket(conn net.Conn) *Socket { func NewSocket(conn net.Conn) *Socket {
socket := &Socket{ socket := &Socket{
conn: conn, conn: conn,
done: make(chan bool, 1),
reader: bufio.NewReader(conn), reader: bufio.NewReader(conn),
receive: make(chan string, 16),
send: make(chan string, 16),
writer: bufio.NewWriter(conn), writer: bufio.NewWriter(conn),
} }
go socket.readLines()
go socket.writeLines()
return socket return socket
} }
@ -42,23 +34,19 @@ func (socket *Socket) String() string {
} }
func (socket *Socket) Close() { func (socket *Socket) Close() {
socket.done <- true if socket.closed {
}
func (socket *Socket) Read() <-chan string {
return socket.receive
}
func (socket *Socket) Write(lines ...string) {
for _, line := range lines {
socket.send <- line
}
return return
}
socket.closed = true
socket.conn.Close()
if DEBUG_NET {
log.Printf("%s closed", socket)
}
} }
func (socket *Socket) readLines() { func (socket *Socket) Read() (line string, err error) {
for { for len(line) == 0 {
line, err := socket.reader.ReadString('\n') line, err = socket.reader.ReadString('\n')
if socket.isError(err, R) { if socket.isError(err, R) {
break break
} }
@ -70,53 +58,32 @@ func (socket *Socket) readLines() {
if DEBUG_NET { if DEBUG_NET {
log.Printf("%s → %s", socket, line) log.Printf("%s → %s", socket, line)
} }
socket.receive <- line
} }
return
close(socket.receive)
socket.Close()
} }
func (socket *Socket) writeLines() { func (socket *Socket) Write(lines ...string) (err error) {
done := false for _, line := range lines {
for !done { err = socket.WriteLine(line)
select { if err != nil {
case line := <-socket.send:
if _, err := socket.writer.WriteString(line); socket.isError(err, W) {
break break
} }
}
return
}
if err := socket.writer.Flush(); socket.isError(err, W) { func (socket *Socket) WriteLine(line string) (err error) {
break if _, err = socket.writer.WriteString(line); socket.isError(err, W) {
return
}
if err = socket.writer.Flush(); socket.isError(err, W) {
return
} }
if DEBUG_NET { if DEBUG_NET {
log.Printf("%s ← %s", socket, line) log.Printf("%s ← %s", socket, line)
} }
return
case done = <-socket.done:
if DEBUG_NET {
log.Printf("%s done", socket)
}
continue
}
}
if done {
socket.conn.Close()
}
if DEBUG_NET {
log.Printf("%s closed", socket)
}
// read incoming messages and discard to avoid hangs
for {
select {
case <-socket.send:
case <-socket.done:
}
}
} }
func (socket *Socket) isError(err error, dir rune) bool { func (socket *Socket) isError(err error, dir rune) bool {