This commit is contained in:
gb 2022-05-23 11:51:08 +08:00
commit 872e84cb86
53 changed files with 5092 additions and 4932 deletions

View File

@ -98,3 +98,9 @@ HGBase_ImageRotate180
HGBase_ImageGrayscale HGBase_ImageGrayscale
HGBase_ReverseImage HGBase_ReverseImage
HGBase_CopyImage HGBase_CopyImage
HGBase_CreateMsgPump
HGBase_DestroyMsgPump
HGBase_RunMsgPump
HGBase_PostPumpMessage
HGBase_ExitMsgPump

View File

@ -35,6 +35,8 @@
<ClCompile Include="..\..\..\modules\base\HGLock.cpp" /> <ClCompile Include="..\..\..\modules\base\HGLock.cpp" />
<ClCompile Include="..\..\..\modules\base\HGLog.cpp" /> <ClCompile Include="..\..\..\modules\base\HGLog.cpp" />
<ClCompile Include="..\..\..\modules\base\HGMd5.cpp" /> <ClCompile Include="..\..\..\modules\base\HGMd5.cpp" />
<ClCompile Include="..\..\..\modules\base\HGMsgPump.cpp" />
<ClCompile Include="..\..\..\modules\base\HGMsgPumpImpl.cpp" />
<ClCompile Include="..\..\..\modules\base\HGThread.cpp" /> <ClCompile Include="..\..\..\modules\base\HGThread.cpp" />
<ClCompile Include="..\..\..\modules\base\HGUtility.cpp" /> <ClCompile Include="..\..\..\modules\base\HGUtility.cpp" />
</ItemGroup> </ItemGroup>
@ -55,6 +57,8 @@
<ClInclude Include="..\..\..\modules\base\HGLock.h" /> <ClInclude Include="..\..\..\modules\base\HGLock.h" />
<ClInclude Include="..\..\..\modules\base\HGLog.h" /> <ClInclude Include="..\..\..\modules\base\HGLog.h" />
<ClInclude Include="..\..\..\modules\base\HGMd5.h" /> <ClInclude Include="..\..\..\modules\base\HGMd5.h" />
<ClInclude Include="..\..\..\modules\base\HGMsgPump.h" />
<ClInclude Include="..\..\..\modules\base\HGMsgPumpImpl.h" />
<ClInclude Include="..\..\..\modules\base\HGThread.h" /> <ClInclude Include="..\..\..\modules\base\HGThread.h" />
<ClInclude Include="..\..\..\modules\base\HGUtility.h" /> <ClInclude Include="..\..\..\modules\base\HGUtility.h" />
</ItemGroup> </ItemGroup>

View File

@ -184,7 +184,7 @@ void CHGTestDlg::OnBnClickedButton2()
m_dsm = NULL; 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; CHGTestDlg* p = (CHGTestDlg*)param;
if (HGTWAIN_DSEVENT_XFERREADY == event) if (HGTWAIN_DSEVENT_XFERREADY == event)

View File

@ -23,7 +23,7 @@ protected:
HGTwainDSM m_dsm; HGTwainDSM m_dsm;
HGTwainDS m_ds; HGTwainDS m_ds;
static void HGAPI DSEventCallback(HGTwainDS ds, HGUInt event, HGPointer param); static void DSEventCallback(HGTwainDS ds, HGUInt event, HGPointer param);
// 实现 // 实现
protected: protected:

View File

