完善gtcp.Server示例,修改gtime.StrToTime调用端

This commit is contained in:
John
2018-07-11 21:21:30 +08:00
parent 77076961bb
commit 9eab2ea49d
6 changed files with 19 additions and 13 deletions

View File

@ -10,7 +10,6 @@ import (
"io"
"net"
"time"
"fmt"
"bytes"
)
@ -40,9 +39,8 @@ func Checksum(buffer []byte) uint32 {
return checksum
}
// 创建原生TCP链接
func NewNetConn(ip string, port int, timeout...int) (net.Conn, error) {
addr := fmt.Sprintf("%s:%d", ip, port)
// 创建原生TCP链接, addr地址格式形如127.0.0.1:80
func NewNetConn(addr string, timeout...int) (net.Conn, error) {
if len(timeout) > 0 {
return net.DialTimeout("tcp", addr, time.Duration(timeout[0]) * time.Millisecond)
} else {

View File

@ -8,7 +8,6 @@ package gtcp
import (
"net"
"fmt"
"time"
"gitee.com/johng/gf/g/container/gmap"
"gitee.com/johng/gf/g/container/gpool"
@ -31,16 +30,15 @@ var (
)
// 创建TCP链接
func NewConn(ip string, port int, timeout...int) (*Conn, error) {
func NewConn(addr string, timeout...int) (*Conn, error) {
var pool *gpool.Pool
addr := fmt.Sprintf("%s:%d", ip, port)
if v := pools.Get(addr); v == nil {
pools.LockFunc(func(m map[string]interface{}) {
if v, ok := m[addr]; ok {
pool = v.(*gpool.Pool)
} else {
pool = gpool.New(gDEFAULT_POOL_EXPIRE, func() (interface{}, error) {
if conn, err := NewNetConn(ip, port, timeout...); err == nil {
if conn, err := NewNetConn(addr, timeout...); err == nil {
return &Conn {
Conn : conn,
pool : pool,

View File

@ -53,7 +53,7 @@ func Time(i interface{}, format...string) time.Time {
s := String(i)
// 优先使用用户输入日期格式进行转换
if len(format) > 0 {
t, _ := gtime.StrToTime(s, format[0])
t, _ := gtime.StrToTimeFormat(s, format[0])
return t
}
t := int64(0)

View File

@ -509,7 +509,7 @@ func Check(val interface{}, rules string, msgs interface{}, params...map[string]
// 日期格式,需要给定日期格式
case "date-format":
if _, err := gtime.StrToTime(value, ruleVal); err == nil {
if _, err := gtime.StrToTimeFormat(value, ruleVal); err == nil {
match = true
}

10
geg/net/gtcp/tcp_conn.go Normal file
View File

@ -0,0 +1,10 @@
package main
import (
"net"
"gitee.com/johng/gf/g/net/gtcp"
)
func main() {
gtcp.NewNetConn("127.0.0.1", )
}

View File

@ -7,10 +7,10 @@ import (
func main() {
gtcp.NewServer(":8999", func(conn net.Conn) {
defer conn.Close()
for {
buffer := make([]byte, 1024)
if length, err := conn.Read(buffer); err == nil {
conn.Write(append([]byte("> "), buffer[0 : length]...))
if data, err := gtcp.Receive(conn); err == nil {
gtcp.Send(conn, append([]byte("> "), data...))
}
}
}).Run()