MCPcopy Index your code
hub / github.com/coder/coder / Bicopy

Function Bicopy

agent/agentssh/bicopy.go:12–47  ·  view source on GitHub ↗

Bicopy copies all of the data between the two connections and will close them after one or both of them are done writing. If the context is canceled, both of the connections will be closed.

(ctx context.Context, c1, c2 io.ReadWriteCloser)

Source from the content-addressed store, hash-verified

10// after one or both of them are done writing. If the context is canceled, both
11// of the connections will be closed.
12func Bicopy(ctx context.Context, c1, c2 io.ReadWriteCloser) {
13 ctx, cancel := context.WithCancel(ctx)
14 defer cancel()
15
16 defer func() {
17 _ = c1.Close()
18 _ = c2.Close()
19 }()
20
21 var wg sync.WaitGroup
22 copyFunc := func(dst io.WriteCloser, src io.Reader) {
23 defer func() {
24 wg.Done()
25 // If one side of the copy fails, ensure the other one exits as
26 // well.
27 cancel()
28 }()
29 _, _ = io.Copy(dst, src)
30 }
31
32 wg.Add(2)
33 go copyFunc(c1, c2)
34 go copyFunc(c2, c1)
35
36 // Convert waitgroup to a channel so we can also wait on the context.
37 done := make(chan struct{})
38 go func() {
39 defer close(done)
40 wg.Wait()
41 }()
42
43 select {
44 case <-ctx.Done():
45 case <-done:
46 }
47}

Callers 10

watchChatDesktopMethod · 0.92
workspaceAgentPTYMethod · 0.92
listenAndPortForwardFunction · 0.92
runCoderConnectStdioFunction · 0.92
sshRemoteForwardFunction · 0.92
TestSSHFunction · 0.92
handleDesktopVNCMethod · 0.92
listenForConnectionsMethod · 0.85
HandleSSHRequestMethod · 0.85
directStreamLocalHandlerFunction · 0.85

Calls 5

CloseMethod · 0.65
CopyMethod · 0.65
AddMethod · 0.65
WaitMethod · 0.65
DoneMethod · 0.45

Tested by 1

TestSSHFunction · 0.74