xP: use buffered reads
This commit is contained in:
parent
c1d2e38840
commit
92f2f6895b
10
xP/xP.go
10
xP/xP.go
@ -4,6 +4,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -27,14 +28,14 @@ var (
|
|||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
func relayReadJSON(conn net.Conn) []byte {
|
func relayReadJSON(r io.Reader) []byte {
|
||||||
var length uint32
|
var length uint32
|
||||||
if err := binary.Read(conn, binary.BigEndian, &length); err != nil {
|
if err := binary.Read(r, binary.BigEndian, &length); err != nil {
|
||||||
log.Println("Event receive failed: " + err.Error())
|
log.Println("Event receive failed: " + err.Error())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
b := make([]byte, length)
|
b := make([]byte, length)
|
||||||
if _, err := io.ReadFull(conn, b); err != nil {
|
if _, err := io.ReadFull(r, b); err != nil {
|
||||||
log.Println("Event receive failed: " + err.Error())
|
log.Println("Event receive failed: " + err.Error())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -60,10 +61,11 @@ func relayReadJSON(conn net.Conn) []byte {
|
|||||||
|
|
||||||
func relayMakeReceiver(ctx context.Context, conn net.Conn) <-chan []byte {
|
func relayMakeReceiver(ctx context.Context, conn net.Conn) <-chan []byte {
|
||||||
p := make(chan []byte, 1)
|
p := make(chan []byte, 1)
|
||||||
|
r := bufio.NewReader(conn)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(p)
|
defer close(p)
|
||||||
for {
|
for {
|
||||||
j := relayReadJSON(conn)
|
j := relayReadJSON(r)
|
||||||
if j == nil {
|
if j == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user