用命名空间ver_1和ver_2来区分1.0和2.0版本

This commit is contained in:
luoliangyi 2022-05-23 18:29:23 +08:00
parent 7b1425e544
commit 0280dd324b
32 changed files with 4212 additions and 3058 deletions

View File

@ -24,6 +24,8 @@
<ClCompile Include="..\..\..\sdk\webservice\HttpUser.cpp" /> <ClCompile Include="..\..\..\sdk\webservice\HttpUser.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\main.cpp" /> <ClCompile Include="..\..\..\sdk\webservice\main.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\Manager.cpp" /> <ClCompile Include="..\..\..\sdk\webservice\Manager.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\ManagerV1.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\ManagerV2.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\MsgPumpCallback.cpp" /> <ClCompile Include="..\..\..\sdk\webservice\MsgPumpCallback.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\SockIoServer.cpp" /> <ClCompile Include="..\..\..\sdk\webservice\SockIoServer.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\SockIoUser.cpp" /> <ClCompile Include="..\..\..\sdk\webservice\SockIoUser.cpp" />
@ -41,6 +43,8 @@
<ClInclude Include="..\..\..\sdk\webservice\HttpServer.h" /> <ClInclude Include="..\..\..\sdk\webservice\HttpServer.h" />
<ClInclude Include="..\..\..\sdk\webservice\HttpUser.h" /> <ClInclude Include="..\..\..\sdk\webservice\HttpUser.h" />
<ClInclude Include="..\..\..\sdk\webservice\Manager.h" /> <ClInclude Include="..\..\..\sdk\webservice\Manager.h" />
<ClInclude Include="..\..\..\sdk\webservice\ManagerV1.h" />
<ClInclude Include="..\..\..\sdk\webservice\ManagerV2.h" />
<ClInclude Include="..\..\..\sdk\webservice\Msg.h" /> <ClInclude Include="..\..\..\sdk\webservice\Msg.h" />
<ClInclude Include="..\..\..\sdk\webservice\MsgPumpCallback.h" /> <ClInclude Include="..\..\..\sdk\webservice\MsgPumpCallback.h" />
<ClInclude Include="..\..\..\sdk\webservice\SockIoServer.h" /> <ClInclude Include="..\..\..\sdk\webservice\SockIoServer.h" />

View File

@ -1,9 +1,7 @@
#include "HttpHead.h" #include "HttpHead.h"
namespace ver_1 const unsigned int asciiTableData[256] =
{ {
const unsigned int asciiTableData[256] =
{
0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 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,
@ -21,10 +19,10 @@ namespace ver_1
0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
/* the upper 128 are all zeroes */ /* the upper 128 are all zeroes */
}; };
static void TrimString(std::string& str) static void TrimString(std::string& str)
{ {
std::string str1; std::string str1;
bool add1 = false; bool add1 = false;
std::string::const_iterator iter1; std::string::const_iterator iter1;
@ -78,67 +76,67 @@ namespace ver_1
} }
str = std::string(str2.rbegin(), str2.rend()); str = std::string(str2.rbegin(), str2.rend());
} }
HttpHead::HttpHead() HttpHead::HttpHead()
{ {
} }
HttpHead::~HttpHead() HttpHead::~HttpHead()
{ {
} }
bool HttpHead::Parse(const std::string& head) bool HttpHead::Parse(const std::string& head)
{ {
AnalysisHead(head, m_requestMethod, m_requestURIPath, m_requestURIQueryInfos, AnalysisHead(head, m_requestMethod, m_requestURIPath, m_requestURIQueryInfos,
m_requestURIFragment, m_requestHttpVersion, m_headInfos); m_requestURIFragment, m_requestHttpVersion, m_headInfos);
return true; return true;
} }
void HttpHead::Clear() void HttpHead::Clear()
{ {
m_requestMethod.clear(); m_requestMethod.clear();
m_requestURIPath.clear(); m_requestURIPath.clear();
m_requestURIQueryInfos.clear(); m_requestURIQueryInfos.clear();
m_requestURIFragment.clear(); m_requestURIFragment.clear();
m_requestHttpVersion.clear(); m_requestHttpVersion.clear();
m_headInfos.clear(); m_headInfos.clear();
} }
std::string HttpHead::GetRequestMethod() const std::string HttpHead::GetRequestMethod() const
{ {
return m_requestMethod; return m_requestMethod;
} }
std::string HttpHead::GetRequestURIPath() const std::string HttpHead::GetRequestURIPath() const
{ {
return m_requestURIPath; return m_requestURIPath;
} }
HttpPairs HttpHead::GetRequestURIQueryInfos() const HttpPairs HttpHead::GetRequestURIQueryInfos() const
{ {
return m_requestURIQueryInfos; return m_requestURIQueryInfos;
} }
std::string HttpHead::GetRequestURIFragment() const std::string HttpHead::GetRequestURIFragment() const
{ {
return m_requestURIFragment; return m_requestURIFragment;
} }
std::string HttpHead::GetRequestHttpVersion() const std::string HttpHead::GetRequestHttpVersion() const
{ {
return m_requestHttpVersion; return m_requestHttpVersion;
} }
HttpPairs HttpHead::GetHeadInfos() const HttpPairs HttpHead::GetHeadInfos() const
{ {
return m_headInfos; return m_headInfos;
} }
int HttpHead::GetContentLength() const int HttpHead::GetContentLength() const
{ {
int len = 0; int len = 0;
for (int i = 0; i < (int)m_headInfos.size(); ++i) for (int i = 0; i < (int)m_headInfos.size(); ++i)
{ {
@ -154,10 +152,10 @@ namespace ver_1
} }
return len; return len;
} }
std::string HttpHead::GetContentType() const std::string HttpHead::GetContentType() const
{ {
std::string type; std::string type;
for (int i = 0; i < (int)m_headInfos.size(); ++i) for (int i = 0; i < (int)m_headInfos.size(); ++i)
{ {
@ -173,10 +171,10 @@ namespace ver_1
} }
return type; return type;
} }
std::string HttpHead::GetValue(const HttpPairs& infos, const std::string& key) std::string HttpHead::GetValue(const HttpPairs& infos, const std::string& key)
{ {
std::string value; std::string value;
for (int i = 0; i < (int)infos.size(); ++i) for (int i = 0; i < (int)infos.size(); ++i)
{ {
@ -188,10 +186,10 @@ namespace ver_1
} }
return value; return value;
} }
void HttpHead::AnalysisURIQuery(const std::string& query, HttpPairs& queryInfos) void HttpHead::AnalysisURIQuery(const std::string& query, HttpPairs& queryInfos)
{ {
std::vector<std::string> queryList; std::vector<std::string> queryList;
char* p = new char[query.size() + 1]; char* p = new char[query.size() + 1];
@ -225,10 +223,10 @@ namespace ver_1
queryInfos.push_back(pr); queryInfos.push_back(pr);
delete[] p; delete[] p;
} }
} }
void HttpHead::AnalysisURI(const std::string& uri, std::string& path, HttpPairs& queryInfos, std::string& fragment) void HttpHead::AnalysisURI(const std::string& uri, std::string& path, HttpPairs& queryInfos, std::string& fragment)
{ {
size_t pathPos = uri.find('/'); size_t pathPos = uri.find('/');
size_t queryPos = uri.find('?'); size_t queryPos = uri.find('?');
size_t fragmentPos = uri.find('#'); size_t fragmentPos = uri.find('#');
@ -270,11 +268,11 @@ namespace ver_1
{ {
fragment = AnalyURIString(uri.substr(fragmentPos + 1)); fragment = AnalyURIString(uri.substr(fragmentPos + 1));
} }
} }
void HttpHead::AnalysisHead(const std::string& head, std::string& requestMethod, std::string& requestURIPath, void HttpHead::AnalysisHead(const std::string& head, std::string& requestMethod, std::string& requestURIPath,
HttpPairs& requestURIQueryInfos, std::string& requestURIFragment, std::string& httpVersion, HttpPairs& headInfos) HttpPairs& requestURIQueryInfos, std::string& requestURIFragment, std::string& httpVersion, HttpPairs& headInfos)
{ {
requestMethod.clear(); requestMethod.clear();
requestURIPath.clear(); requestURIPath.clear();
requestURIQueryInfos.clear(); requestURIQueryInfos.clear();
@ -342,24 +340,24 @@ namespace ver_1
delete[] p; delete[] p;
} }
} }
/*判断ascii码是否是数字0-9*/ /*判断ascii码是否是数字0-9*/
static bool asciiIsDigit(char c) static bool asciiIsDigit(char c)
{ {
/*字符的ascii码&8 结果为0-127则是数字*/ /*字符的ascii码&8 结果为0-127则是数字*/
return asciiTableData[(unsigned char)c & (1 << 3)]; return asciiTableData[(unsigned char)c & (1 << 3)];
} }
static int asciiDigitValue(char c) static int asciiDigitValue(char c)
{ {
if (asciiIsDigit(c)) if (asciiIsDigit(c))
return c - '0'; return c - '0';
return -1; return -1;
} }
static int asciiXdigitValue(char c) static int asciiXdigitValue(char c)
{ {
//printf("-->%c\n",c); //printf("-->%c\n",c);
if (c >= 'A' && c <= 'F') if (c >= 'A' && c <= 'F')
return c - 'A' + 10;//(A B C D E F)->(10 11 12 13 14 15) return c - 'A' + 10;//(A B C D E F)->(10 11 12 13 14 15)
@ -367,10 +365,10 @@ namespace ver_1
return c - 'a' + 10; return c - 'a' + 10;
return asciiDigitValue(c);//('0'...'9')->(0...9) return asciiDigitValue(c);//('0'...'9')->(0...9)
} }
static int unescapeCharacter(const char* scanner) static int unescapeCharacter(const char* scanner)
{ {
int first = asciiXdigitValue(scanner[0]); int first = asciiXdigitValue(scanner[0]);
if (first < 0) if (first < 0)
return -1; return -1;
@ -380,10 +378,10 @@ namespace ver_1
return -1; return -1;
return (first << 4) | second; //== (first*16 | second) == (first*16 + second) return (first << 4) | second; //== (first*16 | second) == (first*16 + second)
} }
static char* unescapeUriString(const char* uriString, bool asciiEscape) static char* unescapeUriString(const char* uriString, bool asciiEscape)
{ {
if (NULL == uriString) if (NULL == uriString)
return NULL; return NULL;
@ -424,10 +422,10 @@ namespace ver_1
} }
return result; return result;
} }
std::string HttpHead::AnalyURIString(const std::string& str) std::string HttpHead::AnalyURIString(const std::string& str)
{ {
std::string ret; std::string ret;
char* decodeStr = unescapeUriString(str.c_str(), false); char* decodeStr = unescapeUriString(str.c_str(), false);
if (NULL != decodeStr) if (NULL != decodeStr)
@ -437,5 +435,4 @@ namespace ver_1
} }
return ret; return ret;
}
} }

