USB和图像处理线程若内在申请失败,则直接退出扫描

This commit is contained in:
gb 2022-05-23 14:10:18 +08:00
parent c040b59488
commit eb5b86de90
6 changed files with 82 additions and 8 deletions

View File

@ -710,8 +710,13 @@ void hg_scanner::thread_handle_image_process(void)
fseek(src, 0, SEEK_END);
length = ftell(src);
fseek(src, 0, SEEK_SET);
buffer.reset(new std::vector<char>);
buffer->resize(length);
buffer = aquire_memory((int)length, false);
if (!buffer.get())
{
status_ = SCANNER_ERR_INSUFFICIENT_MEMORY;
break;
}
fread(buffer->data(), 1, length, src);
fclose(src);
remove(file.c_str());
@ -801,6 +806,9 @@ void hg_scanner::working_done(void*)
case SCANNER_ERR_DEVICE_PC_BUSY:
notify_ui_working_status(STATU_DESC_SCANNER_ERR_DEVICE_PC_BUSY, SANE_EVENT_SCAN_FINISHED, status_);
break;
case SCANNER_ERR_INSUFFICIENT_MEMORY:
notify_ui_working_status(STATU_DESC_SCANNER_ERR_INSUFFICIENT_MEMORY, SANE_EVENT_SCAN_FINISHED, status_);
break;
default:
notify_ui_working_status(user_cancel_ ? STATU_DESC_SCAN_CANCELED : STATU_DESC_SCAN_STOPPED, SANE_EVENT_SCAN_FINISHED, status_);
break;
@ -1719,6 +1727,44 @@ int hg_scanner::try_third_app_after_start(int err)
return err;
}
std::shared_ptr<std::vector<char>> hg_scanner::aquire_memory(int size, bool from_usb)
{
std::shared_ptr<std::vector<char>> mem;
try
{
mem.reset(new std::vector<char>(size));
}
catch (...)
{
mem.reset();
HG_VLOG_MINI_1(LOG_LEVEL_FATAL, "Allocate memory of bytes %u failed!\n", size);
}
if (!mem.get())
{
if (from_usb && final_imgs_.Size())
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
try
{
mem.reset(new std::vector<char>(size));
}
catch (...)
{
mem.reset();
HG_VLOG_MINI_1(LOG_LEVEL_FATAL, "Allocate memory of bytes %u failed secondary after 10 milliseconds!\n", size);
}
}
if (!mem.get())
{
HG_LOG(LOG_LEVEL_FATAL, "Can't aquire enough memory, working must be stopped!\n");
stop();
}
}
return mem;
}
int hg_scanner::notify_ui_working_status(const char* msg, int ev, int status)
{
unsigned int s = status;

View File

@ -261,6 +261,7 @@ protected:
int on_scann_error(int err); // 返回“0”忽略错误继续执行其它值则停止后续工作
int try_third_app_handle_start(bool& handled);
int try_third_app_after_start(int err);
std::shared_ptr<std::vector<char>> aquire_memory(int size, bool from_usb = true);
// callback to ui ...
int notify_ui_working_status(const char* msg, int ev = SANE_EVENT_STATUS, int status = SCANNER_ERR_OK);

View File

@ -350,6 +350,12 @@ void hg_scanner_200::thread_handle_usb_read(void)
else if (usb.u32_Data == HAVE_IMAGE)
{
ret = get_img_data(usb.u32_Count);
if (ret == SCANNER_ERR_INSUFFICIENT_MEMORY)
{
status_ = ret;
break;
}
io_->set_timeout(200);
pop_image();
sw.reset();
@ -371,7 +377,11 @@ void hg_scanner_200::thread_handle_usb_read(void)
get_scanner_status(usb);
if (usb.u32_Data == HAVE_IMAGE)
{
get_img_data(usb.u32_Count);
if(get_img_data(usb.u32_Count) == SCANNER_ERR_INSUFFICIENT_MEMORY)
{
status_ = SCANNER_ERR_INSUFFICIENT_MEMORY;
break;
}
io_->set_timeout(200);
pop_image();
sw.reset();
@ -540,8 +550,11 @@ int hg_scanner_200::get_img_data(unsigned int bytes)
ret = SCANNER_ERR_OK,
index = 0,
block = total;
std::shared_ptr<std::vector<char >> imagedata(new std::vector<char>(total));
std::shared_ptr<std::vector<char >> imagedata(aquire_memory(total));
if (!imagedata.get())
return SCANNER_ERR_INSUFFICIENT_MEMORY;
USBCB usb{GET_IMAGE, 0, total};
ret = writeusb(usb);
if (ret != SCANNER_ERR_OK)

View File

@ -1000,8 +1000,10 @@ int hg_scanner_239::read_one_image_from_usb(void)
ret = status_;
else
{
std::shared_ptr<std::vector<char>> buf(new std::vector<char>);
buf->resize(total);
std::shared_ptr<std::vector<char>> buf(aquire_memory(total));
if (!buf.get())
return SCANNER_ERR_INSUFFICIENT_MEMORY;
// write reading command
ret = write_register(SR_IM_TX, 1);

View File

@ -322,7 +322,13 @@ void hg_scanner_300::thread_handle_usb_read(void)
if (ret == SCANNER_ERR_OK && usb.u32_Count > 0)
{
int totalNum = usb.u32_Count;
std::shared_ptr<std::vector<char >> imagedata(new std::vector<char> (totalNum));
std::shared_ptr<std::vector<char >> imagedata(aquire_memory(totalNum));
if (!imagedata.get())
{
status_ = SCANNER_ERR_INSUFFICIENT_MEMORY;
break;
}
ret = get_img_data(imagedata);
io_->set_timeout(200);

View File

@ -319,7 +319,13 @@ void hg_scanner_400::thread_handle_usb_read(void)
if (ret == SCANNER_ERR_OK && usb.u32_Count > 0)
{
int totalNum = usb.u32_Count;
std::shared_ptr<std::vector<char >> imagedata(new std::vector<char> (totalNum));
std::shared_ptr<std::vector<char >> imagedata(aquire_memory(totalNum));
if (!imagedata.get())
{
status_ = SCANNER_ERR_INSUFFICIENT_MEMORY;
break;
}
ret = get_img_data(imagedata);
io_->set_timeout(200);