mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
完善gtcp.Server示例,修改gtime.StrToTime调用端
This commit is contained in:
@ -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 {
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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
10
geg/net/gtcp/tcp_conn.go
Normal file
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"gitee.com/johng/gf/g/net/gtcp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gtcp.NewNetConn("127.0.0.1", )
|
||||
}
|
||||
@ -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()
|
||||
|
||||
Reference in New Issue
Block a user