View File

@ -5,14 +5,12 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace ver_1 typedef std::pair<std::string, std::string> HttpPair;
{ typedef std::vector<HttpPair> HttpPairs;
typedef std::pair<std::string, std::string> HttpPair;
typedef std::vector<HttpPair> HttpPairs;
class HttpHead class HttpHead
{ {
public: public:
HttpHead(); HttpHead();
~HttpHead(); ~HttpHead();
@ -34,15 +32,14 @@ namespace ver_1
static void AnalysisHead(const std::string& head, std::string& requestMethod, std::string& requestURIPath, static void AnalysisHead(const std::string& head, std::string& requestMethod, std::string& requestURIPath,
HttpPairs& requestURIQueryInfos, std::string& requestURIFragment, std::string& httpVersion, HttpPairs& headInfos); HttpPairs& requestURIQueryInfos, std::string& requestURIFragment, std::string& httpVersion, HttpPairs& headInfos);
private: private:
static std::string AnalyURIString(const std::string& str); static std::string AnalyURIString(const std::string& str);
private: private:
std::string m_requestMethod; std::string m_requestMethod;
std::string m_requestURIPath; std::string m_requestURIPath;
HttpPairs m_requestURIQueryInfos; HttpPairs m_requestURIQueryInfos;
std::string m_requestURIFragment; std::string m_requestURIFragment;
std::string m_requestHttpVersion; std::string m_requestHttpVersion;
HttpPairs m_headInfos; HttpPairs m_headInfos;
}; };
}

View File

