跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

packet-manager.go

237 行 · 6.2 KB历史下载
1package sftp
2
3import (
4 "encoding"
5 "sort"
6 "sync"
7)
8
9// The goal of the packetManager is to keep the outgoing packets in the same
10// order as the incoming as is requires by section 7 of the RFC.
11
12type packetManager struct {
13 requests chan orderedPacket
14 responses chan orderedPacket
15 fini chan struct{}
16 incoming orderedPackets
17 outgoing orderedPackets
18 sender packetSender // connection object
19 working *sync.WaitGroup
20 packetCount uint32
21 // it is not nil if the allocator is enabled
22 alloc *allocator
23 // sequential processes every packet in a single worker, in the order received
24 sequential bool
25}
26
27type packetSender interface {
28 sendPacket(encoding.BinaryMarshaler) error
29}
30
31func newPktMgr(sender packetSender) *packetManager {
32 s := &packetManager{
33 requests: make(chan orderedPacket, SftpServerWorkerCount),
34 responses: make(chan orderedPacket, SftpServerWorkerCount),
35 fini: make(chan struct{}),
36 incoming: make([]orderedPacket, 0, SftpServerWorkerCount),
37 outgoing: make([]orderedPacket, 0, SftpServerWorkerCount),
38 sender: sender,
39 working: &sync.WaitGroup{},
40 }
41 go s.controller()
42 return s
43}
44
45// // packet ordering
46func (s *packetManager) newOrderID() uint32 {
47 s.packetCount++
48 return s.packetCount
49}
50
51// returns the next orderID without incrementing it.
52// This is used before receiving a new packet, with the allocator enabled, to associate
53// the slice allocated for the received packet with the orderID that will be used to mark
54// the allocated slices for reuse once the request is served
55func (s *packetManager) getNextOrderID() uint32 {
56 return s.packetCount + 1
57}
58
59type orderedRequest struct {
60 requestPacket
61 orderid uint32
62}
63
64func (s *packetManager) newOrderedRequest(p requestPacket) orderedRequest {
65 return orderedRequest{requestPacket: p, orderid: s.newOrderID()}
66}
67func (p orderedRequest) orderID() uint32 { return p.orderid }
68
69type orderedResponse struct {
70 responsePacket
71 orderid uint32
72}
73
74func (s *packetManager) newOrderedResponse(p responsePacket, id uint32,
75) orderedResponse {
76 return orderedResponse{responsePacket: p, orderid: id}
77}
78func (p orderedResponse) orderID() uint32 { return p.orderid }
79
80type orderedPacket interface {
81 id() uint32
82 orderID() uint32
83}
84type orderedPackets []orderedPacket
85
86func (o orderedPackets) Sort() {
87 sort.Slice(o, func(i, j int) bool {
88 // The difference, not the values: orderIDs wrap around after 2^32 packets.
89 return int32(o[i].orderID()-o[j].orderID()) < 0
90 })
91}
92
93// // packet registry
94// register incoming packets to be handled
95func (s *packetManager) incomingPacket(pkt orderedRequest) {
96 s.working.Add(1)
97 s.requests <- pkt
98}
99
100// register outgoing packets as being ready
101func (s *packetManager) readyPacket(pkt orderedResponse) {
102 s.responses <- pkt
103 s.working.Done()
104}
105
106// shut down packetManager controller
107func (s *packetManager) close() {
108 // pause until current packets are processed
109 s.working.Wait()
110 close(s.fini)
111}
112
113// Passed a worker function, returns a channel for incoming packets.
114// Keep process packet responses in the order they are received while
115// maximizing throughput of file transfers.
116func (s *packetManager) workerChan(runWorker func(chan orderedRequest),
117) chan orderedRequest {
118 if s.sequential {
119 ch := make(chan orderedRequest)
120 runWorker(ch)
121 pktChan := make(chan orderedRequest, SftpServerWorkerCount)
122 go func() {
123 for pkt := range pktChan {
124 s.incomingPacket(pkt)
125 ch <- pkt
126 }
127 close(ch)
128 s.close()
129 }()
130 return pktChan
131 }
132
133 // multiple workers for faster read/writes
134 rwChan := make(chan orderedRequest, SftpServerWorkerCount)
135 for i := 0; i < SftpServerWorkerCount; i++ {
136 runWorker(rwChan)
137 }
138
139 // single worker to enforce sequential processing of everything else
140 cmdChan := make(chan orderedRequest)
141 runWorker(cmdChan)
142
143 pktChan := make(chan orderedRequest, SftpServerWorkerCount)
144 go func() {
145 for pkt := range pktChan {
146 switch p := pkt.requestPacket.(type) {
147 case *sshFxpReadPacket, *sshFxpWritePacket:
148 s.incomingPacket(pkt)
149 rwChan <- pkt
150 continue
151 case *sshFxpClosePacket:
152 // wait for reads/writes to finish when file is closed
153 // incomingPacket() call must occur after this
154 s.working.Wait()
155 case *sshFxpExtendedPacket:
156 // and before syncing the file they wrote
157 if _, ok := p.SpecificPacket.(*sshFxpFsyncPacket); ok {
158 s.working.Wait()
159 }
160 }
161 s.incomingPacket(pkt)
162 // all non-RW use sequential cmdChan
163 cmdChan <- pkt
164 }
165 close(rwChan)
166 close(cmdChan)
167 s.close()
168 }()
169
170 return pktChan
171}
172
173// process packets
174func (s *packetManager) controller() {
175 for {
176 select {
177 case pkt := <-s.requests:
178 debug("incoming id (oid): %v (%v)", pkt.id(), pkt.orderID())
179 s.incoming = append(s.incoming, pkt)
180 s.incoming.Sort()
181 case pkt := <-s.responses:
182 debug("outgoing id (oid): %v (%v)", pkt.id(), pkt.orderID())
183 s.outgoing = append(s.outgoing, pkt)
184 s.outgoing.Sort()
185 case <-s.fini:
186 return
187 }
188 s.maybeSendPackets()
189 }
190}
191
192// send as many packets as are ready
193func (s *packetManager) maybeSendPackets() {
194 for {
195 if len(s.outgoing) == 0 || len(s.incoming) == 0 {
196 debug("break! -- outgoing: %v; incoming: %v",
197 len(s.outgoing), len(s.incoming))
198 break
199 }
200 out := s.outgoing[0]
201 in := s.incoming[0]
202 // debug("incoming: %v", ids(s.incoming))
203 // debug("outgoing: %v", ids(s.outgoing))
204 if in.orderID() == out.orderID() {
205 debug("Sending packet: %v", out.id())
206 s.sender.sendPacket(out.(encoding.BinaryMarshaler))
207 if s.alloc != nil {
208 // mark for reuse the slices allocated for this request
209 s.alloc.ReleasePages(in.orderID())
210 }
211 // pop off heads
212 copy(s.incoming, s.incoming[1:]) // shift left
213 s.incoming[len(s.incoming)-1] = nil // clear last
214 s.incoming = s.incoming[:len(s.incoming)-1] // remove last
215 copy(s.outgoing, s.outgoing[1:]) // shift left
216 s.outgoing[len(s.outgoing)-1] = nil // clear last
217 s.outgoing = s.outgoing[:len(s.outgoing)-1] // remove last
218 } else {
219 break
220 }
221 }
222}
223
224// func oids(o []orderedPacket) []uint32 {
225// res := make([]uint32, 0, len(o))
226// for _, v := range o {
227// res = append(res, v.orderId())
228// }
229// return res
230// }
231// func ids(o []orderedPacket) []uint32 {
232// res := make([]uint32, 0, len(o))
233// for _, v := range o {
234// res = append(res, v.id())
235// }
236// return res
237// }