mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
gtcp读取功能改进
This commit is contained in:
@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
"io"
|
||||
"bufio"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// 封装的链接对象
|
||||
@ -83,7 +84,10 @@ func (c *Conn) Receive(length int, retry...Retry) ([]byte, error) {
|
||||
} else {
|
||||
buffer = make([]byte, gDEFAULT_READ_BUFFER_SIZE)
|
||||
}
|
||||
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
fmt.Println(c.reader.Buffered())
|
||||
size, err = c.reader.Read(buffer[index:])
|
||||
if size > 0 {
|
||||
index += size
|
||||
@ -94,9 +98,9 @@ func (c *Conn) Receive(length int, retry...Retry) ([]byte, error) {
|
||||
}
|
||||
} else {
|
||||
// 否则读取所有缓冲区数据,直到没有可读数据为止
|
||||
if c.reader.Buffered() < 1 {
|
||||
break
|
||||
}
|
||||
//if c.reader.Buffered() < 1 {
|
||||
// break
|
||||
//}
|
||||
// 如果长度超过了自定义的读取缓冲区,那么自动增长
|
||||
if index >= gDEFAULT_READ_BUFFER_SIZE {
|
||||
buffer = append(buffer, make([]byte, gDEFAULT_READ_BUFFER_SIZE)...)
|
||||
|
||||
14
geg/net/gtcp/gtcp_func.go
Normal file
14
geg/net/gtcp/gtcp_func.go
Normal file
@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitee.com/johng/gf/g/net/gtcp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
data, err := gtcp.SendReceive("www.baidu.com:80", []byte("GET / HTTP/1.1\n\n"), -1)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(string(data))
|
||||
}
|
||||
@ -42,6 +42,4 @@ func main() {
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
select{}
|
||||
}
|
||||
Reference in New Issue
Block a user