@ -4,10 +4,10 @@
namespace ver_1 namespace ver_1
{ {
HttpServer::HttpServer(HGMsgPump msgPump, Manager* manager) HttpServer::HttpServer(HGMsgPump msgPump, ManagerV1* manager)
: WebServer(msgPump, manager) : WebServer(msgPump)
{ {
m_manager = manager;
} }
HttpServer::~HttpServer() HttpServer::~HttpServer()
@ -15,6 +15,11 @@ namespace ver_1
} }
ManagerV1* HttpServer::GetManger()
{
return m_manager;
}
void HttpServer::Connect(const ConnectParam* param) void HttpServer::Connect(const ConnectParam* param)
{ {
assert(NULL != param && this == param->svr); assert(NULL != param && this == param->svr);

View File

@ -5,15 +5,17 @@
namespace ver_1 namespace ver_1
{ {
class Manager;
class HttpServer : public WebServer class HttpServer : public WebServer
{ {
public: public:
HttpServer(HGMsgPump msgPump, Manager* manager); HttpServer(HGMsgPump msgPump, ManagerV1* manager);
virtual ~HttpServer(); virtual ~HttpServer();
ManagerV1* GetManger();
void Connect(const ConnectParam* param); void Connect(const ConnectParam* param);
void Command(const HttpCmdParam* param); void Command(const HttpCmdParam* param);
private:
ManagerV1* m_manager;
}; };
} }

View File

@ -1,6 +1,6 @@
#include "HttpUser.h" #include "HttpUser.h"
#include "WebServer.h" #include "HttpServer.h"
#include "Manager.h" #include "ManagerV1.h"
#include "base/HGInfo.h" #include "base/HGInfo.h"
#include "cJSON.h" #include "cJSON.h"
@ -103,9 +103,9 @@ namespace ver_1
} }
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
HttpUser::HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn) HttpUser::HttpUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, SOCKET sockConn)
#else #else
HttpUser::HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn) HttpUser::HttpUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, int sockConn)
#endif #endif
: WebUser(server, id, ip, port, sockConn) : WebUser(server, id, ip, port, sockConn)
{ {
@ -117,6 +117,11 @@ namespace ver_1
} }
ManagerV1* HttpUser::GetManager()
{
return ((HttpServer*)m_server)->GetManger();
}
void HttpUser::HandleCmd(const HttpCmdParam* param) void HttpUser::HandleCmd(const HttpCmdParam* param)
{ {
assert(NULL != param && m_id == param->usrId); assert(NULL != param && m_id == param->usrId);

View File

@ -5,18 +5,17 @@
namespace ver_1 namespace ver_1
{ {
class WebServer;
class HttpUser : public WebUser class HttpUser : public WebUser
{ {
public: public:
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn); HttpUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, SOCKET sockConn);
#else #else
HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn); HttpUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, int sockConn);
#endif #endif
virtual ~HttpUser(); virtual ~HttpUser();
ManagerV1* GetManager();
void HandleCmd(const HttpCmdParam* param); void HandleCmd(const HttpCmdParam* param);
protected: protected:

File diff suppressed because it is too large Load Diff

View File

@ -2,193 +2,14 @@
#include "base/HGDef.h" #include "base/HGDef.h"
#include "base/HGInc.h" #include "base/HGInc.h"
#include "base/HGLock.h"
#include "base/HGImage.h"
#include "base/HGMsgPump.h" #include "base/HGMsgPump.h"
#include "sane/sane_ex.h"
#include "sane/sane_option_definitions.h"
#include "Msg.h"
#include <string>
namespace ver_1 class Manager
{ {
enum public:
{
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(HGMsgPump msgPump);
~Manager(); virtual ~Manager();
// 打开设备 protected:
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; 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;
};
}

2082
sdk/webservice/ManagerV1.cpp Normal file

File diff suppressed because it is too large Load Diff

194
sdk/webservice/ManagerV1.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 "Manager.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 ManagerV1 : public Manager
{
public:
ManagerV1(HGMsgPump msgPump);
virtual ~ManagerV1();
// 打开设备
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:
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;
};
}

View File

@ -0,0 +1,306 @@
#include "ManagerV2.h"
#include "base/HGBuffer.h"
#include "base/HGBase64.h"
#include "base/HGUtility.h"
#include "base/HGInfo.h"
#include "imgfmt/HGJpeg.h"
#include "imgfmt/HGOfd.h"
#include "imgfmt/HGPdf.h"
#include "imgfmt/HGTiff.h"
#include "imgfmt/HGImgFmt.h"
#include "HGString.h"
namespace ver_2
{
ManagerV2::ManagerV2(HGMsgPump msgPump)
: Manager(msgPump)
{
}
ManagerV2::~ManagerV2()
{
}
void ManagerV2::ScanFinish(const ScanFinishParam* param)
{
assert(NULL != param && this == param->mgr);
}
int ManagerV2::SetGlobalConfig(const GlobalConfig& cfg)
{
if ("date_time" != cfg.fileNameMode && "random" != cfg.fileNameMode)
return -1;
if ("jpg" != cfg.imageFormat && "bmp" != cfg.imageFormat && "png" != cfg.imageFormat && "tif" != cfg.imageFormat
&& "pdf" != cfg.imageFormat && "ofd" != cfg.imageFormat && "ocr-pdf" != cfg.imageFormat && "ocr-ofd" != cfg.imageFormat)
return -1;
if (cfg.imageJpegQuality < 0 || cfg.imageJpegQuality > 100)
return -1;
if ("none" != cfg.imageTiffCompression && "lzw" != cfg.imageTiffCompression && "jpeg" != cfg.imageTiffCompression)
return -1;
if (cfg.imageTiffJpegQuality < 0 || cfg.imageTiffJpegQuality > 100)
return -1;
SetCfgStringValue("global", "fileSavePath", Utf8ToStdString(cfg.fileSavePath));
SetCfgStringValue("global", "fileNamePrefix", Utf8ToStdString(cfg.fileNamePrefix));
SetCfgStringValue("global", "fileNameMode", cfg.fileNameMode);
SetCfgStringValue("global", "imageFormat", cfg.imageFormat);
SetCfgIntValue("global", "imageJpegQuality", cfg.imageJpegQuality);
SetCfgStringValue("global", "imageTiffCompression", cfg.imageTiffCompression);
SetCfgIntValue("global", "imageTiffJpegQuality", cfg.imageTiffJpegQuality);
SetCfgStringValue("global", "uploadHttpHost", cfg.uploadHttpHost);
SetCfgIntValue("global", "uploadHttpPort", cfg.uploadHttpPort);
SetCfgStringValue("global", "uploadHttpPath", cfg.uploadHttpPath);
SetCfgStringValue("global", "uploadFtpUser", cfg.uploadFtpUser);
SetCfgStringValue("global", "uploadFtpPassword", cfg.uploadFtpPassword);
SetCfgStringValue("global", "uploadFtpHost", cfg.uploadFtpHost);
SetCfgIntValue("global", "uploadFtpPort", cfg.uploadFtpPort);
return 0;
}
int ManagerV2::GetGlobalConfig(GlobalConfig& cfg)
{
HGChar docPath[256];
HGBase_GetDocumentsPath(docPath, 256);
cfg.fileSavePath = StdStringToUtf8(GetCfgStringValue("global", "fileSavePath", docPath));
cfg.fileNamePrefix = StdStringToUtf8(GetCfgStringValue("global", "fileNamePrefix", "Huago"));
cfg.fileNameMode = GetCfgStringValue("global", "fileNameMode", "date_time");
cfg.imageFormat = GetCfgStringValue("global", "imageFormat", "jpg");
cfg.imageJpegQuality = GetCfgIntValue("global", "imageJpegQuality", 80);
cfg.imageTiffCompression = GetCfgStringValue("global", "imageTiffCompression", "lzw");
cfg.imageTiffJpegQuality = GetCfgIntValue("global", "imageTiffJpegQuality", 80);
cfg.uploadHttpHost = GetCfgStringValue("global", "uploadHttpHost", "");
cfg.uploadHttpPort = GetCfgIntValue("global", "uploadHttpPort", 80);
cfg.uploadHttpPath = GetCfgStringValue("global", "uploadHttpPath", "/upload.cgi");
cfg.uploadFtpUser = GetCfgStringValue("global", "uploadFtpUser", "");
cfg.uploadFtpPassword = GetCfgStringValue("global", "uploadFtpPassword", "");
cfg.uploadFtpHost = GetCfgStringValue("global", "uploadFtpHost", "");
cfg.uploadFtpPort = GetCfgIntValue("global", "uploadFtpPort", 21);
return 0;
}
int ManagerV2::LoadLocalImage(const std::string& imagePath, std::string& imgBase64)
{
imgBase64.clear();
HGUInt imgType = 0;
HGImgFmt_GetImgFmtType(Utf8ToStdString(imagePath).c_str(), &imgType);
if (0 == imgType)
return -1;
std::string prefix = "data:image/jpeg;base64,";
if (HGIMGFMT_TYPE_BMP == imgType)
prefix = "data:image/bmp;base64,";
else if (HGIMGFMT_TYPE_PNG == imgType)
prefix = "data:image/png;base64,";
else if (HGIMGFMT_TYPE_TIFF == imgType)
prefix = "data:image/tiff;base64,";
else if (HGIMGFMT_TYPE_PDF == imgType)
prefix = "data:image/pdf;base64,";
else if (HGIMGFMT_TYPE_OFD == imgType)
prefix = "data:image/ofd;base64,";
int ret = GetBase64(Utf8ToStdString(imagePath), imgBase64);
if (0 != ret)
return ret;
imgBase64.insert(0, prefix);
return 0;
}
int ManagerV2::SaveLocalImage(const std::string& imgBase64, std::string& imagePath)
{
imagePath.clear();
size_t pos = imgBase64.find(',');
if (std::string::npos == pos)
return -1;
std::string prefix = imgBase64.substr(0, pos + 1);
std::string suffix = "jpg";
if ("data:image/bmp;base64," == prefix)
suffix = "bmp";
else if ("data:image/png;base64," == prefix)
suffix = "png";
else if ("data:image/tiff;base64," == prefix)
suffix = "tif";
else if ("data:image/pdf;base64," == prefix)
suffix = "pdf";
else if ("data:image/ofd;base64," == prefix)
suffix = "ofd";
std::string imagePath2 = GetFilePath(suffix);
int ret = SaveBase64(imagePath2, imagePath.c_str() + pos + 1);
if (0 == ret)
imagePath = StdStringToUtf8(imagePath2);
return ret;
}
int ManagerV2::DeleteLocalFile(const std::string& filePath)
{
return 0;
}
std::string ManagerV2::GetCfgStringValue(const std::string& app, const std::string& key, const std::string& def)
{
HGChar cfgPath[256];
HGBase_GetConfigPath(cfgPath, 256);
strcat(cfgPath, "config2.ini");
HGChar val[256] = { 0 };
HGBase_GetProfileString(cfgPath, app.c_str(), key.c_str(), def.c_str(), val, 256);
return val;
}
int ManagerV2::GetCfgIntValue(const std::string& app, const std::string& key, int def)
{
HGChar cfgPath[256];
HGBase_GetConfigPath(cfgPath, 256);
strcat(cfgPath, "config2.ini");
HGInt val = 0;
HGBase_GetProfileInt(cfgPath, app.c_str(), key.c_str(), def, &val);
return val;
}
bool ManagerV2::GetCfgBoolValue(const std::string& app, const std::string& key, bool def)
{
HGChar cfgPath[256];
HGBase_GetConfigPath(cfgPath, 256);
strcat(cfgPath, "config2.ini");
HGInt val = 0;
HGBase_GetProfileInt(cfgPath, app.c_str(), key.c_str(), (HGInt)def, &val);
return (bool)val;
}
bool ManagerV2::SetCfgStringValue(const std::string& app, const std::string& key, const std::string& val)
{
HGChar cfgPath[256];
HGBase_GetConfigPath(cfgPath, 256);
HGBase_CreateDir(cfgPath);
strcat(cfgPath, "config2.ini");
return (HGBASE_ERR_OK == HGBase_SetProfileString(cfgPath, app.c_str(), key.c_str(), val.c_str()));
}
bool ManagerV2::SetCfgIntValue(const std::string& app, const std::string& key, int val)
{
HGChar cfgPath[256];
HGBase_GetConfigPath(cfgPath, 256);
HGBase_CreateDir(cfgPath);
strcat(cfgPath, "config2.ini");
return (HGBASE_ERR_OK == HGBase_SetProfileInt(cfgPath, app.c_str(), key.c_str(), val));
}
bool ManagerV2::SetCfgBoolValue(const std::string& app, const std::string& key, bool val)
{
HGChar cfgPath[256];
HGBase_GetConfigPath(cfgPath, 256);
HGBase_CreateDir(cfgPath);
strcat(cfgPath, "config2.ini");
return (HGBASE_ERR_OK == HGBase_SetProfileInt(cfgPath, app.c_str(), key.c_str(), (HGInt)val));
}
int ManagerV2::GetBase64(const std::string& fileName, std::string& base64)
{
base64.clear();
int ret = -1;
FILE* file = fopen(fileName.c_str(), "rb");
if (NULL != file)
{
fseek(file, 0, SEEK_END);
long size = ftell(file);
fseek(file, 0, SEEK_SET);
if (size > 0)
{
HGByte* data = new HGByte[size];
long readLen = (long)fread(data, 1, size, file);
if (readLen == size)
{
HGSize base64Size = 0;
HGBase_Base64Encode(data, size, NULL, &base64Size);
uint8_t* base64Data = new uint8_t[base64Size + 1];
HGBase_Base64Encode(data, size, base64Data, &base64Size);
base64Data[base64Size] = 0;
base64 = (const char*)base64Data;
delete[] base64Data;
ret = 0;
}
delete[] data;
}
fclose(file);
}
return ret;
}
int ManagerV2::SaveBase64(const std::string& fileName, const std::string& base64)
{
if (base64.empty())
return -1;
const char* base64Data = base64.c_str();
size_t base64Size = base64.size();
int ret = -1;
FILE* file = fopen(fileName.c_str(), "wb");
if (NULL != file)
{
HGSize size = 0;
HGBase_Base64Decode((const HGByte*)base64Data, (HGSize)base64Size, NULL, &size);
uint8_t* data = new HGByte[size];
HGBase_Base64Decode((const HGByte*)base64Data, (HGSize)base64Size, data, &size);
size_t writeLen = fwrite(data, 1, size, file);
if (writeLen == (size_t)size)
ret = 0;
delete[] data;
fclose(file);
}
return ret;
}
std::string ManagerV2::GetFilePath(const std::string& suffix)
{
HGChar docPath[256];
HGBase_GetDocumentsPath(docPath, 256);
std::string fileSavePath = GetCfgStringValue("global", "fileSavePath", docPath);
std::string fileNamePrefix = GetCfgStringValue("global", "fileNamePrefix", "Huago");
std::string fileNameMode = GetCfgStringValue("global", "fileNameMode", "date_time");
char filePath[256] = { 0 };
if ("random" == fileNameMode)
{
HGChar uuid[256];
HGBase_GetUuid(uuid, 256);
sprintf(filePath, "%s%s%s.%s", fileSavePath.c_str(), fileNamePrefix.c_str(), uuid, suffix.c_str());
}
else
{
timeb tb;
ftime(&tb);
struct tm* p = localtime(&tb.time);
sprintf(filePath, "%s%s%04d%02d%02d%02d%02d%02d.%s", fileSavePath.c_str(), fileNamePrefix.c_str(), (1900 + p->tm_year),
(1 + p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, suffix.c_str());
}
return filePath;
}
std::string ManagerV2::GetImagePath()
{
std::string imageFormat = GetCfgStringValue("global", "imageFormat", "jpg");
return GetFilePath(imageFormat);
}
}

View File

@ -0,0 +1,72 @@
#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 "Manager.h"
#include "Msg.h"
#include <string>
namespace ver_2
{
struct GlobalConfig
{
// 文件保存
std::string fileSavePath;
std::string fileNamePrefix;
std::string fileNameMode;
// 图像保存
std::string imageFormat;
int imageJpegQuality;
std::string imageTiffCompression;
int imageTiffJpegQuality;
// 上传
std::string uploadHttpHost;
unsigned short uploadHttpPort;
std::string uploadHttpPath;
std::string uploadFtpUser;
std::string uploadFtpPassword;
std::string uploadFtpHost;
unsigned short uploadFtpPort;
};
class ManagerV2 : public Manager
{
public:
ManagerV2(HGMsgPump msgPump);
virtual ~ManagerV2();
// 扫描完成
void ScanFinish(const ScanFinishParam* param);
// 设置全局配置
int SetGlobalConfig(const GlobalConfig& cfg);
// 获取全局配置
int GetGlobalConfig(GlobalConfig& cfg);
// 加载本地图像
int LoadLocalImage(const std::string& imagePath, std::string& imgBase64);
// 保存本地图像
int SaveLocalImage(const std::string& imgBase64, std::string& imagePath);
// 删除本地文件
int DeleteLocalFile(const std::string& filePath);
private:
std::string GetCfgStringValue(const std::string& app, const std::string& key, const std::string& def);
int GetCfgIntValue(const std::string& app, const std::string& key, int def);
bool GetCfgBoolValue(const std::string& app, const std::string& key, bool def);
bool SetCfgStringValue(const std::string& app, const std::string& key, const std::string& val);
bool SetCfgIntValue(const std::string& app, const std::string& key, int val);
bool SetCfgBoolValue(const std::string& app, const std::string& key, bool val);
static int GetBase64(const std::string& fileName, std::string& base64);
static int SaveBase64(const std::string& fileName, const std::string& base64);
std::string GetFilePath(const std::string& suffix);
std::string GetImagePath();
private:
};
}

View File

@ -2,61 +2,66 @@
#include "base/HGDef.h" #include "base/HGDef.h"
#include "base/HGInc.h" #include "base/HGInc.h"
#include "HttpHead.h"
#include <string> #include <string>
#include "HttpHead.h"
namespace ver_1 enum
{ {
enum MSGID_CONNECT = 1L,
{ MSGID_DISCONNECT
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 WebServer;
class HttpServer;
class SockIoServer;
struct OpenDevParam struct ConnectParam
{ {
Manager* mgr;
std::string devName;
};
struct CloseDevParam
{
Manager* mgr;
std::string devName;
};
struct ScanFinishParam
{
Manager* mgr;
};
struct ConnectParam
{
WebServer* svr; WebServer* svr;
HGChar ip[16]; std::string ip;
HGUShort port; HGUShort port;
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
SOCKET socket; SOCKET socket;
#else #else
int socket; int socket;
#endif #endif
}; };
struct DisConnectParam struct DisConnectParam
{ {
WebServer* svr; WebServer* svr;
HGUInt usrId; HGUInt usrId;
};
namespace ver_1
{
enum
{
MSGID_OPEN_DEVICE = 3L,
MSGID_CLOSE_DEVICE,
MSGID_SCAN_FINISH,
MSGID_HTTP_COMMAND,
MSGID_SOCKIO_COMMAND,
MSGID_SOCKIO_EVENT
};
class ManagerV1;
class HttpServer;
class SockIoServer;
struct OpenDevParam
{
ManagerV1* mgr;
std::string devName;
};
struct CloseDevParam
{
ManagerV1* mgr;
std::string devName;
};
struct ScanFinishParam
{
ManagerV1* mgr;
}; };
struct HttpCmdParam struct HttpCmdParam
@ -84,3 +89,37 @@ namespace ver_1
HGUInt size; HGUInt size;
}; };
} }
namespace ver_2
{
enum
{
MSGID_SCAN_FINISH = 3L,
MSGID_WS_COMMAND,
MSGID_WS_EVENT
};
class ManagerV2;
class WSServer;
struct ScanFinishParam
{
ManagerV2* mgr;
};
struct WSCmdParam
{
WSServer* svr;
HGUInt usrId;
HGByte* data;
HGUInt size;
};
struct WSEvtParam
{
WSServer* svr;
HGUInt usrId;
HGByte* data;
HGUInt size;
};
}

View File

@ -1,9 +1,11 @@
#include "MsgPumpCallback.h" #include "MsgPumpCallback.h"
#include "base/HGDef.h" #include "base/HGDef.h"
#include "base/HGInc.h" #include "base/HGInc.h"
#include "Manager.h" #include "ManagerV1.h"
#include "ManagerV2.h"
#include "HttpServer.h" #include "HttpServer.h"
#include "SockIoServer.h" #include "SockIoServer.h"
#include "WSServer.h"
namespace ver_1 namespace ver_1
{ {
@ -15,6 +17,20 @@ namespace ver_1
switch (msg->id) switch (msg->id)
{ {
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_OPEN_DEVICE: case MSGID_OPEN_DEVICE:
{ {
OpenDevParam* param = (OpenDevParam*)msg->data; OpenDevParam* param = (OpenDevParam*)msg->data;
@ -36,20 +52,6 @@ namespace ver_1
delete param; delete param;
} }
break; 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: case MSGID_HTTP_COMMAND:
{ {
HttpCmdParam* param = (HttpCmdParam*)msg->data; HttpCmdParam* param = (HttpCmdParam*)msg->data;
@ -74,3 +76,53 @@ namespace ver_1
} }
} }
} }
namespace ver_2
{
void HGMsgPumpCallback(HGMsgPump msgPump, const HGMsg* msg, HGPointer param)
{
(void)msgPump;
(void)param;
assert(NULL != msg);
switch (msg->id)
{
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_SCAN_FINISH:
{
ScanFinishParam* param = (ScanFinishParam*)msg->data;
param->mgr->ScanFinish(param);
delete param;
}
break;
case MSGID_WS_COMMAND:
{
WSCmdParam* param = (WSCmdParam*)msg->data;
param->svr->Command(param);
delete param;
}
break;
case MSGID_WS_EVENT:
{
WSEvtParam* param = (WSEvtParam*)msg->data;
param->svr->Event(param);
delete param;
}
break;
}
}
}

View File

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

View File

@ -4,10 +4,10 @@
namespace ver_1 namespace ver_1
{ {
SockIoServer::SockIoServer(HGMsgPump msgPump, Manager* manager) SockIoServer::SockIoServer(HGMsgPump msgPump, ManagerV1* manager)
: WebServer(msgPump, manager) : WebServer(msgPump)
{ {
m_manager = manager;
} }
SockIoServer::~SockIoServer() SockIoServer::~SockIoServer()
@ -15,6 +15,11 @@ namespace ver_1
} }
ManagerV1* SockIoServer::GetManger()
{
return m_manager;
}
void SockIoServer::Connect(const ConnectParam* param) void SockIoServer::Connect(const ConnectParam* param)
{ {
assert(NULL != param && this == param->svr); assert(NULL != param && this == param->svr);

View File

@ -5,16 +5,18 @@
namespace ver_1 namespace ver_1
{ {
class Manager;
class SockIoServer : public WebServer class SockIoServer : public WebServer
{ {
public: public:
SockIoServer(HGMsgPump msgPump, Manager* manager); SockIoServer(HGMsgPump msgPump, ManagerV1* manager);
virtual ~SockIoServer(); virtual ~SockIoServer();
ManagerV1* GetManger();
void Connect(const ConnectParam* param); void Connect(const ConnectParam* param);
void Command(const SockIoCmdParam* param); void Command(const SockIoCmdParam* param);
void Event(const SockIoEvtParam* param); void Event(const SockIoEvtParam* param);
private:
ManagerV1* m_manager;
}; };
} }

View File

@ -1,18 +1,18 @@
#include "SockIoUser.h" #include "SockIoUser.h"
#include "WebServer.h" #include "SockIoServer.h"
#include "Manager.h" #include "ManagerV1.h"
#include "base/HGInfo.h" #include "base/HGInfo.h"
#include "base/HGUtility.h" #include "base/HGUtility.h"
#include "cJSON.h"
#include "sha1.h" #include "sha1.h"
#include "base64.h" #include "base64.h"
#include "cJSON.h"
namespace ver_1 namespace ver_1
{ {
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
SockIoUser::SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn) SockIoUser::SockIoUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, SOCKET sockConn)
#else #else
SockIoUser::SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn) SockIoUser::SockIoUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, int sockConn)
#endif #endif
: WebUser(server, id, ip, port, sockConn) : WebUser(server, id, ip, port, sockConn)
{ {
@ -24,6 +24,11 @@ namespace ver_1
GetManager()->ResetScanEvent(); GetManager()->ResetScanEvent();
} }
ManagerV1* SockIoUser::GetManager()
{
return ((SockIoServer*)m_server)->GetManger();
}
void SockIoUser::HandleCmd(const SockIoCmdParam* param) void SockIoUser::HandleCmd(const SockIoCmdParam* param)
{ {
assert(NULL != param && m_id == param->usrId); assert(NULL != param && m_id == param->usrId);

View File

@ -5,18 +5,17 @@
namespace ver_1 namespace ver_1
{ {
class WebServer;
class SockIoUser : public WebUser class SockIoUser : public WebUser
{ {
public: public:
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn); SockIoUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, SOCKET sockConn);
#else #else
SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn); SockIoUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, int sockConn);
#endif #endif
virtual ~SockIoUser(); virtual ~SockIoUser();
ManagerV1* GetManager();
void HandleCmd(const SockIoCmdParam* param); void HandleCmd(const SockIoCmdParam* param);
void HandleEvent(const SockIoEvtParam* param); void HandleEvent(const SockIoEvtParam* param);

View File

@ -0,0 +1,54 @@
#include "WSServer.h"
#include "WSUser.h"
#include "base/HGInfo.h"
namespace ver_2
{
WSServer::WSServer(HGMsgPump msgPump, ManagerV2* manager)
: WebServer(msgPump)
{
m_manager = manager;
}
WSServer::~WSServer()
{
}
ManagerV2* WSServer::GetManger()
{
return m_manager;
}
void WSServer::Connect(const ConnectParam* param)
{
assert(NULL != param && this == param->svr);
WebUser* user = new WSUser(this, m_currUserId, param->ip, param->port, param->socket);
user->Open();
++m_currUserId;
m_vectorUser.push_back(user);
}
void WSServer::Command(const WSCmdParam* param)
{
assert(NULL != param && this == param->svr);
int nIndex = GetUserIndex(param->usrId);
if (-1 != nIndex)
{
((WSUser*)m_vectorUser[nIndex])->HandleCmd(param);
}
}
void WSServer::Event(const WSEvtParam* param)
{
assert(NULL != param && this == param->svr);
int nIndex = GetUserIndex(param->usrId);
if (-1 != nIndex)
{
((WSUser*)m_vectorUser[nIndex])->HandleEvent(param);
}
}
}

View File

@ -0,0 +1,22 @@
#pragma once
#include "WebServer.h"
#include "Msg.h"
namespace ver_2
{
class WSServer : public WebServer
{
public:
WSServer(HGMsgPump msgPump, ManagerV2* manager);
virtual ~WSServer();
ManagerV2* GetManger();
void Connect(const ConnectParam* param);
void Command(const WSCmdParam* param);
void Event(const WSEvtParam* param);
private:
ManagerV2* m_manager;
};
}

View File

@ -0,0 +1,549 @@
#include "WSUser.h"
#include "WSServer.h"
#include "ManagerV2.h"
#include "base/HGInfo.h"
#include "cJSON.h"
#include "sha1.h"
#include "base64.h"
namespace ver_2
{
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)
WSUser::WSUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, SOCKET sockConn)
#else
WSUser::WSUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, int sockConn)
#endif
: WebUser(server, id, ip, port, sockConn)
{
}
WSUser::~WSUser()
{
}
ManagerV2* WSUser::GetManager()
{
return ((WSServer*)m_server)->GetManger();
}
void WSUser::HandleCmd(const WSCmdParam* param)
{
assert(NULL != param && m_id == param->usrId);
std::string cmdData((const char*)param->data, param->size);
cJSON* json = cJSON_Parse(cmdData.c_str());
if (NULL != json)
{
std::string func = GetJsonStringValue(json, "func");
if ("set_global_config" == func)
{
}
else if ("get_global_config" == func)
{
}
else if ("load_local_image" == func)
{
}
cJSON_Delete(json);
}
}
void WSUser::HandleEvent(const WSEvtParam* param)
{
assert(NULL != param && m_id == param->usrId);
SendResponse(param->data, param->size, HGTRUE);
}
void WSUser::PostCmdMsg(const HGByte* data, HGUInt dataSize)
{
WSCmdParam* param = new WSCmdParam;
param->svr = (WSServer*)m_server;
param->usrId = m_id;
param->data = new HGByte[dataSize];
param->size = dataSize;
memcpy(param->data, data, dataSize);
HGMsg msg;
msg.id = MSGID_WS_COMMAND;
msg.data = param;
if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_server->GetMsgPump(), &msg))
{
delete[] param->data;
param->size = 0;
delete param;
}
}
void WSUser::PostEventMsg(const HGByte* data, HGUInt dataSize)
{
WSEvtParam* param = new WSEvtParam;
param->svr = (WSServer*)m_server;
param->usrId = m_id;
param->data = new HGByte[dataSize];
param->size = dataSize;
memcpy(param->data, data, dataSize);
HGMsg msg;
msg.id = MSGID_WS_EVENT;
msg.data = param;
if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_server->GetMsgPump(), &msg))
{
delete[] param->data;
param->size = 0;
delete param;
}
}
void WSUser::ThreadFunc()
{
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "WSUser::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)
{
PostCmdMsg(&vAllData[0], vAllData.size());
bHandle = false;
vAllData.clear();
}
}
}
}
}
}
if (NULL != pData)
{
delete[] pData;
pData = NULL;
nDataSize = 0;
nMaskCount = 0;
}
}
bool WSUser::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 ("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);
return true;
}
void WSUser::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 WSUser::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,32 @@
#pragma once
#include "WebUser.h"
#include "Msg.h"
namespace ver_2
{
class WSUser : public WebUser
{
public:
#if defined(HG_CMP_MSC)
WSUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, SOCKET sockConn);
#else
WSUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, int sockConn);
#endif
virtual ~WSUser();
ManagerV2* GetManager();
void HandleCmd(const WSCmdParam* param);
void HandleEvent(const WSEvtParam* param);
protected:
void PostCmdMsg(const HGByte* data, HGUInt dataSize);
void PostEventMsg(const HGByte* data, HGUInt dataSize);
virtual void ThreadFunc();
private:
bool ShakeHand(const std::string& head);
void Pong();
bool SendResponse(const HGByte* data, HGUInt size, HGBool text);
};
}