@ -19,32 +19,36 @@
</ProjectConfiguration> </ProjectConfiguration>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\..\sdk\webservice\1.0\HttpHead.cpp" /> <ClCompile Include="..\..\..\sdk\webservice\HttpHead.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\1.0\HttpUser.cpp" /> <ClCompile Include="..\..\..\sdk\webservice\HttpServer.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\1.0\Manager.cpp" /> <ClCompile Include="..\..\..\sdk\webservice\HttpUser.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\1.0\MsgLoop.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\1.0\SockIoServer.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\1.0\SockIoUser.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\1.0\HttpServer.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\1.0\WebServer.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\1.0\WebUser.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\main.cpp" /> <ClCompile Include="..\..\..\sdk\webservice\main.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\Manager.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\MsgPumpCallback.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\SockIoServer.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\SockIoUser.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\WebServer.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\WebUser.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\WSServer.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\WSUser.cpp" />
<ClCompile Include="..\..\..\third_party\base64\base64.cpp" /> <ClCompile Include="..\..\..\third_party\base64\base64.cpp" />
<ClCompile Include="..\..\..\third_party\json\cJSON.c" /> <ClCompile Include="..\..\..\third_party\json\cJSON.c" />
<ClCompile Include="..\..\..\third_party\sha1\sha1.cpp" /> <ClCompile Include="..\..\..\third_party\sha1\sha1.cpp" />
<ClCompile Include="..\..\..\utility\HGString.cpp" /> <ClCompile Include="..\..\..\utility\HGString.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\sdk\webservice\1.0\HttpHead.h" /> <ClInclude Include="..\..\..\sdk\webservice\HttpHead.h" />
<ClInclude Include="..\..\..\sdk\webservice\1.0\HttpUser.h" /> <ClInclude Include="..\..\..\sdk\webservice\HttpServer.h" />
<ClInclude Include="..\..\..\sdk\webservice\1.0\Manager.h" /> <ClInclude Include="..\..\..\sdk\webservice\HttpUser.h" />
<ClInclude Include="..\..\..\sdk\webservice\1.0\MsgLoop.h" /> <ClInclude Include="..\..\..\sdk\webservice\Manager.h" />
<ClInclude Include="..\..\..\sdk\webservice\1.0\SockIoServer.h" /> <ClInclude Include="..\..\..\sdk\webservice\Msg.h" />
<ClInclude Include="..\..\..\sdk\webservice\1.0\SockIoUser.h" /> <ClInclude Include="..\..\..\sdk\webservice\MsgPumpCallback.h" />
<ClInclude Include="..\..\..\sdk\webservice\1.0\WebMsg.h" /> <ClInclude Include="..\..\..\sdk\webservice\SockIoServer.h" />
<ClInclude Include="..\..\..\sdk\webservice\1.0\HttpServer.h" /> <ClInclude Include="..\..\..\sdk\webservice\SockIoUser.h" />
<ClInclude Include="..\..\..\sdk\webservice\1.0\WebServer.h" /> <ClInclude Include="..\..\..\sdk\webservice\WebServer.h" />
<ClInclude Include="..\..\..\sdk\webservice\1.0\WebUser.h" /> <ClInclude Include="..\..\..\sdk\webservice\WebUser.h" />
<ClInclude Include="..\..\..\sdk\webservice\WSServer.h" />
<ClInclude Include="..\..\..\sdk\webservice\WSUser.h" />
<ClInclude Include="..\..\..\third_party\base64\base64.h" /> <ClInclude Include="..\..\..\third_party\base64\base64.h" />
<ClInclude Include="..\..\..\third_party\json\cJSON.h" /> <ClInclude Include="..\..\..\third_party\json\cJSON.h" />
<ClInclude Include="..\..\..\third_party\sha1\sha1.h" /> <ClInclude Include="..\..\..\third_party\sha1\sha1.h" />

View File

