git-svn-id: svn://47.119.165.148/zhub@142 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
lxy
2021-11-17 07:41:37 +00:00
parent 0ec4363aa8
commit b99fd65a55

View File

@@ -18,8 +18,8 @@ import (
) )
type Client struct { type Client struct {
wlock sync.Mutex // write lock lock sync.Mutex // write lock
rlock sync.Mutex // read lock //rlock sync.Mutex // read lock
appname string // local appname appname string // local appname
addr string // host:port addr string // host:port
@@ -51,8 +51,7 @@ func Create(appname string, addr string, groupid string) (*Client, error) {
} }
client := Client{ client := Client{
wlock: sync.Mutex{}, lock: sync.Mutex{},
rlock: sync.Mutex{},
appname: appname, appname: appname,
addr: addr, addr: addr,
conn: conn, conn: conn,
@@ -133,8 +132,8 @@ subscribe x y z
func (c *Client) Subscribe(topic string, fun func(v string)) { func (c *Client) Subscribe(topic string, fun func(v string)) {
c.send("subscribe " + topic) c.send("subscribe " + topic)
if fun != nil { if fun != nil {
c.wlock.Lock() c.lock.Lock()
defer c.wlock.Unlock() defer c.lock.Unlock()
c.subFun[topic] = fun c.subFun[topic] = fun
} }
} }
@@ -214,8 +213,8 @@ func (c *Client) Lock(key string, duration int) Lock {
lockChan := make(chan int, 2) lockChan := make(chan int, 2)
go func() { go func() {
c.wlock.Lock() c.lock.Lock()
defer c.wlock.Unlock() defer c.lock.Unlock()
c.lockFlag[uuid] = &Lock{ c.lockFlag[uuid] = &Lock{
Key: key, Key: key,
Uuid: uuid, Uuid: uuid,
@@ -287,13 +286,6 @@ func (r Rpc) backTopic() string {
return strings.Split(r.Ruk, "::")[0] return strings.Split(r.Ruk, "::")[0]
} }
func (r Rpc) Retcode() int {
return r.RpcResult.Retcode
}
func (r Rpc) Retinfo() string {
return r.RpcResult.Retinfo
}
func (c Client) Rpc(topic string, message string, back func(res RpcResult)) { func (c Client) Rpc(topic string, message string, back func(res RpcResult)) {
rpc := Rpc{ rpc := Rpc{
Ruk: c.appname + "::" + uuid.New(), Ruk: c.appname + "::" + uuid.New(),
@@ -367,8 +359,8 @@ else if len(vs) gt 1 will send message `* + len(vs)+ "\r\n" +"$"+ len(vs[n])+ "
*/ */
func (c *Client) send(vs ...string) (err error) { func (c *Client) send(vs ...string) (err error) {
//chSend <- vs //chSend <- vs
c.wlock.Lock() c.lock.Lock()
defer c.wlock.Unlock() defer c.lock.Unlock()
a: a:
if len(vs) == 1 { if len(vs) == 1 {
_, err = c.conn.Write([]byte(vs[0] + "\r\n")) _, err = c.conn.Write([]byte(vs[0] + "\r\n"))
@@ -390,8 +382,8 @@ a:
} }
func (c *Client) receive() { func (c *Client) receive() {
c.rlock.Lock() c.lock.Lock()
defer c.rlock.Unlock() defer c.lock.Unlock()
r := bufio.NewReader(c.conn) r := bufio.NewReader(c.conn)
for { for {
@@ -434,8 +426,8 @@ func (c *Client) receive() {
if strings.EqualFold(vs[1], "lock") { // message lock Uuid if strings.EqualFold(vs[1], "lock") { // message lock Uuid
go func() { go func() {
log.Println("lock:" + vs[2]) log.Println("lock:" + vs[2])
c.wlock.Lock() c.lock.Lock()
defer c.wlock.Unlock() defer c.lock.Unlock()
if c.lockFlag[vs[2]] == nil { if c.lockFlag[vs[2]] == nil {
return return