View File

@ -2,12 +2,9 @@
#include "WebUser.h" #include "WebUser.h"
#include "base/HGInfo.h" #include "base/HGInfo.h"
namespace ver_1 WebServer::WebServer(HGMsgPump msgPump)
{ {
WebServer::WebServer(HGMsgPump msgPump, Manager* manager)
{
m_msgPump = msgPump; m_msgPump = msgPump;
m_manager = manager;
m_currUserId = 1; m_currUserId = 1;
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
@ -16,25 +13,20 @@ namespace ver_1
m_sockServer = -1; m_sockServer = -1;
#endif #endif
m_listenThread = NULL; m_listenThread = NULL;
} }
WebServer::~WebServer() WebServer::~WebServer()
{ {
} }
HGMsgPump WebServer::GetMsgPump() HGMsgPump WebServer::GetMsgPump()
{ {
return m_msgPump; return m_msgPump;
} }
Manager* WebServer::GetManager() bool WebServer::Open(HGUShort port)
{ {
return m_manager;
}
bool WebServer::Open(HGUShort port)
{
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
if (INVALID_SOCKET != m_sockServer) if (INVALID_SOCKET != m_sockServer)
#else #else
@ -97,10 +89,10 @@ namespace ver_1
assert(NULL != m_listenThread); assert(NULL != m_listenThread);
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "open webserver success, port=%u", port); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "open webserver success, port=%u", port);
return true; return true;
} }
bool WebServer::Close() bool WebServer::Close()
{ {
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
if (INVALID_SOCKET == m_sockServer) if (INVALID_SOCKET == m_sockServer)
#else #else
@ -128,10 +120,10 @@ namespace ver_1
HGBase_CloseThread(m_listenThread); HGBase_CloseThread(m_listenThread);
m_listenThread = NULL; m_listenThread = NULL;
return true; return true;
} }
void WebServer::DisConnect(const DisConnectParam* param) void WebServer::DisConnect(const DisConnectParam* param)
{ {
assert(NULL != param && this == param->svr); assert(NULL != param && this == param->svr);
int nIndex = GetUserIndex(param->usrId); int nIndex = GetUserIndex(param->usrId);
@ -142,17 +134,17 @@ namespace ver_1
delete pUser; delete pUser;
pUser = NULL; pUser = NULL;
} }
} }
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
void WebServer::PostConnectMsg(const char* ip, uint16_t port, SOCKET sockConn) void WebServer::PostConnectMsg(const std::string& ip, uint16_t port, SOCKET sockConn)
#else #else
void WebServer::PostConnectMsg(const char* ip, uint16_t port, int sockConn) void WebServer::PostConnectMsg(const std::string& ip, uint16_t port, int sockConn)
#endif #endif
{ {
ConnectParam* param = new ConnectParam; ConnectParam* param = new ConnectParam;
param->svr = this; param->svr = this;
strcpy(param->ip, ip); param->ip = ip;
param->port = port; param->port = port;
param->socket = sockConn; param->socket = sockConn;
@ -168,10 +160,10 @@ namespace ver_1
#endif #endif
delete param; delete param;
} }
} }
int WebServer::GetUserIndex(HGUInt id) int WebServer::GetUserIndex(HGUInt id)
{ {
int nIndex = -1; int nIndex = -1;
for (int i = 0; i < (int)m_vectorUser.size(); ++i) for (int i = 0; i < (int)m_vectorUser.size(); ++i)
{ {
@ -183,10 +175,10 @@ namespace ver_1
} }
return nIndex; return nIndex;
} }
void WebServer::ThreadFunc(HGThread thread, HGPointer param) void WebServer::ThreadFunc(HGThread thread, HGPointer param)
{ {
WebServer* p = (WebServer*)param; WebServer* p = (WebServer*)param;
while (1) while (1)
@ -209,5 +201,4 @@ namespace ver_1
p->PostConnectMsg(inet_ntoa(addrClient.sin_addr), ntohs(addrClient.sin_port), socketConn); p->PostConnectMsg(inet_ntoa(addrClient.sin_addr), ntohs(addrClient.sin_port), socketConn);
} }
}
} }

