git-svn-id: svn://47.119.165.148/zhub@63 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
lxy
2021-01-09 10:53:35 +00:00
parent 2dcfc5e9bf
commit f1b7a862f9
15 changed files with 377 additions and 50 deletions

View File

@@ -1,4 +1,4 @@
package zdb
package _zdb
import (
"log"
@@ -9,10 +9,10 @@ import (
"time"
)
func execCmd(rcmd []string, conn net.Conn) {
func ExecCmd(rcmd []string, conn net.Conn) {
defer func() {
if r := recover(); r != nil {
log.Println("execCmd Recovered:", r)
log.Println("ExecCmd Recovered:", r)
}
}()
if len(rcmd) == 0 {
@@ -58,7 +58,7 @@ func execCmd(rcmd []string, conn net.Conn) {
case "daly":
daly(rcmd, conn)
case "timer":
Timer(rcmd, conn)
timer(rcmd, conn)
default:
conn.Write([]byte("-Error: default not supported:[" + strings.Join(rcmd, " ") + "]\r\n"))
return

View File

@@ -1,4 +1,4 @@
package zdb
package _zdb
import (
"bufio"
@@ -16,7 +16,7 @@ var (
zsub = make(map[string][]*ConnContext) // topic -- connx[]
retOk = []byte("+OK")
retHelp = []byte(
"\n--- zdb help ---\n" +
"\n--- _zdb help ---\n" +
"______ _____ _____ \n|___ / | _ \\ | _ \\ \n / / | | | | | |_| | \n / / | | | | | _ { \n / /__ | |_| | | |_| | \n/_____| |_____/ |_____/ \n" +
"had supported command:\n" +
"1. set:\n" +
@@ -33,7 +33,7 @@ var (
" eg: incr a\n" +
"7. decr:\n" +
" eg: decr a\n" +
"--- zdb help ---\n")
"--- _zdb help ---\n")
)
// 数据封装
@@ -56,7 +56,7 @@ func ServerStart(host string, port int) {
log.Fatal(err)
return
}
log.Printf("zdb started listen on: %s:%d \n", host, port)
log.Printf("_zdb started listen on: %s:%d \n", host, port)
// 启动消息监听处理
go func() {
@@ -66,7 +66,7 @@ func ServerStart(host string, port int) {
break
}
execCmd(v.Rcmd, *&*v.Conn)
ExecCmd(v.Rcmd, *&*v.Conn)
}
}()
@@ -108,7 +108,6 @@ func connHandler(conn net.Conn) {
for {
rcmd := make([]string, 0)
line, _, err := reader.ReadLine()
// fmt.Println("line:", string(line)) todo 可使用第一行用于协议头
if err != nil {
log.Println(err)
return

View File

@@ -1,7 +1,7 @@
package zdb
package _zdb
import "testing"
func TestName(t *testing.T) {
func TestService(t *testing.T) {
ServerStart("127.0.0.1", 1216)
}

62
zdb/ztimer.go Normal file
View File

@@ -0,0 +1,62 @@
package _zdb
import (
"fmt"
"github.com/robfig/cron"
"net"
"strings"
"time"
)
var (
zTimer = make(map[string]*ZTimer)
)
type ZTimer struct {
conns []*net.Conn
expr string
topic string
cron *cron.Cron
}
func timer(rcmd []string, conn net.Conn) {
ztimer := zTimer[rcmd[1]]
if ztimer == nil {
ztimer = &ZTimer{
conns: []*net.Conn{},
topic: rcmd[1],
}
zTimer[rcmd[1]] = ztimer
}
_conns := make([]*net.Conn, 0)
for _, c := range ztimer.conns {
if *&conn == *c {
continue
}
_conns = append(_conns, c)
}
_conns = append(_conns, &conn)
ztimer.conns = _conns
if !strings.EqualFold(ztimer.expr, rcmd[2]) {
ztimer.expr = rcmd[2]
if ztimer.cron != nil {
ztimer.cron.Stop()
}
ztimer.cron = func() *cron.Cron {
c := cron.New()
c.AddFunc(ztimer.expr, func() {
fmt.Println(time.Now().Second())
for _, conn := range ztimer.conns {
Send(*conn, "timer", ztimer.topic)
}
})
go c.Run()
return c
}()
}
zTimer[ztimer.topic] = ztimer
fmt.Println("xx")
}