Skip to content

Commit

Permalink
Merge pull request #16 from fanap-infra/fix/readBlock-overflow
Browse files Browse the repository at this point in the history
fix read block overflow
  • Loading branch information
mohammadVatandoost authored Aug 10, 2021
2 parents 31716a4 + 18e925d commit acd2795
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
37 changes: 19 additions & 18 deletions IO.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ func (arch *Archiver) Close() error {
arch.crudMutex.Lock()
defer arch.crudMutex.Unlock()

for _, vms := range arch.openFiles {
for _, vm := range vms {
err := vm.CloseWithNotifyArchiver()
if err != nil {
arch.log.Warnv("Can not close virtual media", "err", err.Error())
return err
}
}
}
//for _, vms := range arch.openFiles {
// for _, vm := range vms {
// err := vm.CloseWithNotifyArchiver()
// if err != nil {
// arch.log.Warnv("Can not close virtual media", "err", err.Error())
// return err
// }
// }
//}
err := arch.fs.Close()
if err != nil {
arch.log.Warnv("Can not close arch", "err", err.Error())
Expand All @@ -26,14 +26,15 @@ func (arch *Archiver) Close() error {
func (arch *Archiver) Closed(fileID uint32) error {
arch.crudMutex.Lock()
defer arch.crudMutex.Unlock()
vms, ok := arch.openFiles[fileID]
if ok {
if len(vms) == 1 {
delete(arch.openFiles, fileID)
} else {
// ToDo: get index in addition to fileID
arch.openFiles[fileID] = vms[:len(vms)-1]
}
}
//arch.log.Infov("virtual media file ")
//vms, ok := arch.openFiles[fileID]
//if ok {
// if len(vms) == 1 {
// delete(arch.openFiles, fileID)
// } else {
// // ToDo: get index in addition to fileID
// arch.openFiles[fileID] = vms[:len(vms)-1]
// }
//}
return nil
}
14 changes: 6 additions & 8 deletions crud.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package archiverMedia

import (
"fmt"

"github.com/fanap-infra/archiverMedia/pkg/vInfo"

"github.com/fanap-infra/archiverMedia/pkg/virtualMedia"
Expand All @@ -17,7 +15,7 @@ func (arch *Archiver) NewVirtualMediaFile(id uint32, fileName string) (*virtualM
return nil, err
}
vm := virtualMedia.NewVirtualMedia(fileName, id, arch.blockSize, vf, arch, arch.log)
arch.openFiles[id] = append(arch.openFiles[id], vm)
//arch.openFiles[id] = append(arch.openFiles[id], vm)
return vm, nil
}

Expand All @@ -38,17 +36,17 @@ func (arch *Archiver) OpenVirtualMediaFile(id uint32) (*virtualMedia.VirtualMedi
return nil, err
}
vm := virtualMedia.OpenVirtualMedia(vf.GetFileName(), id, arch.blockSize, vf, arch, info, arch.log)
arch.openFiles[id] = append(arch.openFiles[id], vm)
//arch.openFiles[id] = append(arch.openFiles[id], vm)
return vm, nil
}

func (arch *Archiver) RemoveVirtualMediaFile(id uint32) error {
arch.crudMutex.Lock()
defer arch.crudMutex.Unlock()
_, ok := arch.openFiles[id]
if ok {
return fmt.Errorf("virtual media id : %d is opened", id)
}
//_, ok := arch.openFiles[id]
//if ok {
// return fmt.Errorf("virtual media id : %d is opened", id)
//}
err := arch.fs.RemoveVirtualFile(id)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/fanap-infra/archiverMedia
go 1.12

require (
github.com/fanap-infra/fsEngine v0.4.14
github.com/fanap-infra/fsEngine v0.4.16
github.com/fanap-infra/log v1.6.1
github.com/golang/protobuf v1.4.2
github.com/stretchr/testify v1.7.0
Expand Down
2 changes: 2 additions & 0 deletions pkg/virtualMedia/IO.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func (vm *VirtualMedia) ReadFrame() (*media.Packet, error) {
}

func (vm *VirtualMedia) GotoTime(frameTime int64) (int64, error) {
vm.rxMUX.Lock()
defer vm.rxMUX.Unlock()
if vm.frameChunkRX != nil {
if vm.frameChunkRX.StartTime <= frameTime &&
vm.frameChunkRX.EndTime >= frameTime {
Expand Down
5 changes: 5 additions & 0 deletions pkg/virtualMedia/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func generateFrameChunk(med *media.PacketChunk) ([]byte, error) {
}

func (vm *VirtualMedia) NextFrameChunk() (*media.PacketChunk, error) {
//ToDo: do not expose this functions , it is not safe thread
frameChunkDataSize := uint32(0)
nextFrameChunk := -1
errorCounter := 0
Expand Down Expand Up @@ -73,13 +74,17 @@ func (vm *VirtualMedia) NextFrameChunk() (*media.PacketChunk, error) {
if err == virtualFile.EndOfFile {
return nil, EndOfFile
}
vm.log.Warnv("can not read data", "id", vm.fileID,
"nextFrameChunk", nextFrameChunk, "frameChunkDataSize", frameChunkDataSize,
"len(vm.vfBuf)", len(vm.vfBuf), "err", err.Error())
return nil, err
}
vm.vfBuf = append(vm.vfBuf, tmpBuf[:n]...)
}
}

func (vm *VirtualMedia) PreviousFrameChunk() (*media.PacketChunk, error) {
//ToDo: do not expose this functions , it is not safe thread
if vm.frameChunkRX != nil {
if vm.frameChunkRX.Index == 1 || vm.frameChunkRX.PreviousChunkSize == 0 {
return nil, fmt.Errorf("there is no previous frame chunk")
Expand Down

0 comments on commit acd2795

Please sign in to comment.