View File

@ -7,37 +7,30 @@
#include "Msg.h" #include "Msg.h"
#include <vector> #include <vector>
namespace ver_1 class WebServer
{ {
class Manager; public:
WebServer(HGMsgPump msgPump);
class WebServer
{
public:
WebServer(HGMsgPump msgPump, Manager* manager);
virtual ~WebServer(); virtual ~WebServer();
HGMsgPump GetMsgPump(); HGMsgPump GetMsgPump();
Manager* GetManager();
bool Open(HGUShort port); bool Open(HGUShort port);
bool Close(); bool Close();
virtual void Connect(const ConnectParam* param) = 0; virtual void Connect(const ConnectParam* param) = 0;
void DisConnect(const DisConnectParam* param); void DisConnect(const DisConnectParam* param);
protected: protected:
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
void PostConnectMsg(const char* ip, uint16_t port, SOCKET sockConn); void PostConnectMsg(const std::string &ip, uint16_t port, SOCKET sockConn);
#else #else
void PostConnectMsg(const char* ip, uint16_t port, int sockConn); void PostConnectMsg(const std::string &ip, uint16_t port, int sockConn);
#endif #endif
int GetUserIndex(HGUInt id); int GetUserIndex(HGUInt id);
static void ThreadFunc(HGThread thread, HGPointer param); static void ThreadFunc(HGThread thread, HGPointer param);
protected: protected:
HGMsgPump m_msgPump; HGMsgPump m_msgPump;
Manager* m_manager;
HGUInt m_currUserId; HGUInt m_currUserId;
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
@ -47,5 +40,4 @@ namespace ver_1
#endif #endif
HGThread m_listenThread; HGThread m_listenThread;
std::vector<class WebUser*> m_vectorUser; std::vector<class WebUser*> m_vectorUser;
}; };
}

