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:
parent
95f267ea4f
commit
6265b70622
@ -59,7 +59,11 @@ func NewClient(server *Server, conn net.Conn) *Client {
|
||||
//
|
||||
|
||||
func (client *Client) readCommands() {
|
||||
for line := range client.socket.Read() {
|
||||
for {
|
||||
line, err := client.socket.Read()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
msg, err := ParseCommand(line)
|
||||
if err != nil {
|
||||
switch err {
|
||||
|
@ -13,27 +13,19 @@ const (
|
||||
)
|
||||
|
||||
type Socket struct {
|
||||
closed bool
|
||||
conn net.Conn
|
||||
done chan bool
|
||||
reader *bufio.Reader
|
||||
receive chan string
|
||||
send chan string
|
||||
writer *bufio.Writer
|
||||
}
|
||||
|
||||
func NewSocket(conn net.Conn) *Socket {
|
||||
socket := &Socket{
|
||||
conn: conn,
|
||||
done: make(chan bool, 1),
|
||||
reader: bufio.NewReader(conn),
|
||||
receive: make(chan string, 16),
|
||||
send: make(chan string, 16),
|
||||
writer: bufio.NewWriter(conn),
|
||||
}
|
||||
|
||||
go socket.readLines()
|
||||
go socket.writeLines()
|
||||
|
||||
return socket
|
||||
}
|
||||
|
||||
@ -42,23 +34,19 @@ func (socket *Socket) String() string {
|
||||
}
|
||||
|
||||
func (socket *Socket) Close() {
|
||||
socket.done <- true
|
||||
}
|
||||
|
||||
func (socket *Socket) Read() <-chan string {
|
||||
return socket.receive
|
||||
}
|
||||
|
||||
func (socket *Socket) Write(lines ...string) {
|
||||
for _, line := range lines {
|
||||
socket.send <- line
|
||||
}
|
||||
if socket.closed {
|
||||
return
|
||||
}
|
||||
socket.closed = true
|
||||
socket.conn.Close()
|
||||
if DEBUG_NET {
|
||||
log.Printf("%s closed", socket)
|
||||
}
|
||||
}
|
||||
|
||||
func (socket *Socket) readLines() {
|
||||
for {
|
||||
line, err := socket.reader.ReadString('\n')
|
||||
func (socket *Socket) Read() (line string, err error) {
|
||||
for len(line) == 0 {
|
||||
line, err = socket.reader.ReadString('\n')
|
||||
if socket.isError(err, R) {
|
||||
break
|
||||
}
|
||||
@ -70,53 +58,32 @@ func (socket *Socket) readLines() {
|
||||
if DEBUG_NET {
|
||||
log.Printf("%s → %s", socket, line)
|
||||
}
|
||||
|
||||
socket.receive <- line
|
||||
}
|
||||
|
||||
close(socket.receive)
|
||||
socket.Close()
|
||||
return
|
||||
}
|
||||
|
||||
func (socket *Socket) writeLines() {
|
||||
done := false
|
||||
for !done {
|
||||
select {
|
||||
case line := <-socket.send:
|
||||
if _, err := socket.writer.WriteString(line); socket.isError(err, W) {
|
||||
func (socket *Socket) Write(lines ...string) (err error) {
|
||||
for _, line := range lines {
|
||||
err = socket.WriteLine(line)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err := socket.writer.Flush(); socket.isError(err, W) {
|
||||
break
|
||||
func (socket *Socket) WriteLine(line string) (err error) {
|
||||
if _, err = socket.writer.WriteString(line); socket.isError(err, W) {
|
||||
return
|
||||
}
|
||||
|
||||
if err = socket.writer.Flush(); socket.isError(err, W) {
|
||||
return
|
||||
}
|
||||
if DEBUG_NET {
|
||||
log.Printf("%s ← %s", socket, line)
|
||||
}
|
||||
|
||||
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:
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (socket *Socket) isError(err error, dir rune) bool {
|
||||
|
Loading…
Reference in New Issue
Block a user