diff --git a/build/windows/HGBase/HGBase.def b/build/windows/HGBase/HGBase.def index a0eb979b..4fb4f9c1 100644 --- a/build/windows/HGBase/HGBase.def +++ b/build/windows/HGBase/HGBase.def @@ -98,3 +98,9 @@ HGBase_ImageRotate180 HGBase_ImageGrayscale HGBase_ReverseImage HGBase_CopyImage + +HGBase_CreateMsgPump +HGBase_DestroyMsgPump +HGBase_RunMsgPump +HGBase_PostPumpMessage +HGBase_ExitMsgPump diff --git a/build/windows/HGBase/HGBase.vcxproj b/build/windows/HGBase/HGBase.vcxproj index 4c12dde8..3dbee6ff 100644 --- a/build/windows/HGBase/HGBase.vcxproj +++ b/build/windows/HGBase/HGBase.vcxproj @@ -35,6 +35,8 @@ + + @@ -55,6 +57,8 @@ + + diff --git a/build/windows/HGTest/HGTestDlg.cpp b/build/windows/HGTest/HGTestDlg.cpp index 188892b5..d35c44a6 100644 --- a/build/windows/HGTest/HGTestDlg.cpp +++ b/build/windows/HGTest/HGTestDlg.cpp @@ -184,7 +184,7 @@ void CHGTestDlg::OnBnClickedButton2() m_dsm = NULL; } -void HGAPI CHGTestDlg::DSEventCallback(HGTwainDS ds, HGUInt event, HGPointer param) +void CHGTestDlg::DSEventCallback(HGTwainDS ds, HGUInt event, HGPointer param) { CHGTestDlg* p = (CHGTestDlg*)param; if (HGTWAIN_DSEVENT_XFERREADY == event) diff --git a/build/windows/HGTest/HGTestDlg.h b/build/windows/HGTest/HGTestDlg.h index 740f4d05..9c707a1f 100644 --- a/build/windows/HGTest/HGTestDlg.h +++ b/build/windows/HGTest/HGTestDlg.h @@ -23,7 +23,7 @@ protected: HGTwainDSM m_dsm; HGTwainDS m_ds; - static void HGAPI DSEventCallback(HGTwainDS ds, HGUInt event, HGPointer param); + static void DSEventCallback(HGTwainDS ds, HGUInt event, HGPointer param); // 实现 protected: diff --git a/build/windows/HGWebService/HGWebService.vcxproj b/build/windows/HGWebService/HGWebService.vcxproj index c766126c..98b00a9d 100644 --- a/build/windows/HGWebService/HGWebService.vcxproj +++ b/build/windows/HGWebService/HGWebService.vcxproj @@ -19,32 +19,36 @@ - - - - - - - - - + + + + + + + + + + + - - - - - - - - - - + + + + + + + + + + + + diff --git a/modules/twain_user/HGTwain.cpp b/modules/twain_user/HGTwain.cpp index 1c8f3eb8..d810b27a 100644 --- a/modules/twain_user/HGTwain.cpp +++ b/modules/twain_user/HGTwain.cpp @@ -149,7 +149,7 @@ HGResult HGAPI HGTwain_GetCapability(HGTwainDS ds, HGUInt cap, HGInt* value) } HGResult HGAPI HGTwain_EnableDSUIOnly(HGTwainDS ds, HGBool showUI, HWND parent, - HGTwain_DSEventCallback func, HGPointer param) + HGDSEventFunc func, HGPointer param) { if (NULL == ds) { @@ -161,7 +161,7 @@ HGResult HGAPI HGTwain_EnableDSUIOnly(HGTwainDS ds, HGBool showUI, HWND parent, } HGResult HGAPI HGTwain_EnableDS(HGTwainDS ds, HGBool showUI, HWND parent, - HGTwain_DSEventCallback func, HGPointer param) + HGDSEventFunc func, HGPointer param) { if (NULL == ds) { diff --git a/modules/twain_user/HGTwain.h b/modules/twain_user/HGTwain.h index 1c4ff235..ca6cdded 100644 --- a/modules/twain_user/HGTwain.h +++ b/modules/twain_user/HGTwain.h @@ -29,7 +29,7 @@ HG_DECLARE_HANDLE(HGTwainDS); * 1) 如果event为HGTWAIN_EVENT_XFERREADY, TWAIN状态从5变为6 * 2) 如果event为HGTWAIN_EVENT_CLOSEDSREQ, 表示需要关闭该DS */ -typedef void (HGAPI *HGTwain_DSEventCallback)(HGTwainDS ds, HGUInt event, HGPointer param); +typedef void (*HGDSEventFunc)(HGTwainDS ds, HGUInt event, HGPointer param); /* 加载DSM * 参数: @@ -151,7 +151,7 @@ HGEXPORT HGResult HGAPI HGTwain_GetCapability(HGTwainDS ds, HGUInt cap, HGInt* v * 1) 执行此函数后,TWAIN状态从4变为5 */ HGEXPORT HGResult HGAPI HGTwain_EnableDSUIOnly(HGTwainDS ds, HGBool showUI, HWND parent, - HGTwain_DSEventCallback func, HGPointer param); + HGDSEventFunc func, HGPointer param); /* 启动DS * 参数: @@ -164,7 +164,7 @@ HGEXPORT HGResult HGAPI HGTwain_EnableDSUIOnly(HGTwainDS ds, HGBool showUI, HWND * 1) 执行此函数后,TWAIN状态从4变为5 */ HGEXPORT HGResult HGAPI HGTwain_EnableDS(HGTwainDS ds, HGBool showUI, HWND parent, - HGTwain_DSEventCallback func, HGPointer param); + HGDSEventFunc func, HGPointer param); /* 停止DS * 参数: diff --git a/modules/twain_user/HGTwainImpl.cpp b/modules/twain_user/HGTwainImpl.cpp index 81aeb433..2a691da3 100644 --- a/modules/twain_user/HGTwainImpl.cpp +++ b/modules/twain_user/HGTwainImpl.cpp @@ -436,7 +436,7 @@ HGResult HGTwainDSMImpl::GetCapability(HGTwainDSImpl* ds, HGUInt cap, HGInt* val return HGBASE_ERR_FAIL; } -HGResult HGTwainDSMImpl::EnableDSUIOnly(HGTwainDSImpl* ds, HGBool showUI, HWND parent, HGTwain_DSEventCallback func, HGPointer param) +HGResult HGTwainDSMImpl::EnableDSUIOnly(HGTwainDSImpl* ds, HGBool showUI, HWND parent, HGDSEventFunc func, HGPointer param) { if (NULL == ds || NULL == func) { @@ -473,7 +473,7 @@ HGResult HGTwainDSMImpl::EnableDSUIOnly(HGTwainDSImpl* ds, HGBool showUI, HWND p return HGBASE_ERR_FAIL; } -HGResult HGTwainDSMImpl::EnableDS(HGTwainDSImpl* ds, HGBool showUI, HWND parent, HGTwain_DSEventCallback func, HGPointer param) +HGResult HGTwainDSMImpl::EnableDS(HGTwainDSImpl* ds, HGBool showUI, HWND parent, HGDSEventFunc func, HGPointer param) { if (NULL == ds || NULL == func) { diff --git a/modules/twain_user/HGTwainImpl.hpp b/modules/twain_user/HGTwainImpl.hpp index 17faadbc..4b2434d1 100644 --- a/modules/twain_user/HGTwainImpl.hpp +++ b/modules/twain_user/HGTwainImpl.hpp @@ -25,7 +25,7 @@ struct HGTwainDSImpl HGBool open; HGBool showUI; HWND parent; - HGTwain_DSEventCallback func; + HGDSEventFunc func; HGPointer param; HGBool enable; }; @@ -50,8 +50,8 @@ public: HGResult SetCapability(HGTwainDSImpl* ds, HGUInt cap, HGInt value); HGResult GetCapability(HGTwainDSImpl* ds, HGUInt cap, HGInt* value); - HGResult EnableDSUIOnly(HGTwainDSImpl* ds, HGBool showUI, HWND parent, HGTwain_DSEventCallback func, HGPointer param); - HGResult EnableDS(HGTwainDSImpl* ds, HGBool showUI, HWND parent, HGTwain_DSEventCallback func, HGPointer param); + HGResult EnableDSUIOnly(HGTwainDSImpl* ds, HGBool showUI, HWND parent, HGDSEventFunc func, HGPointer param); + HGResult EnableDS(HGTwainDSImpl* ds, HGBool showUI, HWND parent, HGDSEventFunc func, HGPointer param); HGResult DisableDS(HGTwainDSImpl* ds); HGResult ImageNativeXfer(HGTwainDSImpl* ds, HGUInt type, HGUInt origin, HGImage* image); HGResult EndXfer(HGTwainDSImpl* ds, HGUInt* count); diff --git a/sdk/webservice/1.0/HttpHead.cpp b/sdk/webservice/1.0/HttpHead.cpp deleted file mode 100644 index 68225095..00000000 --- a/sdk/webservice/1.0/HttpHead.cpp +++ /dev/null @@ -1,438 +0,0 @@ -#include "HttpHead.h" - -const unsigned int asciiTableData[256] = -{ - 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, - 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004, - 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, - 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, - 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, - 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, - 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, - 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, - 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253, - 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, - 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, - 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, - 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073, - 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, - 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, - 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004 - /* the upper 128 are all zeroes */ -}; - -static void TrimString(std::string& str) -{ - std::string str1; - bool add1 = false; - std::string::const_iterator iter1; - for (iter1 = str.begin(); iter1 != str.end(); ++iter1) - { - int c = (HGByte)(*iter1); - if (!add1) - { - if (!isspace(c)) - { - str1.push_back(c); - add1 = true; - } - } - else - { - str1.push_back(c); - } - } - - if (str1.empty()) - { - str.clear(); - return; - } - - std::string str2; - bool add2 = false; - std::string::const_reverse_iterator iter2; - for (iter2 = str1.rbegin(); iter2 != str1.rend(); ++iter2) - { - int c = (HGByte)(*iter2); - if (!add2) - { - if (!isspace(c)) - { - str2.push_back(c); - add2 = true; - } - } - else - { - str2.push_back(c); - } - } - - if (str2.empty()) - { - str.clear(); - return; - } - - str = std::string(str2.rbegin(), str2.rend()); -} - -HttpHead::HttpHead() -{ - -} - -HttpHead::~HttpHead() -{ - -} - -bool HttpHead::Parse(const std::string& head) -{ - AnalysisHead(head, m_requestMethod, m_requestURIPath, m_requestURIQueryInfos, - m_requestURIFragment, m_requestHttpVersion, m_headInfos); - return true; -} - -void HttpHead::Clear() -{ - m_requestMethod.clear(); - m_requestURIPath.clear(); - m_requestURIQueryInfos.clear(); - m_requestURIFragment.clear(); - m_requestHttpVersion.clear(); - m_headInfos.clear(); -} - -std::string HttpHead::GetRequestMethod() const -{ - return m_requestMethod; -} - -std::string HttpHead::GetRequestURIPath() const -{ - return m_requestURIPath; -} - -HttpPairs HttpHead::GetRequestURIQueryInfos() const -{ - return m_requestURIQueryInfos; -} - -std::string HttpHead::GetRequestURIFragment() const -{ - return m_requestURIFragment; -} - -std::string HttpHead::GetRequestHttpVersion() const -{ - return m_requestHttpVersion; -} - -HttpPairs HttpHead::GetHeadInfos() const -{ - return m_headInfos; -} - -int HttpHead::GetContentLength() const -{ - int len = 0; - for (int i = 0; i < (int)m_headInfos.size(); ++i) - { -#if defined(HG_CMP_MSC) - if (0 == _stricmp("Content-Length", m_headInfos[i].first.c_str())) -#else - if (0 == strcasecmp("Content-Length", m_headInfos[i].first.c_str())) -#endif - { - len = atoi(m_headInfos[i].second.c_str()); - break; - } - } - - return len; -} - -std::string HttpHead::GetContentType() const -{ - std::string type; - for (int i = 0; i < (int)m_headInfos.size(); ++i) - { -#if defined(HG_CMP_MSC) - if (0 == _stricmp("Content-Type", m_headInfos[i].first.c_str())) -#else - if (0 == strcasecmp("Content-Type", m_headInfos[i].first.c_str())) -#endif - { - type = m_headInfos[i].second.c_str(); - break; - } - } - - return type; -} - -std::string HttpHead::GetValue(const HttpPairs& infos, const std::string& key) -{ - std::string value; - for (int i = 0; i < (int)infos.size(); ++i) - { - if (key == infos[i].first) - { - value = infos[i].second; - break; - } - } - - return value; -} - -void HttpHead::AnalysisURIQuery(const std::string& query, HttpPairs& queryInfos) -{ - std::vector queryList; - - char* p = new char[query.size() + 1]; - strcpy(p, query.c_str()); - char* pStr = strtok(p, "&"); - if (NULL != pStr) - queryList.push_back(pStr); - while (1) - { - pStr = strtok(NULL, "&"); - if (NULL == pStr) - break; - queryList.push_back(pStr); - } - delete[] p; - - queryInfos.clear(); - for (int i = 0; i < (int)queryList.size(); ++i) - { - p = new char[queryList[i].size() + 1]; - strcpy(p, queryList[i].c_str()); - - std::pair pr; - pStr = strtok(p, "="); - if (NULL != pStr) - pr.first = AnalyURIString(pStr); - pStr = strtok(NULL, "="); - if (NULL != pStr) - pr.second = AnalyURIString(pStr); - - queryInfos.push_back(pr); - delete[] p; - } -} - -void HttpHead::AnalysisURI(const std::string& uri, std::string& path, HttpPairs& queryInfos, std::string& fragment) -{ - size_t pathPos = uri.find('/'); - size_t queryPos = uri.find('?'); - size_t fragmentPos = uri.find('#'); - - path.clear(); - if (std::string::npos != pathPos) - { - size_t count = std::string::npos; - if (queryPos != std::string::npos) - { - assert(queryPos > pathPos); - count = queryPos - pathPos; - } - else if (fragmentPos != std::string::npos) - { - assert(fragmentPos > pathPos); - count = fragmentPos - pathPos; - } - - path = AnalyURIString(uri.substr(pathPos, count)); - } - - queryInfos.clear(); - if (std::string::npos != queryPos) - { - size_t count = std::string::npos; - if (fragmentPos != std::string::npos) - { - assert(fragmentPos > queryPos); - count = fragmentPos - queryPos; - } - - std::string query = uri.substr(queryPos + 1, count - 1); - AnalysisURIQuery(query, queryInfos); - } - - fragment.clear(); - if (std::string::npos != fragmentPos) - { - fragment = AnalyURIString(uri.substr(fragmentPos + 1)); - } -} - -void HttpHead::AnalysisHead(const std::string& head, std::string& requestMethod, std::string& requestURIPath, - HttpPairs& requestURIQueryInfos, std::string& requestURIFragment, std::string& httpVersion, HttpPairs& headInfos) -{ - requestMethod.clear(); - requestURIPath.clear(); - requestURIQueryInfos.clear(); - requestURIFragment.clear(); - httpVersion.clear(); - headInfos.clear(); - - std::vector headList; - - char* p = new char[head.size() + 1]; - strcpy(p, head.c_str()); - char* pStr = strtok(p, "\r\n"); - if (NULL != pStr) - headList.push_back(pStr); - while (1) - { - pStr = strtok(NULL, "\r\n"); - if (NULL == pStr) - break; - headList.push_back(pStr); - } - delete[] p; - - if (headList.size() < 1) - { - return; - } - - std::string requestURI; - - // 解析请求行 - p = new char[headList[0].size() + 1]; - strcpy(p, headList[0].c_str()); - pStr = strtok(p, " "); - if (NULL != pStr) - requestMethod = pStr; - pStr = strtok(NULL, " "); - if (NULL != pStr) - requestURI = pStr; - pStr = strtok(NULL, " "); - if (NULL != pStr) - httpVersion = pStr; - delete[] p; - - // 解析URI - AnalysisURI(requestURI, requestURIPath, requestURIQueryInfos, requestURIFragment); - - // 解析请求头 - for (int i = 1; i < (int)headList.size(); ++i) - { - p = new char[headList[i].size() + 1]; - strcpy(p, headList[i].c_str()); - - std::pair pr; - pStr = strtok(p, ":"); - if (NULL != pStr) - pr.first = pStr; - pStr = strtok(NULL, ":"); - if (NULL != pStr) - pr.second = pStr; - - TrimString(pr.first); - TrimString(pr.second); - headInfos.push_back(pr); - - delete[] p; - } -} - -/*判断ascii码是否是数字0-9*/ -static bool asciiIsDigit(char c) -{ - /*字符的ascii码&8 结果为0-127,则是数字*/ - return asciiTableData[(unsigned char)c & (1 << 3)]; -} - -static int asciiDigitValue(char c) -{ - if (asciiIsDigit(c)) - return c - '0'; - return -1; -} - -static int asciiXdigitValue(char c) -{ - //printf("-->%c\n",c); - if (c >= 'A' && c <= 'F') - return c - 'A' + 10;//(A B C D E F)->(10 11 12 13 14 15) - if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - - return asciiDigitValue(c);//('0'...'9')->(0...9) -} - -static int unescapeCharacter(const char* scanner) -{ - int first = asciiXdigitValue(scanner[0]); - if (first < 0) - return -1; - - int second = asciiXdigitValue(scanner[1]); - if (second < 0) - return -1; - - return (first << 4) | second; //== (first*16 | second) == (first*16 + second) -} - -static char* unescapeUriString(const char* uriString, bool asciiEscape) -{ - if (NULL == uriString) - return NULL; - - int strLen = (int)strlen(uriString); - char *result = (char *)malloc(strLen + 1);//可推测解码后的长度<=原长度 - char *out = result; - - const char *in, *end; - for (in = uriString, end = in + strLen; in < end; ++in) - { - int c = *in; - - //遇到了'%'才去解析 - if ('%' == c) - { - if (in + 3 > end) - break; - //获取%后2个字符的解码值 - c = unescapeCharacter(in + 1); - if (c <= 0) - break; - - if (asciiEscape && c <= 0x7F) - break; - - in += 2;//一般的格式为%后加两个ascii码字符 - } - - *out++ = c;//存储转义结果 - } - - *out = '\0'; - - if (in != end) - { - free(result); - return NULL; - } - - return result; -} - -std::string HttpHead::AnalyURIString(const std::string& str) -{ - std::string ret; - char *decodeStr = unescapeUriString(str.c_str(), false); - if (NULL != decodeStr) - { - ret = decodeStr; - free(decodeStr); - } - - return ret; -} \ No newline at end of file diff --git a/sdk/webservice/1.0/HttpHead.h b/sdk/webservice/1.0/HttpHead.h deleted file mode 100644 index ae69ecfc..00000000 --- a/sdk/webservice/1.0/HttpHead.h +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef __HTTPHEAD_H__ -#define __HTTPHEAD_H__ - -#include "base/HGDef.h" -#include "base/HGInc.h" -#include -#include - -typedef std::pair HttpPair; -typedef std::vector HttpPairs; - -class HttpHead -{ -public: - HttpHead(); - ~HttpHead(); - - bool Parse(const std::string &head); - void Clear(); - - std::string GetRequestMethod() const; - std::string GetRequestURIPath() const; - HttpPairs GetRequestURIQueryInfos() const; - std::string GetRequestURIFragment() const; - std::string GetRequestHttpVersion() const; - HttpPairs GetHeadInfos() const; - int GetContentLength() const; - std::string GetContentType() const; - - static std::string GetValue(const HttpPairs& infos, const std::string &key); - static void AnalysisURIQuery(const std::string& query, HttpPairs& queryInfos); - static void AnalysisURI(const std::string &uri, std::string &path, HttpPairs &queryInfos, std::string& fragment); - static void AnalysisHead(const std::string& head, std::string& requestMethod, std::string& requestURIPath, - HttpPairs& requestURIQueryInfos, std::string& requestURIFragment, std::string &httpVersion, HttpPairs & headInfos); - -private: - static std::string AnalyURIString(const std::string& str); - -private: - std::string m_requestMethod; - std::string m_requestURIPath; - HttpPairs m_requestURIQueryInfos; - std::string m_requestURIFragment; - std::string m_requestHttpVersion; - HttpPairs m_headInfos; -}; - -#endif /* __HTTPHEAD_H__ */ \ No newline at end of file diff --git a/sdk/webservice/1.0/HttpServer.cpp b/sdk/webservice/1.0/HttpServer.cpp deleted file mode 100644 index 59966a4e..00000000 --- a/sdk/webservice/1.0/HttpServer.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "HttpServer.h" -#include "HttpUser.h" -#include "base/HGInfo.h" - -HttpServer::HttpServer(HGMsgPump msgPump, class Manager* manager) - : WebServer(msgPump, manager) -{ - -} - -HttpServer::~HttpServer() -{ - -} - -void HttpServer::Connect(const ConnectParam* param) -{ - assert(NULL != param); - - WebUser* user = new HttpUser(this, m_currUserId, param->ip, param->port, param->socket); - user->Open(); - ++m_currUserId; - m_vectorUser.push_back(user); -} - -void HttpServer::Command(HGUInt usrId, const HttpCmdParam* param) -{ - int nIndex = GetUserIndex(usrId); - if (-1 != nIndex) - { - ((HttpUser*)m_vectorUser[nIndex])->HandleCmd(param); - } -} diff --git a/sdk/webservice/1.0/HttpServer.h b/sdk/webservice/1.0/HttpServer.h deleted file mode 100644 index c482ef1a..00000000 --- a/sdk/webservice/1.0/HttpServer.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef __HTTPSERVER_H__ -#define __HTTPSERVER_H__ - -#include "WebServer.h" -#include "Msg_1_0.h" - -class HttpServer : public WebServer -{ -public: - HttpServer(HGMsgPump msgPump, class Manager *manager); - virtual ~HttpServer(); - - void Connect(const ConnectParam *param); - void Command(HGUInt usrId, const HttpCmdParam *param); -}; - -#endif /* __HTTPSERVER_H__ */ \ No newline at end of file diff --git a/sdk/webservice/1.0/HttpUser.cpp b/sdk/webservice/1.0/HttpUser.cpp deleted file mode 100644 index d4079118..00000000 --- a/sdk/webservice/1.0/HttpUser.cpp +++ /dev/null @@ -1,892 +0,0 @@ -#include "HttpUser.h" -#include "WebServer.h" -#include "Manager.h" -#include "base/HGInfo.h" -#include "cJSON.h" - -static std::string ToJsonPair(const std::string& key, int value, bool sep) -{ - char ret[256]; - sprintf(ret, "\"%s\":%d", key.c_str(), value); - - std::string retStr = ret; - if (sep) - retStr += ", "; - return retStr; -} - -static std::string ToJsonPair(const std::string& key, bool value, bool sep) -{ - char ret[256]; - sprintf(ret, "\"%s\":%s", key.c_str(), value ? "true" : "false"); - - std::string retStr = ret; - if (sep) - retStr += ", "; - return retStr; -} - -static std::string ToJsonPair(const std::string& key, const std::string& value, bool sep) -{ - char ret[256]; - if (value.empty()) - sprintf(ret, "\"%s\":null", key.c_str()); - else - sprintf(ret, "\"%s\":\"%s\"", key.c_str(), value.c_str()); - - std::string retStr = ret; - if (sep) - retStr += ", "; - return retStr; -} - -static int GetJsonIntValue(cJSON *json, const std::string& key) -{ - int ret = 0; - - cJSON* p = json->child; - while (NULL != p) - { - if (0 == strcmp(p->string, key.c_str())) - { - if (p->type == cJSON_Number) - ret = p->valueint; - break; - } - - p = p->next; - } - - return ret; -} - -static bool GetJsonBoolValue(cJSON* json, const std::string& key) -{ - bool ret = false; - - cJSON* p = json->child; - while (NULL != p) - { - if (0 == strcmp(p->string, key.c_str())) - { - if (p->type == cJSON_True) - ret = true; - break; - } - - p = p->next; - } - - return ret; -} - -static std::string GetJsonStringValue(cJSON* json, const std::string& key) -{ - std::string ret; - - cJSON* p = json->child; - while (NULL != p) - { - if (0 == strcmp(p->string, key.c_str())) - { - if (p->type == cJSON_String) - ret = p->valuestring; - break; - } - - p = p->next; - } - - return ret; -} - -#if defined(HG_CMP_MSC) -HttpUser::HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn) -#else -HttpUser::HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn) -#endif - : WebUser(server, id, ip, port, sockConn) -{ - -} - -HttpUser::~HttpUser() -{ - -} - -void HttpUser::HandleCmd(const HttpCmdParam* param) -{ - std::string requestMethod = param->head.GetRequestMethod(); - std::string requestURIPath = param->head.GetRequestURIPath(); - HttpPairs requestURIQueryInfos = param->head.GetRequestURIQueryInfos(); - std::string requestHttpVersion = param->head.GetRequestHttpVersion(); - - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "HttpUser: method=%s, path=%s, httpVersion=%s", - requestMethod.c_str(), requestURIPath.c_str(), requestHttpVersion.c_str()); - - if (requestMethod == "POST" && requestURIPath == "/WebScan/getVersionInfo") - { - if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) - { - std::string query((const char*)param->data, param->size); - - HttpPairs uriQueryInfos; - HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); - } - - std::string devId; - GetManager()->GetCurDevId(devId); - - char* result = new char[devId.size() + 256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, devId.c_str()); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte *)result, (HGUInt)strlen(result), "application/json"); - delete[] result; - } - else if (requestMethod == "GET" && requestURIPath == "/WebScan/getDevices") - { - std::vector devNameList; - GetManager()->GetDevNames(devNameList); - - std::string devNames = "["; - for (int i = 0; i < (int)devNameList.size(); ++i) - { - devNames += "\""; - devNames += devNameList[i]; - devNames += "\""; - if (i != (int)devNameList.size() - 1) - devNames += ","; - } - devNames += "]"; - - char* result = new char[devNames.size() + 256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, devNames.c_str()); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - delete[] result; - } - else if (requestMethod == "GET" && requestURIPath == "/WebScan/image/getImageByPid") - { - std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); - std::vector imgNameList; - std::vector imgBase64List; - GetManager()->GetImageByDevId(devId, imgNameList, imgBase64List); - - std::string imgInfos = "["; - for (int i = 0; i < (int)imgNameList.size(); ++i) - { - imgInfos += "{\"imageName\":\""; - imgInfos += imgNameList[i]; - imgInfos += "\",\"src\":\""; - imgInfos += imgBase64List[i]; - imgInfos += "\"}"; - - if (i != imgNameList.size() - 1) - imgInfos += ","; - } - imgInfos += "]"; - - char *result = new char[imgInfos.size() + 256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgInfos.c_str()); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - delete[] result; - } - else if (requestMethod == "GET" && requestURIPath == "/WebScan/getParams") - { - std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); - DevParam devParam; - GetManager()->GetDevParam(devId, devParam); - - std::string devParamStr = "{"; - devParamStr += ToJsonPair("device", devParam.device, true); - devParamStr += ToJsonPair("autofeeder", devParam.autofeeder, true); - devParamStr += ToJsonPair("pixel", devParam.pixel, true); - devParamStr += ToJsonPair("white", devParam.white, true); - devParamStr += ToJsonPair("discardBlankThre", devParam.discardBlankThre, true); - devParamStr += ToJsonPair("single", devParam.single, true); - devParamStr += ToJsonPair("format", devParam.format, true); - devParamStr += ToJsonPair("resolution", devParam.resolution, true); - devParamStr += ToJsonPair("orentation", devParam.orentation, true); - devParamStr += ToJsonPair("paperType", devParam.paperType, true); - devParamStr += ToJsonPair("splitImage", devParam.splitImage, true); - devParamStr += ToJsonPair("noiseDetachEnable", devParam.noiseDetachEnable, true); - devParamStr += ToJsonPair("noiseDetach", devParam.noiseDetach, true); - devParamStr += "\"upload\":{"; - devParamStr += ToJsonPair("uploadMode", devParam.uploadMode, true); - devParamStr += ToJsonPair("httpUrl", devParam.httpUrl, true); - devParamStr += ToJsonPair("fileName", devParam.fileName, true); - devParamStr += ToJsonPair("httpMethod", devParam.httpMethod, true); - devParamStr += ToJsonPair("header", devParam.header, true); - devParamStr += ToJsonPair("param", devParam.param, true); - devParamStr += ToJsonPair("ftpUrl", devParam.ftpUrl, true); - devParamStr += ToJsonPair("ftpPath", devParam.ftpPath, true); - devParamStr += ToJsonPair("ftpUser", devParam.ftpUser, true); - devParamStr += ToJsonPair("ftpPassword", devParam.ftpPassword, true); - devParamStr += ToJsonPair("ftpPort", devParam.ftpPort, true); - devParamStr += ToJsonPair("ftpMode", devParam.ftpMode, false); - devParamStr += "}}"; - - char* result = new char[devParamStr.size() + 256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, devParamStr.c_str()); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - delete[] result; - } - else if (requestMethod == "POST" && requestURIPath == "/WebScan/setParams") - { - if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) - { - std::string query((const char *)param->data, param->size); - HttpPairs uriQueryInfos; - HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); - - std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); - std::string devParamStr = HttpHead::GetValue(uriQueryInfos, "params"); - DevParam devParam; - - cJSON* json = cJSON_Parse(devParamStr.c_str()); - if (NULL != json) - { - devParam.autofeeder = GetJsonBoolValue(json, "autofeeder"); - devParam.pixel = GetJsonIntValue(json, "pixel"); - devParam.white = GetJsonBoolValue(json, "white"); - devParam.discardBlankThre = GetJsonIntValue(json, "discardBlankThre"); - devParam.single = GetJsonBoolValue(json, "single"); - devParam.format = GetJsonStringValue(json, "format"); - devParam.resolution = GetJsonIntValue(json, "resolution"); - if (0 == devParam.resolution) - devParam.resolution = atoi(GetJsonStringValue(json, "resolution").c_str()); - devParam.orentation = GetJsonIntValue(json, "orentation"); - devParam.paperType = GetJsonStringValue(json, "paperType"); - devParam.splitImage = GetJsonIntValue(json, "splitImage"); - devParam.noiseDetachEnable = GetJsonBoolValue(json, "noiseDetachEnable"); - devParam.noiseDetach = GetJsonIntValue(json, "noiseDetach"); - - cJSON* p = json->child; - while (NULL != p) - { - if (0 == strcmp(p->string, "upload")) - { - break; - } - - p = p->next; - } - - if (NULL != p && p->type == cJSON_Object) - { - devParam.uploadMode = GetJsonIntValue(p, "uploadMode"); - devParam.httpUrl = GetJsonStringValue(p, "httpUrl"); - devParam.fileName = GetJsonStringValue(p, "fileName"); - devParam.httpMethod = GetJsonStringValue(p, "httpMethod"); - devParam.header = GetJsonStringValue(p, "header"); - devParam.param = GetJsonStringValue(p, "param"); - devParam.ftpUrl = GetJsonStringValue(p, "ftpUrl"); - devParam.ftpPath = GetJsonStringValue(p, "ftpPath"); - if (!devParam.ftpPath.empty() && devParam.ftpPath[0] != '/') - devParam.ftpPath.insert(0, "/"); - devParam.ftpUser = GetJsonStringValue(p, "ftpUser"); - devParam.ftpPassword = GetJsonStringValue(p, "ftpPassword"); - devParam.ftpPort = GetJsonIntValue(p, "ftpPort"); - if (0 == devParam.ftpPort) - devParam.ftpPort = atoi(GetJsonStringValue(p, "ftpPort").c_str()); - devParam.ftpMode = GetJsonIntValue(p, "ftpMode"); - } - - cJSON_Delete(json); - } - - GetManager()->SetDevParam(devId, devParam); - - char result[256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"\"}", 200); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - } - } - else if (requestMethod == "POST" && requestURIPath == "/WebScan/majorOfd") - { - if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) - { - std::string query((const char*)param->data, param->size); - HttpPairs uriQueryInfos; - HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); - - std::string majorOfdParam = HttpHead::GetValue(uriQueryInfos, "formDataString"); - - std::string devId; - bool isAuto = false; - cJSON* json = cJSON_Parse(majorOfdParam.c_str()); - if (NULL != json) - { - devId = GetJsonStringValue(json, "pid"); - isAuto = GetJsonBoolValue(json, "isAuto"); - cJSON_Delete(json); - } - - std::string imgBase64; - GetManager()->ExportOfd(devId, isAuto, imgBase64); - - char* result = new char[imgBase64.size() + 256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgBase64.c_str()); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - delete[] result; - } - } - else if (requestMethod == "GET" && requestURIPath == "/WebScan/majorOfdFile") - { - std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); - bool isAuto = ("true" == HttpHead::GetValue(requestURIQueryInfos, "isAuto") ? true : false); - - HGByte* data = NULL; - HGUInt size = 0; - GetManager()->ExportOfdFile(devId, isAuto, &data, &size); - SendResponse(requestHttpVersion.c_str(), 200, "OK", data, size, "application/zip"); - delete[] data; - } - else if (requestMethod == "POST" && requestURIPath == "/WebScan/majorPdf") - { - if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) - { - std::string query((const char*)param->data, param->size); - HttpPairs uriQueryInfos; - HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); - - std::string majorOfdParam = HttpHead::GetValue(uriQueryInfos, "formDataString"); - - std::string devId; - cJSON* json = cJSON_Parse(majorOfdParam.c_str()); - if (NULL != json) - { - devId = GetJsonStringValue(json, "pid"); - cJSON_Delete(json); - } - - std::string imgBase64; - GetManager()->ExportPdf(devId, imgBase64); - - char* result = new char[imgBase64.size() + 256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgBase64.c_str()); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - delete[] result; - } - } - else if (requestMethod == "GET" && requestURIPath == "/WebScan/majorPdfFile") - { - std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); - - HGByte* data = NULL; - HGUInt size = 0; - GetManager()->ExportPdfFile(devId, &data, &size); - SendResponse(requestHttpVersion.c_str(), 200, "OK", data, size, "application/pdf"); - delete[] data; - } - else if (requestMethod == "POST" && requestURIPath == "/WebScan/majorTiff") - { - if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) - { - std::string query((const char*)param->data, param->size); - HttpPairs uriQueryInfos; - HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); - - std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); - std::string imgBase64; - GetManager()->ExportTiff(devId, imgBase64); - - char* result = new char[imgBase64.size() + 256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgBase64.c_str()); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - delete[] result; - } - } - else if (requestMethod == "GET" && requestURIPath == "/WebScan/majorTiffFile") - { - std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); - - HGByte* data = NULL; - HGUInt size = 0; - GetManager()->ExportTiffFile(devId, &data, &size); - SendResponse(requestHttpVersion.c_str(), 200, "OK", data, size, "application/x-tif"); - delete[] data; - } - else if (requestMethod == "GET" && requestURIPath == "/WebScan/downLoadZip") - { - if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) - { - std::string query((const char*)param->data, param->size); - HttpPairs uriQueryInfos; - HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); - - std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); - std::string imgBase64; - GetManager()->ExportZip(devId, imgBase64); - - char* result = new char[imgBase64.size() + 256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgBase64.c_str()); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - delete[] result; - } - } - else if (requestMethod == "GET" && requestURIPath == "/WebScan/downLoadZipFile") - { - std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); - - HGByte* data = NULL; - HGUInt size = 0; - GetManager()->ExportZipFile(devId, &data, &size); - SendResponse(requestHttpVersion.c_str(), 200, "OK", data, size, "application/zip"); - delete[] data; - } - else if (requestMethod == "POST" && requestURIPath == "/WebScan/uploadImage") - { - if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) - { - std::string query((const char*)param->data, param->size); - HttpPairs uriQueryInfos; - HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); - - std::string uploadParamStr = HttpHead::GetValue(uriQueryInfos, "formDataString"); - UploadParam uploadParam; - - cJSON* json = cJSON_Parse(uploadParamStr.c_str()); - if (NULL != json) - { - uploadParam.uploadMode = GetJsonIntValue(json, "uploadMode"); - uploadParam.httpUrl = GetJsonStringValue(json, "httpUrl"); - uploadParam.fileName = GetJsonStringValue(json, "fileName"); - uploadParam.httpMethod = GetJsonStringValue(json, "httpMethod"); - uploadParam.header = GetJsonStringValue(json, "header"); - uploadParam.param = GetJsonStringValue(json, "param"); - uploadParam.ftpUrl = GetJsonStringValue(json, "ftpUrl"); - uploadParam.ftpPath = GetJsonStringValue(json, "ftpPath"); - if (!uploadParam.ftpPath.empty() && uploadParam.ftpPath[0] != '/') - uploadParam.ftpPath.insert(0, "/"); - uploadParam.ftpUser = GetJsonStringValue(json, "ftpUser"); - uploadParam.ftpPassword = GetJsonStringValue(json, "ftpPassword"); - uploadParam.ftpPort = GetJsonIntValue(json, "ftpPort"); - if (0 == uploadParam.ftpPort) - uploadParam.ftpPort = atoi(GetJsonStringValue(json, "ftpPort").c_str()); - uploadParam.ftpMode = GetJsonIntValue(json, "ftpMode"); - uploadParam.format = GetJsonIntValue(json, "format"); - - cJSON_Delete(json); - } - - bool ret = GetManager()->UploadImage(uploadParam); - - char result[256]; - if (ret) - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success"); - else - sprintf(result, "{\"code\":%d, \"msg\":\"%s\", \"data\":\"%s\"}", 201, "uploadImage failed", "fail"); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - } - } - else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/saveImage") - { - if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) - { - std::string query((const char*)param->data, param->size); - HttpPairs uriQueryInfos; - HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); - - std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); - std::string imgName = HttpHead::GetValue(uriQueryInfos, "imageName"); - std::string imgBase64 = HttpHead::GetValue(uriQueryInfos, "image"); - GetManager()->SaveImage(devId, imgName, imgBase64); - - char result[256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success"); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - } - } - else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/deleteImage") - { - if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) - { - std::string query((const char*)param->data, param->size); - HttpPairs uriQueryInfos; - HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); - - std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); - std::string imgName = HttpHead::GetValue(uriQueryInfos, "imageName"); - GetManager()->DeleteImage(devId, imgName); - - char result[256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success"); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - } - } - else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/deleteAllImage") - { - if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) - { - std::string query((const char*)param->data, param->size); - HttpPairs uriQueryInfos; - HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); - - std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); - GetManager()->DeleteAllImage(devId); - - char result[256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success"); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - } - } - else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/mergeHorizontal") - { - if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) - { - std::string query((const char*)param->data, param->size); - HttpPairs uriQueryInfos; - HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); - - std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); - bool isHorizontal = ("false" != HttpHead::GetValue(uriQueryInfos, "isHorizontal")) ? true : false; - - std::vector imgIndexs; - for (int i = 0; i < (int)uriQueryInfos.size(); ++i) - { - if ("indexs[]" == uriQueryInfos[i].first) - { - imgIndexs.push_back(atoi(uriQueryInfos[i].second.c_str())); - } - } - std::string imgName, imgBase64; - GetManager()->MergeImage(devId, isHorizontal, imgIndexs, imgName, imgBase64); - - char *result = new char[256 + imgName.size() + imgBase64.size()]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":{\"imageName\":\"%s\", \"src\":\"%s\"}}", - 200, imgName.c_str(), imgBase64.c_str()); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - delete[] result; - } - } - else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/bookSort") - { - if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) - { - std::string query((const char*)param->data, param->size); - HttpPairs uriQueryInfos; - HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); - - std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); - std::vector imgNameList, imgBase64List; - GetManager()->BookSort(devId, imgNameList, imgBase64List); - - std::string imgInfos = "["; - for (int i = 0; i < (int)imgNameList.size(); ++i) - { - imgInfos += "{\"imageName\":\""; - imgInfos += imgNameList[i]; - imgInfos += "\",\"src\":\""; - imgInfos += imgBase64List[i]; - imgInfos += "\"}"; - - if (i != imgNameList.size() - 1) - imgInfos += ","; - } - imgInfos += "]"; - - char* result = new char[imgInfos.size() + 256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgInfos.c_str()); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - delete[] result; - } - } - else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/exchangeImage") - { - if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) - { - std::string query((const char*)param->data, param->size); - HttpPairs uriQueryInfos; - HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); - - std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); - int index1 = -1, index2 = -1; - for (int i = 0; i < (int)uriQueryInfos.size(); ++i) - { - if ("indexs[]" == uriQueryInfos[i].first) - { - if (-1 == index1) - index1 = atoi(uriQueryInfos[i].second.c_str()); - else if (-1 == index2) - index2 = atoi(uriQueryInfos[i].second.c_str()); - } - } - GetManager()->ExchangeImage(devId, index1, index2); - - char result[256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success"); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - } - } - else if (requestMethod == "GET" && requestURIPath == "/WebScan/getLastBatch") - { - std::string devId; - GetManager()->GetLastBetch(devId); - - char* result = new char[devId.size() + 256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, devId.c_str()); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - delete[] result; - } - else if (requestMethod == "GET" && requestURIPath == "/WebScan/getSerialNumber") - { - std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); - std::string devSerialNo; - GetManager()->GetDevSerialNo(devId, devSerialNo); - - char* result = new char[devSerialNo.size() + 256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, devSerialNo.c_str()); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - delete[] result; - } - else if (requestMethod == "GET" && requestURIPath == "/WebScan/getImageByName") - { - std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); - std::string imgName = HttpHead::GetValue(requestURIQueryInfos, "imageName"); - std::string imgBase64; - GetManager()->GetImageBase64(devId, imgName, imgBase64); - - char* result = new char[imgBase64.size() + 256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, imgBase64.c_str()); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - delete[] result; - } - else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/resetPatchIndex") - { - if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) - { - std::string query((const char*)param->data, param->size); - HttpPairs uriQueryInfos; - HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); - } - - GetManager()->ResetPatchIndex(); - - char result[256]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success"); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - } - else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/split") - { - if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) - { - std::string query((const char*)param->data, param->size); - HttpPairs uriQueryInfos; - HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); - - std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); - std::string imgName = HttpHead::GetValue(uriQueryInfos, "imageName"); - bool isHorizontal = ("false" != HttpHead::GetValue(uriQueryInfos, "isHorizontal")) ? true : false; - int x1 = atoi(HttpHead::GetValue(uriQueryInfos, "x1").c_str()); - int x2 = atoi(HttpHead::GetValue(uriQueryInfos, "x2").c_str()); - int y1 = atoi(HttpHead::GetValue(uriQueryInfos, "y1").c_str()); - int y2 = atoi(HttpHead::GetValue(uriQueryInfos, "y2").c_str()); - std::string imgName1, imgBase64_1, imgName2, imgBase64_2; - GetManager()->SplitImage(devId, imgName, isHorizontal, x1, y1, x2, y2, - imgName1, imgBase64_1, imgName2, imgBase64_2); - - char *result = new char[256 + imgName1.size() + imgBase64_1.size() + imgName2.size() + imgBase64_2.size()]; - sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":{\"oneSrc\":\"%s\", \"oneName\":\"%s\", \"twoSrc\":\"%s\", \"twoName\":\"%s\"}}", - 200, imgBase64_1.c_str(), imgName1.c_str(), imgBase64_2.c_str(), imgName2.c_str()); - SendResponse(requestHttpVersion.c_str(), 200, "OK", - (const HGByte*)result, (HGUInt)strlen(result), "application/json"); - delete[] result; - } - } -} - -void HttpUser::PostCmdMsg(const HttpHead& headInfo, const HGByte* data, HGUInt dataSize) -{ - HttpCmdParam* param = new HttpCmdParam; - param->svr = m_server; - param->usrId = m_id; - param->head = headInfo; - param->data = NULL; - param->size = 0; - if (0 != dataSize) - { - param->data = new HGByte[dataSize]; - param->size = dataSize; - memcpy(param->data, data, dataSize); - } - - HGMsg msg; - msg.id = MSGID_HTTP_COMMAND; - msg.data = param; - if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_server->GetMsgPump(), &msg)) - { - delete[] param->data; - param->size = 0; - delete param; - } -} - -void HttpUser::ThreadFunc() -{ - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "HttpUser::ThreadFunc"); - - char chBuffer[2048]; - const char* pBuffer = chBuffer; - int nBufferSize = 0; - - unsigned char headDataTail[4] = { '\r', '\n', '\r', '\n' }; - unsigned int headDataTailLen = 0; - std::string headData; - - HttpHead headInfo; - uint8_t* data = NULL; - int dataSize = 0; - bool getData = false; - int getDataSize = 0; - - while (1) - { - if (0 == nBufferSize) - { - int len = recv(m_sockConn, chBuffer, 2048, 0); - if (len <= 0) - { - // 这里跳出,可能是服务器关闭了socketConn,或者客户端关闭了socket,或者网络断开 - PostDisConnectMsg(); - break; - } - else - { - pBuffer = chBuffer; - nBufferSize = len; - } - } - - assert(nBufferSize > 0); - - unsigned char b = *pBuffer; - ++pBuffer; - --nBufferSize; - - if (!getData) - { - headData.push_back(b); - if (b == headDataTail[headDataTailLen]) - { - ++headDataTailLen; - } - else - { - headDataTailLen = 0; - if (b == headDataTail[headDataTailLen]) - { - ++headDataTailLen; - } - } - - if (4 == headDataTailLen) - { - headDataTailLen = 0; - if (!headInfo.Parse(headData.c_str())) - { - PostDisConnectMsg(); - break; - } - - headData.clear(); - int contentLen = headInfo.GetContentLength(); - if (contentLen > 0) - { - data = new uint8_t[contentLen]; - dataSize = contentLen; - getData = true; - getDataSize = 0; - } - else - { - PostCmdMsg(headInfo, NULL, 0); - headInfo.Clear(); - } - } - } - else - { - data[getDataSize] = b; - ++getDataSize; - if (getDataSize == dataSize) - { - PostCmdMsg(headInfo, data, dataSize); - - delete [] data; - data = NULL; - dataSize = 0; - getData = false; - getDataSize = 0; - headInfo.Clear(); - } - } - } - - if (NULL != data) - { - delete[] data; - data = NULL; - dataSize = 0; - getData = false; - getDataSize = 0; - } -} - -bool HttpUser::SendResponse(const char* httpVersion, HGUInt errCode, const char* errInfo, - const HGByte* data, HGUInt size, const char* contentType) -{ - if (NULL == httpVersion || NULL == errInfo) - { - return false; - } - - char response[256]; - sprintf(response, "%s %u %s\r\n", httpVersion, errCode, errInfo); - - char head[256]; - if (NULL != data) - { - assert(0 != size); - assert(NULL != contentType); - - sprintf(head, "%s: %u\r\n%s: %s\r\n%s: %s\r\n\r\n", - "Content-Length", size, - "Content-Type", contentType, - "Access-Control-Allow-Origin", "null"); - } - else - { - assert(0 == size); - - sprintf(head, "%s: %s\r\n\r\n", - "Access-Control-Allow-Origin", "null"); - } - - HGBase_EnterLock(m_cs); - send(m_sockConn, response, (int)strlen(response), 0); - send(m_sockConn, head, (int)strlen(head), 0); - if (NULL != data && 0 != size) - send(m_sockConn, (const char *)data, (int)size, 0); - HGBase_LeaveLock(m_cs); - return true; -} diff --git a/sdk/webservice/1.0/HttpUser.h b/sdk/webservice/1.0/HttpUser.h deleted file mode 100644 index 80e93011..00000000 --- a/sdk/webservice/1.0/HttpUser.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef __HTTPUSER_H__ -#define __HTTPUSER_H__ - -#include "WebUser.h" -#include "Msg_1_0.h" - -class WebServer; - -class HttpUser : public WebUser -{ -public: -#if defined(HG_CMP_MSC) - HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn); -#else - HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn); -#endif - virtual ~HttpUser(); - - void HandleCmd(const HttpCmdParam* param); - -protected: - void PostCmdMsg(const HttpHead& headInfo, const HGByte *data, HGUInt dataSize); - virtual void ThreadFunc(); - -private: - bool SendResponse(const char* httpVersion, HGUInt errCode, const char* errInfo, - const HGByte *data, HGUInt size, const char *contentType); -}; - -#endif /* __HTTPUSER_H__ */ diff --git a/sdk/webservice/1.0/Manager.cpp b/sdk/webservice/1.0/Manager.cpp deleted file mode 100644 index 1be346e8..00000000 --- a/sdk/webservice/1.0/Manager.cpp +++ /dev/null @@ -1,2065 +0,0 @@ -#include "Manager.h" -#include "base/HGBuffer.h" -#include "base/HGBase64.h" -#include "base/HGUtility.h" -#include "base/HGInfo.h" -#include "imgfmt/HGJpeg.h" -#include "imgfmt/HGOfd.h" -#include "imgfmt/HGPdf.h" -#include "imgfmt/HGTiff.h" -#include "imgfmt/HGImgFmt.h" -#include "HGString.h" -extern "C" -{ -#include "zip.h" -}; -#include -#include -#include - -DevParam::DevParam() -{ - Reset(); -} - -DevParam::~DevParam() -{ - -} - -void DevParam::Reset() -{ - autofeeder = true; - pixel = 1; - white = false; - discardBlankThre = 5; - single = false; - format = "jpg"; - resolution = 200; - orentation = 0; - paperType = "Auto"; - splitImage = 0; - noiseDetachEnable = true; - noiseDetach = 15; - - uploadMode = 2; - ftpPath = "/images"; - ftpPort = 21; - ftpMode = 2; -} - -void DevParam::Load(const std::string& cfgPath) -{ - HGInt intValue; - HGChar strValue[256]; - - HGBase_GetProfileString(cfgPath.c_str(), "devParam", "device", "", strValue, 256); - device = strValue; - HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "autofeeder", 1, &intValue); - autofeeder = (bool)intValue; - HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "pixel", 1, &intValue); - pixel = intValue; - HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "white", 0, &intValue); - white = (bool)intValue; - HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "discardBlankThre", 5, &intValue); - discardBlankThre = intValue; - HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "single", 0, &intValue); - single = (bool)intValue; - HGBase_GetProfileString(cfgPath.c_str(), "devParam", "format", "jpg", strValue, 256); - format = strValue; - HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "resolution", 200, &intValue); - resolution = intValue; - HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "orentation", 0, &intValue); - orentation = intValue; - HGBase_GetProfileString(cfgPath.c_str(), "devParam", "paperType", "Auto", strValue, 256); - paperType = strValue; - HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "splitImage", 0, &intValue); - splitImage = intValue; - HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "noiseDetachEnable”", 1, &intValue); - noiseDetachEnable = (bool)intValue; - HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "noiseDetach", 15, &intValue); - noiseDetach = intValue; - - HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "uploadMode", 2, &intValue); - uploadMode = intValue; - HGBase_GetProfileString(cfgPath.c_str(), "devParam", "httpUrl", "", strValue, 256); - httpUrl = strValue; - HGBase_GetProfileString(cfgPath.c_str(), "devParam", "fileName", "", strValue, 256); - fileName = strValue; - HGBase_GetProfileString(cfgPath.c_str(), "devParam", "httpMethod", "", strValue, 256); - httpMethod = strValue; - HGBase_GetProfileString(cfgPath.c_str(), "devParam", "header", "", strValue, 256); - header = strValue; - HGBase_GetProfileString(cfgPath.c_str(), "devParam", "param", "", strValue, 256); - param = strValue; - HGBase_GetProfileString(cfgPath.c_str(), "devParam", "ftpUrl", "", strValue, 256); - ftpUrl = strValue; - HGBase_GetProfileString(cfgPath.c_str(), "devParam", "ftpPath", "", strValue, 256); - ftpPath = strValue; - HGBase_GetProfileString(cfgPath.c_str(), "devParam", "ftpUser", "", strValue, 256); - ftpUser = strValue; - HGBase_GetProfileString(cfgPath.c_str(), "devParam", "ftpPassword", "", strValue, 256); - ftpPassword = strValue; - HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "ftpPort", 21, &intValue); - ftpPort = intValue; - HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "ftpMode", 2, &intValue); - ftpMode = intValue; -} - -void DevParam::Save(const std::string& cfgPath) -{ - HGBase_SetProfileString(cfgPath.c_str(), "devParam", "device", device.c_str()); - HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "autofeeder", autofeeder); - HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "pixel", pixel); - HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "white", white); - HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "discardBlankThre", discardBlankThre); - HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "single", single); - HGBase_SetProfileString(cfgPath.c_str(), "devParam", "format", format.c_str()); - HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "resolution", resolution); - HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "orentation", orentation); - HGBase_SetProfileString(cfgPath.c_str(), "devParam", "paperType", paperType.c_str()); - HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "splitImage", splitImage); - HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "noiseDetachEnable", noiseDetachEnable); - HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "noiseDetach", noiseDetach); - - HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "uploadMode", uploadMode); - HGBase_SetProfileString(cfgPath.c_str(), "devParam", "httpUrl", httpUrl.c_str()); - HGBase_SetProfileString(cfgPath.c_str(), "devParam", "fileName", fileName.c_str()); - HGBase_SetProfileString(cfgPath.c_str(), "devParam", "httpMethod", httpMethod.c_str()); - HGBase_SetProfileString(cfgPath.c_str(), "devParam", "header", header.c_str()); - HGBase_SetProfileString(cfgPath.c_str(), "devParam", "param", param.c_str()); - HGBase_SetProfileString(cfgPath.c_str(), "devParam", "ftpUrl", ftpUrl.c_str()); - HGBase_SetProfileString(cfgPath.c_str(), "devParam", "ftpPath", ftpPath.c_str()); - HGBase_SetProfileString(cfgPath.c_str(), "devParam", "ftpUser", ftpUser.c_str()); - HGBase_SetProfileString(cfgPath.c_str(), "devParam", "ftpPassword", ftpPassword.c_str()); - HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "ftpPort", ftpPort); - HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "ftpMode", ftpMode); -} - -Manager::Manager(HGMsgPump msgPump) -{ - m_msgPump = msgPump; - HGBase_CreateLock(&m_lock); - m_devName.clear(); - m_devHandle = NULL; - m_scanEvent = NULL; - m_scanParam = NULL; - - m_scanInsertImgName.clear(); - m_scanIsInsert = false; - m_scanning = false; - - SANE_Int version_code = 0; - SANE_Status status = sane_init_ex(&version_code, sane_ex_callback, this); - assert(SANE_STATUS_GOOD == status); -} - -Manager::~Manager() -{ - if (NULL != m_devHandle) - { - StopScan(); - - m_devParam.Reset(); - sane_close(m_devHandle); - m_devHandle = NULL; - m_devName.clear(); - } - - sane_exit(); - - HGBase_DestroyLock(m_lock); - m_lock = NULL; -} - -void Manager::OpenDev(const OpenDevParam* param) -{ - if (!m_devName.empty() || NULL != m_devHandle) - { - return; - } - - SANE_Handle devHandle = NULL; - SANE_Status status = sane_open(param->devName.c_str(), &devHandle); - if (SANE_STATUS_GOOD == status) - { - assert(NULL != devHandle); - - m_devName = param->devName; - m_devHandle = devHandle; - - std::string filePath = GetFilePath(m_devName); - std::string cfgPath = filePath + "config.ini"; - m_devParam.Load(cfgPath); - SetDevParam(m_devName, m_devParam); - } -} - -void Manager::CloseDev(const CloseDevParam* param) -{ - if (m_devName.empty() || NULL == m_devHandle || m_devName != param->devName) - { - return; - } - - assert(NULL != m_devHandle); - - StopScan(); - - m_devParam.Reset(); - sane_close(m_devHandle); - m_devHandle = NULL; - m_devName.clear(); -} - -void Manager::ScanFinish() -{ - m_scanInsertImgName.clear(); - m_scanIsInsert = false; - m_scanning = false; -} - -void Manager::SetScanEvent(ScanEvent event, void* param) -{ - assert(NULL != event && NULL != param); - - HGBase_EnterLock(m_lock); - m_scanEvent = event; - m_scanParam = param; - HGBase_LeaveLock(m_lock); -} - -void Manager::ResetScanEvent() -{ - HGBase_EnterLock(m_lock); - m_scanEvent = NULL; - m_scanParam = NULL; - HGBase_LeaveLock(m_lock); -} - -bool Manager::Scan(const std::string& insertImgName, bool isInsert) -{ - if (m_scanning) - { - return false; - } - - bool ret = false; - if (NULL != m_devHandle) - { - m_scanInsertImgName = insertImgName; - m_scanIsInsert = isInsert; - if (SANE_STATUS_GOOD == sane_start(m_devHandle)) - { - m_scanning = true; - ret = true; - } - else - { - m_scanInsertImgName.clear(); - m_scanIsInsert = false; - } - } - - return ret; -} - -bool Manager::StopScan() -{ - if (m_scanning) - { - assert(NULL != m_devHandle); - sane_cancel(m_devHandle); - } - - return true; -} - -bool Manager::GetCurDevId(std::string& devId) -{ - devId.clear(); - if (m_scanning) - { - return false; - } - - devId = m_devName; - return true; -} - -bool Manager::GetDevNames(std::vector& devNameList) -{ - devNameList.clear(); - if (m_scanning) - { - return false; - } - - const SANE_Device** device_list; - if (SANE_STATUS_GOOD == sane_get_devices(&device_list, SANE_TRUE)) - { - const SANE_Device** p; - for (p = device_list; *p != NULL; ++p) - { - devNameList.push_back((*p)->name); - } - } - - return true; -} - -bool Manager::GetImageByDevId(const std::string& devId, std::vector& imgNameList, - std::vector& imgBase64List) -{ - imgNameList.clear(); - imgBase64List.clear(); - if (m_scanning) - { - return false; - } - - std::string filePath = GetFilePath(devId); - std::vector fileNameList = GetFileNameList(devId); - HGBase_WriteInfo(HGBASE_INFOTYPE_DEBUG, "filePath=%s", filePath.c_str()); - - for (int i = 0; i < (int)fileNameList.size(); ++i) - { - std::string fileName = filePath + fileNameList[i]; - - HGImage img = NULL; - HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); - if (NULL != img) - { - std::string imgBase64 = "data:image/jpeg;base64,"; - imgBase64 += GetBase64(img); - - imgNameList.push_back(fileNameList[i]); - imgBase64List.push_back(imgBase64); - HGBase_DestroyImage(img); - } - } - - SaveFileNameList(devId, imgNameList); - return true; -} - -bool Manager::GetDevParam(const std::string& devId, DevParam& devParam) -{ - devParam.Reset(); - if (m_scanning) - { - return false; - } - - bool ret = false; - if (NULL != m_devHandle) - { - m_devParam.device = m_devName; - - // 从设备获取到m_devParam - - SANE_Int num_dev_options = 0; - sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL); - for (int i = 1; i < num_dev_options; ++i) - { - const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i); - if (NULL == desp) - continue; - - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "title=%s", Utf8ToStdString(desp->title).c_str()); - - if (SANE_TYPE_BOOL == desp->type) - { - SANE_Bool value = 0; - sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL); - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=BOOL, value=%s", value ? "TRUE" : "FALSE"); - - if (0 == strcmp(desp->title, OPTION_TITLE_TXCF)) - m_devParam.splitImage = value ? 1 : 0; - else if (0 == strcmp(desp->title, OPTION_TITLE_HBTXZDYH)) - m_devParam.noiseDetachEnable = (bool)value; - } - else if (SANE_TYPE_INT == desp->type) - { - SANE_Int value = 0; - sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL); - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=INT, value=%d", value); - - if (0 == strcmp(desp->title, OPTION_TITLE_TGKBYLMD)) - m_devParam.discardBlankThre = value; - else if (0 == strcmp(desp->title, OPTION_TITLE_FBL)) - m_devParam.resolution = value; - } - else if (SANE_TYPE_FIXED == desp->type) - { - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=FIXED"); - } - else if (SANE_TYPE_STRING == desp->type) - { - char value[256] = {0}; - sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, value, NULL); - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=STRING, value=%s", Utf8ToStdString(value).c_str()); - - if (0 == strcmp(OPTION_TITLE_SMZS, desp->title) && 0 == strcmp(OPTION_VALUE_SMZS_LXSM, value)) - m_devParam.autofeeder = true; - else if (0 == strcmp(OPTION_TITLE_SMZS, desp->title) && 0 == strcmp(OPTION_VALUE_SMZS_SMZDZS, value)) - m_devParam.autofeeder = false; - else if (0 == strcmp(OPTION_TITLE_YSMS, desp->title) && (0 == strcmp(OPTION_VALUE_YSMS_24WCS, value) - || 0 == strcmp(OPTION_VALUE_YSMS_YSZDSB, value))) - m_devParam.pixel = 2; - else if (0 == strcmp(OPTION_TITLE_YSMS, desp->title) && 0 == strcmp(OPTION_VALUE_YSMS_256JHD, value)) - m_devParam.pixel = 1; - else if (0 == strcmp(OPTION_TITLE_YSMS, desp->title) && 0 == strcmp(OPTION_VALUE_YSMS_HB, value)) - m_devParam.pixel = 0; - else if (0 == strcmp(OPTION_TITLE_SMYM, desp->title) && 0 == strcmp(OPTION_VALUE_SMYM_SM, value)) - { - m_devParam.white = false; - m_devParam.single = false; - } - else if (0 == strcmp(OPTION_TITLE_SMYM, desp->title) && 0 == strcmp(OPTION_VALUE_SMYM_DM, value)) - { - m_devParam.white = false; - m_devParam.single = true; - } - else if (0 == strcmp(OPTION_TITLE_SMYM, desp->title) && (0 == strcmp(OPTION_VALUE_SMYM_TGKBYTY, value) - || 0 == strcmp(OPTION_VALUE_SMYM_TGKBYFPZ, value))) - { - m_devParam.white = true; - m_devParam.single = false; - } - else if (0 == strcmp(OPTION_TITLE_WGFX, desp->title) && 0 == strcmp(OPTION_VALUE_WGFX_0, value)) - m_devParam.orentation = 0; - else if (0 == strcmp(OPTION_TITLE_WGFX, desp->title) && 0 == strcmp(OPTION_VALUE_WGFX_90, value)) - m_devParam.orentation = 90; - else if (0 == strcmp(OPTION_TITLE_WGFX, desp->title) && 0 == strcmp(OPTION_VALUE_WGFX_180, value)) - m_devParam.orentation = 180; - else if (0 == strcmp(OPTION_TITLE_WGFX, desp->title) && 0 == strcmp(OPTION_VALUE_WGFX__90, value)) - m_devParam.orentation = 270; - else if (0 == strcmp(OPTION_TITLE_ZZCC, desp->title) && 0 == strcmp(OPTION_VALUE_ZZCC_PPYSCC, value)) - m_devParam.paperType = "Auto"; - else if (0 == strcmp(OPTION_TITLE_ZZCC, desp->title) && 0 == strcmp(OPTION_VALUE_ZZCC_A3, value)) - m_devParam.paperType = "A3"; - else if (0 == strcmp(OPTION_TITLE_ZZCC, desp->title) && 0 == strcmp(OPTION_VALUE_ZZCC_A4, value)) - m_devParam.paperType = "A4"; - } - else if (SANE_TYPE_BUTTON == desp->type) - { - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=BUTTON"); - } - else if (SANE_TYPE_GROUP == desp->type) - { - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=GROUP"); - } - } - - std::string filePath = GetFilePath(m_devName); - std::string cfgPath = filePath + "config.ini"; - m_devParam.Save(cfgPath); - ret = true; - } - - devParam = m_devParam; - return ret; -} - -bool Manager::SetDevParam(const std::string& devId, const DevParam& devParam) -{ - if (m_scanning) - { - return false; - } - - m_devParam = devParam; - - std::string filePath = GetFilePath(m_devName); - std::string cfgPath = filePath + "config.ini"; - m_devParam.Save(cfgPath); - - bool ret = false; - if (NULL != m_devHandle) - { - // 设置m_devParam到设备 - - SANE_Int num_dev_options = 0; - sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL); - for (int i = 1; i < num_dev_options; ++i) - { - const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i); - if (NULL == desp) - continue; - - if (SANE_TYPE_BOOL == desp->type) - { - if (0 == strcmp(desp->title, OPTION_TITLE_TXCF)) - { - SANE_Bool value = SANE_FALSE; - if (0 != m_devParam.splitImage) - value = SANE_TRUE; - sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); - } - else if (0 == strcmp(desp->title, OPTION_TITLE_HBTXZDYH)) - { - SANE_Bool value = SANE_FALSE; - if (m_devParam.noiseDetachEnable) - value = SANE_TRUE; - sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); - } - } - else if (SANE_TYPE_INT == desp->type) - { - if (0 == strcmp(desp->title, OPTION_TITLE_TGKBYLMD)) - { - SANE_Int value = m_devParam.discardBlankThre; - sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); - } - else if (0 == strcmp(desp->title, OPTION_TITLE_FBL)) - { - SANE_Int value = m_devParam.resolution; - sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); - } - else if (0 == strcmp(desp->title, OPTION_TITLE_SMSL)) - { - SANE_Int value = 1; - sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); - } - } - else if (SANE_TYPE_FIXED == desp->type) - { - - } - else if (SANE_TYPE_STRING == desp->type) - { - if (0 == strcmp(OPTION_TITLE_SMZS, desp->title)) - { - char value[256] = { 0 }; - strcpy(value, OPTION_VALUE_SMZS_LXSM); - if (!m_devParam.autofeeder) - strcpy(value, OPTION_VALUE_SMZS_SMZDZS); - sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)value, NULL); - } - else if (0 == strcmp(OPTION_TITLE_YSMS, desp->title)) - { - char value[256] = { 0 }; - strcpy(value, OPTION_VALUE_YSMS_24WCS); - if (1 == m_devParam.pixel) - strcpy(value, OPTION_VALUE_YSMS_256JHD); - else if (0 == m_devParam.pixel) - strcpy(value, OPTION_VALUE_YSMS_HB); - sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void *)value, NULL); - } - else if (0 == strcmp(OPTION_TITLE_SMYM, desp->title)) - { - char value[256] = { 0 }; - strcpy(value, OPTION_VALUE_SMYM_SM); - if (m_devParam.single) - strcpy(value, OPTION_VALUE_SMYM_DM); - else if (m_devParam.white) - strcpy(value, OPTION_VALUE_SMYM_TGKBYTY); - sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)value, NULL); - } - else if (0 == strcmp(OPTION_TITLE_WGFX, desp->title)) - { - char value[256] = { 0 }; - strcpy(value, OPTION_VALUE_WGFX_0); - if (90 == m_devParam.orentation) - strcpy(value, OPTION_VALUE_WGFX_90); - else if (180 == m_devParam.orentation) - strcpy(value, OPTION_VALUE_WGFX_180); - else if (270 == m_devParam.orentation) - strcpy(value, OPTION_VALUE_WGFX__90); - sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)value, NULL); - } - else if (0 == strcmp(OPTION_TITLE_ZZCC, desp->title)) - { - char value[256] = { 0 }; - strcpy(value, OPTION_VALUE_ZZCC_PPYSCC); - if ("A3" == m_devParam.paperType) - strcpy(value, OPTION_VALUE_ZZCC_A3); - else if ("A4" == m_devParam.paperType) - strcpy(value, OPTION_VALUE_ZZCC_A4); - sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)value, NULL); - } - } - else if (SANE_TYPE_BUTTON == desp->type) - { - - } - else if (SANE_TYPE_GROUP == desp->type) - { - - } - } - - ret = true; - } - - return ret; -} - -bool Manager::ExportOfd(const std::string& devId, bool isAuto, std::string& imgBase64) -{ - imgBase64.clear(); - if (m_scanning) - { - return false; - } - - HGByte* data = NULL; - HGUInt size = 0; - ExportOfdFile(devId, isAuto, &data, &size); - if (NULL != data) - { - imgBase64 = GetBase64(data, size); - delete[] data; - } - - return !imgBase64.empty(); -} - -bool Manager::ExportOfdFile(const std::string& devId, bool isAuto, const std::string& fileName) -{ - std::string filePath = GetFilePath(devId); - std::vector fileNameList = GetFileNameList(devId); - if (fileNameList.empty()) - { - return false; - } - - HGOfdImageWriter writer = NULL; - HGImgFmt_OpenOfdImageWriter(fileName.c_str(), &writer); - if (NULL == writer) - { - return false; - } - - bool ret = false; - for (int i = 0; i < (int)fileNameList.size(); ++i) - { - HGImage img = NULL; - std::string fileName = filePath + fileNameList[i]; - HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); - if (NULL != img) - { - if (HGBASE_ERR_OK == HGImgFmt_SaveJpegImageToOfdImageWriter(writer, img, NULL)) - ret = true; - HGBase_DestroyImage(img); - } - } - - HGImgFmt_CloseOfdImageWriter(writer); - - if (!ret) - { - HGBase_DeleteFile(fileName.c_str()); - return false; - } - - return true; -} - -bool Manager::ExportOfdFile(const std::string& devId, bool isAuto, HGByte** data, HGUInt* size) -{ - *data = NULL; - *size = 0; - if (m_scanning) - { - return false; - } - - HGChar tmpFileName[512]; - HGBase_GetTmpFileName(tmpFileName, 512); - ExportOfdFile(devId, isAuto, tmpFileName); - - *data = GetBuffer(tmpFileName, size); - HGBase_DeleteFile(tmpFileName); - return (NULL != *data); -} - -bool Manager::ExportPdf(const std::string& devId, std::string& imgBase64) -{ - imgBase64.clear(); - if (m_scanning) - { - return false; - } - - HGByte* data = NULL; - HGUInt size = 0; - ExportPdfFile(devId, &data, &size); - if (NULL != data) - { - imgBase64 = GetBase64(data, size); - delete[] data; - } - - return !imgBase64.empty(); -} - -bool Manager::ExportPdfFile(const std::string& devId, const std::string& fileName) -{ - std::string filePath = GetFilePath(devId); - std::vector fileNameList = GetFileNameList(devId); - if (fileNameList.empty()) - { - return false; - } - - HGPdfImageWriter writer = NULL; - HGImgFmt_OpenPdfImageWriter(fileName.c_str(), &writer); - if (NULL == writer) - { - return false; - } - - bool ret = false; - for (int i = 0; i < (int)fileNameList.size(); ++i) - { - HGImage img = NULL; - std::string fileName = filePath + fileNameList[i]; - HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); - if (NULL != img) - { - if (HGBASE_ERR_OK == HGImgFmt_SaveJpegImageToPdfImageWriter(writer, img, NULL)) - ret = true; - HGBase_DestroyImage(img); - } - } - - HGImgFmt_ClosePdfImageWriter(writer); - - if (!ret) - { - HGBase_DeleteFile(fileName.c_str()); - return false; - } - - return true; -} - -bool Manager::ExportPdfFile(const std::string& devId, HGByte** data, HGUInt* size) -{ - *data = NULL; - *size = 0; - if (m_scanning) - { - return false; - } - - HGChar tmpFileName[512]; - HGBase_GetTmpFileName(tmpFileName, 512); - ExportPdfFile(devId, tmpFileName); - - *data = GetBuffer(tmpFileName, size); - HGBase_DeleteFile(tmpFileName); - return (NULL != *data); -} - -bool Manager::ExportTiff(const std::string& devId, std::string& imgBase64) -{ - imgBase64.clear(); - if (m_scanning) - { - return false; - } - - HGByte* data = NULL; - HGUInt size = 0; - ExportTiffFile(devId, &data, &size); - if (NULL != data) - { - imgBase64 = GetBase64(data, size); - delete[] data; - } - - return !imgBase64.empty(); -} - -bool Manager::ExportTiff(const std::string& devId, const std::string& fileName) -{ - std::string filePath = GetFilePath(devId); - std::vector fileNameList = GetFileNameList(devId); - if (fileNameList.empty()) - { - return false; - } - - HGTiffWriter writer = NULL; - HGImgFmt_OpenTiffWriter(fileName.c_str(), &writer); - if (NULL == writer) - { - return false; - } - - bool ret = false; - for (int i = 0; i < (int)fileNameList.size(); ++i) - { - HGImage img = NULL; - std::string fileName = filePath + fileNameList[i]; - HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); - if (NULL != img) - { - if (HGBASE_ERR_OK == HGImgFmt_SaveImageToTiffWriter(writer, img, NULL)) - ret = true; - HGBase_DestroyImage(img); - } - } - - HGImgFmt_CloseTiffWriter(writer); - - if (!ret) - { - HGBase_DeleteFile(fileName.c_str()); - return false; - } - - return true; -} - -bool Manager::ExportTiffFile(const std::string& devId, HGByte** data, HGUInt* size) -{ - *data = NULL; - *size = 0; - if (m_scanning) - { - return false; - } - - HGChar tmpFileName[512]; - HGBase_GetTmpFileName(tmpFileName, 512); - ExportTiff(devId, tmpFileName); - - *data = GetBuffer(tmpFileName, size); - HGBase_DeleteFile(tmpFileName); - return (NULL != *data); -} - -bool Manager::ExportZip(const std::string& devId, std::string& imgBase64) -{ - imgBase64.clear(); - if (m_scanning) - { - return false; - } - - HGByte* data = NULL; - HGUInt size = 0; - ExportZipFile(devId, &data, &size); - if (NULL != data) - { - imgBase64 = GetBase64(data, size); - delete[] data; - } - - return !imgBase64.empty(); -} - -bool Manager::ExportZipFile(const std::string& devId, const std::string& fileName) -{ - std::string filePath = GetFilePath(devId); - std::vector fileNameList = GetFileNameList(devId); - if (fileNameList.empty()) - { - return false; - } - - int error = 0; - zip* z = zip_open(StdStringToUtf8(fileName.c_str()).c_str(), ZIP_CREATE | ZIP_TRUNCATE, &error); - if (NULL == z) - { - return false; - } - - bool ret = false; - for (int i = 0; i < (int)fileNameList.size(); ++i) - { - std::string fileName = filePath + fileNameList[i]; - zip_source_t* s = zip_source_file(z, StdStringToUtf8(fileName.c_str()).c_str(), 0, 0); - if (NULL != s) - { - if (zip_file_add(z, StdStringToUtf8(fileNameList[i].c_str()).c_str(), s, ZIP_FL_OVERWRITE) >= 0) - { - ret = true; - } - else - { - zip_source_free(s); - } - } - } - - zip_close(z); - z = NULL; - - if (!ret) - { - HGBase_DeleteFile(fileName.c_str()); - return false; - } - - return true; -} - -bool Manager::ExportZipFile(const std::string& devId, HGByte** data, HGUInt* size) -{ - *data = NULL; - *size = 0; - if (m_scanning) - { - return false; - } - - HGChar tmpFileName[512]; - HGBase_GetTmpFileName(tmpFileName, 512); - ExportZipFile(devId, tmpFileName); - - *data = GetBuffer(tmpFileName, size); - HGBase_DeleteFile(tmpFileName); - return (NULL != *data); -} - -bool Manager::UploadImage(const UploadParam& uploadParam) -{ - if (m_scanning) - { - return false; - } - - std::string devId; - GetCurDevId(devId); - - HGChar tmpFileName[512]; - HGBase_GetTmpFileName(tmpFileName, 512); - if (0 == uploadParam.format) - { - strcat(tmpFileName, ".ofd"); - ExportOfdFile(devId, true, tmpFileName); - } - else if (1 == uploadParam.format) - { - strcat(tmpFileName, ".pdf"); - ExportPdfFile(devId, tmpFileName); - } - else - { - strcat(tmpFileName, ".zip"); - ExportZipFile(devId, tmpFileName); - } - - bool ret = false; - if (0 == uploadParam.uploadMode) // HTTP - { - ret = HTTPUpload(tmpFileName, uploadParam.httpUrl, uploadParam.fileName, uploadParam.httpMethod, - uploadParam.header, uploadParam.param); - } - else if (1 == uploadParam.uploadMode) // FTP - { - ret = FTPUpload(tmpFileName, uploadParam.ftpUrl, uploadParam.ftpPort, uploadParam.ftpPath, - uploadParam.ftpUser, uploadParam.ftpPassword, uploadParam.ftpMode); - } - - HGBase_DeleteFile(tmpFileName); - return ret; -} - -bool Manager::SaveImage(const std::string& devId, const std::string& imgName, const std::string& imgBase64) -{ - if (m_scanning) - { - return false; - } - - std::string filePath = GetFilePath(devId); - std::string fileName = filePath + imgName; - - size_t pos = imgBase64.find(","); - if (std::string::npos != pos) - return SaveBase64(fileName, imgBase64.c_str() + pos + 1); - else - return SaveBase64(fileName, imgBase64.c_str()); -} - -bool Manager::DeleteImage(const std::string& devId, const std::string& imgName) -{ - if (m_scanning) - { - return false; - } - - std::string filePath = GetFilePath(devId); - std::string fileName = filePath + imgName; - - bool ret = false; - if (HGBASE_ERR_OK == HGBase_DeleteFile(fileName.c_str())) - { - std::vector fileNameList = GetFileNameList(devId); - for (int i = 0; i < (int)fileNameList.size(); ++i) - { - if (imgName == fileNameList[i]) - { - fileNameList.erase(fileNameList.begin() + i); - break; - } - } - - SaveFileNameList(devId, fileNameList); - ret = true; - } - - return ret; -} - -bool Manager::DeleteAllImage(const std::string& devId) -{ - if (m_scanning) - { - return false; - } - - std::string filePath = GetFilePath(devId); - std::vector fileNameList = GetFileNameList(devId); - - for (int i = 0; i < (int)fileNameList.size(); ++i) - { - std::string fileName = filePath + fileNameList[i]; - HGBase_DeleteFile(fileName.c_str()); - } - fileNameList.clear(); - - SaveFileNameList(devId, fileNameList); - return true; -} - -bool Manager::MergeImage(const std::string& devId, bool isHorizontal, const std::vector& imgIndexList, - std::string& imgName, std::string& imgBase64) -{ - imgName.clear(); - imgBase64.clear(); - if (m_scanning) - { - return false; - } - - std::string filePath = GetFilePath(devId); - std::vector fileNameList = GetFileNameList(devId); - - std::vector imgList; - for (int i = 0; i < (int)imgIndexList.size(); ++i) - { - int index = imgIndexList[i]; - if (index >= 0 && index < (int)fileNameList.size()) - { - std::string fileName = filePath + fileNameList[index]; - HGImage img = NULL; - HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); - if (NULL != img) - imgList.push_back(img); - } - } - - HGUInt width = 0, height = 0; - for (int i = 0; i < (int)imgList.size(); ++i) - { - HGImageInfo imgInfo; - HGBase_GetImageInfo(imgList[i], &imgInfo); - if (isHorizontal) - { - width += imgInfo.width; - if (imgInfo.height > height) - height = imgInfo.height; - } - else - { - height += imgInfo.height; - if (imgInfo.width > width) - width = imgInfo.width; - } - } - - if (width > 0 && height > 0) - { - HGImage img = NULL; - HGBase_CreateImage(width, height, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); - if (NULL != img) - { - HGUInt x = 0, y = 0; - for (int i = 0; i < (int)imgList.size(); ++i) - { - HGImageInfo imgInfo; - HGBase_GetImageInfo(imgList[i], &imgInfo); - - HGImageRoi roi = {x, y, x + imgInfo.width, y + imgInfo.height}; - HGBase_SetImageROI(img, &roi); - HGBase_CopyImage(imgList[i], img); - HGBase_ResetImageROI(img); - - if (isHorizontal) - x += imgInfo.width; - else - y += imgInfo.height; - } - - int maxIndex = 0; - for (int i = 0; i < (int)fileNameList.size(); ++i) - { - int index = atoi(fileNameList[i].c_str()); - if (index > maxIndex) - maxIndex = index; - } - - HGChar name[256]; - sprintf(name, "%d.%s", maxIndex + 1, m_devParam.format.c_str()); - imgName = name; - - HGBase_CreateDir(filePath.c_str()); - std::string fileName = filePath + imgName; - HGImgFmt_SaveImage(img, 0, NULL, fileName.c_str()); - - fileNameList.push_back(imgName); - SaveFileNameList(devId, fileNameList); - - imgBase64 = "data:image/jpeg;base64,"; - imgBase64 += GetBase64(img); - - HGBase_DestroyImage(img); - } - } - - for (int i = 0; i < (int)imgList.size(); ++i) - { - HGBase_DestroyImage(imgList[i]); - } - - return true; -} - -static bool LowerSort(const std::string &str1, const std::string& str2) -{ - return atoi(str1.c_str()) < atoi(str2.c_str()); -} - -bool Manager::BookSort(const std::string& devId, std::vector& imgNameList, - std::vector& imgBase64List) -{ - imgNameList.clear(); - imgBase64List.clear(); - if (m_scanning) - { - return false; - } - - std::vector fileNameList = GetFileNameList(devId); - std::sort(fileNameList.begin(), fileNameList.end(), LowerSort); - SaveFileNameList(devId, fileNameList); - - return GetImageByDevId(devId, imgNameList, imgBase64List); -} - -bool Manager::ExchangeImage(const std::string& devId, int index1, int index2) -{ - if (m_scanning) - { - return false; - } - - std::string filePath = GetFilePath(devId); - std::vector fileNameList = GetFileNameList(devId); - if (index1 < 0 || index1 >= (int)fileNameList.size() || index2 < 0 || index2 >= (int)fileNameList.size()) - return false; - - std::string imgName1 = fileNameList[index1]; - std::string imgName2 = fileNameList[index2]; - fileNameList[index1] = imgName2; - fileNameList[index2] = imgName1; - - SaveFileNameList(devId, fileNameList); - return true; -} - -bool Manager::GetLastBetch(std::string& devId) -{ - devId.clear(); - if (m_scanning) - { - return false; - } - - devId = m_devName; - return true; -} - -bool Manager::ResetPatchIndex() -{ - if (m_scanning) - { - return false; - } - - return true; -} - -bool Manager::SplitImage(const std::string& devId, const std::string& imgName, bool isHorizontal, int x1, int y1, int x2, int y2, - std::string& imgName1, std::string& imgBase64_1, std::string& imgName2, std::string& imgBase64_2) -{ - imgName1.clear(); - imgBase64_1.clear(); - imgName2.clear(); - imgBase64_2.clear(); - if (m_scanning) - { - return false; - } - - std::string filePath = GetFilePath(devId); - std::string fileName = filePath + imgName; - - HGImage img = NULL; - HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); - if (NULL != img) - { - HGImageInfo imgInfo; - HGBase_GetImageInfo(img, &imgInfo); - - for (int i = 0; i < 2; ++i) - { - HGUInt newWidth = isHorizontal ? x1 : imgInfo.width; - if (1 == i) - newWidth = isHorizontal ? (imgInfo.width - x1) : imgInfo.width; - - HGUInt newHeight = isHorizontal ? imgInfo.height : y1; - if (1 == i) - newHeight = isHorizontal ? imgInfo.height : (imgInfo.height - y1); - - HGImage newImg = NULL; - HGBase_CreateImage(newWidth, newHeight, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &newImg); - if (NULL != newImg) - { - HGImageRoi roi; - - if (isHorizontal) - { - roi.left = 0; - roi.top = 0; - roi.right = x1; - roi.bottom = imgInfo.height; - - if (1 == i) - { - roi.left = x1; - roi.right = imgInfo.width; - } - } - else - { - roi.left = 0; - roi.top = 0; - roi.right = imgInfo.width; - roi.bottom = y1; - - if (1 == i) - { - roi.top = y1; - roi.bottom = imgInfo.height; - } - } - - if (1 == i) - { - roi.left = x1; - roi.right = imgInfo.width; - } - - HGBase_SetImageROI(img, &roi); - HGBase_CopyImage(img, newImg); - HGBase_ResetImageROI(img); - - std::vector fileNameList = GetFileNameList(devId); - - int maxIndex = 0; - for (int i = 0; i < (int)fileNameList.size(); ++i) - { - int index = atoi(fileNameList[i].c_str()); - if (index > maxIndex) - maxIndex = index; - } - - HGChar name[256]; - sprintf(name, "%d.%s", maxIndex + 1, m_devParam.format.c_str()); - std::string imgName = name; - - HGBase_CreateDir(filePath.c_str()); - std::string fileName = filePath + imgName; - HGImgFmt_SaveImage(newImg, 0, NULL, fileName.c_str()); - - fileNameList.push_back(imgName); - SaveFileNameList(devId, fileNameList); - - std::string imgBase64 = "data:image/jpeg;base64,"; - imgBase64 += GetBase64(newImg); - - if (0 == i) - { - imgName1 = imgName; - imgBase64_1 = imgBase64; - } - else - { - imgName2 = imgName; - imgBase64_2 = imgBase64; - } - - HGBase_DestroyImage(newImg); - } - } - - HGBase_DestroyImage(img); - } - - return true; -} - -bool Manager::GetDevSerialNo(const std::string& devId, std::string& serialNo) -{ - serialNo.clear(); - if (m_scanning) - { - return false; - } - - serialNo = devId; - return true; -} - -bool Manager::GetImageBase64(const std::string& devId, const std::string& imgName, std::string& imgBase64) -{ - imgBase64.clear(); - if (m_scanning) - { - return false; - } - - std::string filePath = GetFilePath(devId); - std::string fileName = filePath + imgName; - - HGImage img = NULL; - HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); - if (NULL != img) - { - imgBase64 = "data:image/jpeg;base64,"; - imgBase64 += GetBase64(img); - HGBase_DestroyImage(img); - } - - return true; -} - -std::string Manager::GetFilePath(const std::string& devId) -{ - HGChar docsPath[256]; - HGBase_GetDocumentsPath(docsPath, 256); - HGChar imgPath[512]; - sprintf(imgPath, "%s%s/", docsPath, Utf8ToStdString(devId.c_str()).c_str()); - HGChar stdImgPath[512]; - HGBase_StandardiseFileName(imgPath, stdImgPath, 512); - - return stdImgPath; -} - -std::vector Manager::GetFileNameList(const std::string& devId) -{ - std::vector fileNameList; - - std::string filePath = GetFilePath(devId); - std::string cfgName = filePath + "images.txt"; - FILE* file = fopen(cfgName.c_str(), "r"); - if (NULL != file) - { - while (feof(file) == 0) - { - char lineContent[256] = { 0 }; - if (NULL == fgets(lineContent, 256, file) || '\n' == *lineContent) - { - continue; - } - - if (lineContent[strlen(lineContent) - 1] == '\n') - lineContent[strlen(lineContent) - 1] = 0; - - fileNameList.push_back(lineContent); - } - - fclose(file); - } - - return fileNameList; -} - -bool Manager::SaveFileNameList(const std::string& devId, const std::vector& fileNameList) -{ - bool ret = false; - - std::string filePath = GetFilePath(devId); - std::string cfgName = filePath + "images.txt"; - FILE* file = fopen(cfgName.c_str(), "w"); - if (NULL != file) - { - for (int i = 0; i < (int)fileNameList.size(); ++i) - { - fwrite(fileNameList[i].c_str(), 1, fileNameList[i].size(), file); - fwrite("\n", 1, strlen("\n"), file); - } - - fclose(file); - ret = true; - } - - return ret; -} - -std::string Manager::GetBase64(HGImage image) -{ - std::string strBase64; - - if (NULL != image) - { - HGBuffer buffer = NULL; - HGImgFmt_SaveJpegImageToBuffer(image, NULL, &buffer); - if (NULL != buffer) - { - HGByte* data; - HGBase_GetBufferData(buffer, &data); - HGUSize size; - HGBase_GetBufferSize(buffer, &size); - - HGSize base64Size = 0; - HGBase_Base64Encode(data, size, NULL, &base64Size); - uint8_t* base64 = new uint8_t[base64Size + 1]; - HGBase_Base64Encode(data, size, base64, &base64Size); - base64[base64Size] = 0; - strBase64 = (const char*)base64; - delete[] base64; - - HGBase_DestroyBuffer(buffer); - } - } - - return strBase64; -} - -std::string Manager::GetBase64(const HGByte* data, HGUInt size) -{ - std::string strBase64; - - if (NULL != data && 0 != size) - { - HGSize base64Size = 0; - HGBase_Base64Encode(data, size, NULL, &base64Size); - uint8_t* base64 = new uint8_t[base64Size + 1]; - HGBase_Base64Encode(data, size, base64, &base64Size); - base64[base64Size] = 0; - strBase64 = (const char*)base64; - delete[] base64; - } - - return strBase64; -} - -std::string Manager::GetBase64(const std::string& fileName) -{ - std::string strBase64; - - FILE* file = fopen(fileName.c_str(), "rb"); - if (NULL != file) - { - fseek(file, 0, SEEK_END); - long size = ftell(file); - fseek(file, 0, SEEK_SET); - - if (size > 0) - { - HGByte* data = new HGByte[size]; - long readLen = (long)fread(data, 1, size, file); - if (readLen == size) - { - HGSize base64Size = 0; - HGBase_Base64Encode(data, size, NULL, &base64Size); - uint8_t* base64 = new uint8_t[base64Size + 1]; - HGBase_Base64Encode(data, size, base64, &base64Size); - base64[base64Size] = 0; - strBase64 = (const char*)base64; - delete[] base64; - } - - delete[] data; - } - - fclose(file); - } - - return strBase64; -} - -HGByte* Manager::GetBuffer(const std::string& fileName, HGUInt* size) -{ - HGByte* data = NULL; - *size = 0; - - FILE* file = fopen(fileName.c_str(), "rb"); - if (NULL != file) - { - fseek(file, 0, SEEK_END); - *size = ftell(file); - fseek(file, 0, SEEK_SET); - - if (*size > 0) - { - data = new HGByte[*size]; - - HGUInt readLen = (HGUInt)fread(data, 1, *size, file); - if (readLen != *size) - { - delete[] data; - data = NULL; - *size = 0; - } - } - - fclose(file); - } - - return data; -} - -bool Manager::SaveBase64(const std::string& fileName, const char* base64) -{ - if (NULL == base64 || 0 == *base64) - return false; - - HGUInt base64Size = (HGUInt)strlen(base64); - - bool ret = false; - FILE* file = fopen(fileName.c_str(), "wb"); - if (NULL != file) - { - HGSize size = 0; - HGBase_Base64Decode((const HGByte*)base64, (HGSize)base64Size, NULL, &size); - uint8_t* data = new HGByte[size]; - HGBase_Base64Decode((const HGByte*)base64, (HGSize)base64Size, data, &size); - - size_t writeLen = fwrite(data, 1, size, file); - if (writeLen == (size_t)size) - ret = true; - - delete[] data; - fclose(file); - } - - return ret; -} - -static std::string MakePreFileData(const char* pszBoundary, const char* pszRemoteFileName) -{ - char data[512]; - sprintf(data, "--%s\r\nContent-Disposition: form-data; name=\"filedata\"; filename=\"%s\"\r\n", - pszBoundary, pszRemoteFileName); - std::string ret = data; - ret += "Content-Type: application/octet-stream; charset=utf-8\r\n"; - ret += "Content-Transfer-Encoding: binary\r\n"; - ret += "\r\n"; - return ret; -} - -static std::string MakePostFileData(const char* pszBoundary) -{ - char data[512]; - sprintf(data, "\r\n--%s\r\nContent-Disposition: form-data; name=\"submitted\"\r\n\r\nsubmit\r\n--%s--\r\n", pszBoundary, pszBoundary); - return data; -} - -static void ParseHttpURL(const std::string &url, std::string &addr, int &port, std::string &path) -{ - addr.clear(); - port = 0; - path.clear(); - - std::string url2; - std::string::size_type pos = url.find("//"); - if (std::string::npos != pos) - { - std::string protocal = url.substr(0, pos); - if (protocal != "http:") - { - return; - } - - url2 = url.substr(pos + 2); - } - else - { - url2 = url; - } - - std::string addr_port; - pos = url2.find("/"); - if (std::string::npos != pos) - { - addr_port = url2.substr(0, pos); - path = url2.substr(pos); - } - else - { - addr_port = url2; - } - - pos = addr_port.find(":"); - if (std::string::npos != pos) - { - addr = addr_port.substr(0, pos); - port = atoi(addr_port.substr(pos + 1).c_str()); - } - else - { - addr = addr_port; - port = 80; - } -} - -bool Manager::HTTPUpload(const std::string& localFileName, const std::string& httpUrl, const std::string& remoteFileName, - const std::string& httpMethod, const std::string& header, const std::string& param) -{ - unsigned char* fileData = NULL; - long fileSize = 0; - FILE* file = fopen(localFileName.c_str(), "rb"); - if (NULL != file) - { - fseek(file, 0, SEEK_END); - fileSize = ftell(file); - fseek(file, 0, SEEK_SET); - - if (0 != fileSize) - { - fileData = new unsigned char[fileSize]; - fread(fileData, 1, fileSize, file); - } - - fclose(file); - } - - if (NULL == fileData) - { - return false; - } - - std::string addr; - int port; - std::string path; - ParseHttpURL(httpUrl, addr, port, path); - -#if defined(HG_CMP_MSC) - SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0); - assert(INVALID_SOCKET != sockClient); -#else - int sockClient = socket(AF_INET, SOCK_STREAM, 0); - assert(-1 != sockClient); -#endif - -#if defined(HG_CMP_MSC) - u_long ul = 1; // 设为非阻塞 - ioctlsocket(sockClient, FIONBIO, &ul); -#else - ioctl(sockClient, FIONBIO, 1); -#endif - -#if defined(HG_CMP_MSC) - SOCKADDR_IN addrServer = { 0 }; - addrServer.sin_addr.S_un.S_addr = inet_addr(addr.c_str()); - addrServer.sin_family = AF_INET; - addrServer.sin_port = htons(port); - if (0 != connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR_IN))) -#else - struct sockaddr_in addrServer = { 0 }; - addrServer.sin_addr.s_addr = inet_addr(addr.c_str()); - addrServer.sin_family = AF_INET; - addrServer.sin_port = htons(port); - if (0 != connect(sockClient, (struct sockaddr*)&addrServer, sizeof(addrServer))) -#endif - { - fd_set fds; - FD_ZERO(&fds); - FD_SET(sockClient, &fds); - timeval tm; - tm.tv_sec = 1; - tm.tv_usec = 0; - - if (select((int)(sockClient + 1), NULL, &fds, NULL, &tm) <= 0) - { -#if defined(HG_CMP_MSC) - closesocket(sockClient); -#else - close(sockClient); -#endif - delete[] fileData; - return false; - } - - if (!FD_ISSET(sockClient, &fds)) - { -#if defined(HG_CMP_MSC) - closesocket(sockClient); -#else - close(sockClient); -#endif - delete[] fileData; - return false; - } - } - -#if defined(HG_CMP_MSC) - ul = 0; // 设为阻塞 - ioctlsocket(sockClient, FIONBIO, &ul); -#else - ioctl(sockClient, FIONBIO, 0); -#endif - - char remoteName[256]; - HGBase_GetFileName(localFileName.c_str(), remoteName, 256); - - bool ret = false; - - const char* pszBoundary = "---------------------------7d33a816d302b6"; - - std::string strPreFileData = MakePreFileData(pszBoundary, remoteName); - std::string strPostFileData = MakePostFileData(pszBoundary); - - char hostname[128]; - gethostname(hostname, 128); - - std::string head; - char data[512]; - sprintf(data, "POST %s HTTP/1.1\r\nHost: %s\r\n", path.c_str(), hostname); - head += data; - sprintf(data, "Content-Type: multipart/form-data; boundary=%s\r\n", pszBoundary); - head += data; - sprintf(data, "Content-Length: %d\r\n\r\n", (int)strPreFileData.size() + (int)fileSize + (int)strPostFileData.size()); - head += data; - - send(sockClient, head.c_str(), (int)head.size(), 0); - send(sockClient, strPreFileData.c_str(), (int)strPreFileData.size(), 0); - send(sockClient, (const char *)fileData, (int)fileSize, 0); - send(sockClient, strPostFileData.c_str(), (int)strPostFileData.size(), 0); - - char recvBuf[2048] = {0}; - recv(sockClient, recvBuf, 2048, 0); - - std::string strRecv(recvBuf); - std::string::size_type pos = strRecv.find("\r\n"); - if (pos != std::string::npos) - { - std::string head = strRecv.substr(0, pos); - if (head.find("200") != std::string::npos) - { - ret = true; - } - } - -#if defined(HG_CMP_MSC) - closesocket(sockClient); -#else - close(sockClient); -#endif - delete[] fileData; - return ret; -} - -static size_t read_callback(char* ptr, size_t size, size_t nmemb, void* stream) -{ - unsigned long nread; - /* in real-world cases, this would probably get this data differently - as this fread() stuff is exactly what the library already would do - by default internally */ - size_t retcode = fread(ptr, size, nmemb, (FILE*)stream); - - if (retcode > 0) - { - nread = (unsigned long)retcode; - //fprintf(stderr, "*** We read %lu bytes from file\n", nread); - } - - return retcode; -} - -bool Manager::FTPUpload(const std::string& localFileName, const std::string& ftpUrl, int ftpPort, const std::string& ftpPath, - const std::string& ftpUser, const std::string& ftpPassword, int ftpMode) -{ - FILE* file = fopen(localFileName.c_str(), "rb"); - if (NULL == file) - { - return false; - } - - bool ret = false; - fseek(file, 0, SEEK_END); - long fsize = ftell(file); - fseek(file, 0, SEEK_SET); - - curl_global_init(CURL_GLOBAL_ALL); - - /* get a curl handle */ - CURL *curl = curl_easy_init(); - if (NULL != curl) - { - char tmpName[256]; - HGBase_GetUuid(tmpName, 256); - char remoteName[256]; - HGBase_GetFileName(localFileName.c_str(), remoteName, 256); - - char ftp_rnfr[512]; - sprintf(ftp_rnfr, "RNFR %s", tmpName); - char ftp_rnto[512]; - sprintf(ftp_rnto, "RNTO %s", remoteName); - - struct curl_slist* headerlist = NULL; - headerlist = curl_slist_append(headerlist, ftp_rnfr); - headerlist = curl_slist_append(headerlist, ftp_rnto); - - /* we want to use our own read function */ - curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); - - curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); - - char url[512]; - if (!ftpUser.empty() && !ftpPassword.empty()) - { - sprintf(url, "ftp://%s:%s@%s:%d%s/%s", ftpUser.c_str(), ftpPassword.c_str(), - ftpUrl.c_str(), ftpPort, ftpPath.c_str(), tmpName); - } - else - { - sprintf(url, "ftp://%s:%d%s/%s", ftpUrl.c_str(), ftpPort, ftpPath.c_str(), tmpName); - } - curl_easy_setopt(curl, CURLOPT_URL, url); - - curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1); - - /* pass in that last of FTP commands to run after the transfer */ - curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist); - - /* now specify which file to upload */ - curl_easy_setopt(curl, CURLOPT_READDATA, file); - - /* Set the size of the file to upload (optional). If you give a *_LARGE - option you MUST make sure that the type of the passed-in argument is a - curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must - make sure that to pass in a type 'long' argument. */ - curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fsize); - - /* Now run off and do what you have been told! */ - CURLcode res = curl_easy_perform(curl); - /* Check for errors */ - if (res != CURLE_OK) - fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); - else - ret = true; - - /* clean up the FTP commands list */ - curl_slist_free_all(headerlist); - - /* always cleanup */ - curl_easy_cleanup(curl); - } - fclose(file); /* close the local file */ - - curl_global_cleanup(); - return ret; -} - -int Manager::sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param) -{ - (void)hdev; - (void)len; - - Manager* p = (Manager*)param; - switch (code) - { - case SANE_EVENT_DEVICE_ARRIVED: - { - SANE_Device* sane_dev = (SANE_Device*)data; - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "device arrive, name=%s", Utf8ToStdString(sane_dev->name).c_str()); - - HGBase_EnterLock(p->m_lock); - if (NULL != p->m_scanEvent) - p->m_scanEvent(SCANEVENT_ARRIVE, (void*)sane_dev->name, (void*)0, p->m_scanParam); - HGBase_LeaveLock(p->m_lock); - - OpenDevParam* openDevParam = new OpenDevParam; - openDevParam->devName = sane_dev->name; - - HGMsg msg; - msg.id = MSGID_OPEN_DEVICE; - msg.data = openDevParam; - if (HGBASE_ERR_OK != HGBase_PostPumpMessage(p->m_msgPump, &msg)) - { - delete openDevParam; - } - } - break; - case SANE_EVENT_DEVICE_LEFT: - { - SANE_Device* sane_dev = (SANE_Device*)data; - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "device remove, name=%s", Utf8ToStdString(sane_dev->name).c_str()); - - HGBase_EnterLock(p->m_lock); - if (NULL != p->m_scanEvent) - p->m_scanEvent(SCANEVENT_REMOVE, (void*)sane_dev->name, (void*)0, p->m_scanParam); - HGBase_LeaveLock(p->m_lock); - - CloseDevParam* closeDevParam = new CloseDevParam; - closeDevParam->devName = sane_dev->name; - - HGMsg msg; - msg.id = MSGID_CLOSE_DEVICE; - msg.data = closeDevParam; - if (HGBASE_ERR_OK != HGBase_PostPumpMessage(p->m_msgPump, &msg)) - { - delete closeDevParam; - } - } - break; - case SANE_EVENT_STATUS: - { - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_STATUS, msg=%s", Utf8ToStdString((char *)data).c_str()); - - HGBase_EnterLock(p->m_lock); - if (NULL != p->m_scanEvent) - p->m_scanEvent(SCANEVENT_STATUS, (void*)data, (void*)0, p->m_scanParam); - HGBase_LeaveLock(p->m_lock); - } - break; - case SANE_EVENT_ERROR: - { - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_ERROR, msg=%s", Utf8ToStdString((char*)data).c_str()); - - HGBase_EnterLock(p->m_lock); - if (NULL != p->m_scanEvent) - p->m_scanEvent(SCANEVENT_ERROR, (void *)data, (void*)0, p->m_scanParam); - HGBase_LeaveLock(p->m_lock); - } - break; - case SANE_EVENT_WORKING: - { - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_WORKING, msg=%s", Utf8ToStdString((char*)data).c_str()); - - HGBase_EnterLock(p->m_lock); - if (NULL != p->m_scanEvent) - p->m_scanEvent(SCANEVENT_WORKING, (void*)data, (void*)0, p->m_scanParam); - HGBase_LeaveLock(p->m_lock); - } - break; - case SANE_EVENT_IMAGE_OK: - { - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_IMAGE_OK"); - - SANE_Image* sane_img = (SANE_Image*)data; - - HGUInt imgType = 0; - if (sane_img->header.format == SANE_FRAME_GRAY) - imgType = HGBASE_IMGTYPE_GRAY; - else if (sane_img->header.format == SANE_FRAME_RGB) - imgType = HGBASE_IMGTYPE_RGB; - - HGByte* data = sane_img->data; - HGImageInfo imgInfo = { (HGUInt)sane_img->header.pixels_per_line, (HGUInt)sane_img->header.lines, - imgType, (HGUInt)sane_img->header.bytes_per_line, HGBASE_IMGORIGIN_TOP }; - - HGImage img = NULL; - HGBase_CreateImageFromData(data, &imgInfo, nullptr, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); - if (NULL != img) - { - std::string filePath = GetFilePath(p->m_devName); - std::vector fileNameList = GetFileNameList(p->m_devName); - - int maxIndex = 0; - for (int i = 0; i < (int)fileNameList.size(); ++i) - { - int index = atoi(fileNameList[i].c_str()); - if (index > maxIndex) - maxIndex = index; - } - - HGChar name[256]; - sprintf(name, "%d.%s", maxIndex + 1, p->m_devParam.format.c_str()); - std::string imgName = name; - - HGBase_CreateDir(filePath.c_str()); - std::string fileName = filePath + imgName; - HGImgFmt_SaveImage(img, 0, NULL, fileName.c_str()); - - if (0 == p->m_devParam.uploadMode) // HTTP - { - HTTPUpload(fileName, p->m_devParam.httpUrl, p->m_devParam.fileName, p->m_devParam.httpMethod, - p->m_devParam.header, p->m_devParam.param); - } - else if (1 == p->m_devParam.uploadMode) // FTP - { - FTPUpload(fileName, p->m_devParam.ftpUrl, p->m_devParam.ftpPort, p->m_devParam.ftpPath, - p->m_devParam.ftpUser, p->m_devParam.ftpPassword, p->m_devParam.ftpMode); - } - - if (p->m_scanIsInsert) - { - int index = -1; - for (int i = 0; i < (int)fileNameList.size(); ++i) - { - if (fileNameList[i] == p->m_scanInsertImgName) - { - index = i; - break; - } - } - - if (-1 != index) - { - fileNameList.insert(fileNameList.begin() + index, imgName); - } - else - { - fileNameList.push_back(imgName); - } - } - else - { - fileNameList.push_back(imgName); - } - - SaveFileNameList(p->m_devName, fileNameList); - - std::string imgBase64 = "data:image/jpeg;base64,"; - imgBase64 += GetBase64(img); - - HGBase_EnterLock(p->m_lock); - if (NULL != p->m_scanEvent) - p->m_scanEvent(SCANEVENT_IMAGE, (void*)imgName.c_str(), (void *)imgBase64.c_str(), p->m_scanParam); - HGBase_LeaveLock(p->m_lock); - - HGBase_DestroyImage(img); - } - } - break; - case SANE_EVENT_SCAN_FINISHED: - { - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_SCAN_FINISHED, msg=%s", Utf8ToStdString((char*)data).c_str()); - - HGBase_EnterLock(p->m_lock); - if (NULL != p->m_scanEvent) - p->m_scanEvent(SCANEVENT_FINISH, (void*)data, (void*)0, p->m_scanParam); - HGBase_LeaveLock(p->m_lock); - - HGMsg msg; - msg.id = MSGID_SCAN_FINISH; - msg.data = NULL; - HGBase_PostPumpMessage(p->m_msgPump, &msg); - } - break; - } - - return 0; -} diff --git a/sdk/webservice/1.0/Manager.h b/sdk/webservice/1.0/Manager.h deleted file mode 100644 index 74684bfb..00000000 --- a/sdk/webservice/1.0/Manager.h +++ /dev/null @@ -1,191 +0,0 @@ -#ifndef __MANAGER_H__ -#define __MANAGER_H__ - -#include "base/HGDef.h" -#include "base/HGInc.h" -#include "base/HGLock.h" -#include "base/HGImage.h" -#include "base/HGMsgPump.h" -#include "sane/sane_ex.h" -#include "sane/sane_option_definitions.h" -#include "Msg_1_0.h" -#include - -#define SCANEVENT_ARRIVE 1L -#define SCANEVENT_REMOVE 2L -#define SCANEVENT_STATUS 3L -#define SCANEVENT_WORKING 4L -#define SCANEVENT_IMAGE 5L -#define SCANEVENT_FINISH 6L -#define SCANEVENT_ERROR 7L - -struct DevParam -{ - DevParam(); - ~DevParam(); - - void Reset(); - void Load(const std::string& cfgPath); - void Save(const std::string& cfgPath); - - std::string device; // 设备名称,默认null - bool autofeeder; // 自动进纸,默认true - int pixel; // 扫描模式 0:黑白 1:灰度 2:彩色,默认1 - bool white; // 丢弃空白页,默认false - int discardBlankThre; // 跳过空白页阈值 1­-100 默认值5 - bool single; // 单页扫描,默认false - std::string format; // 输出格式 jpg png bmp tiff pdf ofd,默认jpg - int resolution; // 扫描分辨率 范围 100­-600 默认值200 - int orentation; // 图片旋转 0:原图 90:度 180:旋转180度 270:旋转270度,默认0 - std::string paperType; // 扫描幅面 A3:A3幅面 Auto:自适应幅面 A4:A4幅面,默认Auto - int splitImage; // 图像分割 0:disable 1:垂直分割 2: 水平分割,默认0 - bool noiseDetachEnable; // 去除噪点,默认true - int noiseDetach; // 噪点阈值 范围:10­-50 默认值15 - - int uploadMode; // 是否边扫边上传 0:http 1:ftp 2: 不上传,默认2 - std::string httpUrl; // 上传地址,默认null - std::string fileName; // 接收文件参数名,默认null - std::string httpMethod; // 上传方式 GET POST PUT,默认null - std::string header; // 请求头,默认null - std::string param; // 参数 json格式,默认null - std::string ftpUrl; // ftp 地址,默认null - std::string ftpPath; // 路径,默认/images - std::string ftpUser; // ftp 用户名,默认null - std::string ftpPassword; // ftp 密码,默认null - int ftpPort; // 端口号,默认21 - int ftpMode; // 连接模式 1:主动 2:被动,默认2 -}; - -struct UploadParam -{ - UploadParam() - { - uploadMode = 2; - ftpPort = 21; - ftpMode = 2; - format = 2; - } - - int uploadMode; // 上传模式 0:http 1:ftp 默认0 - std::string httpUrl; // 上传地址,默认null - std::string fileName; // 接收文件参数名,默认null - std::string httpMethod; // 上传方式 GET POST PUT,默认null - std::string header; // 请求头,默认null - std::string param; // 参数 json格式,默认null - std::string ftpUrl; // ftp 地址,默认null - std::string ftpPath; // 路径,默认/images - std::string ftpUser; // ftp 用户名,默认null - std::string ftpPassword; // ftp 密码,默认null - int ftpPort; // 端口号,默认21 - int ftpMode; // 连接模式 1:主动 2:被动,默认2 - int format; // 上传格式 0: ofd 1: pdf 2: zip, 默认2 -}; - -typedef void (*ScanEvent)(HGUInt event, void *value1, void *value2, void *param); - -class Manager -{ -public: - Manager(HGMsgPump msgPump); - ~Manager(); - - // 打开设备 - void OpenDev(const OpenDevParam *param); - // 关闭设备 - void CloseDev(const CloseDevParam* param); - // 扫描完成 - void ScanFinish(); - - // 设置回调 - void SetScanEvent(ScanEvent event, void* param); - // 清理回调 - void ResetScanEvent(); - // 扫描 - bool Scan(const std::string& insertImgName, bool isInsert); - // 停止扫描 - bool StopScan(); - // 获取当前连接的设备ID - bool GetCurDevId(std::string& devId); - // 获取连接的设备名列表 - bool GetDevNames(std::vector &devNameList); - // 获取上次的扫描结果 - bool GetImageByDevId(const std::string& devId, std::vector &imgNameList, - std::vector &imgBase64List); - // 获取配置参数 - bool GetDevParam(const std::string& devId, DevParam& devParam); - // 设置配置参数 - bool SetDevParam(const std::string& devId, const DevParam& devParam); - // 生成OFD - bool ExportOfd(const std::string& devId, bool isAuto, std::string &imgBase64); - bool ExportOfdFile(const std::string& devId, bool isAuto, const std::string &fileName); - bool ExportOfdFile(const std::string& devId, bool isAuto, HGByte **data, HGUInt *size); - // 生成PDF - bool ExportPdf(const std::string& devId, std::string& imgBase64); - bool ExportPdfFile(const std::string& devId, const std::string& fileName); - bool ExportPdfFile(const std::string& devId, HGByte** data, HGUInt* size); - // 生成TIFF - bool ExportTiff(const std::string& devId, std::string& imgBase64); - bool ExportTiff(const std::string& devId, const std::string& fileName); - bool ExportTiffFile(const std::string& devId, HGByte** data, HGUInt* size); - // 生成ZIP - bool ExportZip(const std::string& devId, std::string& imgBase64); - bool ExportZipFile(const std::string& devId, const std::string& fileName); - bool ExportZipFile(const std::string& devId, HGByte** data, HGUInt* size); - // 上传图像 - bool UploadImage(const UploadParam& uploadParam); - // 保存图片 - bool SaveImage(const std::string& devId, const std::string& imgName, const std::string& imgBase64); - // 删除图片 - bool DeleteImage(const std::string& devId, const std::string& imgName); - // 删除所有图片 - bool DeleteAllImage(const std::string& devId); - // 图像合并 - bool MergeImage(const std::string& devId, bool isHorizontal, const std::vector &imgIndexList, - std::string &imgName, std::string& imgBase64); - // 自动排序 - bool BookSort(const std::string& devId, std::vector& imgNameList, - std::vector& imgBase64List); - // 交换文件 - bool ExchangeImage(const std::string& devId, int index1, int index2); - // 获取最后批次 - bool GetLastBetch(std::string& devId); - // 重置索引 - bool ResetPatchIndex(); - // 拆分图像 - bool SplitImage(const std::string& devId, const std::string& imgName, bool isHorizontal, int x1, int y1, int x2, int y2, - std::string& imgName1, std::string& imgBase64_1, std::string& imgName2, std::string& imgBase64_2); - // 获取设备序列号 - bool GetDevSerialNo(const std::string& devId, std::string& serialNo); - // 获取图像Base64 - bool GetImageBase64(const std::string& devId, const std::string& imgName, std::string& imgBase64); - -private: - static std::string GetFilePath(const std::string& devId); - static std::vector GetFileNameList(const std::string &devId); - static bool SaveFileNameList(const std::string& devId, const std::vector& fileNameList); - static std::string GetBase64(HGImage image); - static std::string GetBase64(const HGByte *data, HGUInt size); - static std::string GetBase64(const std::string& fileName); - static HGByte* GetBuffer(const std::string& fileName, HGUInt *size); - static bool SaveBase64(const std::string& fileName, const char *base64); - static bool HTTPUpload(const std::string& localFileName, const std::string& httpUrl, const std::string& remoteFileName, - const std::string &httpMethod, const std::string& header, const std::string& param); - static bool FTPUpload(const std::string& localFileName, const std::string& ftpUrl, int ftpPort, const std::string& ftpPath, - const std::string& ftpUser, const std::string& ftpPassword, int ftpMode); - static int sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param); - -private: - HGMsgPump m_msgPump; - HGLock m_lock; - std::string m_devName; - SANE_Handle m_devHandle; - DevParam m_devParam; - ScanEvent m_scanEvent; - void* m_scanParam; - - std::string m_scanInsertImgName; - bool m_scanIsInsert; - bool m_scanning; -}; - -#endif /* __MANAGER_H__ */ diff --git a/sdk/webservice/1.0/MsgPumpCallback_1_0.cpp b/sdk/webservice/1.0/MsgPumpCallback_1_0.cpp deleted file mode 100644 index bd7081aa..00000000 --- a/sdk/webservice/1.0/MsgPumpCallback_1_0.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "MsgPumpCallback_1_0.h" - -void HGMsgPumpCallback(HGMsgPump msgPump, const HGMsg* msg, HGPointer param) -{ - // -} \ No newline at end of file diff --git a/sdk/webservice/1.0/MsgPumpCallback_1_0.h b/sdk/webservice/1.0/MsgPumpCallback_1_0.h deleted file mode 100644 index 5500cd6d..00000000 --- a/sdk/webservice/1.0/MsgPumpCallback_1_0.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef __MSGPUMPCALLBACK_1_0_H__ -#define __MSGPUMPCALLBACK_1_0_H__ - -#include "base/HGMsgPump.h" - -void HGMsgPumpCallback(HGMsgPump msgPump, const HGMsg* msg, HGPointer param); - -#endif /* __MSGPUMPCALLBACK_1_0_H__ */ \ No newline at end of file diff --git a/sdk/webservice/1.0/Msg_1_0.h b/sdk/webservice/1.0/Msg_1_0.h deleted file mode 100644 index 433dfe33..00000000 --- a/sdk/webservice/1.0/Msg_1_0.h +++ /dev/null @@ -1,71 +0,0 @@ -#ifndef __MSG_1_0_H__ -#define __MSG_1_0_H__ - -#include "base/HGDef.h" -#include "base/HGInc.h" -#include "HttpHead.h" -#include - -#define MSGID_OPEN_DEVICE 1L -#define MSGID_CLOSE_DEVICE 2L -#define MSGID_SCAN_FINISH 3L -#define MSGID_CONNECT 4L -#define MSGID_DISCONNECT 5L -#define MSGID_HTTP_COMMAND 6L -#define MSGID_SOCKIO_COMMAND 7L -#define MSGID_SOCKIO_EVENT 8L - -struct OpenDevParam -{ - std::string devName; -}; - -struct CloseDevParam -{ - std::string devName; -}; - -struct ConnectParam -{ - HGPointer svr; - HGChar ip[16]; - HGUShort port; -#if defined(HG_CMP_MSC) - SOCKET socket; -#else - int socket; -#endif -}; - -struct DisConnectParam -{ - HGPointer svr; - HGUInt usrId; -}; - -struct HttpCmdParam -{ - HGPointer svr; - HGUInt usrId; - HttpHead head; - HGByte* data; - HGUInt size; -}; - -struct SockIoCmdParam -{ - HGPointer svr; - HGUInt usrId; - HGByte* data; - HGUInt size; -}; - -struct SockIoEvtParam -{ - HGPointer svr; - HGUInt usrId; - HGByte* data; - HGUInt size; -}; - -#endif /* __MSG_1_0_H__ */ \ No newline at end of file diff --git a/sdk/webservice/1.0/SockIoServer.cpp b/sdk/webservice/1.0/SockIoServer.cpp deleted file mode 100644 index 8655ba28..00000000 --- a/sdk/webservice/1.0/SockIoServer.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "SockIoServer.h" -#include "SockIoUser.h" -#include "base/HGInfo.h" - -SockIoServer::SockIoServer(HGMsgPump msgPump, class Manager* manager) - : WebServer(msgPump, manager) -{ - -} - -SockIoServer::~SockIoServer() -{ - -} - -void SockIoServer::Connect(const ConnectParam* param) -{ - assert(NULL != param); - - WebUser* user = new SockIoUser(this, m_currUserId, param->ip, param->port, param->socket); - user->Open(); - ++m_currUserId; - m_vectorUser.push_back(user); -} - -void SockIoServer::Command(HGUInt usrId, const SockIoCmdParam* param) -{ - int nIndex = GetUserIndex(usrId); - if (-1 != nIndex) - { - ((SockIoUser*)m_vectorUser[nIndex])->HandleCmd(param); - } -} - -void SockIoServer::Event(HGUInt usrId, const SockIoEvtParam* param) -{ - int nIndex = GetUserIndex(usrId); - if (-1 != nIndex) - { - ((SockIoUser*)m_vectorUser[nIndex])->HandleEvent(param); - } -} diff --git a/sdk/webservice/1.0/SockIoServer.h b/sdk/webservice/1.0/SockIoServer.h deleted file mode 100644 index eddfe454..00000000 --- a/sdk/webservice/1.0/SockIoServer.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef __SOCKIOSERVER_H__ -#define __SOCKIOSERVER_H__ - -#include "WebServer.h" -#include "Msg_1_0.h" - -class SockIoServer : public WebServer -{ -public: - SockIoServer(HGMsgPump msgPump, class Manager *manager); - virtual ~SockIoServer(); - - void Connect(const ConnectParam *param); - void Command(HGUInt usrId, const SockIoCmdParam *param); - void Event(HGUInt usrId, const SockIoEvtParam* param); -}; - -#endif /* __SOCKIOSERVER_H__ */ \ No newline at end of file diff --git a/sdk/webservice/1.0/SockIoUser.cpp b/sdk/webservice/1.0/SockIoUser.cpp deleted file mode 100644 index ad7dd69c..00000000 --- a/sdk/webservice/1.0/SockIoUser.cpp +++ /dev/null @@ -1,605 +0,0 @@ -#include "SockIoUser.h" -#include "WebServer.h" -#include "Manager.h" -#include "base/HGInfo.h" -#include "base/HGUtility.h" -#include "sha1.h" -#include "base64.h" -#include "cJSON.h" - -#if defined(HG_CMP_MSC) -SockIoUser::SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn) -#else -SockIoUser::SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn) -#endif - : WebUser(server, id, ip, port, sockConn) -{ - GetManager()->SetScanEvent(ScanCallback, this); -} - -SockIoUser::~SockIoUser() -{ - GetManager()->ResetScanEvent(); -} - -void SockIoUser::HandleCmd(const SockIoCmdParam* param) -{ - std::string user; - std::string data; - GetMsgInfo(param, user, data); - if (user.empty()) - { - return; - } - - if ("scan" == user) - { - std::string imgName; - bool insert = false; - - cJSON* json = cJSON_Parse(data.c_str()); - if (NULL != json) - { - if (NULL != json->child && 0 == strcmp("imageName", json->child->string) - && cJSON_String == json->child->type) - { - imgName = json->child->valuestring; - if (NULL != json->child->next && 0 == strcmp("isInsert", json->child->next->string) - && cJSON_True == json->child->next->type) - insert = true; - } - - cJSON_Delete(json); - } - - bool ret = GetManager()->Scan(imgName, insert); - if (!ret) - { - std::string resp = "42[\"error\", \"scan error\"]"; - SendResponse((const HGByte*)resp.c_str(), (HGUInt)resp.size(), HGTRUE); - } - } - else if ("stop" == user) - { - bool ret = GetManager()->StopScan(); - assert(ret); - - std::string resp = "42[\"success\", \"stop scan success!\"]"; - SendResponse((const HGByte*)resp.c_str(), (HGUInt)resp.size(), HGTRUE); - } -} - -void SockIoUser::HandleEvent(const SockIoEvtParam* param) -{ - SendResponse(param->data, param->size, HGTRUE); -} - -void SockIoUser::PostCmdMsg(const HGByte* data, HGUInt dataSize) -{ - SockIoCmdParam* param = new SockIoCmdParam; - param->svr = m_server; - param->usrId = m_id; - param->data = new HGByte[dataSize]; - param->size = dataSize; - memcpy(param->data, data, dataSize); - - HGMsg msg; - msg.id = MSGID_SOCKIO_COMMAND; - msg.data = param; - if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_server->GetMsgPump(), &msg)) - { - delete[] param->data; - param->size = 0; - delete param; - } -} - -void SockIoUser::PostEventMsg(const HGByte* data, HGUInt dataSize) -{ - SockIoEvtParam* param = new SockIoEvtParam; - param->svr = m_server; - param->usrId = m_id; - param->data = new HGByte[dataSize]; - param->size = dataSize; - memcpy(param->data, data, dataSize); - - HGMsg msg; - msg.id = MSGID_SOCKIO_EVENT; - msg.data = param; - if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_server->GetMsgPump(), &msg)) - { - delete[] param->data; - param->size = 0; - delete param; - } -} - -void SockIoUser::ThreadFunc() -{ - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SockIoUser::ThreadFunc"); - - char chBuffer[2048]; - const char* pBuffer = chBuffer; - int nBufferSize = 0; - bool bConnect = false; - - unsigned char connectDataTail[4] = { '\r', '\n', '\r', '\n' }; - unsigned int connectDataTailLen = 0; - std::string connectData; - - uint8_t* pData = NULL; - int nDataSize = 0; - uint8_t* pDataEx = NULL; - int nRemainSize = 0; - uint8_t headData[20]; - uint32_t nHeadDataLen = 0; - uint8_t vMask[4]; - uint32_t nMaskCount = 0; - bool bHandle = false; - std::vector vAllData; - - while (1) - { - if (0 == nBufferSize) - { - int len = recv(m_sockConn, chBuffer, 2048, 0); - if (len <= 0) - { - // 这里跳出,可能是服务器关闭了socketConn,或者客户端关闭了socket,或者网络断开 - PostDisConnectMsg(); - break; - } - else - { - pBuffer = chBuffer; - nBufferSize = len; - } - } - - assert(nBufferSize > 0); - - if (!bConnect) - { - unsigned char b = *pBuffer; - ++pBuffer; - --nBufferSize; - - connectData.push_back(b); - if (b == connectDataTail[connectDataTailLen]) - { - ++connectDataTailLen; - } - else - { - connectDataTailLen = 0; - if (b == connectDataTail[connectDataTailLen]) - { - ++connectDataTailLen; - } - } - - if (4 == connectDataTailLen) - { - connectDataTailLen = 0; - bool shakeRet = ShakeHand(connectData); - connectData.clear(); - - if (!shakeRet) - { - PostDisConnectMsg(); - break; - } - - bConnect = true; - } - } - else - { - if (NULL == pData) - { - assert(0 == nDataSize); - - uint8_t b = *pBuffer; - ++pBuffer; - --nBufferSize; - headData[nHeadDataLen] = b; - ++nHeadDataLen; - - if (1 == nHeadDataLen) - { - if ((0x80 | 0x08) == headData[0]) // 断开连接 - { - PostDisConnectMsg(); - break; - } - else if ((0x80 | 0x09) == headData[0]) // PING帧 - { - // - } - else if ((0x80 | 0x0A) == headData[0]) // PONG帧 - { - // - } - else if ((0x00 | 0x01) == headData[0] || (0x00 | 0x02) == headData[0] || (0x00 | 0x00) == headData[0] || (0x80 | 0x00) == headData[0] - || (0x80 | 0x01) == headData[0] || (0x80 | 0x02) == headData[0]) // 数据帧 - { - if ((0x80 | 0x00) == headData[0] || (0x80 | 0x01) == headData[0] || (0x80 | 0x02) == headData[0]) - { - // 分片结束 - bHandle = true; - } - else - { - // 分片帧 - bHandle = false; - } - } - else // 帧错误,断开连接 - { - PostDisConnectMsg(); - break; - } - } - else if (2 == nHeadDataLen) - { - if (0 == (headData[1] & 0x80)) // 必须经过掩码处理 - { - PostDisConnectMsg(); - break; - } - - if ((0x80 | 0x09) == headData[0]) // PING帧 - { - if (0x80 != headData[1]) - { - PostDisConnectMsg(); - break; - } - } - else if ((0x80 | 0x0A) == headData[0]) // PONG帧 - { - if (0x80 != headData[1]) - { - PostDisConnectMsg(); - break; - } - } - else - { - if ((headData[1] & 0x7F) <= 125) - { - uint32_t nCmdSize = (headData[1] & 0x7F); - nHeadDataLen = 0; - - if (0 == nCmdSize) - { - PostDisConnectMsg(); - break; - } - - nDataSize = nCmdSize; - nRemainSize = nCmdSize; - pData = new uint8_t[nDataSize]; - pDataEx = pData; - } - } - } - else if (4 == nHeadDataLen) - { - if ((0x80 | 0x09) == headData[0]) // PING帧 - { - // - } - else if ((0x80 | 0x0A) == headData[0]) // PONG帧 - { - // - } - else - { - if ((headData[1] & 0x7F) == 126) - { - uint32_t nCmdSize = ntohs(*(uint16_t*)&headData[2]); - nHeadDataLen = 0; - - if (0 == nCmdSize) - { - PostDisConnectMsg(); - break; - } - - nDataSize = nCmdSize; - nRemainSize = nCmdSize; - pData = new uint8_t[nDataSize]; - pDataEx = pData; - } - } - } - else if (6 == nHeadDataLen) - { - if ((0x80 | 0x09) == headData[0]) // PING帧 - { - nHeadDataLen = 0; - - Pong(); - } - else if ((0x80 | 0x0A) == headData[0]) // PONG帧 - { - nHeadDataLen = 0; - } - } - else if (10 == nHeadDataLen) - { - if ((headData[1] & 0x7F) == 127) // 这里一定会等于127 - { - uint32_t nCmdSizeHigh = ntohl(*(uint32_t*)&headData[2]); - uint32_t nCmdSize = ntohl(*(uint32_t*)&headData[6]); - nHeadDataLen = 0; - - if ((0 != nCmdSizeHigh) || (0 == nCmdSize)) - { - PostDisConnectMsg(); - break; - } - - nDataSize = nCmdSize; - nRemainSize = nCmdSize; - pData = new uint8_t[nDataSize]; - pDataEx = pData; - } - } - } - else - { - if (4 != nMaskCount) - { - uint8_t b = *pBuffer; - ++pBuffer; - --nBufferSize; - vMask[nMaskCount] = b; - ++nMaskCount; - } - else - { - int nWriteSize = HGMIN(nBufferSize, nRemainSize); - memcpy(pDataEx, pBuffer, nWriteSize); - pBuffer += nWriteSize; - nBufferSize -= nWriteSize; - pDataEx += nWriteSize; - nRemainSize -= nWriteSize; - - if (0 == nRemainSize) - { - assert(pDataEx == pData + nDataSize); - - for (int i = 0; i < nDataSize; ++i) - { - int j = i % 4; - pData[i] = pData[i] ^ vMask[j]; - vAllData.push_back(pData[i]); - } - - delete[] pData; - pData = NULL; - nDataSize = 0; - nMaskCount = 0; - - if (bHandle) - { - if (1 == vAllData.size() && '2' == vAllData[0]) - { - // socket.io pong - char data = '3'; - SendResponse((const HGByte*)&data, 1, HGTRUE); - } - else - { - PostCmdMsg(&vAllData[0], vAllData.size()); - } - - bHandle = false; - vAllData.clear(); - } - } - } - } - } - } - - if (NULL != pData) - { - delete[] pData; - pData = NULL; - nDataSize = 0; - nMaskCount = 0; - } -} - -void SockIoUser::ScanCallback(HGUInt event, void* value1, void* value2, void* param) -{ - SockIoUser* p = (SockIoUser*)param; - - char *resp = NULL; - if (SCANEVENT_ARRIVE == event) - { - resp = new char[256]; - sprintf(resp, "42[\"success\", \"%s\"]", (const char*)value1); - } - else if (SCANEVENT_REMOVE == event) - { - - } - else if (SCANEVENT_WORKING == event) - { - resp = new char[256]; - sprintf(resp, "42[\"event\", \"%s\"]", "......"); - } - else if (SCANEVENT_FINISH == event) - { - resp = new char[256]; - sprintf(resp, "42[\"result\", {\"code\":204, \"msg\":\"%s\"}]", (const char *)value1); - } - else if (SCANEVENT_ERROR == event) - { - resp = new char[256]; - sprintf(resp, "42[\"error\", \"%s\"]", (const char*)value1); - } - else if (SCANEVENT_IMAGE == event) - { - const char* imgName = (const char*)value1; - const char* imgBase64 = (const char*)value2; - resp = new char[256 + strlen(imgName) + strlen(imgBase64)]; - sprintf(resp, "42[\"image\", {\"code\":201, \"imageName\":\"%s\", \"image\":\"%s\"}]", imgName, imgBase64); - } - - if (NULL != resp) - { - p->PostEventMsg((const HGByte*)resp, strlen(resp)); - delete[] resp; - } -} - -void SockIoUser::GetMsgInfo(const SockIoCmdParam* param, std::string& user, std::string& data) -{ - user.clear(); - data.clear(); - - std::string paramStr((const char *)param->data, param->size); - size_t pos = paramStr.find('['); - if (std::string::npos == pos) - { - return; - } - - std::string msgType = paramStr.substr(0, pos); - if ("42" != msgType) - { - return; - } - - std::string msgInfo = paramStr.substr(pos); - cJSON* json = cJSON_Parse(msgInfo.c_str()); - if (NULL != json) - { - if (NULL != json->child) - { - user = json->child->valuestring; - if (NULL != json->child->next) - data = json->child->next->valuestring; - } - - cJSON_Delete(json); - } -} - -bool SockIoUser::ShakeHand(const std::string& head) -{ - std::string requestMethod; - std::string requestURIPath; - HttpPairs requestURIQueryInfos; - std::string requestURIFragment; - std::string httpVersion; - HttpPairs headInfos; - HttpHead::AnalysisHead(head, requestMethod, requestURIPath, requestURIQueryInfos, - requestURIFragment, httpVersion, headInfos); - - if ("websocket" != HttpHead::GetValue(requestURIQueryInfos, "transport")) - return false; - if ("Upgrade" != HttpHead::GetValue(headInfos, "Connection")) - return false; - if ("websocket" != HttpHead::GetValue(headInfos, "Upgrade")) - return false; - - std::string key = HttpHead::GetValue(headInfos, "Sec-WebSocket-Key"); - if (key.empty()) - return false; - - key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; - - unsigned int message_digest[5]; - SHA1 sha; - sha.Reset(); - sha << key.c_str(); - sha.Result(message_digest); - - for (int i = 0; i < 5; ++i) - message_digest[i] = htonl(message_digest[i]); - std::string serverKey = base64_encode((const unsigned char*)message_digest, 20); - - std::string handShakeResp = "HTTP/1.1 101 Switching Protocols\r\n"; - handShakeResp += "Upgrade: websocket\r\n"; - handShakeResp += "Connection: Upgrade\r\n"; - handShakeResp += "Sec-WebSocket-Accept:"; - handShakeResp += serverKey; - handShakeResp += "\r\n\r\n"; - send(m_sockConn, handShakeResp.c_str(), (int)handShakeResp.length(), 0); - - char uuid[256] = {0}; - HGBase_GetUuid(uuid, 256); - - std::string resp = "0{"; - resp += "\"sid\":\""; - resp += uuid; - resp += "\","; - resp += "\"upgrades\":[\"websocket\"],"; - resp += "\"pingInterval\":25000,"; - resp += "\"pingTimeout\":60000"; - resp += "}"; - SendResponse((const HGByte*)resp.c_str(), (int)resp.size(), HGTRUE); - - resp = "40"; - SendResponse((const HGByte*)resp.c_str(), (int)resp.size(), HGTRUE); - return true; -} - -void SockIoUser::Pong() -{ - uint8_t vHead[2]; - vHead[0] = 0x80 | 0x0A; - vHead[1] = 0; - - HGBase_EnterLock(m_cs); - send(m_sockConn, (const char*)vHead, 2, 0); - HGBase_LeaveLock(m_cs); -} - -bool SockIoUser::SendResponse(const HGByte* data, HGUInt size, HGBool text) -{ - if (NULL == data || 0 == size) - { - return false; - } - - uint32_t nHeadLen = 0; - - uint8_t vHead[20] = { 0 }; - vHead[0] = text ? (0x80 | 0x01) : (0x80 | 0x02); - if (size <= 125) - { - vHead[1] = (uint8_t)size; - nHeadLen = 2; - } - else if (size <= 0xFFFF) - { - vHead[1] = 126; - uint16_t payloadLength16b = htons((uint16_t)size); - memcpy(&vHead[2], &payloadLength16b, 2); - nHeadLen = 4; - } - else - { - vHead[1] = 127; - vHead[2] = 0; - vHead[3] = 0; - vHead[4] = 0; - vHead[5] = 0; - uint32_t payloadLength32b = htonl(size); - memcpy(&vHead[6], &payloadLength32b, 4); - nHeadLen = 10; - } - - HGBase_EnterLock(m_cs); - send(m_sockConn, (const char*)vHead, nHeadLen, 0); - send(m_sockConn, (const char*)data, size, 0); - HGBase_LeaveLock(m_cs); - return true; -} diff --git a/sdk/webservice/1.0/SockIoUser.h b/sdk/webservice/1.0/SockIoUser.h deleted file mode 100644 index 5232a889..00000000 --- a/sdk/webservice/1.0/SockIoUser.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef __SOCKIOUSER_H__ -#define __SOCKIOUSER_H__ - -#include "WebUser.h" -#include "Msg_1_0.h" - -class WebServer; - -class SockIoUser : public WebUser -{ -public: -#if defined(HG_CMP_MSC) - SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn); -#else - SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn); -#endif - virtual ~SockIoUser(); - - void HandleCmd(const SockIoCmdParam* param); - void HandleEvent(const SockIoEvtParam* param); - -protected: - void PostCmdMsg(const HGByte* data, HGUInt dataSize); - void PostEventMsg(const HGByte* data, HGUInt dataSize); - virtual void ThreadFunc(); - -private: - static void ScanCallback(HGUInt event, void* value1, void *value2, void* param); - static void GetMsgInfo(const SockIoCmdParam* param, std::string &user, std::string& data); - bool ShakeHand(const std::string& head); - void Pong(); - bool SendResponse(const HGByte* data, HGUInt size, HGBool text); -}; - -#endif /* __SOCKIOUSER_H__ */ \ No newline at end of file diff --git a/sdk/webservice/1.0/WebServer.cpp b/sdk/webservice/1.0/WebServer.cpp deleted file mode 100644 index f989853c..00000000 --- a/sdk/webservice/1.0/WebServer.cpp +++ /dev/null @@ -1,210 +0,0 @@ -#include "WebServer.h" -#include "WebUser.h" -#include "base/HGInfo.h" - -WebServer::WebServer(HGMsgPump msgPump, Manager* manager) -{ - m_msgPump = msgPump; - m_manager = manager; - - m_currUserId = 1; -#if defined(HG_CMP_MSC) - m_sockServer = INVALID_SOCKET; -#else - m_sockServer = -1; -#endif - m_listenThread = NULL; -} - -WebServer::~WebServer() -{ - -} - -HGMsgPump WebServer::GetMsgPump() -{ - return m_msgPump; -} - -Manager* WebServer::GetManager() -{ - return m_manager; -} - -bool WebServer::Open(HGUShort port) -{ -#if defined(HG_CMP_MSC) - if (INVALID_SOCKET != m_sockServer) -#else - if (-1 != m_sockServer) -#endif - { - return false; - } - -#if defined(HG_CMP_MSC) - SOCKET sockServer = socket(AF_INET, SOCK_STREAM, 0); - if (INVALID_SOCKET == sockServer) -#else - int sockServer = socket(AF_INET, SOCK_STREAM, 0); - if (-1 == sockServer) -#endif - { - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "open webserver failed 1, port=%u", port); - return false; - } - - // bind -#if defined(HG_CMP_MSC) - SOCKADDR_IN addrServer; - addrServer.sin_addr.S_un.S_addr = INADDR_ANY; - addrServer.sin_family = AF_INET; - addrServer.sin_port = htons(port); - if (0 != bind(sockServer, (SOCKADDR*)&addrServer, sizeof(SOCKADDR_IN))) -#else - struct sockaddr_in addrServer; - addrServer.sin_addr.s_addr = htonl(INADDR_ANY); - addrServer.sin_family = AF_INET; - addrServer.sin_port = htons(port); - if (0 != bind(sockServer, (struct sockaddr*)&addrServer, sizeof(addrServer))) -#endif - { -#if defined(HG_CMP_MSC) - closesocket(sockServer); -#else - close(sockServer); -#endif - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "open webserver failed 2, port=%u", port); - return false; - } - - // listen - if (0 != listen(sockServer, 5)) - { -#if defined(HG_CMP_MSC) - closesocket(sockServer); -#else - close(sockServer); -#endif - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "open webserver failed 3, port=%u", port); - return false; - } - - m_sockServer = sockServer; - HGBase_OpenThread(ThreadFunc, this, &m_listenThread); - assert(NULL != m_listenThread); - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "open webserver success, port=%u", port); - return true; -} - -bool WebServer::Close() -{ -#if defined(HG_CMP_MSC) - if (INVALID_SOCKET == m_sockServer) -#else - if (-1 == m_sockServer) -#endif - { - return false; - } - - while (!m_vectorUser.empty()) - { - WebUser* pUser = m_vectorUser[0]; - m_vectorUser.erase(m_vectorUser.begin()); - delete pUser; - pUser = NULL; - } - -#if defined(HG_CMP_MSC) - closesocket(m_sockServer); - m_sockServer = INVALID_SOCKET; -#else - close(m_sockServer); - m_sockServer = -1; -#endif - HGBase_CloseThread(m_listenThread); - m_listenThread = NULL; - return true; -} - -void WebServer::DisConnect(HGUInt usrId) -{ - assert(0 != usrId); - - int nIndex = GetUserIndex(usrId); - if (-1 != nIndex) - { - WebUser* pUser = m_vectorUser[nIndex]; - m_vectorUser.erase(m_vectorUser.begin() + nIndex); - delete pUser; - pUser = NULL; - } -} - -#if defined(HG_CMP_MSC) -void WebServer::PostConnectMsg(const char* ip, uint16_t port, SOCKET sockConn) -#else -void WebServer::PostConnectMsg(const char* ip, uint16_t port, int sockConn) -#endif -{ - ConnectParam* param = new ConnectParam; - param->svr = this; - strcpy(param->ip, ip); - param->port = port; - param->socket = sockConn; - - HGMsg msg; - msg.id = MSGID_CONNECT; - msg.data = param; - if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_msgPump, &msg)) - { -#if defined(HG_CMP_MSC) - closesocket(param->socket); -#else - close(param->socket); -#endif - delete param; - } -} - -int WebServer::GetUserIndex(HGUInt id) -{ - int nIndex = -1; - for (int i = 0; i < (int)m_vectorUser.size(); ++i) - { - if (id == m_vectorUser[i]->GetId()) - { - nIndex = i; - break; - } - } - - return nIndex; -} - -void WebServer::ThreadFunc(HGThread thread, HGPointer param) -{ - WebServer* p = (WebServer*)param; - - while (1) - { -#if defined(HG_CMP_MSC) - SOCKADDR_IN addrClient; - int len = sizeof(SOCKADDR_IN); - SOCKET socketConn = accept(p->m_sockServer, (SOCKADDR*)&addrClient, &len); - if (INVALID_SOCKET == socketConn) -#else - struct sockaddr_in addrClient; - socklen_t len = sizeof(addrClient); - int socketConn = accept(p->m_sockServer, (struct sockaddr*)&addrClient, &len); - if (-1 == socketConn) -#endif - { - // Ƿرm_sockServer - break; - } - - p->PostConnectMsg(inet_ntoa(addrClient.sin_addr), ntohs(addrClient.sin_port), socketConn); - } -} \ No newline at end of file diff --git a/sdk/webservice/1.0/WebServer.h b/sdk/webservice/1.0/WebServer.h deleted file mode 100644 index 6c1df4d5..00000000 --- a/sdk/webservice/1.0/WebServer.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef __WEBSERVER_H__ -#define __WEBSERVER_H__ - -#include "base/HGDef.h" -#include "base/HGInc.h" -#include "base/HGThread.h" -#include "base/HGMsgPump.h" -#include "Msg_1_0.h" -#include - -class Manager; - -class WebServer -{ -public: - WebServer(HGMsgPump msgPump, Manager* manager); - virtual ~WebServer(); - - HGMsgPump GetMsgPump(); - Manager* GetManager(); - - bool Open(HGUShort port); - bool Close(); - - virtual void Connect(const ConnectParam* param) = 0; - void DisConnect(HGUInt usrId); - -protected: -#if defined(HG_CMP_MSC) - void PostConnectMsg(const char* ip, uint16_t port, SOCKET sockConn); -#else - void PostConnectMsg(const char* ip, uint16_t port, int sockConn); -#endif - int GetUserIndex(HGUInt id); - static void ThreadFunc(HGThread thread, HGPointer param); - -protected: - HGMsgPump m_msgPump; - Manager* m_manager; - - HGUInt m_currUserId; -#if defined(HG_CMP_MSC) - SOCKET m_sockServer; -#else - int m_sockServer; -#endif - HGThread m_listenThread; - std::vector m_vectorUser; -}; - -#endif /* __WEBSERVER_H__ */ \ No newline at end of file diff --git a/sdk/webservice/1.0/WebUser.cpp b/sdk/webservice/1.0/WebUser.cpp deleted file mode 100644 index 9f0c3f60..00000000 --- a/sdk/webservice/1.0/WebUser.cpp +++ /dev/null @@ -1,80 +0,0 @@ -#include "WebUser.h" -#include "WebServer.h" -#include "base/HGInfo.h" - -#if defined(HG_CMP_MSC) -WebUser::WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn) -#else -WebUser::WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn) -#endif -{ - m_server = server; - HGBase_CreateLock(&m_cs); - m_id = id; - strcpy(m_ip, ip); - m_port = port; - - m_sockConn = sockConn; - m_thread = NULL; -} - -WebUser::~WebUser() -{ -#if defined(HG_CMP_MSC) - closesocket(m_sockConn); - m_sockConn = INVALID_SOCKET; -#else - close(m_sockConn); - m_sockConn = -1; -#endif - HGBase_CloseThread(m_thread); - m_thread = NULL; - - m_port = 0; - memset(m_ip, 0, sizeof(m_ip)); - m_id = 0; - HGBase_DestroyLock(m_cs); - m_server = NULL; -} - -void WebUser::Open() -{ - HGBase_OpenThread(ThreadFunc, this, &m_thread); - assert(NULL != m_thread); -} - -HGUInt WebUser::GetId() -{ - return m_id; -} - -Manager* WebUser::GetManager() -{ - return m_server->GetManager(); -} - -void WebUser::PostDisConnectMsg() -{ - DisConnectParam* param = new DisConnectParam; - param->svr = m_server; - param->usrId = m_id; - - HGMsg msg; - msg.id = MSGID_DISCONNECT; - msg.data = param; - if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_server->GetMsgPump(), &msg)) - { - delete param; - } -} - -void WebUser::ThreadFunc() -{ - HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "WebUser::ThreadFunc"); -} - -void WebUser::ThreadFunc(HGThread thread, HGPointer param) -{ - WebUser* p = (WebUser*)param; - p->ThreadFunc(); -} diff --git a/sdk/webservice/1.0/WebUser.h b/sdk/webservice/1.0/WebUser.h deleted file mode 100644 index 9dcf15af..00000000 --- a/sdk/webservice/1.0/WebUser.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef __WEBUSER_H__ -#define __WEBUSER_H__ - -#include "base/HGDef.h" -#include "base/HGInc.h" -#include "base/HGLock.h" -#include "base/HGThread.h" -#include "base/HGMsgPump.h" - -class WebServer; -class Manager; - -class WebUser -{ -public: -#if defined(HG_CMP_MSC) - WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn); -#else - WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn); -#endif - virtual ~WebUser(); - - void Open(); - HGUInt GetId(); - Manager* GetManager(); - -protected: - void PostDisConnectMsg(); - virtual void ThreadFunc(); - -private: - static void ThreadFunc(HGThread thread, HGPointer param); - -protected: - WebServer* m_server; - HGLock m_cs; - HGUInt m_id; - char m_ip[16]; - uint16_t m_port; - -#if defined(HG_CMP_MSC) - SOCKET m_sockConn; -#else - int m_sockConn; -#endif - HGThread m_thread; -}; - -#endif /* __WEBUSER_H__ */ diff --git a/sdk/webservice/HttpHead.cpp b/sdk/webservice/HttpHead.cpp new file mode 100644 index 00000000..47af99e9 --- /dev/null +++ b/sdk/webservice/HttpHead.cpp @@ -0,0 +1,441 @@ +#include "HttpHead.h" + +namespace ver_1 +{ + const unsigned int asciiTableData[256] = + { + 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, + 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004, + 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, + 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, + 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, + 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, + 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, + 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, + 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253, + 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, + 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, + 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, + 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073, + 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, + 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, + 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004 + /* the upper 128 are all zeroes */ + }; + + static void TrimString(std::string& str) + { + std::string str1; + bool add1 = false; + std::string::const_iterator iter1; + for (iter1 = str.begin(); iter1 != str.end(); ++iter1) + { + int c = (HGByte)(*iter1); + if (!add1) + { + if (!isspace(c)) + { + str1.push_back(c); + add1 = true; + } + } + else + { + str1.push_back(c); + } + } + + if (str1.empty()) + { + str.clear(); + return; + } + + std::string str2; + bool add2 = false; + std::string::const_reverse_iterator iter2; + for (iter2 = str1.rbegin(); iter2 != str1.rend(); ++iter2) + { + int c = (HGByte)(*iter2); + if (!add2) + { + if (!isspace(c)) + { + str2.push_back(c); + add2 = true; + } + } + else + { + str2.push_back(c); + } + } + + if (str2.empty()) + { + str.clear(); + return; + } + + str = std::string(str2.rbegin(), str2.rend()); + } + + HttpHead::HttpHead() + { + + } + + HttpHead::~HttpHead() + { + + } + + bool HttpHead::Parse(const std::string& head) + { + AnalysisHead(head, m_requestMethod, m_requestURIPath, m_requestURIQueryInfos, + m_requestURIFragment, m_requestHttpVersion, m_headInfos); + return true; + } + + void HttpHead::Clear() + { + m_requestMethod.clear(); + m_requestURIPath.clear(); + m_requestURIQueryInfos.clear(); + m_requestURIFragment.clear(); + m_requestHttpVersion.clear(); + m_headInfos.clear(); + } + + std::string HttpHead::GetRequestMethod() const + { + return m_requestMethod; + } + + std::string HttpHead::GetRequestURIPath() const + { + return m_requestURIPath; + } + + HttpPairs HttpHead::GetRequestURIQueryInfos() const + { + return m_requestURIQueryInfos; + } + + std::string HttpHead::GetRequestURIFragment() const + { + return m_requestURIFragment; + } + + std::string HttpHead::GetRequestHttpVersion() const + { + return m_requestHttpVersion; + } + + HttpPairs HttpHead::GetHeadInfos() const + { + return m_headInfos; + } + + int HttpHead::GetContentLength() const + { + int len = 0; + for (int i = 0; i < (int)m_headInfos.size(); ++i) + { +#if defined(HG_CMP_MSC) + if (0 == _stricmp("Content-Length", m_headInfos[i].first.c_str())) +#else + if (0 == strcasecmp("Content-Length", m_headInfos[i].first.c_str())) +#endif + { + len = atoi(m_headInfos[i].second.c_str()); + break; + } + } + + return len; + } + + std::string HttpHead::GetContentType() const + { + std::string type; + for (int i = 0; i < (int)m_headInfos.size(); ++i) + { +#if defined(HG_CMP_MSC) + if (0 == _stricmp("Content-Type", m_headInfos[i].first.c_str())) +#else + if (0 == strcasecmp("Content-Type", m_headInfos[i].first.c_str())) +#endif + { + type = m_headInfos[i].second.c_str(); + break; + } + } + + return type; + } + + std::string HttpHead::GetValue(const HttpPairs& infos, const std::string& key) + { + std::string value; + for (int i = 0; i < (int)infos.size(); ++i) + { + if (key == infos[i].first) + { + value = infos[i].second; + break; + } + } + + return value; + } + + void HttpHead::AnalysisURIQuery(const std::string& query, HttpPairs& queryInfos) + { + std::vector queryList; + + char* p = new char[query.size() + 1]; + strcpy(p, query.c_str()); + char* pStr = strtok(p, "&"); + if (NULL != pStr) + queryList.push_back(pStr); + while (1) + { + pStr = strtok(NULL, "&"); + if (NULL == pStr) + break; + queryList.push_back(pStr); + } + delete[] p; + + queryInfos.clear(); + for (int i = 0; i < (int)queryList.size(); ++i) + { + p = new char[queryList[i].size() + 1]; + strcpy(p, queryList[i].c_str()); + + std::pair pr; + pStr = strtok(p, "="); + if (NULL != pStr) + pr.first = AnalyURIString(pStr); + pStr = strtok(NULL, "="); + if (NULL != pStr) + pr.second = AnalyURIString(pStr); + + queryInfos.push_back(pr); + delete[] p; + } + } + + void HttpHead::AnalysisURI(const std::string& uri, std::string& path, HttpPairs& queryInfos, std::string& fragment) + { + size_t pathPos = uri.find('/'); + size_t queryPos = uri.find('?'); + size_t fragmentPos = uri.find('#'); + + path.clear(); + if (std::string::npos != pathPos) + { + size_t count = std::string::npos; + if (queryPos != std::string::npos) + { + assert(queryPos > pathPos); + count = queryPos - pathPos; + } + else if (fragmentPos != std::string::npos) + { + assert(fragmentPos > pathPos); + count = fragmentPos - pathPos; + } + + path = AnalyURIString(uri.substr(pathPos, count)); + } + + queryInfos.clear(); + if (std::string::npos != queryPos) + { + size_t count = std::string::npos; + if (fragmentPos != std::string::npos) + { + assert(fragmentPos > queryPos); + count = fragmentPos - queryPos; + } + + std::string query = uri.substr(queryPos + 1, count - 1); + AnalysisURIQuery(query, queryInfos); + } + + fragment.clear(); + if (std::string::npos != fragmentPos) + { + fragment = AnalyURIString(uri.substr(fragmentPos + 1)); + } + } + + void HttpHead::AnalysisHead(const std::string& head, std::string& requestMethod, std::string& requestURIPath, + HttpPairs& requestURIQueryInfos, std::string& requestURIFragment, std::string& httpVersion, HttpPairs& headInfos) + { + requestMethod.clear(); + requestURIPath.clear(); + requestURIQueryInfos.clear(); + requestURIFragment.clear(); + httpVersion.clear(); + headInfos.clear(); + + std::vector headList; + + char* p = new char[head.size() + 1]; + strcpy(p, head.c_str()); + char* pStr = strtok(p, "\r\n"); + if (NULL != pStr) + headList.push_back(pStr); + while (1) + { + pStr = strtok(NULL, "\r\n"); + if (NULL == pStr) + break; + headList.push_back(pStr); + } + delete[] p; + + if (headList.size() < 1) + { + return; + } + + std::string requestURI; + + // 解析请求行 + p = new char[headList[0].size() + 1]; + strcpy(p, headList[0].c_str()); + pStr = strtok(p, " "); + if (NULL != pStr) + requestMethod = pStr; + pStr = strtok(NULL, " "); + if (NULL != pStr) + requestURI = pStr; + pStr = strtok(NULL, " "); + if (NULL != pStr) + httpVersion = pStr; + delete[] p; + + // 解析URI + AnalysisURI(requestURI, requestURIPath, requestURIQueryInfos, requestURIFragment); + + // 解析请求头 + for (int i = 1; i < (int)headList.size(); ++i) + { + p = new char[headList[i].size() + 1]; + strcpy(p, headList[i].c_str()); + + std::pair pr; + pStr = strtok(p, ":"); + if (NULL != pStr) + pr.first = pStr; + pStr = strtok(NULL, ":"); + if (NULL != pStr) + pr.second = pStr; + + TrimString(pr.first); + TrimString(pr.second); + headInfos.push_back(pr); + + delete[] p; + } + } + + /*判断ascii码是否是数字0-9*/ + static bool asciiIsDigit(char c) + { + /*字符的ascii码&8 结果为0-127,则是数字*/ + return asciiTableData[(unsigned char)c & (1 << 3)]; + } + + static int asciiDigitValue(char c) + { + if (asciiIsDigit(c)) + return c - '0'; + return -1; + } + + static int asciiXdigitValue(char c) + { + //printf("-->%c\n",c); + if (c >= 'A' && c <= 'F') + return c - 'A' + 10;//(A B C D E F)->(10 11 12 13 14 15) + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + + return asciiDigitValue(c);//('0'...'9')->(0...9) + } + + static int unescapeCharacter(const char* scanner) + { + int first = asciiXdigitValue(scanner[0]); + if (first < 0) + return -1; + + int second = asciiXdigitValue(scanner[1]); + if (second < 0) + return -1; + + return (first << 4) | second; //== (first*16 | second) == (first*16 + second) + } + + static char* unescapeUriString(const char* uriString, bool asciiEscape) + { + if (NULL == uriString) + return NULL; + + int strLen = (int)strlen(uriString); + char* result = (char*)malloc(strLen + 1);//可推测解码后的长度<=原长度 + char* out = result; + + const char* in, * end; + for (in = uriString, end = in + strLen; in < end; ++in) + { + int c = *in; + + //遇到了'%'才去解析 + if ('%' == c) + { + if (in + 3 > end) + break; + //获取%后2个字符的解码值 + c = unescapeCharacter(in + 1); + if (c <= 0) + break; + + if (asciiEscape && c <= 0x7F) + break; + + in += 2;//一般的格式为%后加两个ascii码字符 + } + + *out++ = c;//存储转义结果 + } + + *out = '\0'; + + if (in != end) + { + free(result); + return NULL; + } + + return result; + } + + std::string HttpHead::AnalyURIString(const std::string& str) + { + std::string ret; + char* decodeStr = unescapeUriString(str.c_str(), false); + if (NULL != decodeStr) + { + ret = decodeStr; + free(decodeStr); + } + + return ret; + } +} \ No newline at end of file diff --git a/sdk/webservice/HttpHead.h b/sdk/webservice/HttpHead.h new file mode 100644 index 00000000..a756a10b --- /dev/null +++ b/sdk/webservice/HttpHead.h @@ -0,0 +1,48 @@ +#pragma once + +#include "base/HGDef.h" +#include "base/HGInc.h" +#include +#include + +namespace ver_1 +{ + typedef std::pair HttpPair; + typedef std::vector HttpPairs; + + class HttpHead + { + public: + HttpHead(); + ~HttpHead(); + + bool Parse(const std::string& head); + void Clear(); + + std::string GetRequestMethod() const; + std::string GetRequestURIPath() const; + HttpPairs GetRequestURIQueryInfos() const; + std::string GetRequestURIFragment() const; + std::string GetRequestHttpVersion() const; + HttpPairs GetHeadInfos() const; + int GetContentLength() const; + std::string GetContentType() const; + + static std::string GetValue(const HttpPairs& infos, const std::string& key); + static void AnalysisURIQuery(const std::string& query, HttpPairs& queryInfos); + static void AnalysisURI(const std::string& uri, std::string& path, HttpPairs& queryInfos, std::string& fragment); + static void AnalysisHead(const std::string& head, std::string& requestMethod, std::string& requestURIPath, + HttpPairs& requestURIQueryInfos, std::string& requestURIFragment, std::string& httpVersion, HttpPairs& headInfos); + + private: + static std::string AnalyURIString(const std::string& str); + + private: + std::string m_requestMethod; + std::string m_requestURIPath; + HttpPairs m_requestURIQueryInfos; + std::string m_requestURIFragment; + std::string m_requestHttpVersion; + HttpPairs m_headInfos; + }; +} \ No newline at end of file diff --git a/sdk/webservice/HttpServer.cpp b/sdk/webservice/HttpServer.cpp new file mode 100644 index 00000000..3856fd55 --- /dev/null +++ b/sdk/webservice/HttpServer.cpp @@ -0,0 +1,38 @@ +#include "HttpServer.h" +#include "HttpUser.h" +#include "base/HGInfo.h" + +namespace ver_1 +{ + HttpServer::HttpServer(HGMsgPump msgPump, Manager* manager) + : WebServer(msgPump, manager) + { + + } + + HttpServer::~HttpServer() + { + + } + + void HttpServer::Connect(const ConnectParam* param) + { + assert(NULL != param && this == param->svr); + + WebUser* user = new HttpUser(this, m_currUserId, param->ip, param->port, param->socket); + user->Open(); + ++m_currUserId; + m_vectorUser.push_back(user); + } + + void HttpServer::Command(const HttpCmdParam* param) + { + assert(NULL != param && this == param->svr); + + int nIndex = GetUserIndex(param->usrId); + if (-1 != nIndex) + { + ((HttpUser*)m_vectorUser[nIndex])->HandleCmd(param); + } + } +} diff --git a/sdk/webservice/HttpServer.h b/sdk/webservice/HttpServer.h new file mode 100644 index 00000000..aede1bf1 --- /dev/null +++ b/sdk/webservice/HttpServer.h @@ -0,0 +1,19 @@ +#pragma once + +#include "WebServer.h" +#include "Msg.h" + +namespace ver_1 +{ + class Manager; + + class HttpServer : public WebServer + { + public: + HttpServer(HGMsgPump msgPump, Manager* manager); + virtual ~HttpServer(); + + void Connect(const ConnectParam* param); + void Command(const HttpCmdParam* param); + }; +} \ No newline at end of file diff --git a/sdk/webservice/HttpUser.cpp b/sdk/webservice/HttpUser.cpp new file mode 100644 index 00000000..67a27abe --- /dev/null +++ b/sdk/webservice/HttpUser.cpp @@ -0,0 +1,897 @@ +#include "HttpUser.h" +#include "WebServer.h" +#include "Manager.h" +#include "base/HGInfo.h" +#include "cJSON.h" + +namespace ver_1 +{ + static std::string ToJsonPair(const std::string& key, int value, bool sep) + { + char ret[256]; + sprintf(ret, "\"%s\":%d", key.c_str(), value); + + std::string retStr = ret; + if (sep) + retStr += ", "; + return retStr; + } + + static std::string ToJsonPair(const std::string& key, bool value, bool sep) + { + char ret[256]; + sprintf(ret, "\"%s\":%s", key.c_str(), value ? "true" : "false"); + + std::string retStr = ret; + if (sep) + retStr += ", "; + return retStr; + } + + static std::string ToJsonPair(const std::string& key, const std::string& value, bool sep) + { + char ret[256]; + if (value.empty()) + sprintf(ret, "\"%s\":null", key.c_str()); + else + sprintf(ret, "\"%s\":\"%s\"", key.c_str(), value.c_str()); + + std::string retStr = ret; + if (sep) + retStr += ", "; + return retStr; + } + + static int GetJsonIntValue(cJSON* json, const std::string& key) + { + int ret = 0; + + cJSON* p = json->child; + while (NULL != p) + { + if (0 == strcmp(p->string, key.c_str())) + { + if (p->type == cJSON_Number) + ret = p->valueint; + break; + } + + p = p->next; + } + + return ret; + } + + static bool GetJsonBoolValue(cJSON* json, const std::string& key) + { + bool ret = false; + + cJSON* p = json->child; + while (NULL != p) + { + if (0 == strcmp(p->string, key.c_str())) + { + if (p->type == cJSON_True) + ret = true; + break; + } + + p = p->next; + } + + return ret; + } + + static std::string GetJsonStringValue(cJSON* json, const std::string& key) + { + std::string ret; + + cJSON* p = json->child; + while (NULL != p) + { + if (0 == strcmp(p->string, key.c_str())) + { + if (p->type == cJSON_String) + ret = p->valuestring; + break; + } + + p = p->next; + } + + return ret; + } + +#if defined(HG_CMP_MSC) + HttpUser::HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn) +#else + HttpUser::HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn) +#endif + : WebUser(server, id, ip, port, sockConn) + { + + } + + HttpUser::~HttpUser() + { + + } + + void HttpUser::HandleCmd(const HttpCmdParam* param) + { + assert(NULL != param && m_id == param->usrId); + + std::string requestMethod = param->head.GetRequestMethod(); + std::string requestURIPath = param->head.GetRequestURIPath(); + HttpPairs requestURIQueryInfos = param->head.GetRequestURIQueryInfos(); + std::string requestHttpVersion = param->head.GetRequestHttpVersion(); + + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "HttpUser: method=%s, path=%s, httpVersion=%s", + requestMethod.c_str(), requestURIPath.c_str(), requestHttpVersion.c_str()); + + if (requestMethod == "POST" && requestURIPath == "/WebScan/getVersionInfo") + { + if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) + { + std::string query((const char*)param->data, param->size); + + HttpPairs uriQueryInfos; + HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); + } + + std::string devId; + GetManager()->GetCurDevId(devId); + + char* result = new char[devId.size() + 256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, devId.c_str()); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + delete[] result; + } + else if (requestMethod == "GET" && requestURIPath == "/WebScan/getDevices") + { + std::vector devNameList; + GetManager()->GetDevNames(devNameList); + + std::string devNames = "["; + for (int i = 0; i < (int)devNameList.size(); ++i) + { + devNames += "\""; + devNames += devNameList[i]; + devNames += "\""; + if (i != (int)devNameList.size() - 1) + devNames += ","; + } + devNames += "]"; + + char* result = new char[devNames.size() + 256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, devNames.c_str()); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + delete[] result; + } + else if (requestMethod == "GET" && requestURIPath == "/WebScan/image/getImageByPid") + { + std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); + std::vector imgNameList; + std::vector imgBase64List; + GetManager()->GetImageByDevId(devId, imgNameList, imgBase64List); + + std::string imgInfos = "["; + for (int i = 0; i < (int)imgNameList.size(); ++i) + { + imgInfos += "{\"imageName\":\""; + imgInfos += imgNameList[i]; + imgInfos += "\",\"src\":\""; + imgInfos += imgBase64List[i]; + imgInfos += "\"}"; + + if (i != imgNameList.size() - 1) + imgInfos += ","; + } + imgInfos += "]"; + + char* result = new char[imgInfos.size() + 256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgInfos.c_str()); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + delete[] result; + } + else if (requestMethod == "GET" && requestURIPath == "/WebScan/getParams") + { + std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); + DevParam devParam; + GetManager()->GetDevParam(devId, devParam); + + std::string devParamStr = "{"; + devParamStr += ToJsonPair("device", devParam.device, true); + devParamStr += ToJsonPair("autofeeder", devParam.autofeeder, true); + devParamStr += ToJsonPair("pixel", devParam.pixel, true); + devParamStr += ToJsonPair("white", devParam.white, true); + devParamStr += ToJsonPair("discardBlankThre", devParam.discardBlankThre, true); + devParamStr += ToJsonPair("single", devParam.single, true); + devParamStr += ToJsonPair("format", devParam.format, true); + devParamStr += ToJsonPair("resolution", devParam.resolution, true); + devParamStr += ToJsonPair("orentation", devParam.orentation, true); + devParamStr += ToJsonPair("paperType", devParam.paperType, true); + devParamStr += ToJsonPair("splitImage", devParam.splitImage, true); + devParamStr += ToJsonPair("noiseDetachEnable", devParam.noiseDetachEnable, true); + devParamStr += ToJsonPair("noiseDetach", devParam.noiseDetach, true); + devParamStr += "\"upload\":{"; + devParamStr += ToJsonPair("uploadMode", devParam.uploadMode, true); + devParamStr += ToJsonPair("httpUrl", devParam.httpUrl, true); + devParamStr += ToJsonPair("fileName", devParam.fileName, true); + devParamStr += ToJsonPair("httpMethod", devParam.httpMethod, true); + devParamStr += ToJsonPair("header", devParam.header, true); + devParamStr += ToJsonPair("param", devParam.param, true); + devParamStr += ToJsonPair("ftpUrl", devParam.ftpUrl, true); + devParamStr += ToJsonPair("ftpPath", devParam.ftpPath, true); + devParamStr += ToJsonPair("ftpUser", devParam.ftpUser, true); + devParamStr += ToJsonPair("ftpPassword", devParam.ftpPassword, true); + devParamStr += ToJsonPair("ftpPort", devParam.ftpPort, true); + devParamStr += ToJsonPair("ftpMode", devParam.ftpMode, false); + devParamStr += "}}"; + + char* result = new char[devParamStr.size() + 256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, devParamStr.c_str()); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + delete[] result; + } + else if (requestMethod == "POST" && requestURIPath == "/WebScan/setParams") + { + if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) + { + std::string query((const char*)param->data, param->size); + HttpPairs uriQueryInfos; + HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); + + std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); + std::string devParamStr = HttpHead::GetValue(uriQueryInfos, "params"); + DevParam devParam; + + cJSON* json = cJSON_Parse(devParamStr.c_str()); + if (NULL != json) + { + devParam.autofeeder = GetJsonBoolValue(json, "autofeeder"); + devParam.pixel = GetJsonIntValue(json, "pixel"); + devParam.white = GetJsonBoolValue(json, "white"); + devParam.discardBlankThre = GetJsonIntValue(json, "discardBlankThre"); + devParam.single = GetJsonBoolValue(json, "single"); + devParam.format = GetJsonStringValue(json, "format"); + devParam.resolution = GetJsonIntValue(json, "resolution"); + if (0 == devParam.resolution) + devParam.resolution = atoi(GetJsonStringValue(json, "resolution").c_str()); + devParam.orentation = GetJsonIntValue(json, "orentation"); + devParam.paperType = GetJsonStringValue(json, "paperType"); + devParam.splitImage = GetJsonIntValue(json, "splitImage"); + devParam.noiseDetachEnable = GetJsonBoolValue(json, "noiseDetachEnable"); + devParam.noiseDetach = GetJsonIntValue(json, "noiseDetach"); + + cJSON* p = json->child; + while (NULL != p) + { + if (0 == strcmp(p->string, "upload")) + { + break; + } + + p = p->next; + } + + if (NULL != p && p->type == cJSON_Object) + { + devParam.uploadMode = GetJsonIntValue(p, "uploadMode"); + devParam.httpUrl = GetJsonStringValue(p, "httpUrl"); + devParam.fileName = GetJsonStringValue(p, "fileName"); + devParam.httpMethod = GetJsonStringValue(p, "httpMethod"); + devParam.header = GetJsonStringValue(p, "header"); + devParam.param = GetJsonStringValue(p, "param"); + devParam.ftpUrl = GetJsonStringValue(p, "ftpUrl"); + devParam.ftpPath = GetJsonStringValue(p, "ftpPath"); + if (!devParam.ftpPath.empty() && devParam.ftpPath[0] != '/') + devParam.ftpPath.insert(0, "/"); + devParam.ftpUser = GetJsonStringValue(p, "ftpUser"); + devParam.ftpPassword = GetJsonStringValue(p, "ftpPassword"); + devParam.ftpPort = GetJsonIntValue(p, "ftpPort"); + if (0 == devParam.ftpPort) + devParam.ftpPort = atoi(GetJsonStringValue(p, "ftpPort").c_str()); + devParam.ftpMode = GetJsonIntValue(p, "ftpMode"); + } + + cJSON_Delete(json); + } + + GetManager()->SetDevParam(devId, devParam); + + char result[256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"\"}", 200); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + } + } + else if (requestMethod == "POST" && requestURIPath == "/WebScan/majorOfd") + { + if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) + { + std::string query((const char*)param->data, param->size); + HttpPairs uriQueryInfos; + HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); + + std::string majorOfdParam = HttpHead::GetValue(uriQueryInfos, "formDataString"); + + std::string devId; + bool isAuto = false; + cJSON* json = cJSON_Parse(majorOfdParam.c_str()); + if (NULL != json) + { + devId = GetJsonStringValue(json, "pid"); + isAuto = GetJsonBoolValue(json, "isAuto"); + cJSON_Delete(json); + } + + std::string imgBase64; + GetManager()->ExportOfd(devId, isAuto, imgBase64); + + char* result = new char[imgBase64.size() + 256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgBase64.c_str()); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + delete[] result; + } + } + else if (requestMethod == "GET" && requestURIPath == "/WebScan/majorOfdFile") + { + std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); + bool isAuto = ("true" == HttpHead::GetValue(requestURIQueryInfos, "isAuto") ? true : false); + + HGByte* data = NULL; + HGUInt size = 0; + GetManager()->ExportOfdFile(devId, isAuto, &data, &size); + SendResponse(requestHttpVersion.c_str(), 200, "OK", data, size, "application/zip"); + delete[] data; + } + else if (requestMethod == "POST" && requestURIPath == "/WebScan/majorPdf") + { + if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) + { + std::string query((const char*)param->data, param->size); + HttpPairs uriQueryInfos; + HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); + + std::string majorOfdParam = HttpHead::GetValue(uriQueryInfos, "formDataString"); + + std::string devId; + cJSON* json = cJSON_Parse(majorOfdParam.c_str()); + if (NULL != json) + { + devId = GetJsonStringValue(json, "pid"); + cJSON_Delete(json); + } + + std::string imgBase64; + GetManager()->ExportPdf(devId, imgBase64); + + char* result = new char[imgBase64.size() + 256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgBase64.c_str()); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + delete[] result; + } + } + else if (requestMethod == "GET" && requestURIPath == "/WebScan/majorPdfFile") + { + std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); + + HGByte* data = NULL; + HGUInt size = 0; + GetManager()->ExportPdfFile(devId, &data, &size); + SendResponse(requestHttpVersion.c_str(), 200, "OK", data, size, "application/pdf"); + delete[] data; + } + else if (requestMethod == "POST" && requestURIPath == "/WebScan/majorTiff") + { + if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) + { + std::string query((const char*)param->data, param->size); + HttpPairs uriQueryInfos; + HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); + + std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); + std::string imgBase64; + GetManager()->ExportTiff(devId, imgBase64); + + char* result = new char[imgBase64.size() + 256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgBase64.c_str()); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + delete[] result; + } + } + else if (requestMethod == "GET" && requestURIPath == "/WebScan/majorTiffFile") + { + std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); + + HGByte* data = NULL; + HGUInt size = 0; + GetManager()->ExportTiffFile(devId, &data, &size); + SendResponse(requestHttpVersion.c_str(), 200, "OK", data, size, "application/x-tif"); + delete[] data; + } + else if (requestMethod == "GET" && requestURIPath == "/WebScan/downLoadZip") + { + if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) + { + std::string query((const char*)param->data, param->size); + HttpPairs uriQueryInfos; + HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); + + std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); + std::string imgBase64; + GetManager()->ExportZip(devId, imgBase64); + + char* result = new char[imgBase64.size() + 256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgBase64.c_str()); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + delete[] result; + } + } + else if (requestMethod == "GET" && requestURIPath == "/WebScan/downLoadZipFile") + { + std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); + + HGByte* data = NULL; + HGUInt size = 0; + GetManager()->ExportZipFile(devId, &data, &size); + SendResponse(requestHttpVersion.c_str(), 200, "OK", data, size, "application/zip"); + delete[] data; + } + else if (requestMethod == "POST" && requestURIPath == "/WebScan/uploadImage") + { + if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) + { + std::string query((const char*)param->data, param->size); + HttpPairs uriQueryInfos; + HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); + + std::string uploadParamStr = HttpHead::GetValue(uriQueryInfos, "formDataString"); + UploadParam uploadParam; + + cJSON* json = cJSON_Parse(uploadParamStr.c_str()); + if (NULL != json) + { + uploadParam.uploadMode = GetJsonIntValue(json, "uploadMode"); + uploadParam.httpUrl = GetJsonStringValue(json, "httpUrl"); + uploadParam.fileName = GetJsonStringValue(json, "fileName"); + uploadParam.httpMethod = GetJsonStringValue(json, "httpMethod"); + uploadParam.header = GetJsonStringValue(json, "header"); + uploadParam.param = GetJsonStringValue(json, "param"); + uploadParam.ftpUrl = GetJsonStringValue(json, "ftpUrl"); + uploadParam.ftpPath = GetJsonStringValue(json, "ftpPath"); + if (!uploadParam.ftpPath.empty() && uploadParam.ftpPath[0] != '/') + uploadParam.ftpPath.insert(0, "/"); + uploadParam.ftpUser = GetJsonStringValue(json, "ftpUser"); + uploadParam.ftpPassword = GetJsonStringValue(json, "ftpPassword"); + uploadParam.ftpPort = GetJsonIntValue(json, "ftpPort"); + if (0 == uploadParam.ftpPort) + uploadParam.ftpPort = atoi(GetJsonStringValue(json, "ftpPort").c_str()); + uploadParam.ftpMode = GetJsonIntValue(json, "ftpMode"); + uploadParam.format = GetJsonIntValue(json, "format"); + + cJSON_Delete(json); + } + + bool ret = GetManager()->UploadImage(uploadParam); + + char result[256]; + if (ret) + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success"); + else + sprintf(result, "{\"code\":%d, \"msg\":\"%s\", \"data\":\"%s\"}", 201, "uploadImage failed", "fail"); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + } + } + else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/saveImage") + { + if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) + { + std::string query((const char*)param->data, param->size); + HttpPairs uriQueryInfos; + HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); + + std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); + std::string imgName = HttpHead::GetValue(uriQueryInfos, "imageName"); + std::string imgBase64 = HttpHead::GetValue(uriQueryInfos, "image"); + GetManager()->SaveImage(devId, imgName, imgBase64); + + char result[256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success"); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + } + } + else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/deleteImage") + { + if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) + { + std::string query((const char*)param->data, param->size); + HttpPairs uriQueryInfos; + HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); + + std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); + std::string imgName = HttpHead::GetValue(uriQueryInfos, "imageName"); + GetManager()->DeleteImage(devId, imgName); + + char result[256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success"); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + } + } + else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/deleteAllImage") + { + if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) + { + std::string query((const char*)param->data, param->size); + HttpPairs uriQueryInfos; + HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); + + std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); + GetManager()->DeleteAllImage(devId); + + char result[256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success"); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + } + } + else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/mergeHorizontal") + { + if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) + { + std::string query((const char*)param->data, param->size); + HttpPairs uriQueryInfos; + HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); + + std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); + bool isHorizontal = ("false" != HttpHead::GetValue(uriQueryInfos, "isHorizontal")) ? true : false; + + std::vector imgIndexs; + for (int i = 0; i < (int)uriQueryInfos.size(); ++i) + { + if ("indexs[]" == uriQueryInfos[i].first) + { + imgIndexs.push_back(atoi(uriQueryInfos[i].second.c_str())); + } + } + std::string imgName, imgBase64; + GetManager()->MergeImage(devId, isHorizontal, imgIndexs, imgName, imgBase64); + + char* result = new char[256 + imgName.size() + imgBase64.size()]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":{\"imageName\":\"%s\", \"src\":\"%s\"}}", + 200, imgName.c_str(), imgBase64.c_str()); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + delete[] result; + } + } + else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/bookSort") + { + if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) + { + std::string query((const char*)param->data, param->size); + HttpPairs uriQueryInfos; + HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); + + std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); + std::vector imgNameList, imgBase64List; + GetManager()->BookSort(devId, imgNameList, imgBase64List); + + std::string imgInfos = "["; + for (int i = 0; i < (int)imgNameList.size(); ++i) + { + imgInfos += "{\"imageName\":\""; + imgInfos += imgNameList[i]; + imgInfos += "\",\"src\":\""; + imgInfos += imgBase64List[i]; + imgInfos += "\"}"; + + if (i != imgNameList.size() - 1) + imgInfos += ","; + } + imgInfos += "]"; + + char* result = new char[imgInfos.size() + 256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":%s}", 200, imgInfos.c_str()); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + delete[] result; + } + } + else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/exchangeImage") + { + if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) + { + std::string query((const char*)param->data, param->size); + HttpPairs uriQueryInfos; + HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); + + std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); + int index1 = -1, index2 = -1; + for (int i = 0; i < (int)uriQueryInfos.size(); ++i) + { + if ("indexs[]" == uriQueryInfos[i].first) + { + if (-1 == index1) + index1 = atoi(uriQueryInfos[i].second.c_str()); + else if (-1 == index2) + index2 = atoi(uriQueryInfos[i].second.c_str()); + } + } + GetManager()->ExchangeImage(devId, index1, index2); + + char result[256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success"); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + } + } + else if (requestMethod == "GET" && requestURIPath == "/WebScan/getLastBatch") + { + std::string devId; + GetManager()->GetLastBetch(devId); + + char* result = new char[devId.size() + 256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, devId.c_str()); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + delete[] result; + } + else if (requestMethod == "GET" && requestURIPath == "/WebScan/getSerialNumber") + { + std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); + std::string devSerialNo; + GetManager()->GetDevSerialNo(devId, devSerialNo); + + char* result = new char[devSerialNo.size() + 256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, devSerialNo.c_str()); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + delete[] result; + } + else if (requestMethod == "GET" && requestURIPath == "/WebScan/getImageByName") + { + std::string devId = HttpHead::GetValue(requestURIQueryInfos, "pid"); + std::string imgName = HttpHead::GetValue(requestURIQueryInfos, "imageName"); + std::string imgBase64; + GetManager()->GetImageBase64(devId, imgName, imgBase64); + + char* result = new char[imgBase64.size() + 256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, imgBase64.c_str()); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + delete[] result; + } + else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/resetPatchIndex") + { + if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) + { + std::string query((const char*)param->data, param->size); + HttpPairs uriQueryInfos; + HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); + } + + GetManager()->ResetPatchIndex(); + + char result[256]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":\"%s\"}", 200, "success"); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + } + else if (requestMethod == "POST" && requestURIPath == "/WebScan/image/split") + { + if (std::string::npos != param->head.GetContentType().find("application/x-www-form-urlencoded")) + { + std::string query((const char*)param->data, param->size); + HttpPairs uriQueryInfos; + HttpHead::AnalysisURIQuery(query.c_str(), uriQueryInfos); + + std::string devId = HttpHead::GetValue(uriQueryInfos, "pid"); + std::string imgName = HttpHead::GetValue(uriQueryInfos, "imageName"); + bool isHorizontal = ("false" != HttpHead::GetValue(uriQueryInfos, "isHorizontal")) ? true : false; + int x1 = atoi(HttpHead::GetValue(uriQueryInfos, "x1").c_str()); + int x2 = atoi(HttpHead::GetValue(uriQueryInfos, "x2").c_str()); + int y1 = atoi(HttpHead::GetValue(uriQueryInfos, "y1").c_str()); + int y2 = atoi(HttpHead::GetValue(uriQueryInfos, "y2").c_str()); + std::string imgName1, imgBase64_1, imgName2, imgBase64_2; + GetManager()->SplitImage(devId, imgName, isHorizontal, x1, y1, x2, y2, + imgName1, imgBase64_1, imgName2, imgBase64_2); + + char* result = new char[256 + imgName1.size() + imgBase64_1.size() + imgName2.size() + imgBase64_2.size()]; + sprintf(result, "{\"code\":%d, \"msg\":null, \"data\":{\"oneSrc\":\"%s\", \"oneName\":\"%s\", \"twoSrc\":\"%s\", \"twoName\":\"%s\"}}", + 200, imgBase64_1.c_str(), imgName1.c_str(), imgBase64_2.c_str(), imgName2.c_str()); + SendResponse(requestHttpVersion.c_str(), 200, "OK", + (const HGByte*)result, (HGUInt)strlen(result), "application/json"); + delete[] result; + } + } + } + + void HttpUser::PostCmdMsg(const HttpHead& headInfo, const HGByte* data, HGUInt dataSize) + { + HttpCmdParam* param = new HttpCmdParam; + param->svr = (HttpServer*)m_server; + param->usrId = m_id; + param->head = headInfo; + param->data = NULL; + param->size = 0; + if (0 != dataSize) + { + param->data = new HGByte[dataSize]; + param->size = dataSize; + memcpy(param->data, data, dataSize); + } + + HGMsg msg; + msg.id = MSGID_HTTP_COMMAND; + msg.data = param; + if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_server->GetMsgPump(), &msg)) + { + delete[] param->data; + param->size = 0; + delete param; + } + } + + void HttpUser::ThreadFunc() + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "HttpUser::ThreadFunc"); + + char chBuffer[2048]; + const char* pBuffer = chBuffer; + int nBufferSize = 0; + + unsigned char headDataTail[4] = { '\r', '\n', '\r', '\n' }; + unsigned int headDataTailLen = 0; + std::string headData; + + HttpHead headInfo; + uint8_t* data = NULL; + int dataSize = 0; + bool getData = false; + int getDataSize = 0; + + while (1) + { + if (0 == nBufferSize) + { + int len = recv(m_sockConn, chBuffer, 2048, 0); + if (len <= 0) + { + // 这里跳出,可能是服务器关闭了socketConn,或者客户端关闭了socket,或者网络断开 + PostDisConnectMsg(); + break; + } + else + { + pBuffer = chBuffer; + nBufferSize = len; + } + } + + assert(nBufferSize > 0); + + unsigned char b = *pBuffer; + ++pBuffer; + --nBufferSize; + + if (!getData) + { + headData.push_back(b); + if (b == headDataTail[headDataTailLen]) + { + ++headDataTailLen; + } + else + { + headDataTailLen = 0; + if (b == headDataTail[headDataTailLen]) + { + ++headDataTailLen; + } + } + + if (4 == headDataTailLen) + { + headDataTailLen = 0; + if (!headInfo.Parse(headData.c_str())) + { + PostDisConnectMsg(); + break; + } + + headData.clear(); + int contentLen = headInfo.GetContentLength(); + if (contentLen > 0) + { + data = new uint8_t[contentLen]; + dataSize = contentLen; + getData = true; + getDataSize = 0; + } + else + { + PostCmdMsg(headInfo, NULL, 0); + headInfo.Clear(); + } + } + } + else + { + data[getDataSize] = b; + ++getDataSize; + if (getDataSize == dataSize) + { + PostCmdMsg(headInfo, data, dataSize); + + delete[] data; + data = NULL; + dataSize = 0; + getData = false; + getDataSize = 0; + headInfo.Clear(); + } + } + } + + if (NULL != data) + { + delete[] data; + data = NULL; + dataSize = 0; + getData = false; + getDataSize = 0; + } + } + + bool HttpUser::SendResponse(const char* httpVersion, HGUInt errCode, const char* errInfo, + const HGByte* data, HGUInt size, const char* contentType) + { + if (NULL == httpVersion || NULL == errInfo) + { + return false; + } + + char response[256]; + sprintf(response, "%s %u %s\r\n", httpVersion, errCode, errInfo); + + char head[256]; + if (NULL != data) + { + assert(0 != size); + assert(NULL != contentType); + + sprintf(head, "%s: %u\r\n%s: %s\r\n%s: %s\r\n\r\n", + "Content-Length", size, + "Content-Type", contentType, + "Access-Control-Allow-Origin", "null"); + } + else + { + assert(0 == size); + + sprintf(head, "%s: %s\r\n\r\n", + "Access-Control-Allow-Origin", "null"); + } + + HGBase_EnterLock(m_cs); + send(m_sockConn, response, (int)strlen(response), 0); + send(m_sockConn, head, (int)strlen(head), 0); + if (NULL != data && 0 != size) + send(m_sockConn, (const char*)data, (int)size, 0); + HGBase_LeaveLock(m_cs); + return true; + } +} diff --git a/sdk/webservice/HttpUser.h b/sdk/webservice/HttpUser.h new file mode 100644 index 00000000..87527ac8 --- /dev/null +++ b/sdk/webservice/HttpUser.h @@ -0,0 +1,30 @@ +#pragma once + +#include "WebUser.h" +#include "Msg.h" + +namespace ver_1 +{ + class WebServer; + + class HttpUser : public WebUser + { + public: +#if defined(HG_CMP_MSC) + HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn); +#else + HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn); +#endif + virtual ~HttpUser(); + + void HandleCmd(const HttpCmdParam* param); + + protected: + void PostCmdMsg(const HttpHead& headInfo, const HGByte* data, HGUInt dataSize); + virtual void ThreadFunc(); + + private: + bool SendResponse(const char* httpVersion, HGUInt errCode, const char* errInfo, + const HGByte* data, HGUInt size, const char* contentType); + }; +} diff --git a/sdk/webservice/Manager.cpp b/sdk/webservice/Manager.cpp new file mode 100644 index 00000000..d547ec43 --- /dev/null +++ b/sdk/webservice/Manager.cpp @@ -0,0 +1,2082 @@ +#include "Manager.h" +#include "base/HGBuffer.h" +#include "base/HGBase64.h" +#include "base/HGUtility.h" +#include "base/HGInfo.h" +#include "imgfmt/HGJpeg.h" +#include "imgfmt/HGOfd.h" +#include "imgfmt/HGPdf.h" +#include "imgfmt/HGTiff.h" +#include "imgfmt/HGImgFmt.h" +#include "HGString.h" +extern "C" +{ +#include "zip.h" +}; +#include +#include +#include + +namespace ver_1 +{ + DevParam::DevParam() + { + Reset(); + } + + DevParam::~DevParam() + { + + } + + void DevParam::Reset() + { + autofeeder = true; + pixel = 1; + white = false; + discardBlankThre = 5; + single = false; + format = "jpg"; + resolution = 200; + orentation = 0; + paperType = "Auto"; + splitImage = 0; + noiseDetachEnable = true; + noiseDetach = 15; + + uploadMode = 2; + ftpPath = "/images"; + ftpPort = 21; + ftpMode = 2; + } + + void DevParam::Load(const std::string& cfgPath) + { + HGInt intValue; + HGChar strValue[256]; + + HGBase_GetProfileString(cfgPath.c_str(), "devParam", "device", "", strValue, 256); + device = strValue; + HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "autofeeder", 1, &intValue); + autofeeder = (bool)intValue; + HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "pixel", 1, &intValue); + pixel = intValue; + HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "white", 0, &intValue); + white = (bool)intValue; + HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "discardBlankThre", 5, &intValue); + discardBlankThre = intValue; + HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "single", 0, &intValue); + single = (bool)intValue; + HGBase_GetProfileString(cfgPath.c_str(), "devParam", "format", "jpg", strValue, 256); + format = strValue; + HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "resolution", 200, &intValue); + resolution = intValue; + HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "orentation", 0, &intValue); + orentation = intValue; + HGBase_GetProfileString(cfgPath.c_str(), "devParam", "paperType", "Auto", strValue, 256); + paperType = strValue; + HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "splitImage", 0, &intValue); + splitImage = intValue; + HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "noiseDetachEnable”", 1, &intValue); + noiseDetachEnable = (bool)intValue; + HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "noiseDetach", 15, &intValue); + noiseDetach = intValue; + + HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "uploadMode", 2, &intValue); + uploadMode = intValue; + HGBase_GetProfileString(cfgPath.c_str(), "devParam", "httpUrl", "", strValue, 256); + httpUrl = strValue; + HGBase_GetProfileString(cfgPath.c_str(), "devParam", "fileName", "", strValue, 256); + fileName = strValue; + HGBase_GetProfileString(cfgPath.c_str(), "devParam", "httpMethod", "", strValue, 256); + httpMethod = strValue; + HGBase_GetProfileString(cfgPath.c_str(), "devParam", "header", "", strValue, 256); + header = strValue; + HGBase_GetProfileString(cfgPath.c_str(), "devParam", "param", "", strValue, 256); + param = strValue; + HGBase_GetProfileString(cfgPath.c_str(), "devParam", "ftpUrl", "", strValue, 256); + ftpUrl = strValue; + HGBase_GetProfileString(cfgPath.c_str(), "devParam", "ftpPath", "", strValue, 256); + ftpPath = strValue; + HGBase_GetProfileString(cfgPath.c_str(), "devParam", "ftpUser", "", strValue, 256); + ftpUser = strValue; + HGBase_GetProfileString(cfgPath.c_str(), "devParam", "ftpPassword", "", strValue, 256); + ftpPassword = strValue; + HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "ftpPort", 21, &intValue); + ftpPort = intValue; + HGBase_GetProfileInt(cfgPath.c_str(), "devParam", "ftpMode", 2, &intValue); + ftpMode = intValue; + } + + void DevParam::Save(const std::string& cfgPath) + { + HGBase_SetProfileString(cfgPath.c_str(), "devParam", "device", device.c_str()); + HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "autofeeder", autofeeder); + HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "pixel", pixel); + HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "white", white); + HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "discardBlankThre", discardBlankThre); + HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "single", single); + HGBase_SetProfileString(cfgPath.c_str(), "devParam", "format", format.c_str()); + HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "resolution", resolution); + HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "orentation", orentation); + HGBase_SetProfileString(cfgPath.c_str(), "devParam", "paperType", paperType.c_str()); + HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "splitImage", splitImage); + HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "noiseDetachEnable", noiseDetachEnable); + HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "noiseDetach", noiseDetach); + + HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "uploadMode", uploadMode); + HGBase_SetProfileString(cfgPath.c_str(), "devParam", "httpUrl", httpUrl.c_str()); + HGBase_SetProfileString(cfgPath.c_str(), "devParam", "fileName", fileName.c_str()); + HGBase_SetProfileString(cfgPath.c_str(), "devParam", "httpMethod", httpMethod.c_str()); + HGBase_SetProfileString(cfgPath.c_str(), "devParam", "header", header.c_str()); + HGBase_SetProfileString(cfgPath.c_str(), "devParam", "param", param.c_str()); + HGBase_SetProfileString(cfgPath.c_str(), "devParam", "ftpUrl", ftpUrl.c_str()); + HGBase_SetProfileString(cfgPath.c_str(), "devParam", "ftpPath", ftpPath.c_str()); + HGBase_SetProfileString(cfgPath.c_str(), "devParam", "ftpUser", ftpUser.c_str()); + HGBase_SetProfileString(cfgPath.c_str(), "devParam", "ftpPassword", ftpPassword.c_str()); + HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "ftpPort", ftpPort); + HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "ftpMode", ftpMode); + } + + Manager::Manager(HGMsgPump msgPump) + { + m_msgPump = msgPump; + HGBase_CreateLock(&m_lock); + m_devName.clear(); + m_devHandle = NULL; + m_scanEvent = NULL; + m_scanParam = NULL; + + m_scanInsertImgName.clear(); + m_scanIsInsert = false; + m_scanning = false; + + SANE_Int version_code = 0; + SANE_Status status = sane_init_ex(&version_code, sane_ex_callback, this); + assert(SANE_STATUS_GOOD == status); + } + + Manager::~Manager() + { + if (NULL != m_devHandle) + { + StopScan(); + + m_devParam.Reset(); + sane_close(m_devHandle); + m_devHandle = NULL; + m_devName.clear(); + } + + sane_exit(); + + HGBase_DestroyLock(m_lock); + m_lock = NULL; + } + + void Manager::OpenDev(const OpenDevParam* param) + { + assert(NULL != param && this == param->mgr); + + if (!m_devName.empty() || NULL != m_devHandle) + { + return; + } + + SANE_Handle devHandle = NULL; + SANE_Status status = sane_open(param->devName.c_str(), &devHandle); + if (SANE_STATUS_GOOD == status) + { + assert(NULL != devHandle); + + m_devName = param->devName; + m_devHandle = devHandle; + + std::string filePath = GetFilePath(m_devName); + std::string cfgPath = filePath + "config.ini"; + m_devParam.Load(cfgPath); + SetDevParam(m_devName, m_devParam); + } + } + + void Manager::CloseDev(const CloseDevParam* param) + { + assert(NULL != param && this == param->mgr); + + if (m_devName.empty() || NULL == m_devHandle || m_devName != param->devName) + { + return; + } + + assert(NULL != m_devHandle); + + StopScan(); + + m_devParam.Reset(); + sane_close(m_devHandle); + m_devHandle = NULL; + m_devName.clear(); + } + + void Manager::ScanFinish(const ScanFinishParam* param) + { + assert(NULL != param && this == param->mgr); + + m_scanInsertImgName.clear(); + m_scanIsInsert = false; + m_scanning = false; + } + + void Manager::SetScanEvent(ScanEvent event, void* param) + { + assert(NULL != event && NULL != param); + + HGBase_EnterLock(m_lock); + m_scanEvent = event; + m_scanParam = param; + HGBase_LeaveLock(m_lock); + } + + void Manager::ResetScanEvent() + { + HGBase_EnterLock(m_lock); + m_scanEvent = NULL; + m_scanParam = NULL; + HGBase_LeaveLock(m_lock); + } + + bool Manager::Scan(const std::string& insertImgName, bool isInsert) + { + if (m_scanning) + { + return false; + } + + bool ret = false; + if (NULL != m_devHandle) + { + m_scanInsertImgName = insertImgName; + m_scanIsInsert = isInsert; + if (SANE_STATUS_GOOD == sane_start(m_devHandle)) + { + m_scanning = true; + ret = true; + } + else + { + m_scanInsertImgName.clear(); + m_scanIsInsert = false; + } + } + + return ret; + } + + bool Manager::StopScan() + { + if (m_scanning) + { + assert(NULL != m_devHandle); + sane_cancel(m_devHandle); + } + + return true; + } + + bool Manager::GetCurDevId(std::string& devId) + { + devId.clear(); + if (m_scanning) + { + return false; + } + + devId = m_devName; + return true; + } + + bool Manager::GetDevNames(std::vector& devNameList) + { + devNameList.clear(); + if (m_scanning) + { + return false; + } + + const SANE_Device** device_list; + if (SANE_STATUS_GOOD == sane_get_devices(&device_list, SANE_TRUE)) + { + const SANE_Device** p; + for (p = device_list; *p != NULL; ++p) + { + devNameList.push_back((*p)->name); + } + } + + return true; + } + + bool Manager::GetImageByDevId(const std::string& devId, std::vector& imgNameList, + std::vector& imgBase64List) + { + imgNameList.clear(); + imgBase64List.clear(); + if (m_scanning) + { + return false; + } + + std::string filePath = GetFilePath(devId); + std::vector fileNameList = GetFileNameList(devId); + HGBase_WriteInfo(HGBASE_INFOTYPE_DEBUG, "filePath=%s", filePath.c_str()); + + for (int i = 0; i < (int)fileNameList.size(); ++i) + { + std::string fileName = filePath + fileNameList[i]; + + HGImage img = NULL; + HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); + if (NULL != img) + { + std::string imgBase64 = "data:image/jpeg;base64,"; + imgBase64 += GetBase64(img); + + imgNameList.push_back(fileNameList[i]); + imgBase64List.push_back(imgBase64); + HGBase_DestroyImage(img); + } + } + + SaveFileNameList(devId, imgNameList); + return true; + } + + bool Manager::GetDevParam(const std::string& devId, DevParam& devParam) + { + devParam.Reset(); + if (m_scanning) + { + return false; + } + + bool ret = false; + if (NULL != m_devHandle) + { + m_devParam.device = m_devName; + + // 从设备获取到m_devParam + + SANE_Int num_dev_options = 0; + sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL); + for (int i = 1; i < num_dev_options; ++i) + { + const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i); + if (NULL == desp) + continue; + + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "title=%s", Utf8ToStdString(desp->title).c_str()); + + if (SANE_TYPE_BOOL == desp->type) + { + SANE_Bool value = 0; + sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL); + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=BOOL, value=%s", value ? "TRUE" : "FALSE"); + + if (0 == strcmp(desp->title, OPTION_TITLE_TXCF)) + m_devParam.splitImage = value ? 1 : 0; + else if (0 == strcmp(desp->title, OPTION_TITLE_HBTXZDYH)) + m_devParam.noiseDetachEnable = (bool)value; + } + else if (SANE_TYPE_INT == desp->type) + { + SANE_Int value = 0; + sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL); + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=INT, value=%d", value); + + if (0 == strcmp(desp->title, OPTION_TITLE_TGKBYLMD)) + m_devParam.discardBlankThre = value; + else if (0 == strcmp(desp->title, OPTION_TITLE_FBL)) + m_devParam.resolution = value; + } + else if (SANE_TYPE_FIXED == desp->type) + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=FIXED"); + } + else if (SANE_TYPE_STRING == desp->type) + { + char value[256] = {0}; + sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, value, NULL); + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=STRING, value=%s", Utf8ToStdString(value).c_str()); + + if (0 == strcmp(OPTION_TITLE_SMZS, desp->title) && 0 == strcmp(OPTION_VALUE_SMZS_LXSM, value)) + m_devParam.autofeeder = true; + else if (0 == strcmp(OPTION_TITLE_SMZS, desp->title) && 0 == strcmp(OPTION_VALUE_SMZS_SMZDZS, value)) + m_devParam.autofeeder = false; + else if (0 == strcmp(OPTION_TITLE_YSMS, desp->title) && (0 == strcmp(OPTION_VALUE_YSMS_24WCS, value) + || 0 == strcmp(OPTION_VALUE_YSMS_YSZDSB, value))) + m_devParam.pixel = 2; + else if (0 == strcmp(OPTION_TITLE_YSMS, desp->title) && 0 == strcmp(OPTION_VALUE_YSMS_256JHD, value)) + m_devParam.pixel = 1; + else if (0 == strcmp(OPTION_TITLE_YSMS, desp->title) && 0 == strcmp(OPTION_VALUE_YSMS_HB, value)) + m_devParam.pixel = 0; + else if (0 == strcmp(OPTION_TITLE_SMYM, desp->title) && 0 == strcmp(OPTION_VALUE_SMYM_SM, value)) + { + m_devParam.white = false; + m_devParam.single = false; + } + else if (0 == strcmp(OPTION_TITLE_SMYM, desp->title) && 0 == strcmp(OPTION_VALUE_SMYM_DM, value)) + { + m_devParam.white = false; + m_devParam.single = true; + } + else if (0 == strcmp(OPTION_TITLE_SMYM, desp->title) && (0 == strcmp(OPTION_VALUE_SMYM_TGKBYTY, value) + || 0 == strcmp(OPTION_VALUE_SMYM_TGKBYFPZ, value))) + { + m_devParam.white = true; + m_devParam.single = false; + } + else if (0 == strcmp(OPTION_TITLE_WGFX, desp->title) && 0 == strcmp(OPTION_VALUE_WGFX_0, value)) + m_devParam.orentation = 0; + else if (0 == strcmp(OPTION_TITLE_WGFX, desp->title) && 0 == strcmp(OPTION_VALUE_WGFX_90, value)) + m_devParam.orentation = 90; + else if (0 == strcmp(OPTION_TITLE_WGFX, desp->title) && 0 == strcmp(OPTION_VALUE_WGFX_180, value)) + m_devParam.orentation = 180; + else if (0 == strcmp(OPTION_TITLE_WGFX, desp->title) && 0 == strcmp(OPTION_VALUE_WGFX__90, value)) + m_devParam.orentation = 270; + else if (0 == strcmp(OPTION_TITLE_ZZCC, desp->title) && 0 == strcmp(OPTION_VALUE_ZZCC_PPYSCC, value)) + m_devParam.paperType = "Auto"; + else if (0 == strcmp(OPTION_TITLE_ZZCC, desp->title) && 0 == strcmp(OPTION_VALUE_ZZCC_A3, value)) + m_devParam.paperType = "A3"; + else if (0 == strcmp(OPTION_TITLE_ZZCC, desp->title) && 0 == strcmp(OPTION_VALUE_ZZCC_A4, value)) + m_devParam.paperType = "A4"; + } + else if (SANE_TYPE_BUTTON == desp->type) + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=BUTTON"); + } + else if (SANE_TYPE_GROUP == desp->type) + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, " valueType=GROUP"); + } + } + + std::string filePath = GetFilePath(m_devName); + std::string cfgPath = filePath + "config.ini"; + m_devParam.Save(cfgPath); + ret = true; + } + + devParam = m_devParam; + return ret; + } + + bool Manager::SetDevParam(const std::string& devId, const DevParam& devParam) + { + if (m_scanning) + { + return false; + } + + m_devParam = devParam; + + std::string filePath = GetFilePath(m_devName); + std::string cfgPath = filePath + "config.ini"; + m_devParam.Save(cfgPath); + + bool ret = false; + if (NULL != m_devHandle) + { + // 设置m_devParam到设备 + + SANE_Int num_dev_options = 0; + sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL); + for (int i = 1; i < num_dev_options; ++i) + { + const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i); + if (NULL == desp) + continue; + + if (SANE_TYPE_BOOL == desp->type) + { + if (0 == strcmp(desp->title, OPTION_TITLE_TXCF)) + { + SANE_Bool value = SANE_FALSE; + if (0 != m_devParam.splitImage) + value = SANE_TRUE; + sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); + } + else if (0 == strcmp(desp->title, OPTION_TITLE_HBTXZDYH)) + { + SANE_Bool value = SANE_FALSE; + if (m_devParam.noiseDetachEnable) + value = SANE_TRUE; + sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); + } + } + else if (SANE_TYPE_INT == desp->type) + { + if (0 == strcmp(desp->title, OPTION_TITLE_TGKBYLMD)) + { + SANE_Int value = m_devParam.discardBlankThre; + sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); + } + else if (0 == strcmp(desp->title, OPTION_TITLE_FBL)) + { + SANE_Int value = m_devParam.resolution; + sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); + } + else if (0 == strcmp(desp->title, OPTION_TITLE_SMSL)) + { + SANE_Int value = 1; + sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, &value, NULL); + } + } + else if (SANE_TYPE_FIXED == desp->type) + { + + } + else if (SANE_TYPE_STRING == desp->type) + { + if (0 == strcmp(OPTION_TITLE_SMZS, desp->title)) + { + char value[256] = { 0 }; + strcpy(value, OPTION_VALUE_SMZS_LXSM); + if (!m_devParam.autofeeder) + strcpy(value, OPTION_VALUE_SMZS_SMZDZS); + sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)value, NULL); + } + else if (0 == strcmp(OPTION_TITLE_YSMS, desp->title)) + { + char value[256] = { 0 }; + strcpy(value, OPTION_VALUE_YSMS_24WCS); + if (1 == m_devParam.pixel) + strcpy(value, OPTION_VALUE_YSMS_256JHD); + else if (0 == m_devParam.pixel) + strcpy(value, OPTION_VALUE_YSMS_HB); + sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void *)value, NULL); + } + else if (0 == strcmp(OPTION_TITLE_SMYM, desp->title)) + { + char value[256] = { 0 }; + strcpy(value, OPTION_VALUE_SMYM_SM); + if (m_devParam.single) + strcpy(value, OPTION_VALUE_SMYM_DM); + else if (m_devParam.white) + strcpy(value, OPTION_VALUE_SMYM_TGKBYTY); + sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)value, NULL); + } + else if (0 == strcmp(OPTION_TITLE_WGFX, desp->title)) + { + char value[256] = { 0 }; + strcpy(value, OPTION_VALUE_WGFX_0); + if (90 == m_devParam.orentation) + strcpy(value, OPTION_VALUE_WGFX_90); + else if (180 == m_devParam.orentation) + strcpy(value, OPTION_VALUE_WGFX_180); + else if (270 == m_devParam.orentation) + strcpy(value, OPTION_VALUE_WGFX__90); + sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)value, NULL); + } + else if (0 == strcmp(OPTION_TITLE_ZZCC, desp->title)) + { + char value[256] = { 0 }; + strcpy(value, OPTION_VALUE_ZZCC_PPYSCC); + if ("A3" == m_devParam.paperType) + strcpy(value, OPTION_VALUE_ZZCC_A3); + else if ("A4" == m_devParam.paperType) + strcpy(value, OPTION_VALUE_ZZCC_A4); + sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, (void*)value, NULL); + } + } + else if (SANE_TYPE_BUTTON == desp->type) + { + + } + else if (SANE_TYPE_GROUP == desp->type) + { + + } + } + + ret = true; + } + + return ret; + } + + bool Manager::ExportOfd(const std::string& devId, bool isAuto, std::string& imgBase64) + { + imgBase64.clear(); + if (m_scanning) + { + return false; + } + + HGByte* data = NULL; + HGUInt size = 0; + ExportOfdFile(devId, isAuto, &data, &size); + if (NULL != data) + { + imgBase64 = GetBase64(data, size); + delete[] data; + } + + return !imgBase64.empty(); + } + + bool Manager::ExportOfdFile(const std::string& devId, bool isAuto, const std::string& fileName) + { + std::string filePath = GetFilePath(devId); + std::vector fileNameList = GetFileNameList(devId); + if (fileNameList.empty()) + { + return false; + } + + HGOfdImageWriter writer = NULL; + HGImgFmt_OpenOfdImageWriter(fileName.c_str(), &writer); + if (NULL == writer) + { + return false; + } + + bool ret = false; + for (int i = 0; i < (int)fileNameList.size(); ++i) + { + HGImage img = NULL; + std::string fileName = filePath + fileNameList[i]; + HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); + if (NULL != img) + { + if (HGBASE_ERR_OK == HGImgFmt_SaveJpegImageToOfdImageWriter(writer, img, NULL)) + ret = true; + HGBase_DestroyImage(img); + } + } + + HGImgFmt_CloseOfdImageWriter(writer); + + if (!ret) + { + HGBase_DeleteFile(fileName.c_str()); + return false; + } + + return true; + } + + bool Manager::ExportOfdFile(const std::string& devId, bool isAuto, HGByte** data, HGUInt* size) + { + *data = NULL; + *size = 0; + if (m_scanning) + { + return false; + } + + HGChar tmpFileName[512]; + HGBase_GetTmpFileName(tmpFileName, 512); + ExportOfdFile(devId, isAuto, tmpFileName); + + *data = GetBuffer(tmpFileName, size); + HGBase_DeleteFile(tmpFileName); + return (NULL != *data); + } + + bool Manager::ExportPdf(const std::string& devId, std::string& imgBase64) + { + imgBase64.clear(); + if (m_scanning) + { + return false; + } + + HGByte* data = NULL; + HGUInt size = 0; + ExportPdfFile(devId, &data, &size); + if (NULL != data) + { + imgBase64 = GetBase64(data, size); + delete[] data; + } + + return !imgBase64.empty(); + } + + bool Manager::ExportPdfFile(const std::string& devId, const std::string& fileName) + { + std::string filePath = GetFilePath(devId); + std::vector fileNameList = GetFileNameList(devId); + if (fileNameList.empty()) + { + return false; + } + + HGPdfImageWriter writer = NULL; + HGImgFmt_OpenPdfImageWriter(fileName.c_str(), &writer); + if (NULL == writer) + { + return false; + } + + bool ret = false; + for (int i = 0; i < (int)fileNameList.size(); ++i) + { + HGImage img = NULL; + std::string fileName = filePath + fileNameList[i]; + HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); + if (NULL != img) + { + if (HGBASE_ERR_OK == HGImgFmt_SaveJpegImageToPdfImageWriter(writer, img, NULL)) + ret = true; + HGBase_DestroyImage(img); + } + } + + HGImgFmt_ClosePdfImageWriter(writer); + + if (!ret) + { + HGBase_DeleteFile(fileName.c_str()); + return false; + } + + return true; + } + + bool Manager::ExportPdfFile(const std::string& devId, HGByte** data, HGUInt* size) + { + *data = NULL; + *size = 0; + if (m_scanning) + { + return false; + } + + HGChar tmpFileName[512]; + HGBase_GetTmpFileName(tmpFileName, 512); + ExportPdfFile(devId, tmpFileName); + + *data = GetBuffer(tmpFileName, size); + HGBase_DeleteFile(tmpFileName); + return (NULL != *data); + } + + bool Manager::ExportTiff(const std::string& devId, std::string& imgBase64) + { + imgBase64.clear(); + if (m_scanning) + { + return false; + } + + HGByte* data = NULL; + HGUInt size = 0; + ExportTiffFile(devId, &data, &size); + if (NULL != data) + { + imgBase64 = GetBase64(data, size); + delete[] data; + } + + return !imgBase64.empty(); + } + + bool Manager::ExportTiff(const std::string& devId, const std::string& fileName) + { + std::string filePath = GetFilePath(devId); + std::vector fileNameList = GetFileNameList(devId); + if (fileNameList.empty()) + { + return false; + } + + HGTiffWriter writer = NULL; + HGImgFmt_OpenTiffWriter(fileName.c_str(), &writer); + if (NULL == writer) + { + return false; + } + + bool ret = false; + for (int i = 0; i < (int)fileNameList.size(); ++i) + { + HGImage img = NULL; + std::string fileName = filePath + fileNameList[i]; + HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); + if (NULL != img) + { + if (HGBASE_ERR_OK == HGImgFmt_SaveImageToTiffWriter(writer, img, NULL)) + ret = true; + HGBase_DestroyImage(img); + } + } + + HGImgFmt_CloseTiffWriter(writer); + + if (!ret) + { + HGBase_DeleteFile(fileName.c_str()); + return false; + } + + return true; + } + + bool Manager::ExportTiffFile(const std::string& devId, HGByte** data, HGUInt* size) + { + *data = NULL; + *size = 0; + if (m_scanning) + { + return false; + } + + HGChar tmpFileName[512]; + HGBase_GetTmpFileName(tmpFileName, 512); + ExportTiff(devId, tmpFileName); + + *data = GetBuffer(tmpFileName, size); + HGBase_DeleteFile(tmpFileName); + return (NULL != *data); + } + + bool Manager::ExportZip(const std::string& devId, std::string& imgBase64) + { + imgBase64.clear(); + if (m_scanning) + { + return false; + } + + HGByte* data = NULL; + HGUInt size = 0; + ExportZipFile(devId, &data, &size); + if (NULL != data) + { + imgBase64 = GetBase64(data, size); + delete[] data; + } + + return !imgBase64.empty(); + } + + bool Manager::ExportZipFile(const std::string& devId, const std::string& fileName) + { + std::string filePath = GetFilePath(devId); + std::vector fileNameList = GetFileNameList(devId); + if (fileNameList.empty()) + { + return false; + } + + int error = 0; + zip* z = zip_open(StdStringToUtf8(fileName.c_str()).c_str(), ZIP_CREATE | ZIP_TRUNCATE, &error); + if (NULL == z) + { + return false; + } + + bool ret = false; + for (int i = 0; i < (int)fileNameList.size(); ++i) + { + std::string fileName = filePath + fileNameList[i]; + zip_source_t* s = zip_source_file(z, StdStringToUtf8(fileName.c_str()).c_str(), 0, 0); + if (NULL != s) + { + if (zip_file_add(z, StdStringToUtf8(fileNameList[i].c_str()).c_str(), s, ZIP_FL_OVERWRITE) >= 0) + { + ret = true; + } + else + { + zip_source_free(s); + } + } + } + + zip_close(z); + z = NULL; + + if (!ret) + { + HGBase_DeleteFile(fileName.c_str()); + return false; + } + + return true; + } + + bool Manager::ExportZipFile(const std::string& devId, HGByte** data, HGUInt* size) + { + *data = NULL; + *size = 0; + if (m_scanning) + { + return false; + } + + HGChar tmpFileName[512]; + HGBase_GetTmpFileName(tmpFileName, 512); + ExportZipFile(devId, tmpFileName); + + *data = GetBuffer(tmpFileName, size); + HGBase_DeleteFile(tmpFileName); + return (NULL != *data); + } + + bool Manager::UploadImage(const UploadParam& uploadParam) + { + if (m_scanning) + { + return false; + } + + std::string devId; + GetCurDevId(devId); + + HGChar tmpFileName[512]; + HGBase_GetTmpFileName(tmpFileName, 512); + if (0 == uploadParam.format) + { + strcat(tmpFileName, ".ofd"); + ExportOfdFile(devId, true, tmpFileName); + } + else if (1 == uploadParam.format) + { + strcat(tmpFileName, ".pdf"); + ExportPdfFile(devId, tmpFileName); + } + else + { + strcat(tmpFileName, ".zip"); + ExportZipFile(devId, tmpFileName); + } + + bool ret = false; + if (0 == uploadParam.uploadMode) // HTTP + { + ret = HTTPUpload(tmpFileName, uploadParam.httpUrl, uploadParam.fileName, uploadParam.httpMethod, + uploadParam.header, uploadParam.param); + } + else if (1 == uploadParam.uploadMode) // FTP + { + ret = FTPUpload(tmpFileName, uploadParam.ftpUrl, uploadParam.ftpPort, uploadParam.ftpPath, + uploadParam.ftpUser, uploadParam.ftpPassword, uploadParam.ftpMode); + } + + HGBase_DeleteFile(tmpFileName); + return ret; + } + + bool Manager::SaveImage(const std::string& devId, const std::string& imgName, const std::string& imgBase64) + { + if (m_scanning) + { + return false; + } + + std::string filePath = GetFilePath(devId); + std::string fileName = filePath + imgName; + + size_t pos = imgBase64.find(","); + if (std::string::npos != pos) + return SaveBase64(fileName, imgBase64.c_str() + pos + 1); + else + return SaveBase64(fileName, imgBase64.c_str()); + } + + bool Manager::DeleteImage(const std::string& devId, const std::string& imgName) + { + if (m_scanning) + { + return false; + } + + std::string filePath = GetFilePath(devId); + std::string fileName = filePath + imgName; + + bool ret = false; + if (HGBASE_ERR_OK == HGBase_DeleteFile(fileName.c_str())) + { + std::vector fileNameList = GetFileNameList(devId); + for (int i = 0; i < (int)fileNameList.size(); ++i) + { + if (imgName == fileNameList[i]) + { + fileNameList.erase(fileNameList.begin() + i); + break; + } + } + + SaveFileNameList(devId, fileNameList); + ret = true; + } + + return ret; + } + + bool Manager::DeleteAllImage(const std::string& devId) + { + if (m_scanning) + { + return false; + } + + std::string filePath = GetFilePath(devId); + std::vector fileNameList = GetFileNameList(devId); + + for (int i = 0; i < (int)fileNameList.size(); ++i) + { + std::string fileName = filePath + fileNameList[i]; + HGBase_DeleteFile(fileName.c_str()); + } + fileNameList.clear(); + + SaveFileNameList(devId, fileNameList); + return true; + } + + bool Manager::MergeImage(const std::string& devId, bool isHorizontal, const std::vector& imgIndexList, + std::string& imgName, std::string& imgBase64) + { + imgName.clear(); + imgBase64.clear(); + if (m_scanning) + { + return false; + } + + std::string filePath = GetFilePath(devId); + std::vector fileNameList = GetFileNameList(devId); + + std::vector imgList; + for (int i = 0; i < (int)imgIndexList.size(); ++i) + { + int index = imgIndexList[i]; + if (index >= 0 && index < (int)fileNameList.size()) + { + std::string fileName = filePath + fileNameList[index]; + HGImage img = NULL; + HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); + if (NULL != img) + imgList.push_back(img); + } + } + + HGUInt width = 0, height = 0; + for (int i = 0; i < (int)imgList.size(); ++i) + { + HGImageInfo imgInfo; + HGBase_GetImageInfo(imgList[i], &imgInfo); + if (isHorizontal) + { + width += imgInfo.width; + if (imgInfo.height > height) + height = imgInfo.height; + } + else + { + height += imgInfo.height; + if (imgInfo.width > width) + width = imgInfo.width; + } + } + + if (width > 0 && height > 0) + { + HGImage img = NULL; + HGBase_CreateImage(width, height, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); + if (NULL != img) + { + HGUInt x = 0, y = 0; + for (int i = 0; i < (int)imgList.size(); ++i) + { + HGImageInfo imgInfo; + HGBase_GetImageInfo(imgList[i], &imgInfo); + + HGImageRoi roi = {x, y, x + imgInfo.width, y + imgInfo.height}; + HGBase_SetImageROI(img, &roi); + HGBase_CopyImage(imgList[i], img); + HGBase_ResetImageROI(img); + + if (isHorizontal) + x += imgInfo.width; + else + y += imgInfo.height; + } + + int maxIndex = 0; + for (int i = 0; i < (int)fileNameList.size(); ++i) + { + int index = atoi(fileNameList[i].c_str()); + if (index > maxIndex) + maxIndex = index; + } + + HGChar name[256]; + sprintf(name, "%d.%s", maxIndex + 1, m_devParam.format.c_str()); + imgName = name; + + HGBase_CreateDir(filePath.c_str()); + std::string fileName = filePath + imgName; + HGImgFmt_SaveImage(img, 0, NULL, fileName.c_str()); + + fileNameList.push_back(imgName); + SaveFileNameList(devId, fileNameList); + + imgBase64 = "data:image/jpeg;base64,"; + imgBase64 += GetBase64(img); + + HGBase_DestroyImage(img); + } + } + + for (int i = 0; i < (int)imgList.size(); ++i) + { + HGBase_DestroyImage(imgList[i]); + } + + return true; + } + + static bool LowerSort(const std::string &str1, const std::string& str2) + { + return atoi(str1.c_str()) < atoi(str2.c_str()); + } + + bool Manager::BookSort(const std::string& devId, std::vector& imgNameList, + std::vector& imgBase64List) + { + imgNameList.clear(); + imgBase64List.clear(); + if (m_scanning) + { + return false; + } + + std::vector fileNameList = GetFileNameList(devId); + std::sort(fileNameList.begin(), fileNameList.end(), LowerSort); + SaveFileNameList(devId, fileNameList); + + return GetImageByDevId(devId, imgNameList, imgBase64List); + } + + bool Manager::ExchangeImage(const std::string& devId, int index1, int index2) + { + if (m_scanning) + { + return false; + } + + std::string filePath = GetFilePath(devId); + std::vector fileNameList = GetFileNameList(devId); + if (index1 < 0 || index1 >= (int)fileNameList.size() || index2 < 0 || index2 >= (int)fileNameList.size()) + return false; + + std::string imgName1 = fileNameList[index1]; + std::string imgName2 = fileNameList[index2]; + fileNameList[index1] = imgName2; + fileNameList[index2] = imgName1; + + SaveFileNameList(devId, fileNameList); + return true; + } + + bool Manager::GetLastBetch(std::string& devId) + { + devId.clear(); + if (m_scanning) + { + return false; + } + + devId = m_devName; + return true; + } + + bool Manager::ResetPatchIndex() + { + if (m_scanning) + { + return false; + } + + return true; + } + + bool Manager::SplitImage(const std::string& devId, const std::string& imgName, bool isHorizontal, int x1, int y1, int x2, int y2, + std::string& imgName1, std::string& imgBase64_1, std::string& imgName2, std::string& imgBase64_2) + { + imgName1.clear(); + imgBase64_1.clear(); + imgName2.clear(); + imgBase64_2.clear(); + if (m_scanning) + { + return false; + } + + std::string filePath = GetFilePath(devId); + std::string fileName = filePath + imgName; + + HGImage img = NULL; + HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); + if (NULL != img) + { + HGImageInfo imgInfo; + HGBase_GetImageInfo(img, &imgInfo); + + for (int i = 0; i < 2; ++i) + { + HGUInt newWidth = isHorizontal ? x1 : imgInfo.width; + if (1 == i) + newWidth = isHorizontal ? (imgInfo.width - x1) : imgInfo.width; + + HGUInt newHeight = isHorizontal ? imgInfo.height : y1; + if (1 == i) + newHeight = isHorizontal ? imgInfo.height : (imgInfo.height - y1); + + HGImage newImg = NULL; + HGBase_CreateImage(newWidth, newHeight, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &newImg); + if (NULL != newImg) + { + HGImageRoi roi; + + if (isHorizontal) + { + roi.left = 0; + roi.top = 0; + roi.right = x1; + roi.bottom = imgInfo.height; + + if (1 == i) + { + roi.left = x1; + roi.right = imgInfo.width; + } + } + else + { + roi.left = 0; + roi.top = 0; + roi.right = imgInfo.width; + roi.bottom = y1; + + if (1 == i) + { + roi.top = y1; + roi.bottom = imgInfo.height; + } + } + + if (1 == i) + { + roi.left = x1; + roi.right = imgInfo.width; + } + + HGBase_SetImageROI(img, &roi); + HGBase_CopyImage(img, newImg); + HGBase_ResetImageROI(img); + + std::vector fileNameList = GetFileNameList(devId); + + int maxIndex = 0; + for (int i = 0; i < (int)fileNameList.size(); ++i) + { + int index = atoi(fileNameList[i].c_str()); + if (index > maxIndex) + maxIndex = index; + } + + HGChar name[256]; + sprintf(name, "%d.%s", maxIndex + 1, m_devParam.format.c_str()); + std::string imgName = name; + + HGBase_CreateDir(filePath.c_str()); + std::string fileName = filePath + imgName; + HGImgFmt_SaveImage(newImg, 0, NULL, fileName.c_str()); + + fileNameList.push_back(imgName); + SaveFileNameList(devId, fileNameList); + + std::string imgBase64 = "data:image/jpeg;base64,"; + imgBase64 += GetBase64(newImg); + + if (0 == i) + { + imgName1 = imgName; + imgBase64_1 = imgBase64; + } + else + { + imgName2 = imgName; + imgBase64_2 = imgBase64; + } + + HGBase_DestroyImage(newImg); + } + } + + HGBase_DestroyImage(img); + } + + return true; + } + + bool Manager::GetDevSerialNo(const std::string& devId, std::string& serialNo) + { + serialNo.clear(); + if (m_scanning) + { + return false; + } + + serialNo = devId; + return true; + } + + bool Manager::GetImageBase64(const std::string& devId, const std::string& imgName, std::string& imgBase64) + { + imgBase64.clear(); + if (m_scanning) + { + return false; + } + + std::string filePath = GetFilePath(devId); + std::string fileName = filePath + imgName; + + HGImage img = NULL; + HGImgFmt_LoadImage(fileName.c_str(), 0, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); + if (NULL != img) + { + imgBase64 = "data:image/jpeg;base64,"; + imgBase64 += GetBase64(img); + HGBase_DestroyImage(img); + } + + return true; + } + + std::string Manager::GetFilePath(const std::string& devId) + { + HGChar docsPath[256]; + HGBase_GetDocumentsPath(docsPath, 256); + HGChar imgPath[512]; + sprintf(imgPath, "%s%s/", docsPath, Utf8ToStdString(devId.c_str()).c_str()); + HGChar stdImgPath[512]; + HGBase_StandardiseFileName(imgPath, stdImgPath, 512); + + return stdImgPath; + } + + std::vector Manager::GetFileNameList(const std::string& devId) + { + std::vector fileNameList; + + std::string filePath = GetFilePath(devId); + std::string cfgName = filePath + "images.txt"; + FILE* file = fopen(cfgName.c_str(), "r"); + if (NULL != file) + { + while (feof(file) == 0) + { + char lineContent[256] = { 0 }; + if (NULL == fgets(lineContent, 256, file) || '\n' == *lineContent) + { + continue; + } + + if (lineContent[strlen(lineContent) - 1] == '\n') + lineContent[strlen(lineContent) - 1] = 0; + + fileNameList.push_back(lineContent); + } + + fclose(file); + } + + return fileNameList; + } + + bool Manager::SaveFileNameList(const std::string& devId, const std::vector& fileNameList) + { + bool ret = false; + + std::string filePath = GetFilePath(devId); + std::string cfgName = filePath + "images.txt"; + FILE* file = fopen(cfgName.c_str(), "w"); + if (NULL != file) + { + for (int i = 0; i < (int)fileNameList.size(); ++i) + { + fwrite(fileNameList[i].c_str(), 1, fileNameList[i].size(), file); + fwrite("\n", 1, strlen("\n"), file); + } + + fclose(file); + ret = true; + } + + return ret; + } + + std::string Manager::GetBase64(HGImage image) + { + std::string strBase64; + + if (NULL != image) + { + HGBuffer buffer = NULL; + HGImgFmt_SaveJpegImageToBuffer(image, NULL, &buffer); + if (NULL != buffer) + { + HGByte* data; + HGBase_GetBufferData(buffer, &data); + HGUSize size; + HGBase_GetBufferSize(buffer, &size); + + HGSize base64Size = 0; + HGBase_Base64Encode(data, size, NULL, &base64Size); + uint8_t* base64 = new uint8_t[base64Size + 1]; + HGBase_Base64Encode(data, size, base64, &base64Size); + base64[base64Size] = 0; + strBase64 = (const char*)base64; + delete[] base64; + + HGBase_DestroyBuffer(buffer); + } + } + + return strBase64; + } + + std::string Manager::GetBase64(const HGByte* data, HGUInt size) + { + std::string strBase64; + + if (NULL != data && 0 != size) + { + HGSize base64Size = 0; + HGBase_Base64Encode(data, size, NULL, &base64Size); + uint8_t* base64 = new uint8_t[base64Size + 1]; + HGBase_Base64Encode(data, size, base64, &base64Size); + base64[base64Size] = 0; + strBase64 = (const char*)base64; + delete[] base64; + } + + return strBase64; + } + + std::string Manager::GetBase64(const std::string& fileName) + { + std::string strBase64; + + FILE* file = fopen(fileName.c_str(), "rb"); + if (NULL != file) + { + fseek(file, 0, SEEK_END); + long size = ftell(file); + fseek(file, 0, SEEK_SET); + + if (size > 0) + { + HGByte* data = new HGByte[size]; + long readLen = (long)fread(data, 1, size, file); + if (readLen == size) + { + HGSize base64Size = 0; + HGBase_Base64Encode(data, size, NULL, &base64Size); + uint8_t* base64 = new uint8_t[base64Size + 1]; + HGBase_Base64Encode(data, size, base64, &base64Size); + base64[base64Size] = 0; + strBase64 = (const char*)base64; + delete[] base64; + } + + delete[] data; + } + + fclose(file); + } + + return strBase64; + } + + HGByte* Manager::GetBuffer(const std::string& fileName, HGUInt* size) + { + HGByte* data = NULL; + *size = 0; + + FILE* file = fopen(fileName.c_str(), "rb"); + if (NULL != file) + { + fseek(file, 0, SEEK_END); + *size = ftell(file); + fseek(file, 0, SEEK_SET); + + if (*size > 0) + { + data = new HGByte[*size]; + + HGUInt readLen = (HGUInt)fread(data, 1, *size, file); + if (readLen != *size) + { + delete[] data; + data = NULL; + *size = 0; + } + } + + fclose(file); + } + + return data; + } + + bool Manager::SaveBase64(const std::string& fileName, const char* base64) + { + if (NULL == base64 || 0 == *base64) + return false; + + HGUInt base64Size = (HGUInt)strlen(base64); + + bool ret = false; + FILE* file = fopen(fileName.c_str(), "wb"); + if (NULL != file) + { + HGSize size = 0; + HGBase_Base64Decode((const HGByte*)base64, (HGSize)base64Size, NULL, &size); + uint8_t* data = new HGByte[size]; + HGBase_Base64Decode((const HGByte*)base64, (HGSize)base64Size, data, &size); + + size_t writeLen = fwrite(data, 1, size, file); + if (writeLen == (size_t)size) + ret = true; + + delete[] data; + fclose(file); + } + + return ret; + } + + static std::string MakePreFileData(const char* pszBoundary, const char* pszRemoteFileName) + { + char data[512]; + sprintf(data, "--%s\r\nContent-Disposition: form-data; name=\"filedata\"; filename=\"%s\"\r\n", + pszBoundary, pszRemoteFileName); + std::string ret = data; + ret += "Content-Type: application/octet-stream; charset=utf-8\r\n"; + ret += "Content-Transfer-Encoding: binary\r\n"; + ret += "\r\n"; + return ret; + } + + static std::string MakePostFileData(const char* pszBoundary) + { + char data[512]; + sprintf(data, "\r\n--%s\r\nContent-Disposition: form-data; name=\"submitted\"\r\n\r\nsubmit\r\n--%s--\r\n", pszBoundary, pszBoundary); + return data; + } + + static void ParseHttpURL(const std::string &url, std::string &addr, int &port, std::string &path) + { + addr.clear(); + port = 0; + path.clear(); + + std::string url2; + std::string::size_type pos = url.find("//"); + if (std::string::npos != pos) + { + std::string protocal = url.substr(0, pos); + if (protocal != "http:") + { + return; + } + + url2 = url.substr(pos + 2); + } + else + { + url2 = url; + } + + std::string addr_port; + pos = url2.find("/"); + if (std::string::npos != pos) + { + addr_port = url2.substr(0, pos); + path = url2.substr(pos); + } + else + { + addr_port = url2; + } + + pos = addr_port.find(":"); + if (std::string::npos != pos) + { + addr = addr_port.substr(0, pos); + port = atoi(addr_port.substr(pos + 1).c_str()); + } + else + { + addr = addr_port; + port = 80; + } + } + + bool Manager::HTTPUpload(const std::string& localFileName, const std::string& httpUrl, const std::string& remoteFileName, + const std::string& httpMethod, const std::string& header, const std::string& param) + { + unsigned char* fileData = NULL; + long fileSize = 0; + FILE* file = fopen(localFileName.c_str(), "rb"); + if (NULL != file) + { + fseek(file, 0, SEEK_END); + fileSize = ftell(file); + fseek(file, 0, SEEK_SET); + + if (0 != fileSize) + { + fileData = new unsigned char[fileSize]; + fread(fileData, 1, fileSize, file); + } + + fclose(file); + } + + if (NULL == fileData) + { + return false; + } + + std::string addr; + int port; + std::string path; + ParseHttpURL(httpUrl, addr, port, path); + + #if defined(HG_CMP_MSC) + SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0); + assert(INVALID_SOCKET != sockClient); + #else + int sockClient = socket(AF_INET, SOCK_STREAM, 0); + assert(-1 != sockClient); + #endif + + #if defined(HG_CMP_MSC) + u_long ul = 1; // 设为非阻塞 + ioctlsocket(sockClient, FIONBIO, &ul); + #else + ioctl(sockClient, FIONBIO, 1); + #endif + + #if defined(HG_CMP_MSC) + SOCKADDR_IN addrServer = { 0 }; + addrServer.sin_addr.S_un.S_addr = inet_addr(addr.c_str()); + addrServer.sin_family = AF_INET; + addrServer.sin_port = htons(port); + if (0 != connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR_IN))) + #else + struct sockaddr_in addrServer = { 0 }; + addrServer.sin_addr.s_addr = inet_addr(addr.c_str()); + addrServer.sin_family = AF_INET; + addrServer.sin_port = htons(port); + if (0 != connect(sockClient, (struct sockaddr*)&addrServer, sizeof(addrServer))) + #endif + { + fd_set fds; + FD_ZERO(&fds); + FD_SET(sockClient, &fds); + timeval tm; + tm.tv_sec = 1; + tm.tv_usec = 0; + + if (select((int)(sockClient + 1), NULL, &fds, NULL, &tm) <= 0) + { + #if defined(HG_CMP_MSC) + closesocket(sockClient); + #else + close(sockClient); + #endif + delete[] fileData; + return false; + } + + if (!FD_ISSET(sockClient, &fds)) + { + #if defined(HG_CMP_MSC) + closesocket(sockClient); + #else + close(sockClient); + #endif + delete[] fileData; + return false; + } + } + + #if defined(HG_CMP_MSC) + ul = 0; // 设为阻塞 + ioctlsocket(sockClient, FIONBIO, &ul); + #else + ioctl(sockClient, FIONBIO, 0); + #endif + + char remoteName[256]; + HGBase_GetFileName(localFileName.c_str(), remoteName, 256); + + bool ret = false; + + const char* pszBoundary = "---------------------------7d33a816d302b6"; + + std::string strPreFileData = MakePreFileData(pszBoundary, remoteName); + std::string strPostFileData = MakePostFileData(pszBoundary); + + char hostname[128]; + gethostname(hostname, 128); + + std::string head; + char data[512]; + sprintf(data, "POST %s HTTP/1.1\r\nHost: %s\r\n", path.c_str(), hostname); + head += data; + sprintf(data, "Content-Type: multipart/form-data; boundary=%s\r\n", pszBoundary); + head += data; + sprintf(data, "Content-Length: %d\r\n\r\n", (int)strPreFileData.size() + (int)fileSize + (int)strPostFileData.size()); + head += data; + + send(sockClient, head.c_str(), (int)head.size(), 0); + send(sockClient, strPreFileData.c_str(), (int)strPreFileData.size(), 0); + send(sockClient, (const char *)fileData, (int)fileSize, 0); + send(sockClient, strPostFileData.c_str(), (int)strPostFileData.size(), 0); + + char recvBuf[2048] = {0}; + recv(sockClient, recvBuf, 2048, 0); + + std::string strRecv(recvBuf); + std::string::size_type pos = strRecv.find("\r\n"); + if (pos != std::string::npos) + { + std::string head = strRecv.substr(0, pos); + if (head.find("200") != std::string::npos) + { + ret = true; + } + } + + #if defined(HG_CMP_MSC) + closesocket(sockClient); + #else + close(sockClient); + #endif + delete[] fileData; + return ret; + } + + static size_t read_callback(char* ptr, size_t size, size_t nmemb, void* stream) + { + unsigned long nread; + /* in real-world cases, this would probably get this data differently + as this fread() stuff is exactly what the library already would do + by default internally */ + size_t retcode = fread(ptr, size, nmemb, (FILE*)stream); + + if (retcode > 0) + { + nread = (unsigned long)retcode; + //fprintf(stderr, "*** We read %lu bytes from file\n", nread); + } + + return retcode; + } + + bool Manager::FTPUpload(const std::string& localFileName, const std::string& ftpUrl, int ftpPort, const std::string& ftpPath, + const std::string& ftpUser, const std::string& ftpPassword, int ftpMode) + { + FILE* file = fopen(localFileName.c_str(), "rb"); + if (NULL == file) + { + return false; + } + + bool ret = false; + fseek(file, 0, SEEK_END); + long fsize = ftell(file); + fseek(file, 0, SEEK_SET); + + curl_global_init(CURL_GLOBAL_ALL); + + /* get a curl handle */ + CURL *curl = curl_easy_init(); + if (NULL != curl) + { + char tmpName[256]; + HGBase_GetUuid(tmpName, 256); + char remoteName[256]; + HGBase_GetFileName(localFileName.c_str(), remoteName, 256); + + char ftp_rnfr[512]; + sprintf(ftp_rnfr, "RNFR %s", tmpName); + char ftp_rnto[512]; + sprintf(ftp_rnto, "RNTO %s", remoteName); + + struct curl_slist* headerlist = NULL; + headerlist = curl_slist_append(headerlist, ftp_rnfr); + headerlist = curl_slist_append(headerlist, ftp_rnto); + + /* we want to use our own read function */ + curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); + + curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); + + char url[512]; + if (!ftpUser.empty() && !ftpPassword.empty()) + { + sprintf(url, "ftp://%s:%s@%s:%d%s/%s", ftpUser.c_str(), ftpPassword.c_str(), + ftpUrl.c_str(), ftpPort, ftpPath.c_str(), tmpName); + } + else + { + sprintf(url, "ftp://%s:%d%s/%s", ftpUrl.c_str(), ftpPort, ftpPath.c_str(), tmpName); + } + curl_easy_setopt(curl, CURLOPT_URL, url); + + curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1); + + /* pass in that last of FTP commands to run after the transfer */ + curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist); + + /* now specify which file to upload */ + curl_easy_setopt(curl, CURLOPT_READDATA, file); + + /* Set the size of the file to upload (optional). If you give a *_LARGE + option you MUST make sure that the type of the passed-in argument is a + curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must + make sure that to pass in a type 'long' argument. */ + curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fsize); + + /* Now run off and do what you have been told! */ + CURLcode res = curl_easy_perform(curl); + /* Check for errors */ + if (res != CURLE_OK) + fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); + else + ret = true; + + /* clean up the FTP commands list */ + curl_slist_free_all(headerlist); + + /* always cleanup */ + curl_easy_cleanup(curl); + } + fclose(file); /* close the local file */ + + curl_global_cleanup(); + return ret; + } + + int Manager::sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param) + { + (void)hdev; + (void)len; + + Manager* p = (Manager*)param; + switch (code) + { + case SANE_EVENT_DEVICE_ARRIVED: + { + SANE_Device* sane_dev = (SANE_Device*)data; + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "device arrive, name=%s", Utf8ToStdString(sane_dev->name).c_str()); + + HGBase_EnterLock(p->m_lock); + if (NULL != p->m_scanEvent) + p->m_scanEvent(SCANEVENT_ARRIVE, (void*)sane_dev->name, (void*)0, p->m_scanParam); + HGBase_LeaveLock(p->m_lock); + + OpenDevParam* openDevParam = new OpenDevParam; + openDevParam->mgr = p; + openDevParam->devName = sane_dev->name; + + HGMsg msg; + msg.id = MSGID_OPEN_DEVICE; + msg.data = openDevParam; + if (HGBASE_ERR_OK != HGBase_PostPumpMessage(p->m_msgPump, &msg)) + { + delete openDevParam; + } + } + break; + case SANE_EVENT_DEVICE_LEFT: + { + SANE_Device* sane_dev = (SANE_Device*)data; + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "device remove, name=%s", Utf8ToStdString(sane_dev->name).c_str()); + + HGBase_EnterLock(p->m_lock); + if (NULL != p->m_scanEvent) + p->m_scanEvent(SCANEVENT_REMOVE, (void*)sane_dev->name, (void*)0, p->m_scanParam); + HGBase_LeaveLock(p->m_lock); + + CloseDevParam* closeDevParam = new CloseDevParam; + closeDevParam->mgr = p; + closeDevParam->devName = sane_dev->name; + + HGMsg msg; + msg.id = MSGID_CLOSE_DEVICE; + msg.data = closeDevParam; + if (HGBASE_ERR_OK != HGBase_PostPumpMessage(p->m_msgPump, &msg)) + { + delete closeDevParam; + } + } + break; + case SANE_EVENT_STATUS: + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_STATUS, msg=%s", Utf8ToStdString((char *)data).c_str()); + + HGBase_EnterLock(p->m_lock); + if (NULL != p->m_scanEvent) + p->m_scanEvent(SCANEVENT_STATUS, (void*)data, (void*)0, p->m_scanParam); + HGBase_LeaveLock(p->m_lock); + } + break; + case SANE_EVENT_ERROR: + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_ERROR, msg=%s", Utf8ToStdString((char*)data).c_str()); + + HGBase_EnterLock(p->m_lock); + if (NULL != p->m_scanEvent) + p->m_scanEvent(SCANEVENT_ERROR, (void *)data, (void*)0, p->m_scanParam); + HGBase_LeaveLock(p->m_lock); + } + break; + case SANE_EVENT_WORKING: + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_WORKING, msg=%s", Utf8ToStdString((char*)data).c_str()); + + HGBase_EnterLock(p->m_lock); + if (NULL != p->m_scanEvent) + p->m_scanEvent(SCANEVENT_WORKING, (void*)data, (void*)0, p->m_scanParam); + HGBase_LeaveLock(p->m_lock); + } + break; + case SANE_EVENT_IMAGE_OK: + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_IMAGE_OK"); + + SANE_Image* sane_img = (SANE_Image*)data; + + HGUInt imgType = 0; + if (sane_img->header.format == SANE_FRAME_GRAY) + imgType = HGBASE_IMGTYPE_GRAY; + else if (sane_img->header.format == SANE_FRAME_RGB) + imgType = HGBASE_IMGTYPE_RGB; + + HGByte* data = sane_img->data; + HGImageInfo imgInfo = { (HGUInt)sane_img->header.pixels_per_line, (HGUInt)sane_img->header.lines, + imgType, (HGUInt)sane_img->header.bytes_per_line, HGBASE_IMGORIGIN_TOP }; + + HGImage img = NULL; + HGBase_CreateImageFromData(data, &imgInfo, nullptr, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img); + if (NULL != img) + { + std::string filePath = GetFilePath(p->m_devName); + std::vector fileNameList = GetFileNameList(p->m_devName); + + int maxIndex = 0; + for (int i = 0; i < (int)fileNameList.size(); ++i) + { + int index = atoi(fileNameList[i].c_str()); + if (index > maxIndex) + maxIndex = index; + } + + HGChar name[256]; + sprintf(name, "%d.%s", maxIndex + 1, p->m_devParam.format.c_str()); + std::string imgName = name; + + HGBase_CreateDir(filePath.c_str()); + std::string fileName = filePath + imgName; + HGImgFmt_SaveImage(img, 0, NULL, fileName.c_str()); + + if (0 == p->m_devParam.uploadMode) // HTTP + { + HTTPUpload(fileName, p->m_devParam.httpUrl, p->m_devParam.fileName, p->m_devParam.httpMethod, + p->m_devParam.header, p->m_devParam.param); + } + else if (1 == p->m_devParam.uploadMode) // FTP + { + FTPUpload(fileName, p->m_devParam.ftpUrl, p->m_devParam.ftpPort, p->m_devParam.ftpPath, + p->m_devParam.ftpUser, p->m_devParam.ftpPassword, p->m_devParam.ftpMode); + } + + if (p->m_scanIsInsert) + { + int index = -1; + for (int i = 0; i < (int)fileNameList.size(); ++i) + { + if (fileNameList[i] == p->m_scanInsertImgName) + { + index = i; + break; + } + } + + if (-1 != index) + { + fileNameList.insert(fileNameList.begin() + index, imgName); + } + else + { + fileNameList.push_back(imgName); + } + } + else + { + fileNameList.push_back(imgName); + } + + SaveFileNameList(p->m_devName, fileNameList); + + std::string imgBase64 = "data:image/jpeg;base64,"; + imgBase64 += GetBase64(img); + + HGBase_EnterLock(p->m_lock); + if (NULL != p->m_scanEvent) + p->m_scanEvent(SCANEVENT_IMAGE, (void*)imgName.c_str(), (void *)imgBase64.c_str(), p->m_scanParam); + HGBase_LeaveLock(p->m_lock); + + HGBase_DestroyImage(img); + } + } + break; + case SANE_EVENT_SCAN_FINISHED: + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_SCAN_FINISHED, msg=%s", Utf8ToStdString((char*)data).c_str()); + + HGBase_EnterLock(p->m_lock); + if (NULL != p->m_scanEvent) + p->m_scanEvent(SCANEVENT_FINISH, (void*)data, (void*)0, p->m_scanParam); + HGBase_LeaveLock(p->m_lock); + + ScanFinishParam* scanFinishParam = new ScanFinishParam; + scanFinishParam->mgr = p; + + HGMsg msg; + msg.id = MSGID_SCAN_FINISH; + msg.data = scanFinishParam; + if (HGBASE_ERR_OK != HGBase_PostPumpMessage(p->m_msgPump, &msg)) + { + delete scanFinishParam; + } + } + break; + } + + return 0; + } +} diff --git a/sdk/webservice/Manager.h b/sdk/webservice/Manager.h new file mode 100644 index 00000000..bff26026 --- /dev/null +++ b/sdk/webservice/Manager.h @@ -0,0 +1,194 @@ +#pragma once + +#include "base/HGDef.h" +#include "base/HGInc.h" +#include "base/HGLock.h" +#include "base/HGImage.h" +#include "base/HGMsgPump.h" +#include "sane/sane_ex.h" +#include "sane/sane_option_definitions.h" +#include "Msg.h" +#include + +namespace ver_1 +{ + enum + { + SCANEVENT_ARRIVE = 1L, + SCANEVENT_REMOVE, + SCANEVENT_STATUS, + SCANEVENT_WORKING, + SCANEVENT_IMAGE, + SCANEVENT_FINISH, + SCANEVENT_ERROR + }; + + struct DevParam + { + DevParam(); + ~DevParam(); + + void Reset(); + void Load(const std::string& cfgPath); + void Save(const std::string& cfgPath); + + std::string device; // 设备名称,默认null + bool autofeeder; // 自动进纸,默认true + int pixel; // 扫描模式 0:黑白 1:灰度 2:彩色,默认1 + bool white; // 丢弃空白页,默认false + int discardBlankThre; // 跳过空白页阈值 1­-100 默认值5 + bool single; // 单页扫描,默认false + std::string format; // 输出格式 jpg png bmp tiff pdf ofd,默认jpg + int resolution; // 扫描分辨率 范围 100­-600 默认值200 + int orentation; // 图片旋转 0:原图 90:度 180:旋转180度 270:旋转270度,默认0 + std::string paperType; // 扫描幅面 A3:A3幅面 Auto:自适应幅面 A4:A4幅面,默认Auto + int splitImage; // 图像分割 0:disable 1:垂直分割 2: 水平分割,默认0 + bool noiseDetachEnable; // 去除噪点,默认true + int noiseDetach; // 噪点阈值 范围:10­-50 默认值15 + + int uploadMode; // 是否边扫边上传 0:http 1:ftp 2: 不上传,默认2 + std::string httpUrl; // 上传地址,默认null + std::string fileName; // 接收文件参数名,默认null + std::string httpMethod; // 上传方式 GET POST PUT,默认null + std::string header; // 请求头,默认null + std::string param; // 参数 json格式,默认null + std::string ftpUrl; // ftp 地址,默认null + std::string ftpPath; // 路径,默认/images + std::string ftpUser; // ftp 用户名,默认null + std::string ftpPassword; // ftp 密码,默认null + int ftpPort; // 端口号,默认21 + int ftpMode; // 连接模式 1:主动 2:被动,默认2 + }; + + struct UploadParam + { + UploadParam() + { + uploadMode = 2; + ftpPort = 21; + ftpMode = 2; + format = 2; + } + + int uploadMode; // 上传模式 0:http 1:ftp 默认0 + std::string httpUrl; // 上传地址,默认null + std::string fileName; // 接收文件参数名,默认null + std::string httpMethod; // 上传方式 GET POST PUT,默认null + std::string header; // 请求头,默认null + std::string param; // 参数 json格式,默认null + std::string ftpUrl; // ftp 地址,默认null + std::string ftpPath; // 路径,默认/images + std::string ftpUser; // ftp 用户名,默认null + std::string ftpPassword; // ftp 密码,默认null + int ftpPort; // 端口号,默认21 + int ftpMode; // 连接模式 1:主动 2:被动,默认2 + int format; // 上传格式 0: ofd 1: pdf 2: zip, 默认2 + }; + + typedef void (*ScanEvent)(HGUInt event, void* value1, void* value2, void* param); + + class Manager + { + public: + Manager(HGMsgPump msgPump); + ~Manager(); + + // 打开设备 + void OpenDev(const OpenDevParam* param); + // 关闭设备 + void CloseDev(const CloseDevParam* param); + // 扫描完成 + void ScanFinish(const ScanFinishParam* param); + + // 设置回调 + void SetScanEvent(ScanEvent event, void* param); + // 清理回调 + void ResetScanEvent(); + // 扫描 + bool Scan(const std::string& insertImgName, bool isInsert); + // 停止扫描 + bool StopScan(); + // 获取当前连接的设备ID + bool GetCurDevId(std::string& devId); + // 获取连接的设备名列表 + bool GetDevNames(std::vector& devNameList); + // 获取上次的扫描结果 + bool GetImageByDevId(const std::string& devId, std::vector& imgNameList, + std::vector& imgBase64List); + // 获取配置参数 + bool GetDevParam(const std::string& devId, DevParam& devParam); + // 设置配置参数 + bool SetDevParam(const std::string& devId, const DevParam& devParam); + // 生成OFD + bool ExportOfd(const std::string& devId, bool isAuto, std::string& imgBase64); + bool ExportOfdFile(const std::string& devId, bool isAuto, const std::string& fileName); + bool ExportOfdFile(const std::string& devId, bool isAuto, HGByte** data, HGUInt* size); + // 生成PDF + bool ExportPdf(const std::string& devId, std::string& imgBase64); + bool ExportPdfFile(const std::string& devId, const std::string& fileName); + bool ExportPdfFile(const std::string& devId, HGByte** data, HGUInt* size); + // 生成TIFF + bool ExportTiff(const std::string& devId, std::string& imgBase64); + bool ExportTiff(const std::string& devId, const std::string& fileName); + bool ExportTiffFile(const std::string& devId, HGByte** data, HGUInt* size); + // 生成ZIP + bool ExportZip(const std::string& devId, std::string& imgBase64); + bool ExportZipFile(const std::string& devId, const std::string& fileName); + bool ExportZipFile(const std::string& devId, HGByte** data, HGUInt* size); + // 上传图像 + bool UploadImage(const UploadParam& uploadParam); + // 保存图片 + bool SaveImage(const std::string& devId, const std::string& imgName, const std::string& imgBase64); + // 删除图片 + bool DeleteImage(const std::string& devId, const std::string& imgName); + // 删除所有图片 + bool DeleteAllImage(const std::string& devId); + // 图像合并 + bool MergeImage(const std::string& devId, bool isHorizontal, const std::vector& imgIndexList, + std::string& imgName, std::string& imgBase64); + // 自动排序 + bool BookSort(const std::string& devId, std::vector& imgNameList, + std::vector& imgBase64List); + // 交换文件 + bool ExchangeImage(const std::string& devId, int index1, int index2); + // 获取最后批次 + bool GetLastBetch(std::string& devId); + // 重置索引 + bool ResetPatchIndex(); + // 拆分图像 + bool SplitImage(const std::string& devId, const std::string& imgName, bool isHorizontal, int x1, int y1, int x2, int y2, + std::string& imgName1, std::string& imgBase64_1, std::string& imgName2, std::string& imgBase64_2); + // 获取设备序列号 + bool GetDevSerialNo(const std::string& devId, std::string& serialNo); + // 获取图像Base64 + bool GetImageBase64(const std::string& devId, const std::string& imgName, std::string& imgBase64); + + private: + static std::string GetFilePath(const std::string& devId); + static std::vector GetFileNameList(const std::string& devId); + static bool SaveFileNameList(const std::string& devId, const std::vector& fileNameList); + static std::string GetBase64(HGImage image); + static std::string GetBase64(const HGByte* data, HGUInt size); + static std::string GetBase64(const std::string& fileName); + static HGByte* GetBuffer(const std::string& fileName, HGUInt* size); + static bool SaveBase64(const std::string& fileName, const char* base64); + static bool HTTPUpload(const std::string& localFileName, const std::string& httpUrl, const std::string& remoteFileName, + const std::string& httpMethod, const std::string& header, const std::string& param); + static bool FTPUpload(const std::string& localFileName, const std::string& ftpUrl, int ftpPort, const std::string& ftpPath, + const std::string& ftpUser, const std::string& ftpPassword, int ftpMode); + static int sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param); + + private: + HGMsgPump m_msgPump; + HGLock m_lock; + std::string m_devName; + SANE_Handle m_devHandle; + DevParam m_devParam; + ScanEvent m_scanEvent; + void* m_scanParam; + + std::string m_scanInsertImgName; + bool m_scanIsInsert; + bool m_scanning; + }; +} diff --git a/sdk/webservice/Msg.h b/sdk/webservice/Msg.h new file mode 100644 index 00000000..fdfdbc58 --- /dev/null +++ b/sdk/webservice/Msg.h @@ -0,0 +1,86 @@ +#pragma once + +#include "base/HGDef.h" +#include "base/HGInc.h" +#include "HttpHead.h" +#include + +namespace ver_1 +{ + enum + { + MSGID_OPEN_DEVICE = 1L, + MSGID_CLOSE_DEVICE, + MSGID_SCAN_FINISH, + MSGID_CONNECT, + MSGID_DISCONNECT, + MSGID_HTTP_COMMAND, + MSGID_SOCKIO_COMMAND, + MSGID_SOCKIO_EVENT + }; + + class Manager; + class WebServer; + class HttpServer; + class SockIoServer; + + struct OpenDevParam + { + Manager* mgr; + std::string devName; + }; + + struct CloseDevParam + { + Manager* mgr; + std::string devName; + }; + + struct ScanFinishParam + { + Manager* mgr; + }; + + struct ConnectParam + { + WebServer* svr; + HGChar ip[16]; + HGUShort port; +#if defined(HG_CMP_MSC) + SOCKET socket; +#else + int socket; +#endif + }; + + struct DisConnectParam + { + WebServer* svr; + HGUInt usrId; + }; + + struct HttpCmdParam + { + HttpServer* svr; + HGUInt usrId; + HttpHead head; + HGByte* data; + HGUInt size; + }; + + struct SockIoCmdParam + { + SockIoServer* svr; + HGUInt usrId; + HGByte* data; + HGUInt size; + }; + + struct SockIoEvtParam + { + SockIoServer* svr; + HGUInt usrId; + HGByte* data; + HGUInt size; + }; +} \ No newline at end of file diff --git a/sdk/webservice/MsgPumpCallback.cpp b/sdk/webservice/MsgPumpCallback.cpp new file mode 100644 index 00000000..ac981277 --- /dev/null +++ b/sdk/webservice/MsgPumpCallback.cpp @@ -0,0 +1,76 @@ +#include "MsgPumpCallback.h" +#include "base/HGDef.h" +#include "base/HGInc.h" +#include "Manager.h" +#include "HttpServer.h" +#include "SockIoServer.h" + +namespace ver_1 +{ + void HGMsgPumpCallback(HGMsgPump msgPump, const HGMsg* msg, HGPointer param) + { + (void)msgPump; + (void)param; + assert(NULL != msg); + + switch (msg->id) + { + case MSGID_OPEN_DEVICE: + { + OpenDevParam* param = (OpenDevParam*)msg->data; + param->mgr->OpenDev(param); + delete param; + } + break; + case MSGID_CLOSE_DEVICE: + { + CloseDevParam* param = (CloseDevParam*)msg->data; + param->mgr->CloseDev(param); + delete param; + } + break; + case MSGID_SCAN_FINISH: + { + ScanFinishParam* param = (ScanFinishParam*)msg->data; + param->mgr->ScanFinish(param); + delete param; + } + break; + case MSGID_CONNECT: + { + ConnectParam* param = (ConnectParam*)msg->data; + param->svr->Connect(param); + delete param; + } + break; + case MSGID_DISCONNECT: + { + DisConnectParam* param = (DisConnectParam*)msg->data; + param->svr->DisConnect(param); + delete param; + } + break; + case MSGID_HTTP_COMMAND: + { + HttpCmdParam* param = (HttpCmdParam*)msg->data; + param->svr->Command(param); + delete param; + } + break; + case MSGID_SOCKIO_COMMAND: + { + SockIoCmdParam* param = (SockIoCmdParam*)msg->data; + param->svr->Command(param); + delete param; + } + break; + case MSGID_SOCKIO_EVENT: + { + SockIoEvtParam* param = (SockIoEvtParam*)msg->data; + param->svr->Event(param); + delete param; + } + break; + } + } +} \ No newline at end of file diff --git a/sdk/webservice/MsgPumpCallback.h b/sdk/webservice/MsgPumpCallback.h new file mode 100644 index 00000000..1426f7e3 --- /dev/null +++ b/sdk/webservice/MsgPumpCallback.h @@ -0,0 +1,8 @@ +#pragma once + +#include "base/HGMsgPump.h" + +namespace ver_1 +{ + void HGMsgPumpCallback(HGMsgPump msgPump, const HGMsg* msg, HGPointer param); +} \ No newline at end of file diff --git a/sdk/webservice/SockIoServer.cpp b/sdk/webservice/SockIoServer.cpp new file mode 100644 index 00000000..f2daa9e1 --- /dev/null +++ b/sdk/webservice/SockIoServer.cpp @@ -0,0 +1,49 @@ +#include "SockIoServer.h" +#include "SockIoUser.h" +#include "base/HGInfo.h" + +namespace ver_1 +{ + SockIoServer::SockIoServer(HGMsgPump msgPump, Manager* manager) + : WebServer(msgPump, manager) + { + + } + + SockIoServer::~SockIoServer() + { + + } + + void SockIoServer::Connect(const ConnectParam* param) + { + assert(NULL != param && this == param->svr); + + WebUser* user = new SockIoUser(this, m_currUserId, param->ip, param->port, param->socket); + user->Open(); + ++m_currUserId; + m_vectorUser.push_back(user); + } + + void SockIoServer::Command(const SockIoCmdParam* param) + { + assert(NULL != param && this == param->svr); + + int nIndex = GetUserIndex(param->usrId); + if (-1 != nIndex) + { + ((SockIoUser*)m_vectorUser[nIndex])->HandleCmd(param); + } + } + + void SockIoServer::Event(const SockIoEvtParam* param) + { + assert(NULL != param && this == param->svr); + + int nIndex = GetUserIndex(param->usrId); + if (-1 != nIndex) + { + ((SockIoUser*)m_vectorUser[nIndex])->HandleEvent(param); + } + } +} diff --git a/sdk/webservice/SockIoServer.h b/sdk/webservice/SockIoServer.h new file mode 100644 index 00000000..1e73ff08 --- /dev/null +++ b/sdk/webservice/SockIoServer.h @@ -0,0 +1,20 @@ +#pragma once + +#include "WebServer.h" +#include "Msg.h" + +namespace ver_1 +{ + class Manager; + + class SockIoServer : public WebServer + { + public: + SockIoServer(HGMsgPump msgPump, Manager* manager); + virtual ~SockIoServer(); + + void Connect(const ConnectParam* param); + void Command(const SockIoCmdParam* param); + void Event(const SockIoEvtParam* param); + }; +} \ No newline at end of file diff --git a/sdk/webservice/SockIoUser.cpp b/sdk/webservice/SockIoUser.cpp new file mode 100644 index 00000000..4c76b245 --- /dev/null +++ b/sdk/webservice/SockIoUser.cpp @@ -0,0 +1,612 @@ +#include "SockIoUser.h" +#include "WebServer.h" +#include "Manager.h" +#include "base/HGInfo.h" +#include "base/HGUtility.h" +#include "sha1.h" +#include "base64.h" +#include "cJSON.h" + +namespace ver_1 +{ +#if defined(HG_CMP_MSC) + SockIoUser::SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn) +#else + SockIoUser::SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn) +#endif + : WebUser(server, id, ip, port, sockConn) + { + GetManager()->SetScanEvent(ScanCallback, this); + } + + SockIoUser::~SockIoUser() + { + GetManager()->ResetScanEvent(); + } + + void SockIoUser::HandleCmd(const SockIoCmdParam* param) + { + assert(NULL != param && m_id == param->usrId); + + std::string user; + std::string data; + GetMsgInfo(param, user, data); + if (user.empty()) + { + return; + } + + if ("scan" == user) + { + std::string imgName; + bool insert = false; + + cJSON* json = cJSON_Parse(data.c_str()); + if (NULL != json) + { + if (NULL != json->child && 0 == strcmp("imageName", json->child->string) + && cJSON_String == json->child->type) + { + imgName = json->child->valuestring; + if (NULL != json->child->next && 0 == strcmp("isInsert", json->child->next->string) + && cJSON_True == json->child->next->type) + insert = true; + } + + cJSON_Delete(json); + } + + bool ret = GetManager()->Scan(imgName, insert); + if (!ret) + { + std::string resp = "42[\"error\", \"scan error\"]"; + SendResponse((const HGByte*)resp.c_str(), (HGUInt)resp.size(), HGTRUE); + } + } + else if ("stop" == user) + { + bool ret = GetManager()->StopScan(); + assert(ret); + + std::string resp = "42[\"success\", \"stop scan success!\"]"; + SendResponse((const HGByte*)resp.c_str(), (HGUInt)resp.size(), HGTRUE); + } + } + + void SockIoUser::HandleEvent(const SockIoEvtParam* param) + { + assert(NULL != param && m_id == param->usrId); + + SendResponse(param->data, param->size, HGTRUE); + } + + void SockIoUser::PostCmdMsg(const HGByte* data, HGUInt dataSize) + { + SockIoCmdParam* param = new SockIoCmdParam; + param->svr = (SockIoServer*)m_server; + param->usrId = m_id; + param->data = new HGByte[dataSize]; + param->size = dataSize; + memcpy(param->data, data, dataSize); + + HGMsg msg; + msg.id = MSGID_SOCKIO_COMMAND; + msg.data = param; + if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_server->GetMsgPump(), &msg)) + { + delete[] param->data; + param->size = 0; + delete param; + } + } + + void SockIoUser::PostEventMsg(const HGByte* data, HGUInt dataSize) + { + SockIoEvtParam* param = new SockIoEvtParam; + param->svr = (SockIoServer*)m_server; + param->usrId = m_id; + param->data = new HGByte[dataSize]; + param->size = dataSize; + memcpy(param->data, data, dataSize); + + HGMsg msg; + msg.id = MSGID_SOCKIO_EVENT; + msg.data = param; + if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_server->GetMsgPump(), &msg)) + { + delete[] param->data; + param->size = 0; + delete param; + } + } + + void SockIoUser::ThreadFunc() + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SockIoUser::ThreadFunc"); + + char chBuffer[2048]; + const char* pBuffer = chBuffer; + int nBufferSize = 0; + bool bConnect = false; + + unsigned char connectDataTail[4] = { '\r', '\n', '\r', '\n' }; + unsigned int connectDataTailLen = 0; + std::string connectData; + + uint8_t* pData = NULL; + int nDataSize = 0; + uint8_t* pDataEx = NULL; + int nRemainSize = 0; + uint8_t headData[20]; + uint32_t nHeadDataLen = 0; + uint8_t vMask[4]; + uint32_t nMaskCount = 0; + bool bHandle = false; + std::vector vAllData; + + while (1) + { + if (0 == nBufferSize) + { + int len = recv(m_sockConn, chBuffer, 2048, 0); + if (len <= 0) + { + // 这里跳出,可能是服务器关闭了socketConn,或者客户端关闭了socket,或者网络断开 + PostDisConnectMsg(); + break; + } + else + { + pBuffer = chBuffer; + nBufferSize = len; + } + } + + assert(nBufferSize > 0); + + if (!bConnect) + { + unsigned char b = *pBuffer; + ++pBuffer; + --nBufferSize; + + connectData.push_back(b); + if (b == connectDataTail[connectDataTailLen]) + { + ++connectDataTailLen; + } + else + { + connectDataTailLen = 0; + if (b == connectDataTail[connectDataTailLen]) + { + ++connectDataTailLen; + } + } + + if (4 == connectDataTailLen) + { + connectDataTailLen = 0; + bool shakeRet = ShakeHand(connectData); + connectData.clear(); + + if (!shakeRet) + { + PostDisConnectMsg(); + break; + } + + bConnect = true; + } + } + else + { + if (NULL == pData) + { + assert(0 == nDataSize); + + uint8_t b = *pBuffer; + ++pBuffer; + --nBufferSize; + headData[nHeadDataLen] = b; + ++nHeadDataLen; + + if (1 == nHeadDataLen) + { + if ((0x80 | 0x08) == headData[0]) // 断开连接 + { + PostDisConnectMsg(); + break; + } + else if ((0x80 | 0x09) == headData[0]) // PING帧 + { + // + } + else if ((0x80 | 0x0A) == headData[0]) // PONG帧 + { + // + } + else if ((0x00 | 0x01) == headData[0] || (0x00 | 0x02) == headData[0] || (0x00 | 0x00) == headData[0] || (0x80 | 0x00) == headData[0] + || (0x80 | 0x01) == headData[0] || (0x80 | 0x02) == headData[0]) // 数据帧 + { + if ((0x80 | 0x00) == headData[0] || (0x80 | 0x01) == headData[0] || (0x80 | 0x02) == headData[0]) + { + // 分片结束 + bHandle = true; + } + else + { + // 分片帧 + bHandle = false; + } + } + else // 帧错误,断开连接 + { + PostDisConnectMsg(); + break; + } + } + else if (2 == nHeadDataLen) + { + if (0 == (headData[1] & 0x80)) // 必须经过掩码处理 + { + PostDisConnectMsg(); + break; + } + + if ((0x80 | 0x09) == headData[0]) // PING帧 + { + if (0x80 != headData[1]) + { + PostDisConnectMsg(); + break; + } + } + else if ((0x80 | 0x0A) == headData[0]) // PONG帧 + { + if (0x80 != headData[1]) + { + PostDisConnectMsg(); + break; + } + } + else + { + if ((headData[1] & 0x7F) <= 125) + { + uint32_t nCmdSize = (headData[1] & 0x7F); + nHeadDataLen = 0; + + if (0 == nCmdSize) + { + PostDisConnectMsg(); + break; + } + + nDataSize = nCmdSize; + nRemainSize = nCmdSize; + pData = new uint8_t[nDataSize]; + pDataEx = pData; + } + } + } + else if (4 == nHeadDataLen) + { + if ((0x80 | 0x09) == headData[0]) // PING帧 + { + // + } + else if ((0x80 | 0x0A) == headData[0]) // PONG帧 + { + // + } + else + { + if ((headData[1] & 0x7F) == 126) + { + uint32_t nCmdSize = ntohs(*(uint16_t*)&headData[2]); + nHeadDataLen = 0; + + if (0 == nCmdSize) + { + PostDisConnectMsg(); + break; + } + + nDataSize = nCmdSize; + nRemainSize = nCmdSize; + pData = new uint8_t[nDataSize]; + pDataEx = pData; + } + } + } + else if (6 == nHeadDataLen) + { + if ((0x80 | 0x09) == headData[0]) // PING帧 + { + nHeadDataLen = 0; + + Pong(); + } + else if ((0x80 | 0x0A) == headData[0]) // PONG帧 + { + nHeadDataLen = 0; + } + } + else if (10 == nHeadDataLen) + { + if ((headData[1] & 0x7F) == 127) // 这里一定会等于127 + { + uint32_t nCmdSizeHigh = ntohl(*(uint32_t*)&headData[2]); + uint32_t nCmdSize = ntohl(*(uint32_t*)&headData[6]); + nHeadDataLen = 0; + + if ((0 != nCmdSizeHigh) || (0 == nCmdSize)) + { + PostDisConnectMsg(); + break; + } + + nDataSize = nCmdSize; + nRemainSize = nCmdSize; + pData = new uint8_t[nDataSize]; + pDataEx = pData; + } + } + } + else + { + if (4 != nMaskCount) + { + uint8_t b = *pBuffer; + ++pBuffer; + --nBufferSize; + vMask[nMaskCount] = b; + ++nMaskCount; + } + else + { + int nWriteSize = HGMIN(nBufferSize, nRemainSize); + memcpy(pDataEx, pBuffer, nWriteSize); + pBuffer += nWriteSize; + nBufferSize -= nWriteSize; + pDataEx += nWriteSize; + nRemainSize -= nWriteSize; + + if (0 == nRemainSize) + { + assert(pDataEx == pData + nDataSize); + + for (int i = 0; i < nDataSize; ++i) + { + int j = i % 4; + pData[i] = pData[i] ^ vMask[j]; + vAllData.push_back(pData[i]); + } + + delete[] pData; + pData = NULL; + nDataSize = 0; + nMaskCount = 0; + + if (bHandle) + { + if (1 == vAllData.size() && '2' == vAllData[0]) + { + // socket.io pong + char data = '3'; + SendResponse((const HGByte*)&data, 1, HGTRUE); + } + else + { + PostCmdMsg(&vAllData[0], vAllData.size()); + } + + bHandle = false; + vAllData.clear(); + } + } + } + } + } + } + + if (NULL != pData) + { + delete[] pData; + pData = NULL; + nDataSize = 0; + nMaskCount = 0; + } + } + + void SockIoUser::ScanCallback(HGUInt event, void* value1, void* value2, void* param) + { + SockIoUser* p = (SockIoUser*)param; + + char* resp = NULL; + if (SCANEVENT_ARRIVE == event) + { + resp = new char[256]; + sprintf(resp, "42[\"success\", \"%s\"]", (const char*)value1); + } + else if (SCANEVENT_REMOVE == event) + { + + } + else if (SCANEVENT_WORKING == event) + { + resp = new char[256]; + sprintf(resp, "42[\"event\", \"%s\"]", "......"); + } + else if (SCANEVENT_FINISH == event) + { + resp = new char[256]; + sprintf(resp, "42[\"result\", {\"code\":204, \"msg\":\"%s\"}]", (const char*)value1); + } + else if (SCANEVENT_ERROR == event) + { + resp = new char[256]; + sprintf(resp, "42[\"error\", \"%s\"]", (const char*)value1); + } + else if (SCANEVENT_IMAGE == event) + { + const char* imgName = (const char*)value1; + const char* imgBase64 = (const char*)value2; + resp = new char[256 + strlen(imgName) + strlen(imgBase64)]; + sprintf(resp, "42[\"image\", {\"code\":201, \"imageName\":\"%s\", \"image\":\"%s\"}]", imgName, imgBase64); + } + + if (NULL != resp) + { + p->PostEventMsg((const HGByte*)resp, strlen(resp)); + delete[] resp; + } + } + + void SockIoUser::GetMsgInfo(const SockIoCmdParam* param, std::string& user, std::string& data) + { + user.clear(); + data.clear(); + + std::string paramStr((const char*)param->data, param->size); + size_t pos = paramStr.find('['); + if (std::string::npos == pos) + { + return; + } + + std::string msgType = paramStr.substr(0, pos); + if ("42" != msgType) + { + return; + } + + std::string msgInfo = paramStr.substr(pos); + cJSON* json = cJSON_Parse(msgInfo.c_str()); + if (NULL != json) + { + if (NULL != json->child) + { + user = json->child->valuestring; + if (NULL != json->child->next) + data = json->child->next->valuestring; + } + + cJSON_Delete(json); + } + } + + bool SockIoUser::ShakeHand(const std::string& head) + { + std::string requestMethod; + std::string requestURIPath; + HttpPairs requestURIQueryInfos; + std::string requestURIFragment; + std::string httpVersion; + HttpPairs headInfos; + HttpHead::AnalysisHead(head, requestMethod, requestURIPath, requestURIQueryInfos, + requestURIFragment, httpVersion, headInfos); + + if ("websocket" != HttpHead::GetValue(requestURIQueryInfos, "transport")) + return false; + if ("Upgrade" != HttpHead::GetValue(headInfos, "Connection")) + return false; + if ("websocket" != HttpHead::GetValue(headInfos, "Upgrade")) + return false; + + std::string key = HttpHead::GetValue(headInfos, "Sec-WebSocket-Key"); + if (key.empty()) + return false; + + key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; + + unsigned int message_digest[5]; + SHA1 sha; + sha.Reset(); + sha << key.c_str(); + sha.Result(message_digest); + + for (int i = 0; i < 5; ++i) + message_digest[i] = htonl(message_digest[i]); + std::string serverKey = base64_encode((const unsigned char*)message_digest, 20); + + std::string handShakeResp = "HTTP/1.1 101 Switching Protocols\r\n"; + handShakeResp += "Upgrade: websocket\r\n"; + handShakeResp += "Connection: Upgrade\r\n"; + handShakeResp += "Sec-WebSocket-Accept:"; + handShakeResp += serverKey; + handShakeResp += "\r\n\r\n"; + send(m_sockConn, handShakeResp.c_str(), (int)handShakeResp.length(), 0); + + char uuid[256] = { 0 }; + HGBase_GetUuid(uuid, 256); + + std::string resp = "0{"; + resp += "\"sid\":\""; + resp += uuid; + resp += "\","; + resp += "\"upgrades\":[\"websocket\"],"; + resp += "\"pingInterval\":25000,"; + resp += "\"pingTimeout\":60000"; + resp += "}"; + SendResponse((const HGByte*)resp.c_str(), (int)resp.size(), HGTRUE); + + resp = "40"; + SendResponse((const HGByte*)resp.c_str(), (int)resp.size(), HGTRUE); + return true; + } + + void SockIoUser::Pong() + { + uint8_t vHead[2]; + vHead[0] = 0x80 | 0x0A; + vHead[1] = 0; + + HGBase_EnterLock(m_cs); + send(m_sockConn, (const char*)vHead, 2, 0); + HGBase_LeaveLock(m_cs); + } + + bool SockIoUser::SendResponse(const HGByte* data, HGUInt size, HGBool text) + { + if (NULL == data || 0 == size) + { + return false; + } + + uint32_t nHeadLen = 0; + + uint8_t vHead[20] = { 0 }; + vHead[0] = text ? (0x80 | 0x01) : (0x80 | 0x02); + if (size <= 125) + { + vHead[1] = (uint8_t)size; + nHeadLen = 2; + } + else if (size <= 0xFFFF) + { + vHead[1] = 126; + uint16_t payloadLength16b = htons((uint16_t)size); + memcpy(&vHead[2], &payloadLength16b, 2); + nHeadLen = 4; + } + else + { + vHead[1] = 127; + vHead[2] = 0; + vHead[3] = 0; + vHead[4] = 0; + vHead[5] = 0; + uint32_t payloadLength32b = htonl(size); + memcpy(&vHead[6], &payloadLength32b, 4); + nHeadLen = 10; + } + + HGBase_EnterLock(m_cs); + send(m_sockConn, (const char*)vHead, nHeadLen, 0); + send(m_sockConn, (const char*)data, size, 0); + HGBase_LeaveLock(m_cs); + return true; + } +} diff --git a/sdk/webservice/SockIoUser.h b/sdk/webservice/SockIoUser.h new file mode 100644 index 00000000..add71447 --- /dev/null +++ b/sdk/webservice/SockIoUser.h @@ -0,0 +1,35 @@ +#pragma once + +#include "WebUser.h" +#include "Msg.h" + +namespace ver_1 +{ + class WebServer; + + class SockIoUser : public WebUser + { + public: +#if defined(HG_CMP_MSC) + SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn); +#else + SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn); +#endif + virtual ~SockIoUser(); + + void HandleCmd(const SockIoCmdParam* param); + void HandleEvent(const SockIoEvtParam* param); + + protected: + void PostCmdMsg(const HGByte* data, HGUInt dataSize); + void PostEventMsg(const HGByte* data, HGUInt dataSize); + virtual void ThreadFunc(); + + private: + static void ScanCallback(HGUInt event, void* value1, void* value2, void* param); + static void GetMsgInfo(const SockIoCmdParam* param, std::string& user, std::string& data); + bool ShakeHand(const std::string& head); + void Pong(); + bool SendResponse(const HGByte* data, HGUInt size, HGBool text); + }; +} \ No newline at end of file diff --git a/sdk/webservice/WSServer.cpp b/sdk/webservice/WSServer.cpp new file mode 100644 index 00000000..e69de29b diff --git a/sdk/webservice/WSServer.h b/sdk/webservice/WSServer.h new file mode 100644 index 00000000..e69de29b diff --git a/sdk/webservice/WSUser.cpp b/sdk/webservice/WSUser.cpp new file mode 100644 index 00000000..e69de29b diff --git a/sdk/webservice/WSUser.h b/sdk/webservice/WSUser.h new file mode 100644 index 00000000..e69de29b diff --git a/sdk/webservice/WebServer.cpp b/sdk/webservice/WebServer.cpp new file mode 100644 index 00000000..3b6b03cb --- /dev/null +++ b/sdk/webservice/WebServer.cpp @@ -0,0 +1,213 @@ +#include "WebServer.h" +#include "WebUser.h" +#include "base/HGInfo.h" + +namespace ver_1 +{ + WebServer::WebServer(HGMsgPump msgPump, Manager* manager) + { + m_msgPump = msgPump; + m_manager = manager; + + m_currUserId = 1; +#if defined(HG_CMP_MSC) + m_sockServer = INVALID_SOCKET; +#else + m_sockServer = -1; +#endif + m_listenThread = NULL; + } + + WebServer::~WebServer() + { + + } + + HGMsgPump WebServer::GetMsgPump() + { + return m_msgPump; + } + + Manager* WebServer::GetManager() + { + return m_manager; + } + + bool WebServer::Open(HGUShort port) + { +#if defined(HG_CMP_MSC) + if (INVALID_SOCKET != m_sockServer) +#else + if (-1 != m_sockServer) +#endif + { + return false; + } + +#if defined(HG_CMP_MSC) + SOCKET sockServer = socket(AF_INET, SOCK_STREAM, 0); + if (INVALID_SOCKET == sockServer) +#else + int sockServer = socket(AF_INET, SOCK_STREAM, 0); + if (-1 == sockServer) +#endif + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "open webserver failed 1, port=%u", port); + return false; + } + + // bind +#if defined(HG_CMP_MSC) + SOCKADDR_IN addrServer; + addrServer.sin_addr.S_un.S_addr = INADDR_ANY; + addrServer.sin_family = AF_INET; + addrServer.sin_port = htons(port); + if (0 != bind(sockServer, (SOCKADDR*)&addrServer, sizeof(SOCKADDR_IN))) +#else + struct sockaddr_in addrServer; + addrServer.sin_addr.s_addr = htonl(INADDR_ANY); + addrServer.sin_family = AF_INET; + addrServer.sin_port = htons(port); + if (0 != bind(sockServer, (struct sockaddr*)&addrServer, sizeof(addrServer))) +#endif + { +#if defined(HG_CMP_MSC) + closesocket(sockServer); +#else + close(sockServer); +#endif + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "open webserver failed 2, port=%u", port); + return false; + } + + // listen + if (0 != listen(sockServer, 5)) + { +#if defined(HG_CMP_MSC) + closesocket(sockServer); +#else + close(sockServer); +#endif + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "open webserver failed 3, port=%u", port); + return false; + } + + m_sockServer = sockServer; + HGBase_OpenThread(ThreadFunc, this, &m_listenThread); + assert(NULL != m_listenThread); + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "open webserver success, port=%u", port); + return true; + } + + bool WebServer::Close() + { +#if defined(HG_CMP_MSC) + if (INVALID_SOCKET == m_sockServer) +#else + if (-1 == m_sockServer) +#endif + { + return false; + } + + while (!m_vectorUser.empty()) + { + WebUser* pUser = m_vectorUser[0]; + m_vectorUser.erase(m_vectorUser.begin()); + delete pUser; + pUser = NULL; + } + +#if defined(HG_CMP_MSC) + closesocket(m_sockServer); + m_sockServer = INVALID_SOCKET; +#else + close(m_sockServer); + m_sockServer = -1; +#endif + HGBase_CloseThread(m_listenThread); + m_listenThread = NULL; + return true; + } + + void WebServer::DisConnect(const DisConnectParam* param) + { + assert(NULL != param && this == param->svr); + + int nIndex = GetUserIndex(param->usrId); + if (-1 != nIndex) + { + WebUser* pUser = m_vectorUser[nIndex]; + m_vectorUser.erase(m_vectorUser.begin() + nIndex); + delete pUser; + pUser = NULL; + } + } + +#if defined(HG_CMP_MSC) + void WebServer::PostConnectMsg(const char* ip, uint16_t port, SOCKET sockConn) +#else + void WebServer::PostConnectMsg(const char* ip, uint16_t port, int sockConn) +#endif + { + ConnectParam* param = new ConnectParam; + param->svr = this; + strcpy(param->ip, ip); + param->port = port; + param->socket = sockConn; + + HGMsg msg; + msg.id = MSGID_CONNECT; + msg.data = param; + if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_msgPump, &msg)) + { +#if defined(HG_CMP_MSC) + closesocket(param->socket); +#else + close(param->socket); +#endif + delete param; + } + } + + int WebServer::GetUserIndex(HGUInt id) + { + int nIndex = -1; + for (int i = 0; i < (int)m_vectorUser.size(); ++i) + { + if (id == m_vectorUser[i]->GetId()) + { + nIndex = i; + break; + } + } + + return nIndex; + } + + void WebServer::ThreadFunc(HGThread thread, HGPointer param) + { + WebServer* p = (WebServer*)param; + + while (1) + { +#if defined(HG_CMP_MSC) + SOCKADDR_IN addrClient; + int len = sizeof(SOCKADDR_IN); + SOCKET socketConn = accept(p->m_sockServer, (SOCKADDR*)&addrClient, &len); + if (INVALID_SOCKET == socketConn) +#else + struct sockaddr_in addrClient; + socklen_t len = sizeof(addrClient); + int socketConn = accept(p->m_sockServer, (struct sockaddr*)&addrClient, &len); + if (-1 == socketConn) +#endif + { + // Ƿرm_sockServer + break; + } + + p->PostConnectMsg(inet_ntoa(addrClient.sin_addr), ntohs(addrClient.sin_port), socketConn); + } + } +} \ No newline at end of file diff --git a/sdk/webservice/WebServer.h b/sdk/webservice/WebServer.h new file mode 100644 index 00000000..6e5ec69a --- /dev/null +++ b/sdk/webservice/WebServer.h @@ -0,0 +1,51 @@ +#pragma once + +#include "base/HGDef.h" +#include "base/HGInc.h" +#include "base/HGThread.h" +#include "base/HGMsgPump.h" +#include "Msg.h" +#include + +namespace ver_1 +{ + class Manager; + + class WebServer + { + public: + WebServer(HGMsgPump msgPump, Manager* manager); + virtual ~WebServer(); + + HGMsgPump GetMsgPump(); + Manager* GetManager(); + + bool Open(HGUShort port); + bool Close(); + + virtual void Connect(const ConnectParam* param) = 0; + void DisConnect(const DisConnectParam* param); + + protected: +#if defined(HG_CMP_MSC) + void PostConnectMsg(const char* ip, uint16_t port, SOCKET sockConn); +#else + void PostConnectMsg(const char* ip, uint16_t port, int sockConn); +#endif + int GetUserIndex(HGUInt id); + static void ThreadFunc(HGThread thread, HGPointer param); + + protected: + HGMsgPump m_msgPump; + Manager* m_manager; + + HGUInt m_currUserId; +#if defined(HG_CMP_MSC) + SOCKET m_sockServer; +#else + int m_sockServer; +#endif + HGThread m_listenThread; + std::vector m_vectorUser; + }; +} \ No newline at end of file diff --git a/sdk/webservice/WebUser.cpp b/sdk/webservice/WebUser.cpp new file mode 100644 index 00000000..ef5e7d9c --- /dev/null +++ b/sdk/webservice/WebUser.cpp @@ -0,0 +1,83 @@ +#include "WebUser.h" +#include "WebServer.h" +#include "base/HGInfo.h" + +namespace ver_1 +{ +#if defined(HG_CMP_MSC) + WebUser::WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn) +#else + WebUser::WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn) +#endif + { + m_server = server; + HGBase_CreateLock(&m_cs); + m_id = id; + strcpy(m_ip, ip); + m_port = port; + + m_sockConn = sockConn; + m_thread = NULL; + } + + WebUser::~WebUser() + { +#if defined(HG_CMP_MSC) + closesocket(m_sockConn); + m_sockConn = INVALID_SOCKET; +#else + close(m_sockConn); + m_sockConn = -1; +#endif + HGBase_CloseThread(m_thread); + m_thread = NULL; + + m_port = 0; + memset(m_ip, 0, sizeof(m_ip)); + m_id = 0; + HGBase_DestroyLock(m_cs); + m_server = NULL; + } + + void WebUser::Open() + { + HGBase_OpenThread(ThreadFunc, this, &m_thread); + assert(NULL != m_thread); + } + + HGUInt WebUser::GetId() + { + return m_id; + } + + Manager* WebUser::GetManager() + { + return m_server->GetManager(); + } + + void WebUser::PostDisConnectMsg() + { + DisConnectParam* param = new DisConnectParam; + param->svr = m_server; + param->usrId = m_id; + + HGMsg msg; + msg.id = MSGID_DISCONNECT; + msg.data = param; + if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_server->GetMsgPump(), &msg)) + { + delete param; + } + } + + void WebUser::ThreadFunc() + { + HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "WebUser::ThreadFunc"); + } + + void WebUser::ThreadFunc(HGThread thread, HGPointer param) + { + WebUser* p = (WebUser*)param; + p->ThreadFunc(); + } +} diff --git a/sdk/webservice/WebUser.h b/sdk/webservice/WebUser.h new file mode 100644 index 00000000..0eaecd05 --- /dev/null +++ b/sdk/webservice/WebUser.h @@ -0,0 +1,49 @@ +#pragma once + +#include "base/HGDef.h" +#include "base/HGInc.h" +#include "base/HGLock.h" +#include "base/HGThread.h" +#include "base/HGMsgPump.h" + +namespace ver_1 +{ + class WebServer; + class Manager; + + class WebUser + { + public: +#if defined(HG_CMP_MSC) + WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn); +#else + WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn); +#endif + virtual ~WebUser(); + + void Open(); + HGUInt GetId(); + Manager* GetManager(); + + protected: + void PostDisConnectMsg(); + virtual void ThreadFunc(); + + private: + static void ThreadFunc(HGThread thread, HGPointer param); + + protected: + WebServer* m_server; + HGLock m_cs; + HGUInt m_id; + char m_ip[16]; + uint16_t m_port; + +#if defined(HG_CMP_MSC) + SOCKET m_sockConn; +#else + int m_sockConn; +#endif + HGThread m_thread; + }; +} diff --git a/sdk/webservice/main.cpp b/sdk/webservice/main.cpp index d88be18b..aeee4761 100644 --- a/sdk/webservice/main.cpp +++ b/sdk/webservice/main.cpp @@ -3,10 +3,11 @@ #include "base/HGThread.h" #include "base/HGUtility.h" #include "base/HGMsgPump.h" -#include "1.0/Manager.h" -#include "1.0/HttpServer.h" -#include "1.0/SockIoServer.h" -#include "1.0/MsgPumpCallback_1_0.h" +#include "Manager.h" +#include "HttpServer.h" +#include "SockIoServer.h" +#include "WSServer.h" +#include "MsgPumpCallback.h" static void ThreadFunc(HGThread thread, HGPointer param) { @@ -19,21 +20,19 @@ static void ThreadFunc(HGThread thread, HGPointer param) HGInt verNum = 2; HGBase_GetProfileInt(cfgPath, "version", "verNum", 2, &verNum); - if (1 == verNum) // 使用1.0版本接口 + if (1 == verNum) // 使用V1版本接口 { - Manager manager(msgPump); - HttpServer httpServer(msgPump, &manager); - SockIoServer sockIoServer(msgPump, &manager); + ver_1::Manager manager(msgPump); + ver_1::HttpServer httpServer(msgPump, &manager); + ver_1::SockIoServer sockIoServer(msgPump, &manager); httpServer.Open(18999); sockIoServer.Open(28999); - - HGBase_RunMsgPump(msgPump, HGMsgPumpCallback, NULL); - + HGBase_RunMsgPump(msgPump, ver_1::HGMsgPumpCallback, NULL); sockIoServer.Close(); httpServer.Close(); } - else // 使用2.0版本接口 + else // 使用V2版本接口 { } diff --git a/third_party/base64/base64.h b/third_party/base64/base64.h index 65d5db8b..a3207d09 100644 --- a/third_party/base64/base64.h +++ b/third_party/base64/base64.h @@ -1,4 +1,9 @@ + +#ifndef _BASE64_H_ +#define _BASE64_H_ #include std::string base64_encode(unsigned char const* , unsigned int len); std::string base64_decode(std::string const& s); + +#endif