View File

@ -2,26 +2,24 @@
#include "WebServer.h" #include "WebServer.h"
#include "base/HGInfo.h" #include "base/HGInfo.h"
namespace ver_1
{
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
WebUser::WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn) WebUser::WebUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, SOCKET sockConn)
#else #else
WebUser::WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn) WebUser::WebUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, int sockConn)
#endif #endif
{ {
m_server = server; m_server = server;
HGBase_CreateLock(&m_cs); HGBase_CreateLock(&m_cs);
m_id = id; m_id = id;
strcpy(m_ip, ip); m_ip = ip;
m_port = port; m_port = port;
m_sockConn = sockConn; m_sockConn = sockConn;
m_thread = NULL; m_thread = NULL;
} }
WebUser::~WebUser() WebUser::~WebUser()
{ {
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
closesocket(m_sockConn); closesocket(m_sockConn);
m_sockConn = INVALID_SOCKET; m_sockConn = INVALID_SOCKET;
@ -33,30 +31,25 @@ namespace ver_1
m_thread = NULL; m_thread = NULL;
m_port = 0; m_port = 0;
memset(m_ip, 0, sizeof(m_ip)); m_ip.clear();
m_id = 0; m_id = 0;
HGBase_DestroyLock(m_cs); HGBase_DestroyLock(m_cs);
m_server = NULL; m_server = NULL;
} }
void WebUser::Open() void WebUser::Open()
{ {
HGBase_OpenThread(ThreadFunc, this, &m_thread); HGBase_OpenThread(ThreadFunc, this, &m_thread);
assert(NULL != m_thread); assert(NULL != m_thread);
} }
HGUInt WebUser::GetId() HGUInt WebUser::GetId()
{ {
return m_id; return m_id;
} }
Manager* WebUser::GetManager() void WebUser::PostDisConnectMsg()
{ {
return m_server->GetManager();
}
void WebUser::PostDisConnectMsg()
{
DisConnectParam* param = new DisConnectParam; DisConnectParam* param = new DisConnectParam;
param->svr = m_server; param->svr = m_server;
param->usrId = m_id; param->usrId = m_id;
@ -68,16 +61,15 @@ namespace ver_1
{ {
delete param; delete param;
} }
} }
void WebUser::ThreadFunc() void WebUser::ThreadFunc()
{ {
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "WebUser::ThreadFunc"); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "WebUser::ThreadFunc");
} }
void WebUser::ThreadFunc(HGThread thread, HGPointer param) void WebUser::ThreadFunc(HGThread thread, HGPointer param)
{ {
WebUser* p = (WebUser*)param; WebUser* p = (WebUser*)param;
p->ThreadFunc(); p->ThreadFunc();
}
} }

