reid from https://github.com/michuanhaohao/reid-strong-baseline
554325746@qq.com
2020-03-25 b2500a8eb6665ce6efe0a7d954b6f101af83d7ec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package rpc
 
import (
    "context"
 
    "time"
 
    "basic.com/valib/deliver.git"
)
 
// Reciever recv from ipc
type Reciever struct {
    ctx    context.Context
    ipcURL string
    out    chan []byte
    chCap  int
 
    shm      bool
    fnLogger func(...interface{})
}
 
// NewReciever new recv
func NewReciever(url string, out chan []byte, shm bool, fn func(...interface{})) *Reciever {
    return &Reciever{
        ipcURL: url,
        out:    out,
        chCap:  cap(out),
 
        shm:      shm,
        fnLogger: fn,
    }
}
 
// Run run a IPC client
func (r *Reciever) Run(ctx context.Context) {
 
    count := 0
 
    i := r.createIPC(ctx, 50*time.Millisecond, 200)
 
    for {
        select {
        case <-ctx.Done():
            i.Close()
            return
        default:
 
            if r.shm {
                if i == nil {
                    r.fnLogger("!!!!!!SDK Recv createIPC not ready error:", r.ipcURL)
                    i = r.createIPC(ctx, 50*time.Millisecond, 10)
                }
                if i == nil {
                    r.fnLogger("!!!!!!SDK Recv createIPC error:", r.ipcURL)
                    continue
                }
                if d, err := i.Recv(); err != nil {
 
                    i.Close()
                    r.fnLogger("SDK RECV:", r.ipcURL, "ERROR:", err)
 
                    i = r.createIPC(ctx, 50*time.Millisecond, 20)
                    r.fnLogger("To Reid Recver CREATE SHM:", r.ipcURL)
                } else {
                    if d != nil {
                        if len(r.out) > r.chCap/2 {
                            for i := 0; i < r.chCap/2; i++ {
                                <-r.out
                            }
                        }
                        r.out <- d
                        r.fnLogger("~~~shm recv from:", r.ipcURL, "image:", len(d))
                    }
                }
 
            } else {
                if msg, err := i.Recv(); err != nil {
                    // logo.Errorln("recv error : ", err, " url: ", r.ipcURL)
                } else {
                    count++
                    if count > 10 {
                        count = 0
                        r.fnLogger("~~~mangos recv image:", len(msg))
                    }
                    if len(msg) > 2 {
                        if len(r.out) > r.chCap/2 {
                            for i := 0; i < r.chCap/2; i++ {
                                <-r.out
                            }
                        }
                        r.out <- msg
                    }
                }
            }
            time.Sleep(10 * time.Millisecond)
        }
    }
}
 
func (r *Reciever) createIPC(ctx context.Context, wait time.Duration, loop int) deliver.Deliver {
 
    mode := deliver.PushPull
    if r.shm {
        mode = deliver.Shm
    }
 
    try := 0
 
    c, err := deliver.NewClientWithError(mode, r.ipcURL)
loopR:
    for {
        select {
        case <-ctx.Done():
            return nil
        default:
            if err == nil {
                break loopR
            }
            if loop > 0 {
                try++
                if try > loop {
                    return nil
                }
                time.Sleep(wait)
            } else {
                time.Sleep(time.Second)
            }
            c, err = deliver.NewClientWithError(mode, r.ipcURL)
        }
    }
    return c
}