This commit is contained in:
jerry901 2022-05-12 20:05:53 +09:00
parent f9f1f00404
commit 8ef36894b7

View File

@ -1,7 +1,7 @@
package net package net
import ( import (
"errors" "io"
"net" "net"
"time" "time"
@ -70,27 +70,45 @@ func (conn *Conn) WriteChunks(c [][]byte) (n int, err error) {
} }
func (conn *Conn) ReadBytes() ([]byte, error) { func (conn *Conn) ReadBytes() ([]byte, error) {
ret := make([]byte, 0) // ret := make([]byte, 0)
buf := make([]byte, BUF_SIZE) // buf := make([]byte, BUF_SIZE)
for { // for {
n, err := conn.Read(buf) // n, err := conn.Read(buf)
// if err != nil {
// switch err.(type) {
// case *net.OpError:
// return nil, errors.New("timed out")
// default:
// return nil, err
// }
// }
// ret = append(ret, buf[:n]...)
// if n == 0 {
// return nil, io.EOF
// }
// if n < BUF_SIZE {
// break
// }
// }
buf := make([]byte, 0, 4096) // big buffer
tmp := make([]byte, 256) // using small tmo buffer for demonstrating
for {
n, err := conn.Read(tmp)
if err != nil { if err != nil {
switch err.(type) { if err != io.EOF {
case *net.OpError:
return nil, errors.New("timed out")
default:
return nil, err return nil, err
} }
}
ret = append(ret, buf[:n]...)
if n < BUF_SIZE {
break break
} }
}
return ret, nil buf = append(buf, tmp[:n]...)
}
return buf, nil
} }
func (lConn *Conn) HandleHttp(p *packet.HttpPacket) { func (lConn *Conn) HandleHttp(p *packet.HttpPacket) {