@ -149,7 +149,7 @@ HGResult HGAPI HGTwain_GetCapability(HGTwainDS ds, HGUInt cap, HGInt* value)
} }
HGResult HGAPI HGTwain_EnableDSUIOnly(HGTwainDS ds, HGBool showUI, HWND parent, HGResult HGAPI HGTwain_EnableDSUIOnly(HGTwainDS ds, HGBool showUI, HWND parent,
HGTwain_DSEventCallback func, HGPointer param) HGDSEventFunc func, HGPointer param)
{ {
if (NULL == ds) 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, HGResult HGAPI HGTwain_EnableDS(HGTwainDS ds, HGBool showUI, HWND parent,
HGTwain_DSEventCallback func, HGPointer param) HGDSEventFunc func, HGPointer param)
{ {
if (NULL == ds) if (NULL == ds)
{ {

View File

@ -29,7 +29,7 @@ HG_DECLARE_HANDLE(HGTwainDS);
* 1) event为HGTWAIN_EVENT_XFERREADY, TWAIN状态从5变为6 * 1) event为HGTWAIN_EVENT_XFERREADY, TWAIN状态从5变为6
* 2) event为HGTWAIN_EVENT_CLOSEDSREQ, DS * 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 /* 加载DSM
* : * :
@ -151,7 +151,7 @@ HGEXPORT HGResult HGAPI HGTwain_GetCapability(HGTwainDS ds, HGUInt cap, HGInt* v
* 1) TWAIN状态从4变为5 * 1) TWAIN状态从4变为5
*/ */
HGEXPORT HGResult HGAPI HGTwain_EnableDSUIOnly(HGTwainDS ds, HGBool showUI, HWND parent, HGEXPORT HGResult HGAPI HGTwain_EnableDSUIOnly(HGTwainDS ds, HGBool showUI, HWND parent,
HGTwain_DSEventCallback func, HGPointer param); HGDSEventFunc func, HGPointer param);
/* 启动DS /* 启动DS
* : * :
@ -164,7 +164,7 @@ HGEXPORT HGResult HGAPI HGTwain_EnableDSUIOnly(HGTwainDS ds, HGBool showUI, HWND
* 1) TWAIN状态从4变为5 * 1) TWAIN状态从4变为5
*/ */
HGEXPORT HGResult HGAPI HGTwain_EnableDS(HGTwainDS ds, HGBool showUI, HWND parent, HGEXPORT HGResult HGAPI HGTwain_EnableDS(HGTwainDS ds, HGBool showUI, HWND parent,
HGTwain_DSEventCallback func, HGPointer param); HGDSEventFunc func, HGPointer param);
/* 停止DS /* 停止DS
* : * :

View File

@ -436,7 +436,7 @@ HGResult HGTwainDSMImpl::GetCapability(HGTwainDSImpl* ds, HGUInt cap, HGInt* val
return HGBASE_ERR_FAIL; 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) if (NULL == ds || NULL == func)
{ {
@ -473,7 +473,7 @@ HGResult HGTwainDSMImpl::EnableDSUIOnly(HGTwainDSImpl* ds, HGBool showUI, HWND p
return HGBASE_ERR_FAIL; 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) if (NULL == ds || NULL == func)
{ {

View File

@ -25,7 +25,7 @@ struct HGTwainDSImpl
HGBool open; HGBool open;
HGBool showUI; HGBool showUI;
HWND parent; HWND parent;
HGTwain_DSEventCallback func; HGDSEventFunc func;
HGPointer param; HGPointer param;
HGBool enable; HGBool enable;
}; };
@ -50,8 +50,8 @@ public:
HGResult SetCapability(HGTwainDSImpl* ds, HGUInt cap, HGInt value); HGResult SetCapability(HGTwainDSImpl* ds, HGUInt cap, HGInt value);
HGResult GetCapability(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 EnableDSUIOnly(HGTwainDSImpl* ds, HGBool showUI, HWND parent, HGDSEventFunc func, HGPointer param);
HGResult EnableDS(HGTwainDSImpl* ds, HGBool showUI, HWND parent, HGTwain_DSEventCallback func, HGPointer param); HGResult EnableDS(HGTwainDSImpl* ds, HGBool showUI, HWND parent, HGDSEventFunc func, HGPointer param);
HGResult DisableDS(HGTwainDSImpl* ds); HGResult DisableDS(HGTwainDSImpl* ds);
HGResult ImageNativeXfer(HGTwainDSImpl* ds, HGUInt type, HGUInt origin, HGImage* image); HGResult ImageNativeXfer(HGTwainDSImpl* ds, HGUInt type, HGUInt origin, HGImage* image);
HGResult EndXfer(HGTwainDSImpl* ds, HGUInt* count); HGResult EndXfer(HGTwainDSImpl* ds, HGUInt* count);

View File

@ -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<std::string> 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 <std::string, std::string> 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<std::string> 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 <std::string, std::string> 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;
}

View File

@ -1,48 +0,0 @@
#ifndef __HTTPHEAD_H__
#define __HTTPHEAD_H__
#include "base/HGDef.h"
#include "base/HGInc.h"
#include <string>
#include <vector>
typedef std::pair<std::string, std::string> HttpPair;
typedef std::vector<HttpPair> 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__ */

View File

@ -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);
}
}

View File

@ -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__ */

View File

@ -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<std::string> 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<std::string> imgNameList;
std::vector<std::string> 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<int> 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<std::string> 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;
}

View File

@ -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__ */

File diff suppressed because it is too large Load Diff

View File

@ -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 <string>
#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; // 扫描幅面 A3A3幅面 Auto:自适应幅面 A4A4幅面默认Auto
int splitImage; // 图像分割 0disable 1垂直分割 2 水平分割默认0
bool noiseDetachEnable; // 去除噪点默认true
int noiseDetach; // 噪点阈值 范围10­-50 默认值15
int uploadMode; // 是否边扫边上传 0http 1ftp 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; // 上传模式 0http 1ftp 默认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<std::string> &devNameList);
// 获取上次的扫描结果
bool GetImageByDevId(const std::string& devId, std::vector<std::string> &imgNameList,
std::vector<std::string> &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<int> &imgIndexList,
std::string &imgName, std::string& imgBase64);
// 自动排序
bool BookSort(const std::string& devId, std::vector<std::string>& imgNameList,
std::vector<std::string>& 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<std::string> GetFileNameList(const std::string &devId);
static bool SaveFileNameList(const std::string& devId, const std::vector<std::string>& 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__ */

View File

@ -1,6 +0,0 @@
#include "MsgPumpCallback_1_0.h"
void HGMsgPumpCallback(HGMsgPump msgPump, const HGMsg* msg, HGPointer param)
{
// 处理
}

View File

@ -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__ */

View File

@ -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 <string>
#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__ */

View File

@ -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);
}
}

View File

@ -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__ */

View File

@ -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<uint8_t> 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;
}

View File

@ -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__ */

View File

@ -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);
}
}

View File

@ -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 <vector>
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<class WebUser*> m_vectorUser;
};
#endif /* __WEBSERVER_H__ */

View File

@ -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();
}

View File

@ -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__ */

441
sdk/webservice/HttpHead.cpp Normal file
View File

@ -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<std::string> 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 <std::string, std::string> 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<std::string> 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 <std::string, std::string> 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;
}
}

48
sdk/webservice/HttpHead.h Normal file
View File

@ -0,0 +1,48 @@
#pragma once
#include "base/HGDef.h"
#include "base/HGInc.h"
#include <string>
#include <vector>
namespace ver_1
{
typedef std::pair<std::string, std::string> HttpPair;
typedef std::vector<HttpPair> 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;
};
}

View File

@ -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);
}
}
}

View File

@ -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);
};
}

897
sdk/webservice/HttpUser.cpp Normal file
View File

@ -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<std::string> 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<std::string> imgNameList;
std::vector<std::string> 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<int> 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<std::string> 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;
}
}

30
sdk/webservice/HttpUser.h Normal file
View File

@ -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);
};
}

2082
sdk/webservice/Manager.cpp Normal file

File diff suppressed because it is too large Load Diff

194
sdk/webservice/Manager.h Normal file
View File

