HGGitLab

Commit e3853cb5 authored by luoliangyi's avatar luoliangyi

完善HTTP服务器,增加URI解析功能

parent 5ace6997
......@@ -19,6 +19,8 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\sdk\webservice\Device.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\HTTPHead.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\HTTPUser.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\main.cpp" />
<ClCompile Include="..\..\..\sdk\webservice\WebServer.cpp" />
......@@ -28,6 +30,8 @@
<ClCompile Include="..\..\..\third_party\sha1\sha1.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\sdk\webservice\Device.h" />
<ClInclude Include="..\..\..\sdk\webservice\HTTPHead.h" />
<ClInclude Include="..\..\..\sdk\webservice\HTTPUser.h" />
<ClInclude Include="..\..\..\sdk\webservice\WebMsg.h" />
<ClInclude Include="..\..\..\sdk\webservice\WebServer.h" />
......@@ -104,15 +108,15 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_CONSOLE;_WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_CONSOLE;_WINSOCK_DEPRECATED_NO_WARNINGS;SDK_USE_TWAIN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<AdditionalIncludeDirectories>../../../third_party/sha1/;../../../third_party/base64/;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>../../../third_party/sha1/;../../../third_party/base64/;../../../release/include/;../../../third_party/sane/;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>../Debug/HGBase.lib;../Debug/HGImgFmt.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>../Debug/HGBase.lib;../Debug/HGImgFmt.lib;../Debug/HGTwainUser.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
......
#include "Device.h"
#include "../../base/HGBuffer.h"
#include "../../base/HGBase64.h"
#include "../../imgfmt/HGJpeg.h"
Device::Device()
{
#if defined(HG_CMP_MSC) && defined(SDK_USE_TWAIN)
m_twainDSM = NULL;
HGTwain_LoadDSM(&m_twainDSM);
HGTwain_OpenDSM(m_twainDSM);
m_twainDS = NULL;
HGTwain_GetDefaultDS(m_twainDSM, &m_twainDS);
#else
m_saneDeviceHandle = NULL;
SANE_Int v = 0;
sane_init_ex(&v, sane_ex_callback, this);
#endif
}
Device::~Device()
{
#if defined(HG_CMP_MSC) && defined(SDK_USE_TWAIN)
HGTwain_CloseDSM(m_twainDSM);
HGTwain_UnloadDSM(m_twainDSM);
m_twainDSM = NULL;
#else
if (nullptr != m_saneDeviceHandle)
{
sane_cancel(m_saneDeviceHandle);
sane_close(m_saneDeviceHandle);
m_saneDeviceHandle = NULL;
}
sane_exit();
#endif
}
void Device::Scan()
{
}
void Device::StopScan()
{
}
void Device::GetCurDevId(std::string& devId)
{
devId = "123456";
}
void Device::GetDevNames(std::string& devNames)
{
devNames = "[";
for (int i = 0; i < 3; ++i)
{
char devName[32];
sprintf(devName, "Scanner%d", i);
devNames += "\"";
devNames += devName;
devNames += "\"";
if (i != 3 - 1)
devNames += ",";
}
devNames += "]";
}
void Device::GetImageByDevId(const std::string& devId, std::string& imgInfos)
{
imgInfos = "[";
for (int i = 0; i < 3; ++i)
{
char fileName[256];
sprintf(fileName, "D:/%d.jpg", i);
HGImage img = NULL;
HGImgFmt_LoadJpegImage(fileName, NULL, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &img);
if (NULL != img)
{
std::string base64 = GetBase64(img, 0);
HGBase_DestroyImage(img);
imgInfos += "{\"imageName\":\"";
imgInfos += fileName;
imgInfos += "\",\"src\":\"";
imgInfos += "data:image/jpeg;base64,";
imgInfos += base64;
imgInfos += "\"}";
if (i != 3 - 1)
imgInfos += ",";
}
}
imgInfos += "]";
}
void Device::GetDevParam(const std::string& devId, std::string& devParam)
{
devParam = "{";
devParam += "\"device\":\"Scanner0\",";
devParam += "\"autofeeder\":true,";
devParam += "\"pixel\":1,";
devParam += "\"white\":0,";
devParam += "\"single\":false,";
devParam += "\"format\":\"jpg\",";
devParam += "\"resolution\":200,";
devParam += "\"serialNum\":\"HG1060A50213P1\",";
devParam += "\"fwVersion\":\"G460210104\",";
devParam += "\"sdkVersion\":\"1.0.0.0\",";
devParam += "\"upload\":{";
devParam += "\"uploadMode\":2,";
devParam += "\"httpUrl\":null,";
devParam += "\"fileName\":null,";
devParam += "\"httpMethod\":null,";
devParam += "\"header\":null,";
devParam += "\"param\":null,";
devParam += "\"ftpUrl\":null,";
devParam += "\"ftpPath\":null,";
devParam += "\"ftpUser\":null,";
devParam += "\"ftpPassword\":null,";
devParam += "\"ftpPort\":21,";
devParam += "\"ftpMode\":2";
devParam += "}";
devParam += "}";
}
void Device::SetDevParam(const std::string& devId, const std::string& devParam)
{
// devParamΪjson
}
void Device::ExportOfd()
{
}
void Device::ExportPdf()
{
}
void Device::UploadImage(const std::string& param)
{
}
void Device::SaveImage(const std::string& devId, const std::string& imgName, const std::string& imgBase64)
{
}
void Device::DeleteImage(const std::string& devId, const std::string& imgName)
{
}
void Device::DeleteAllImage(const std::string& devId)
{
}
void Device::GetLastBetch(std::string& devId)
{
}
void Device::GetDevSerialNo(const std::string& devId, std::string& serialNo)
{
}
void Device::GetImageBase64(const std::string& devId, const std::string& imgName, std::string& imgBase64)
{
}
std::string Device::GetBase64(HGImage image, HGUInt quality)
{
std::string strBase64;
if (NULL != image)
{
HGBuffer buffer = NULL;
HGImgFmt_SaveJpegImageToBuffer(image, NULL, quality, &buffer);
if (NULL != buffer)
{
HGByte* data;
HGBase_GetBufferData(buffer, &data);
HGUSize size;
HGBase_GetBufferSize(buffer, &size);
HGSize base64Size = 0;
HGBase_Base64Encode(data, size, NULL, &base64Size);
uint8_t* base64 = new uint8_t[base64Size + 1];
HGBase_Base64Encode(data, size, base64, &base64Size);
base64[base64Size] = 0;
strBase64 = (const char*)base64;
delete[] base64;
HGBase_DestroyBuffer(buffer);
}
}
return strBase64;
}
#if defined(HG_CMP_MSC) && defined(SDK_USE_TWAIN)
void HGAPI Device::DSEventCallback(HGTwainDS ds, HGUInt event, HGPointer param)
{
}
#else
int Device::sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param)
{
return 0;
}
#endif
\ No newline at end of file
#ifndef __DEVICE_H__
#define __DEVICE_H__
#include "../../base/HGDef.h"
#if defined(HG_CMP_MSC) && defined(SDK_USE_TWAIN)
#include "../../twain_user/HGTwain.h"
#else
#include "sane_ex/sane_ex.h"
#endif
#include <string>
#if 0
struct UploadParam
{
int uploadMode; // 上传模式, 0-http, 1-ftp, 2-不上传
std::string httpUrl; // http上传地址
std::string httpFileName; // http接收文件名
std::string httpMethod;// http上传方式
std::string httpHeader;// http上传头
std::string httpParam;// http上传参数
std::string ftpUrl; // ftp上传地址
std::string ftpFilePath; // ftp上传路径
std::string ftpUser; // ftp上传用户名
std::string ftpPassword; // ftp上传密码
int ftpPort; // ftp端口号
int ftpMode; // ftp连接模式,1-主动,2-被动
int uploadFormat; // 上传格式,0-ofd, 1-pdf, 2-zip
};
struct DevParam
{
std::string devName; // 设备名
bool autoFeeder; // 是否自动进纸
int pixelMode; // 0-黑白, 1-灰度, 2-彩色
int discardBlank; // 是否丢弃空白页, 1-是,0-否
bool singleScan; // 是否单页扫描
std::string format; // 格式,"jpg"
int resolution; // 分辨率,DPI
std::string serialNo; // 序列号
std::string fwVersion;// 固件版本号
std::string sdkVersion;// sdk版本号
UploadParam uploadParam;// 上传参数
};
#endif
class Device
{
public:
Device();
~Device();
// 扫描
void Scan();
// 停止扫描
void StopScan();
// 获取当前连接的设备ID
void GetCurDevId(std::string& devId);
// 获取连接的设备名列表
void GetDevNames(std::string& devNames);
// 获取上次的扫描结果
void GetImageByDevId(const std::string& devId, std::string& imgInfos);
// 获取配置参数
void GetDevParam(const std::string& devId, std::string& devParam);
// 设置配置参数
void SetDevParam(const std::string& devId, const std::string& devParam);
// 生成OFD
void ExportOfd();
// 生成PDF
void ExportPdf();
// 上传图像
void UploadImage(const std::string& param);
// 保存图片
void SaveImage(const std::string& devId, const std::string& imgName, const std::string& imgBase64);
// 删除图片
void DeleteImage(const std::string& devId, const std::string& imgName);
// 删除所有图片
void DeleteAllImage(const std::string& devId);
// 获取最后批次
void GetLastBetch(std::string& devId);
// 获取设备序列号
void GetDevSerialNo(const std::string& devId, std::string& serialNo);
// 获取图像Base64
void GetImageBase64(const std::string& devId, const std::string& imgName, std::string& imgBase64);
private:
static std::string GetBase64(HGImage image, HGUInt quality);
#if defined(HG_CMP_MSC) && defined(SDK_USE_TWAIN)
static void HGAPI DSEventCallback(HGTwainDS ds, HGUInt event, HGPointer param);
#else
static int sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param);
#endif
private:
#if defined(HG_CMP_MSC) && defined(SDK_USE_TWAIN)
HGTwainDSM m_twainDSM;
HGTwainDS m_twainDS;
#else
SANE_Handle m_saneDeviceHandle;
#endif
};
#endif /* __DEVICE_H__ */
\ No newline at end of file
#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 */
};
HttpHead::HttpHead()
{
}
HttpHead::~HttpHead()
{
}
bool HttpHead::Parse(const std::string& head)
{
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 false;
std::string requestURI;
// 解析请求行
p = new char[headList[0].size() + 1];
strcpy(p, headList[0].c_str());
pStr = strtok(p, " ");
if (NULL != pStr)
m_requestMethod = pStr;
pStr = strtok(NULL, " ");
if (NULL != pStr)
requestURI = pStr;
pStr = strtok(NULL, " ");
if (NULL != pStr)
m_httpVersion = pStr;
delete[] p;
// 解析URI
AnalysisURI(requestURI, m_requestURIPath, m_requestURIQueryInfos, m_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);
m_headInfos.push_back(pr);
delete[] p;
}
return true;
}
void HttpHead::Clear()
{
m_requestMethod.clear();
m_requestURIPath.clear();
m_requestURIQueryInfos.clear();
m_requestURIFragment.clear();
m_httpVersion.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::GetHttpVersion() const
{
return m_httpVersion;
}
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;
}
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::TrimString(std::string& str)
{
int s = (int)str.find_first_not_of(" ");
int e = (int)str.find_last_not_of(" ");
str = str.substr(s, e - s + 1);
}
/*判断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, second;
first = asciiXdigitValue(scanner[0]);
if (first < 0)
return -1;
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)
{
int strLen;
char* result, * out; //分配内存,存放解码后的字符串
const char* in, * end;
int c;
if (!uriString)
return NULL;
strLen = strlen(uriString);
result = (char *)malloc(strLen + 1);//可推测解码后的长度<=原长度
out = result;
for (in = uriString, end = in + strLen; in < end; ++in)
{
c = *in;
//遇到了'%'才去解析
if ('%' == c)
{
if (in + 3 > end)
break;
//获取%后2个字符的解码值
c = unescapeCharacter(in + 1);
if (c <= 0)
break;
if (asciiEscape && c <= 0x7F)
break;
if (strchr("/", c) != NULL)
break;
in += 2;//一般的格式为%后加两个ascii码字符
}
*out++ = c;//存储转义结果
}
*out = '\0';
if (in != end)
{
free(result);
return NULL;
}
return result;
}
std::string HttpHead::AnalyURIString(const std::string& str)
{
std::string ret;
char *decodeStr = unescapeUriString(str.c_str(), false);
if (NULL != decodeStr)
{
ret = decodeStr;
free(decodeStr);
}
return ret;
}
\ No newline at end of file
#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 GetHttpVersion() const;
HttpPairs GetHeadInfos() const;
int GetContentLength() const;
std::string GetContentType() const;
static void AnalysisURIQuery(const std::string& query, HttpPairs& queryInfos);
static void AnalysisURI(const std::string &uri, std::string &path, HttpPairs &queryInfos, std::string& fragment);
private:
static void TrimString(std::string& str);
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_httpVersion;
HttpPairs m_headInfos;
};
#endif /* __HTTPHEAD_H__ */
\ No newline at end of file
This diff is collapsed.
#ifndef __HTTPUSER_H__
#define __HTTPUSER_H__
#include "WebUser.h"
#include "WebMsg.h"
#include "../../base/HGImage.h"
#include <string>
#include <vector>
class HttpHeadInfo
{
public:
HttpHeadInfo();
~HttpHeadInfo();
bool Parse(const char* head);
void Clear();
std::string GetRequestMethod();
std::string GetRequestURI();
std::string GetHttpVersion();
int GetRequestHeadCount();
std::string GetRequestHeadKey(int idx);
std::string GetRequestHeadValue(int idx);
int GetContentLength();
std::string GetContentType();
private:
static void TrimString(std::string& str);
private:
std::string m_requestMethod;
std::string m_requestURI;
std::string m_httpVersion;
std::vector<std::pair<std::string, std::string>> m_headInfos;
};
#include "WebUser.h"
class HTTPUser : public WebUser
{
......@@ -51,7 +20,6 @@ protected:
virtual void ThreadFunc();
private:
static std::string GetBase64(HGImage image, uint32_t quality);
bool SendHTTPResponse(const char *httpVersion, int errCode, const char *errInfo, const char *result);
};
......
#include "WSUser.h"
#include "WebServer.h"
#include "Device.h"
#include "sha1.h"
#include "base64.h"
extern Device* g_device;
#if defined(HG_CMP_MSC)
WSUser::WSUser(class WebServer *server, HGUInt id, const char *ip, uint16_t port, SOCKET sockConn)
#else
......
#ifndef __WSUSER_H__
#define __WSUSER_H__
#include "WebUser.h"
#include "WebMsg.h"
#include "WebUser.h"
class WSUser : public WebUser
{
......
......@@ -2,6 +2,8 @@
#define __WEBMSG_H__
#include "../../base/HGDef.h"
#include "../../base/HGInc.h"
#include "HTTPHead.h"
#include <string>
#define WEB_MSGID_QUIT 1
......@@ -30,15 +32,13 @@ struct ConnectParam
struct WSCmdParam
{
std::string data; // json
std::string data;
};
struct HTTPCmdParam
{
std::string requestMethod;
std::string requestURI;
std::string httpVersion;
std::string data; // json
HttpHead head;
std::string data;
};
#endif /* __WEBMSG_H__ */
\ No newline at end of file
#include "WebServer.h"
#include "WSUser.h"
#include "HTTPUser.h"
#include "../../base/HGInfo.h"
WebServer::WebServer(ServerType type)
: m_svrType(type)
......@@ -67,7 +68,7 @@ bool WebServer::Open(HGUShort port)
if (-1 == sockServer)
#endif
{
printf("open webserver failed 1\n");
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "open webserver failed 1");
return false;
}
......@@ -91,7 +92,7 @@ bool WebServer::Open(HGUShort port)
#else
close(sockServer);
#endif
printf("open webserver failed 2\n");
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "open webserver failed 2");
return false;
}
......@@ -103,7 +104,7 @@ bool WebServer::Open(HGUShort port)
#else
close(sockServer);
#endif
printf("open webserver failed 3\n");
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "open webserver failed 3");
return false;
}
......@@ -114,7 +115,10 @@ bool WebServer::Open(HGUShort port)
HGBase_OpenThread(ThreadFunc, this, &m_listenThread);
assert(NULL != m_listenThread);
printf("open webserver success, port=%u\n", port);
if (ServerType_WS == m_svrType)
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "open websocket-server success, post=%u", port);
else
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "open http-server success, post=%u", port);
return true;
}
......
#include "../../base/HGDef.h"
#include "../../base/HGInc.h"
#include "../../base/HGThread.h"
#include "Device.h"
#include "WebServer.h"
Device* g_device = NULL;
static void WSThreadFunc(HGThread thread, HGPointer param)
{
WebServer* server = (WebServer*)param;
......@@ -31,6 +34,8 @@ int main()
assert(0 == ret);
#endif
g_device = new Device();
WebServer wsServer(ServerType_WS);
HGThread wsThread = NULL;
HGBase_OpenThread(WSThreadFunc, &wsServer, &wsThread);
......@@ -43,6 +48,9 @@ int main()
HGBase_CloseThread(wsThread);
wsThread = NULL;
delete g_device;
g_device = NULL;
#if defined(HG_CMP_MSC)
WSACleanup();
#endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment