From 7385a596ec1a6831474d5815d3d6c614c667f270 Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期一, 26 八月 2019 14:15:55 +0800 Subject: [PATCH] Merge branch 'master' of ssh://192.168.1.14:29418/valib/deliver --- shm.go | 71 ++++++++++++++++++++++++++++------- 1 files changed, 56 insertions(+), 15 deletions(-) diff --git a/shm.go b/shm.go index 0edb422..f36dba2 100644 --- a/shm.go +++ b/shm.go @@ -3,7 +3,9 @@ import ( "errors" "fmt" - "io" + // "io" + + "time" "basic.com/valib/shm.git" ) @@ -12,6 +14,8 @@ type SHM struct { rw *shm.ReadWriteCloser typ td + + data []byte } // Send impl interface Diliver @@ -20,12 +24,26 @@ return errors.New("please init shm producer first") } - n, err := s.rw.Write(data) - if n < 1 { - fmt.Println("recv data less than 1 length") + ch := make(chan int) + go func(){ + n, _ := s.rw.Write(data) + ch <- n + }() + select{ + case <-ch: + return nil + case <- time.After(3 * time.Second): + return errors.New("send time out") } - return err + return errors.New("send should't here") + + // n, err := s.rw.Write(data) + // if n < 1 { + // fmt.Println("recv data less than 1 length") + // } + + // return err } // Recv impl interface Diliver @@ -35,14 +53,33 @@ return nil, errors.New("please open shm consumer first") } - data := make([]byte, maxRecvSize) - n, err := s.rw.Read(data) - if err == nil || err == io.EOF { - data = data[:n:n] - return data, nil + ch := make(chan int) + go func(){ + n, _ := s.rw.Read(s.data) + ch <- n + }() + select{ + case d := <-ch: + if d > 0{ + data := make([]byte, d) + copy(data, s.data) + return data, nil + } + case <- time.After(3 * time.Second): + return nil, errors.New("recv time out") } - return nil, err + return nil, errors.New("recv should't here") + + + // data := make([]byte, maxRecvSize) + // n, err := s.rw.Read(data) + // if err == nil || err == io.EOF { + // data = data[:n:n] + // return data, nil + // } + + // return nil, err } // Close impl interface Deliver @@ -73,18 +110,21 @@ return nil, errors.New("shmServer created recv error parameters") } } - if len(param) != 2 { - return nil, errors.New("shmServer created recv too much parameters") - } + blocks, size := 2, maxRecvSize + if len(param) == 2 { + blocks, size = param[0], param[1] + // return nil, errors.New("shmServer created recv too much parameters") + } shm.Unlink(url) - rw, err := shm.CreateSimplex(url, 0644, param[0], param[1]) + rw, err := shm.CreateSimplex(url, 0644, blocks, size) if err == nil { fmt.Println(rw.Name()) return &SHM{ rw, agent, + make([]byte, maxRecvSize), }, nil } @@ -101,6 +141,7 @@ return &SHM{ rw, coactee, + make([]byte, maxRecvSize), }, nil } return nil, err -- Gitblit v1.8.0