View File

@ -5,38 +5,35 @@
#include "base/HGLock.h" #include "base/HGLock.h"
#include "base/HGThread.h" #include "base/HGThread.h"
#include "base/HGMsgPump.h" #include "base/HGMsgPump.h"
#include <string>
namespace ver_1 class WebServer;
class WebUser
{ {
class WebServer; public:
class Manager;
class WebUser
{
public:
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn); WebUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, SOCKET sockConn);
#else #else
WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn); WebUser(WebServer* server, HGUInt id, const std::string& ip, uint16_t port, int sockConn);
#endif #endif
virtual ~WebUser(); virtual ~WebUser();
void Open(); void Open();
HGUInt GetId(); HGUInt GetId();
Manager* GetManager();
protected: protected:
void PostDisConnectMsg(); void PostDisConnectMsg();
virtual void ThreadFunc(); virtual void ThreadFunc();
private: private:
static void ThreadFunc(HGThread thread, HGPointer param); static void ThreadFunc(HGThread thread, HGPointer param);
protected: protected:
WebServer* m_server; WebServer* m_server;
HGLock m_cs; HGLock m_cs;
HGUInt m_id; HGUInt m_id;
char m_ip[16]; std::string m_ip;
uint16_t m_port; uint16_t m_port;
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
@ -45,5 +42,4 @@ namespace ver_1
int m_sockConn; int m_sockConn;
#endif #endif
HGThread m_thread; HGThread m_thread;
}; };
}