@ -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 <string>
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; // 扫描幅面 A3A3幅面 Auto:自适应幅面 A4A4幅面默认Auto
int splitImage; // 图像分割 0disable 1垂直分割 2 水平分割默认0
bool noiseDetachEnable; // 去除噪点默认true
int noiseDetach; // 噪点阈值 范围10­-50 默认值15
int uploadMode; // 是否边扫边上传 0http 1ftp 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; // 上传模式 0http 1ftp 默认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<std::string>& devNameList);
// 获取上次的扫描结果
bool GetImageByDevId(const std::string& devId, std::vector<std::string>& imgNameList,
std::vector<std::string>& 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<int>& imgIndexList,
std::string& imgName, std::string& imgBase64);
// 自动排序
bool BookSort(const std::string& devId, std::vector<std::string>& imgNameList,
std::vector<std::string>& 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<std::string> GetFileNameList(const std::string& devId);
static bool SaveFileNameList(const std::string& devId, const std::vector<std::string>& 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;
};
}

86
sdk/webservice/Msg.h Normal file
View File

@ -0,0 +1,86 @@
#pragma once
#include "base/HGDef.h"
#include "base/HGInc.h"
#include "HttpHead.h"
#include <string>
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;
};
}

View File

@ -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;
}
}
}

View File

@ -0,0 +1,8 @@
#pragma once
#include "base/HGMsgPump.h"
namespace ver_1
{
void HGMsgPumpCallback(HGMsgPump msgPump, const HGMsg* msg, HGPointer param);
}

View File

@ -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);
}
}
}

View File

@ -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);
};
}

View File

@ -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<uint8_t> 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;
}
}

View File

@ -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);
};
}

View File

View File

View File

0
sdk/webservice/WSUser.h Normal file
View File

View File

@ -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);
}
}
}

View File

@ -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 <vector>
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<class WebUser*> m_vectorUser;
};
}

View File

@ -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();
}
}

49
sdk/webservice/WebUser.h Normal file
View File

@ -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;
};
}

View File

@ -3,10 +3,11 @@
#include "base/HGThread.h" #include "base/HGThread.h"
#include "base/HGUtility.h" #include "base/HGUtility.h"
#include "base/HGMsgPump.h" #include "base/HGMsgPump.h"
#include "1.0/Manager.h" #include "Manager.h"
#include "1.0/HttpServer.h" #include "HttpServer.h"
#include "1.0/SockIoServer.h" #include "SockIoServer.h"
#include "1.0/MsgPumpCallback_1_0.h" #include "WSServer.h"
#include "MsgPumpCallback.h"
static void ThreadFunc(HGThread thread, HGPointer param) static void ThreadFunc(HGThread thread, HGPointer param)
{ {
@ -19,21 +20,19 @@ static void ThreadFunc(HGThread thread, HGPointer param)
HGInt verNum = 2; HGInt verNum = 2;
HGBase_GetProfileInt(cfgPath, "version", "verNum", 2, &verNum); HGBase_GetProfileInt(cfgPath, "version", "verNum", 2, &verNum);
if (1 == verNum) // 使用1.0版本接口 if (1 == verNum) // 使用V1版本接口
{ {
Manager manager(msgPump); ver_1::Manager manager(msgPump);
HttpServer httpServer(msgPump, &manager); ver_1::HttpServer httpServer(msgPump, &manager);
SockIoServer sockIoServer(msgPump, &manager); ver_1::SockIoServer sockIoServer(msgPump, &manager);
httpServer.Open(18999); httpServer.Open(18999);
sockIoServer.Open(28999); sockIoServer.Open(28999);
HGBase_RunMsgPump(msgPump, ver_1::HGMsgPumpCallback, NULL);
HGBase_RunMsgPump(msgPump, HGMsgPumpCallback, NULL);
sockIoServer.Close(); sockIoServer.Close();
httpServer.Close(); httpServer.Close();
} }
else // 使用2.0版本接口 else // 使用V2版本接口
{ {
} }

View File

@ -1,4 +1,9 @@
#ifndef _BASE64_H_
#define _BASE64_H_
#include <string> #include <string>
std::string base64_encode(unsigned char const* , unsigned int len); std::string base64_encode(unsigned char const* , unsigned int len);
std::string base64_decode(std::string const& s); std::string base64_decode(std::string const& s);
#endif