gtcp读取功能改进

This commit is contained in:
John
2018-07-20 18:14:21 +08:00
parent a5b1f7d39b
commit a03fa624f7
3 changed files with 21 additions and 5 deletions

View File

@ -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
View 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))
}

View File

@ -42,6 +42,4 @@ func main() {
}
time.Sleep(time.Second)
}
select{}
}