View File

@ -3,7 +3,8 @@
#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 "Manager.h" #include "ManagerV1.h"
#include "ManagerV2.h"
#include "HttpServer.h" #include "HttpServer.h"
#include "SockIoServer.h" #include "SockIoServer.h"
#include "WSServer.h" #include "WSServer.h"
@ -22,7 +23,7 @@ static void ThreadFunc(HGThread thread, HGPointer param)
HGBase_GetProfileInt(cfgPath, "version", "verNum", 2, &verNum); HGBase_GetProfileInt(cfgPath, "version", "verNum", 2, &verNum);
if (1 == verNum) // 使用V1版本接口 if (1 == verNum) // 使用V1版本接口
{ {
ver_1::Manager manager(msgPump); ver_1::ManagerV1 manager(msgPump);
ver_1::HttpServer httpServer(msgPump, &manager); ver_1::HttpServer httpServer(msgPump, &manager);
ver_1::SockIoServer sockIoServer(msgPump, &manager); ver_1::SockIoServer sockIoServer(msgPump, &manager);
@ -34,7 +35,12 @@ static void ThreadFunc(HGThread thread, HGPointer param)
} }
else // 使用V2版本接口 else // 使用V2版本接口
{ {
ver_2::ManagerV2 manager(msgPump);
ver_2::WSServer wsServer(msgPump, &manager);
wsServer.Open(38999);
HGBase_RunMsgPump(msgPump, ver_2::HGMsgPumpCallback, NULL);
wsServer.Close();
} }
} }

View File

@ -32,20 +32,20 @@ static std::string Utf8ToAnsi(const char* text)
} }
#endif #endif
std::string Utf8ToStdString(const char* utf8) std::string Utf8ToStdString(const std::string& utf8Str)
{ {
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
return Utf8ToAnsi(utf8); return Utf8ToAnsi(utf8Str.c_str());
#else #else
return utf8; return utf8Str;
#endif #endif
} }
std::string StdStringToUtf8(const char* str) std::string StdStringToUtf8(const std::string& stdStr)
{ {
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
return AnsiToUtf8(str); return AnsiToUtf8(stdStr.c_str());
#else #else
return str; return stdStr;
#endif #endif
} }

View File

@ -6,9 +6,9 @@
// 标准字符串windows下为ansi编码linux下为utf8编码 // 标准字符串windows下为ansi编码linux下为utf8编码
// UTF8转标准字符串 // UTF8转标准字符串
std::string Utf8ToStdString(const char* utf8); std::string Utf8ToStdString(const std::string& utf8Str);
// 标准字符串转UTF8 // 标准字符串转UTF8
std::string StdStringToUtf8(const char* str); std::string StdStringToUtf8(const std::string& stdStr);
#endif /* __HGSTRING_H__ */ #endif /* __HGSTRING_H__ */