Skip to content

Commit d1a5840

Browse files
committed
fix
1 parent 225cdeb commit d1a5840

1 file changed

Lines changed: 40 additions & 33 deletions

File tree

internal/dutagent/worker.go

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ func sendDownloadError(stream Stream, s *session, transferID string, downloadMet
4646
sendErr := safeSend(stream, res)
4747
if sendErr != nil {
4848
log.Printf("handleDownloadFileTransfer: error sending error response: %v", sendErr)
49+
4950
return false
5051
}
5152

5253
s.removeDownload(transferID)
5354
delete(downloadMetadataSent, transferID)
55+
5456
return true
5557
}
5658

@@ -78,59 +80,71 @@ func sendDownloadMetadata(stream Stream, s *session, transferID string, download
7880
sendErr := safeSend(stream, res)
7981
if sendErr != nil {
8082
log.Printf("handleDownloadFileTransfer: error sending metadata: %v", sendErr)
83+
8184
return false
8285
}
8386

8487
downloadMetadataSent[transferID] = true
88+
89+
return true
90+
}
91+
92+
// sendDownloadChunk sends a file chunk to the client and marks final chunks.
93+
func sendDownloadChunk(stream Stream, s *session, transferID string, chunk *pb.FileChunk, isFinal bool) bool {
94+
if chunk == nil || len(chunk.GetChunkData()) == 0 {
95+
return false
96+
}
97+
98+
res := &pb.RunResponse{
99+
Msg: &pb.RunResponse_FileChunk{FileChunk: chunk},
100+
}
101+
102+
sendErr := safeSend(stream, res)
103+
if sendErr != nil {
104+
log.Printf("handleDownloadFileTransfer: error sending chunk: %v", sendErr)
105+
106+
return false
107+
}
108+
109+
if isFinal {
110+
s.markDownloadAwaitingAck(transferID)
111+
}
112+
85113
return true
86114
}
87115

88116
// handleDownloadFileTransfer processes a single download transfer for sending to the client.
89-
//
90-
//nolint:funlen
91-
func handleDownloadFileTransfer(stream Stream, s *session, transferID string, downloadMetadataSent map[string]bool) (bool, error) {
117+
func handleDownloadFileTransfer(stream Stream, s *session, transferID string, downloadMetadataSent map[string]bool) bool {
92118
// Skip if waiting for client acknowledgment
93119
download := s.getDownload(transferID)
94120
if download != nil && download.awaitingFinalAck {
95-
return false, nil
121+
return false
96122
}
97123

98124
// Send metadata first
99125
if !downloadMetadataSent[transferID] {
100126
if sendDownloadMetadata(stream, s, transferID, downloadMetadataSent) {
101-
return true, nil
127+
return true
102128
}
103-
return false, nil
129+
130+
return false
104131
}
105132

106133
// Get next chunk
107134
chunk, isFinal, err := s.getNextChunk(transferID)
108135
if err != nil {
109136
if sendDownloadError(stream, s, transferID, downloadMetadataSent, err) {
110-
return true, nil
137+
return true
111138
}
112-
return false, nil
113-
}
114139

115-
if chunk != nil && len(chunk.GetChunkData()) > 0 {
116-
res := &pb.RunResponse{
117-
Msg: &pb.RunResponse_FileChunk{FileChunk: chunk},
118-
}
119-
120-
sendErr := safeSend(stream, res)
121-
if sendErr != nil {
122-
log.Printf("handleDownloadFileTransfer: error sending chunk: %v", sendErr)
123-
// Stream is closed, return cleanly
124-
return false, nil
125-
}
140+
return false
141+
}
126142

127-
// Mark final chunk as awaiting client acknowledgment
128-
if isFinal {
129-
s.markDownloadAwaitingAck(transferID)
130-
}
143+
if sendDownloadChunk(stream, s, transferID, chunk, isFinal) {
144+
return false
131145
}
132146

133-
return false, nil
147+
return false
134148
}
135149

136150
// toClientWorker sends messages from the module session to the client.
@@ -268,12 +282,7 @@ func toClientWorker(ctx context.Context, stream Stream, s *session) error {
268282
}
269283
}
270284

271-
_, err := handleDownloadFileTransfer(stream, s, transferID, downloadMetadataSent)
272-
if err != nil {
273-
log.Printf("toClientWorker: error in download file transfer: %v", err)
274-
// Stream is likely closed, return cleanly without panic
275-
return nil
276-
}
285+
_ = handleDownloadFileTransfer(stream, s, transferID, downloadMetadataSent)
277286
}
278287
}
279288
}
@@ -439,7 +448,6 @@ func fromClientWorker(ctx context.Context, stream Stream, s *session) error {
439448

440449
s.removeUpload(transferID)
441450
}
442-
443451
case *pb.RunRequest_FileTransferRequest:
444452
ftReq := msg.FileTransferRequest
445453
if ftReq == nil {
@@ -451,7 +459,6 @@ func fromClientWorker(ctx context.Context, stream Stream, s *session) error {
451459
// Check if this is a known transfer (module called RequestFile)
452460
upload := s.getUpload(transferID)
453461
if upload == nil {
454-
455462
// Send rejection
456463
res := &pb.RunResponse{
457464
Msg: &pb.RunResponse_FileTransferResponse{

0 commit comments

Comments
 (0)