tweaked behavior after local connection read
This commit is contained in:
parent
fbed26d94b
commit
3848be53bd
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -100,11 +101,16 @@ func (h *WsHandler) writeRemote(conn *websocket.Conn) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *WsHandler) sendSpecial(header *packer.Header, service string) {
|
func (h *WsHandler) sendPackedMessage(header *packer.Header, data []byte, service string) {
|
||||||
p := packer.NewPacker(header)
|
p := packer.NewPacker(header)
|
||||||
p.SetService(service)
|
if len(data) > 0 {
|
||||||
|
p.Data.AppendBytes(data)
|
||||||
|
}
|
||||||
|
if service != "" {
|
||||||
|
p.SetService(service)
|
||||||
|
}
|
||||||
|
|
||||||
// Avoid blocking on the data channel if the websocket is already closed
|
// Avoid blocking on the data channel if the websocket closes or is already closed
|
||||||
select {
|
select {
|
||||||
case h.dataChan <- p:
|
case h.dataChan <- p:
|
||||||
case <-h.ctx.Done():
|
case <-h.ctx.Done():
|
||||||
|
@ -142,7 +148,7 @@ func (h *WsHandler) getLocalConn(p *packer.Packer) net.Conn {
|
||||||
func (h *WsHandler) writeLocal(p *packer.Packer) {
|
func (h *WsHandler) writeLocal(p *packer.Packer) {
|
||||||
conn := h.getLocalConn(p)
|
conn := h.getLocalConn(p)
|
||||||
if conn == nil {
|
if conn == nil {
|
||||||
h.sendSpecial(&p.Header, "error")
|
h.sendPackedMessage(&p.Header, nil, "error")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,7 +158,7 @@ func (h *WsHandler) writeLocal(p *packer.Packer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := conn.Write(p.Data.Data()); err != nil {
|
if _, err := conn.Write(p.Data.Data()); err != nil {
|
||||||
h.sendSpecial(&p.Header, "error")
|
h.sendPackedMessage(&p.Header, nil, "error")
|
||||||
loginfo.Println("failed to write to local connection", err)
|
loginfo.Println("failed to write to local connection", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -174,18 +180,16 @@ func (h *WsHandler) readLocal(key string, header *packer.Header) {
|
||||||
for {
|
for {
|
||||||
size, err := conn.Read(buf)
|
size, err := conn.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == io.EOF {
|
if err == io.EOF || strings.Contains(err.Error(), "use of closed network connection") {
|
||||||
h.sendSpecial(header, "end")
|
h.sendPackedMessage(header, nil, "end")
|
||||||
} else {
|
} else {
|
||||||
loginfo.Println("failed to read from local connection for", key, err)
|
loginfo.Println("failed to read from local connection for", key, err)
|
||||||
h.sendSpecial(header, "error")
|
h.sendPackedMessage(header, nil, "error")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
p := packer.NewPacker(header)
|
h.sendPackedMessage(header, buf[:size], "")
|
||||||
p.Data.AppendBytes(buf[:size])
|
|
||||||
h.dataChan <- p
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue