整理websdk,将消息循环独立成通用模块

This commit is contained in:
luoliangyi 2022-05-21 18:09:34 +08:00
parent 9fc3f86d45
commit 77afae2c9e
24 changed files with 509 additions and 506 deletions

View File

@ -0,0 +1,59 @@
#include "HGMsgPump.h"
#include "HGMsgPumpImpl.h"
HGResult HGAPI HGBase_CreateMsgPump(HGMsgPump* msgPump)
{
if (NULL == msgPump)
{
return HGBASE_ERR_INVALIDARG;
}
HGMsgPumpImpl* msgPumpImpl = new HGMsgPumpImpl;
*msgPump = (HGMsgPump)msgPumpImpl;
return HGBASE_ERR_OK;
}
HGResult HGAPI HGBase_DestroyMsgPump(HGMsgPump msgPump)
{
if (NULL == msgPump)
{
return HGBASE_ERR_INVALIDARG;
}
HGMsgPumpImpl* msgPumpImpl = (HGMsgPumpImpl*)msgPump;
delete msgPumpImpl;
return HGBASE_ERR_OK;
}
HGResult HGAPI HGBase_RunMsgPump(HGMsgPump msgPump, HGMsgPumpFunc func, HGPointer param)
{
if (NULL == msgPump)
{
return HGBASE_ERR_INVALIDARG;
}
HGMsgPumpImpl* msgPumpImpl = (HGMsgPumpImpl*)msgPump;
return msgPumpImpl->Run(func, param);
}
HGResult HGAPI HGBase_PostPumpMessage(HGMsgPump msgPump, const HGMsg* msg)
{
if (NULL == msgPump)
{
return HGBASE_ERR_INVALIDARG;
}
HGMsgPumpImpl* msgPumpImpl = (HGMsgPumpImpl*)msgPump;
return msgPumpImpl->PostMessage(msg);
}
HGResult HGAPI HGBase_ExitMsgPump(HGMsgPump msgPump)
{
if (NULL == msgPump)
{
return HGBASE_ERR_INVALIDARG;
}
HGMsgPumpImpl* msgPumpImpl = (HGMsgPumpImpl*)msgPump;
return msgPumpImpl->Exit();
}

28
modules/base/HGMsgPump.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef __HGMSGPUMP_H__
#define __HGMSGPUMP_H__
#include "HGDef.h"
#include "HGBaseErr.h"
HG_DECLARE_HANDLE(HGMsgPump);
/* 消息结构体, 可以自定义 */
typedef struct
{
HGUInt id; /* 消息ID, 不能为0 */
HGPointer data; /* 携带的数据 */
}HGMsg;
typedef void (*HGMsgPumpFunc)(HGMsgPump msgPump, const HGMsg* msg, HGPointer param);
HGEXPORT HGResult HGAPI HGBase_CreateMsgPump(HGMsgPump *msgPump);
HGEXPORT HGResult HGAPI HGBase_DestroyMsgPump(HGMsgPump msgPump);
HGEXPORT HGResult HGAPI HGBase_RunMsgPump(HGMsgPump msgPump, HGMsgPumpFunc func, HGPointer param);
HGEXPORT HGResult HGAPI HGBase_PostPumpMessage(HGMsgPump msgPump, const HGMsg *msg);
HGEXPORT HGResult HGAPI HGBase_ExitMsgPump(HGMsgPump msgPump);
#endif /* __HGMSGPUMP_H__ */

View File

@ -0,0 +1,102 @@
#include "HGMsgPumpImpl.h"
HGMsgPumpImpl::HGMsgPumpImpl()
{
HGBase_CreateEvent(HGTRUE, HGFALSE, &m_msgEvent);
HGBase_CreateLock(&m_msgLock);
m_bRecvMsg = HGTRUE;
}
HGMsgPumpImpl::~HGMsgPumpImpl()
{
HGBase_DestroyLock(m_msgLock);
m_msgLock = NULL;
HGBase_DestroyEvent(m_msgEvent);
m_msgEvent = NULL;
}
HGResult HGMsgPumpImpl::Run(HGMsgPumpFunc func, HGPointer param)
{
if (NULL == func)
{
return HGBASE_ERR_INVALIDARG;
}
while (1)
{
HGBase_WaitEvent(m_msgEvent);
HGMsg msg = { 0 };
bool getMsg = false;
HGBase_EnterLock(m_msgLock);
if (!m_listMsg.empty())
{
msg = m_listMsg.front();
m_listMsg.pop_front();
getMsg = true;
}
if (m_listMsg.empty())
{
HGBase_ResetEvent(m_msgEvent);
}
HGBase_LeaveLock(m_msgLock);
if (!getMsg)
{
continue;
}
if (0 == msg.id)
{
assert(NULL == msg.data);
break;
}
else
{
func((HGMsgPump)this, &msg, param);
}
}
return HGBASE_ERR_OK;
}
HGResult HGMsgPumpImpl::PostMessage(const HGMsg* msg)
{
if (NULL == msg || 0 == msg->id)
{
return HGBASE_ERR_INVALIDARG;
}
HGResult ret = HGBASE_ERR_FAIL;
HGBase_EnterLock(m_msgLock);
if (m_bRecvMsg)
{
m_listMsg.push_back(*msg);
HGBase_SetEvent(m_msgEvent);
ret = HGBASE_ERR_OK;
}
HGBase_LeaveLock(m_msgLock);
return ret;
}
HGResult HGMsgPumpImpl::Exit()
{
HGMsg msg;
msg.data = 0;
msg.data = NULL;
HGResult ret = HGBASE_ERR_FAIL;
HGBase_EnterLock(m_msgLock);
if (m_bRecvMsg)
{
m_listMsg.push_back(msg);
HGBase_SetEvent(m_msgEvent);
m_bRecvMsg = HGFALSE;
ret = HGBASE_ERR_OK;
}
HGBase_LeaveLock(m_msgLock);
return ret;
}

View File

@ -0,0 +1,28 @@
#ifndef __HGMSGPUMPIMPL_H__
#define __HGMSGPUMPIMPL_H__
#include "HGDef.h"
#include "HGInc.h"
#include "HGEvent.h"
#include "HGLock.h"
#include "HGMsgPump.h"
#include <list>
class HGMsgPumpImpl
{
public:
HGMsgPumpImpl();
~HGMsgPumpImpl();
HGResult Run(HGMsgPumpFunc func, HGPointer param);
HGResult PostMessage(const HGMsg* msg);
HGResult Exit();
private:
HGEvent m_msgEvent;
HGLock m_msgLock;
HGBool m_bRecvMsg;
std::list<HGMsg> m_listMsg;
};
#endif /* __HGMSGPUMPIMPL_H__ */

View File

@ -2,8 +2,8 @@
#include "HttpUser.h" #include "HttpUser.h"
#include "base/HGInfo.h" #include "base/HGInfo.h"
HttpServer::HttpServer(class MsgLoop* loop, class Manager* manager) HttpServer::HttpServer(HGMsgPump msgPump, class Manager* manager)
: WebServer(loop, manager) : WebServer(msgPump, manager)
{ {
} }

View File

@ -2,12 +2,12 @@
#define __HTTPSERVER_H__ #define __HTTPSERVER_H__
#include "WebServer.h" #include "WebServer.h"
#include "WebMsg.h" #include "Msg_1_0.h"
class HttpServer : public WebServer class HttpServer : public WebServer
{ {
public: public:
HttpServer(class MsgLoop *loop, class Manager *manager); HttpServer(HGMsgPump msgPump, class Manager *manager);
virtual ~HttpServer(); virtual ~HttpServer();
void Connect(const ConnectParam *param); void Connect(const ConnectParam *param);

View File

@ -1,5 +1,5 @@
#include "HttpUser.h" #include "HttpUser.h"
#include "MsgLoop.h" #include "WebServer.h"
#include "Manager.h" #include "Manager.h"
#include "base/HGInfo.h" #include "base/HGInfo.h"
#include "cJSON.h" #include "cJSON.h"
@ -101,9 +101,9 @@ static std::string GetJsonStringValue(cJSON* json, const std::string& key)
} }
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
HttpUser::HttpUser(class WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn) HttpUser::HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn)
#else #else
HttpUser::HttpUser(class WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn) HttpUser::HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn)
#endif #endif
: WebUser(server, id, ip, port, sockConn) : WebUser(server, id, ip, port, sockConn)
{ {
@ -715,6 +715,32 @@ void HttpUser::HandleCmd(const HttpCmdParam* param)
} }
} }
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() void HttpUser::ThreadFunc()
{ {
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "HttpUser::ThreadFunc"); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "HttpUser::ThreadFunc");
@ -741,12 +767,7 @@ void HttpUser::ThreadFunc()
if (len <= 0) if (len <= 0)
{ {
// 这里跳出可能是服务器关闭了socketConn或者客户端关闭了socket或者网络断开 // 这里跳出可能是服务器关闭了socketConn或者客户端关闭了socket或者网络断开
WebMsg msg; PostDisConnectMsg();
msg.msgId = WEB_MSGID_DISCONNET;
msg.svr = m_server;
msg.usrId = m_id;
msg.param = NULL;
GetLoop()->Send(&msg);
break; break;
} }
else else
@ -783,12 +804,7 @@ void HttpUser::ThreadFunc()
headDataTailLen = 0; headDataTailLen = 0;
if (!headInfo.Parse(headData.c_str())) if (!headInfo.Parse(headData.c_str()))
{ {
WebMsg msg; PostDisConnectMsg();
msg.msgId = WEB_MSGID_DISCONNET;
msg.svr = m_server;
msg.usrId = m_id;
msg.param = NULL;
GetLoop()->Send(&msg);
break; break;
} }
@ -803,22 +819,7 @@ void HttpUser::ThreadFunc()
} }
else else
{ {
HttpCmdParam* param = new HttpCmdParam; PostCmdMsg(headInfo, NULL, 0);
param->head = headInfo;
param->data = NULL;
param->size = 0;
WebMsg msg;
msg.msgId = WEB_MSGID_COMMAND;
msg.svr = m_server;
msg.usrId = m_id;
msg.param = param;
bool b = GetLoop()->Send(&msg);
if (!b)
{
delete param;
}
headInfo.Clear(); headInfo.Clear();
} }
} }
@ -829,24 +830,7 @@ void HttpUser::ThreadFunc()
++getDataSize; ++getDataSize;
if (getDataSize == dataSize) if (getDataSize == dataSize)
{ {
HttpCmdParam* param = new HttpCmdParam; PostCmdMsg(headInfo, data, dataSize);
param->head = headInfo;
param->data = new HGByte [dataSize];
param->size = dataSize;
memcpy(param->data, data, dataSize);
WebMsg msg;
msg.msgId = WEB_MSGID_COMMAND;
msg.svr = m_server;
msg.usrId = m_id;
msg.param = param;
bool b = GetLoop()->Send(&msg);
if (!b)
{
delete[] param->data;
param->size = 0;
delete param;
}
delete [] data; delete [] data;
data = NULL; data = NULL;

View File

@ -2,21 +2,24 @@
#define __HTTPUSER_H__ #define __HTTPUSER_H__
#include "WebUser.h" #include "WebUser.h"
#include "WebMsg.h" #include "Msg_1_0.h"
class WebServer;
class HttpUser : public WebUser class HttpUser : public WebUser
{ {
public: public:
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
HttpUser(class WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn); HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn);
#else #else
HttpUser(class WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn); HttpUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn);
#endif #endif
virtual ~HttpUser(); virtual ~HttpUser();
void HandleCmd(const HttpCmdParam* param); void HandleCmd(const HttpCmdParam* param);
protected: protected:
void PostCmdMsg(const HttpHead& headInfo, const HGByte *data, HGUInt dataSize);
virtual void ThreadFunc(); virtual void ThreadFunc();
private: private:

View File

@ -1,5 +1,4 @@
#include "Manager.h" #include "Manager.h"
#include "MsgLoop.h"
#include "base/HGBuffer.h" #include "base/HGBuffer.h"
#include "base/HGBase64.h" #include "base/HGBase64.h"
#include "base/HGUtility.h" #include "base/HGUtility.h"
@ -137,9 +136,9 @@ void DevParam::Save(const std::string& cfgPath)
HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "ftpMode", ftpMode); HGBase_SetProfileInt(cfgPath.c_str(), "devParam", "ftpMode", ftpMode);
} }
Manager::Manager(class MsgLoop* loop) Manager::Manager(HGMsgPump msgPump)
{ {
m_loop = loop; m_msgPump = msgPump;
HGBase_CreateLock(&m_lock); HGBase_CreateLock(&m_lock);
m_devName.clear(); m_devName.clear();
m_devHandle = NULL; m_devHandle = NULL;
@ -1894,13 +1893,10 @@ int Manager::sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned i
OpenDevParam* openDevParam = new OpenDevParam; OpenDevParam* openDevParam = new OpenDevParam;
openDevParam->devName = sane_dev->name; openDevParam->devName = sane_dev->name;
WebMsg msg; HGMsg msg;
msg.msgId = WEB_MSGID_OPENDEV; msg.id = MSGID_OPEN_DEVICE;
msg.svr = NULL; msg.data = openDevParam;
msg.usrId = 0; if (HGBASE_ERR_OK != HGBase_PostPumpMessage(p->m_msgPump, &msg))
msg.param = openDevParam;
bool b = p->m_loop->Send(&msg);
if (!b)
{ {
delete openDevParam; delete openDevParam;
} }
@ -1919,13 +1915,10 @@ int Manager::sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned i
CloseDevParam* closeDevParam = new CloseDevParam; CloseDevParam* closeDevParam = new CloseDevParam;
closeDevParam->devName = sane_dev->name; closeDevParam->devName = sane_dev->name;
WebMsg msg; HGMsg msg;
msg.msgId = WEB_MSGID_CLOSEDEV; msg.id = MSGID_CLOSE_DEVICE;
msg.svr = NULL; msg.data = closeDevParam;
msg.usrId = 0; if (HGBASE_ERR_OK != HGBase_PostPumpMessage(p->m_msgPump, &msg))
msg.param = closeDevParam;
bool b = p->m_loop->Send(&msg);
if (!b)
{ {
delete closeDevParam; delete closeDevParam;
} }
@ -2060,12 +2053,10 @@ int Manager::sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned i
p->m_scanEvent(SCANEVENT_FINISH, (void*)data, (void*)0, p->m_scanParam); p->m_scanEvent(SCANEVENT_FINISH, (void*)data, (void*)0, p->m_scanParam);
HGBase_LeaveLock(p->m_lock); HGBase_LeaveLock(p->m_lock);
WebMsg msg; HGMsg msg;
msg.msgId = WEB_MSGID_SCANFINISH; msg.id = MSGID_SCAN_FINISH;
msg.svr = NULL; msg.data = NULL;
msg.usrId = 0; HGBase_PostPumpMessage(p->m_msgPump, &msg);
msg.param = NULL;
p->m_loop->Send(&msg);
} }
break; break;
} }

View File

@ -5,10 +5,10 @@
#include "base/HGInc.h" #include "base/HGInc.h"
#include "base/HGLock.h" #include "base/HGLock.h"
#include "base/HGImage.h" #include "base/HGImage.h"
#include "base/HGMsgPump.h"
#include "sane/sane_ex.h" #include "sane/sane_ex.h"
#include "sane/sane_option_definitions.h" #include "sane/sane_option_definitions.h"
#include "WebMsg.h" #include "Msg_1_0.h"
#include <vector>
#include <string> #include <string>
#define SCANEVENT_ARRIVE 1L #define SCANEVENT_ARRIVE 1L
@ -86,7 +86,7 @@ typedef void (*ScanEvent)(HGUInt event, void *value1, void *value2, void *param)
class Manager class Manager
{ {
public: public:
Manager(class MsgLoop* loop); Manager(HGMsgPump msgPump);
~Manager(); ~Manager();
// 打开设备 // 打开设备
@ -175,7 +175,7 @@ private:
static int sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param); static int sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param);
private: private:
class MsgLoop* m_loop; HGMsgPump m_msgPump;
HGLock m_lock; HGLock m_lock;
std::string m_devName; std::string m_devName;
SANE_Handle m_devHandle; SANE_Handle m_devHandle;

View File

@ -1,181 +0,0 @@
#include "MsgLoop.h"
MsgLoop::MsgLoop()
{
HGBase_CreateEvent(HGTRUE, HGFALSE, &m_msgEvent);
HGBase_CreateLock(&m_msgLock);
m_bRecvMsg = HGTRUE;
m_manager = new Manager(this);
m_httpServer = new HttpServer(this, m_manager);
m_sockIoServer = new SockIoServer(this, m_manager);
}
MsgLoop::~MsgLoop()
{
delete m_httpServer;
m_httpServer = NULL;
delete m_sockIoServer;
m_sockIoServer = NULL;
delete m_manager;
m_manager = NULL;
HGBase_DestroyLock(m_msgLock);
m_msgLock = NULL;
HGBase_DestroyEvent(m_msgEvent);
m_msgEvent = NULL;
}
bool MsgLoop::Send(const WebMsg* msg)
{
if (NULL == msg)
{
return false;
}
bool ret = false;
HGBase_EnterLock(m_msgLock);
if (m_bRecvMsg)
{
m_listMsg.push_back(*msg);
HGBase_SetEvent(m_msgEvent);
if (WEB_MSGID_QUIT == msg->msgId)
m_bRecvMsg = HGFALSE;
ret = true;
}
HGBase_LeaveLock(m_msgLock);
return ret;
}
void MsgLoop::Loop()
{
m_httpServer->Open(18999);
m_sockIoServer->Open(28999);
while (1)
{
HGBase_WaitEvent(m_msgEvent);
WebMsg msg = { 0 };
HGBase_EnterLock(m_msgLock);
if (!m_listMsg.empty())
{
msg = m_listMsg.front();
m_listMsg.pop_front();
}
if (m_listMsg.empty())
{
HGBase_ResetEvent(m_msgEvent);
}
HGBase_LeaveLock(m_msgLock);
if (0 == msg.msgId)
{
continue;
}
if (WEB_MSGID_QUIT == msg.msgId)
{
assert(NULL == msg.svr);
assert(0 == msg.usrId);
assert(NULL == msg.param);
break;
}
else if (WEB_MSGID_CONNET == msg.msgId)
{
assert(NULL != msg.svr);
assert(0 == msg.usrId);
assert(NULL != msg.param);
ConnectParam* param = (ConnectParam*)msg.param;
if (msg.svr == m_httpServer)
m_httpServer->Connect(param);
else if (msg.svr == m_sockIoServer)
m_sockIoServer->Connect(param);
delete param;
}
else if (WEB_MSGID_DISCONNET == msg.msgId)
{
assert(NULL != msg.svr);
assert(0 != msg.usrId);
assert(NULL == msg.param);
if (msg.svr == m_httpServer)
m_httpServer->DisConnect(msg.usrId);
else if (msg.svr == m_sockIoServer)
m_sockIoServer->DisConnect(msg.usrId);
}
else if (WEB_MSGID_COMMAND == msg.msgId)
{
assert(NULL != msg.svr);
assert(0 != msg.usrId);
assert(NULL != msg.param);
if (msg.svr == m_httpServer)
{
HttpCmdParam* param = (HttpCmdParam*)msg.param;
m_httpServer->Command(msg.usrId, param);
delete[] param->data;
param->size = 0;
delete param;
}
else if (msg.svr == m_sockIoServer)
{
SockIoCmdParam* param = (SockIoCmdParam*)msg.param;
m_sockIoServer->Command(msg.usrId, param);
delete[] param->data;
param->size = 0;
delete param;
}
}
else if (WEB_MSGID_EVENT == msg.msgId)
{
assert(NULL != msg.svr);
assert(0 != msg.usrId);
assert(NULL != msg.param);
if (msg.svr == m_sockIoServer)
{
SockIoEvtParam* param = (SockIoEvtParam*)msg.param;
m_sockIoServer->Event(msg.usrId, param);
delete[] param->data;
param->size = 0;
delete param;
}
}
else if (WEB_MSGID_OPENDEV == msg.msgId)
{
assert(NULL == msg.svr);
assert(0 == msg.usrId);
assert(NULL != msg.param);
OpenDevParam* param = (OpenDevParam*)msg.param;
m_manager->OpenDev(param);
delete param;
}
else if (WEB_MSGID_CLOSEDEV == msg.msgId)
{
assert(NULL == msg.svr);
assert(0 == msg.usrId);
assert(NULL != msg.param);
CloseDevParam* param = (CloseDevParam*)msg.param;
m_manager->CloseDev(param);
delete param;
}
else if (WEB_MSGID_SCANFINISH == msg.msgId)
{
assert(NULL == msg.svr);
assert(0 == msg.usrId);
assert(NULL == msg.param);
m_manager->ScanFinish();
}
}
m_httpServer->Close();
m_sockIoServer->Close();
}

View File

@ -1,34 +0,0 @@
#ifndef __MSGLOOP_H__
#define __MSGLOOP_H__
#include "base/HGDef.h"
#include "base/HGInc.h"
#include "base/HGEvent.h"
#include "base/HGLock.h"
#include "WebMsg.h"
#include "Manager.h"
#include "HttpServer.h"
#include "SockIoServer.h"
#include <list>
class MsgLoop
{
public:
MsgLoop();
~MsgLoop();
bool Send(const WebMsg* msg);
void Loop();
private:
HGEvent m_msgEvent;
HGLock m_msgLock;
HGBool m_bRecvMsg;
std::list<WebMsg> m_listMsg;
Manager *m_manager;
HttpServer *m_httpServer;
SockIoServer *m_sockIoServer;
};
#endif /* __MSGLOOP_H__ */

View File

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

View File

@ -0,0 +1,8 @@
#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,60 +1,19 @@
#ifndef __WEBMSG_H__ #ifndef __MSG_1_0_H__
#define __WEBMSG_H__ #define __MSG_1_0_H__
#include "base/HGDef.h" #include "base/HGDef.h"
#include "base/HGInc.h" #include "base/HGInc.h"
#include "HttpHead.h" #include "HttpHead.h"
#include <string> #include <string>
enum WebMsgId #define MSGID_OPEN_DEVICE 1L
{ #define MSGID_CLOSE_DEVICE 2L
WEB_MSGID_QUIT = 1, #define MSGID_SCAN_FINISH 3L
WEB_MSGID_CONNET, #define MSGID_CONNECT 4L
WEB_MSGID_DISCONNET, #define MSGID_DISCONNECT 5L
WEB_MSGID_COMMAND, #define MSGID_HTTP_COMMAND 6L
WEB_MSGID_EVENT, #define MSGID_SOCKIO_COMMAND 7L
WEB_MSGID_OPENDEV, #define MSGID_SOCKIO_EVENT 8L
WEB_MSGID_CLOSEDEV,
WEB_MSGID_SCANFINISH
};
struct WebMsg
{
HGUInt msgId; /* WEB_MSGID_** */
HGPointer svr; /* server */
HGUInt usrId; /* usrId */
HGPointer param; /* param */
};
struct ConnectParam
{
HGChar ip[16];
HGUShort port;
#if defined(HG_CMP_MSC)
SOCKET socket;
#else
int socket;
#endif
};
struct HttpCmdParam
{
HttpHead head;
HGByte* data;
HGUInt size;
};
struct SockIoCmdParam
{
HGByte* data;
HGUInt size;
};
struct SockIoEvtParam
{
HGByte* data;
HGUInt size;
};
struct OpenDevParam struct OpenDevParam
{ {
@ -66,4 +25,47 @@ struct CloseDevParam
std::string devName; std::string devName;
}; };
#endif /* __WEBMSG_H__ */ 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

@ -2,8 +2,8 @@
#include "SockIoUser.h" #include "SockIoUser.h"
#include "base/HGInfo.h" #include "base/HGInfo.h"
SockIoServer::SockIoServer(class MsgLoop* loop, class Manager* manager) SockIoServer::SockIoServer(HGMsgPump msgPump, class Manager* manager)
: WebServer(loop, manager) : WebServer(msgPump, manager)
{ {
} }

View File

@ -2,12 +2,12 @@
#define __SOCKIOSERVER_H__ #define __SOCKIOSERVER_H__
#include "WebServer.h" #include "WebServer.h"
#include "WebMsg.h" #include "Msg_1_0.h"
class SockIoServer : public WebServer class SockIoServer : public WebServer
{ {
public: public:
SockIoServer(class MsgLoop *loop, class Manager *manager); SockIoServer(HGMsgPump msgPump, class Manager *manager);
virtual ~SockIoServer(); virtual ~SockIoServer();
void Connect(const ConnectParam *param); void Connect(const ConnectParam *param);

View File

@ -1,5 +1,5 @@
#include "SockIoUser.h" #include "SockIoUser.h"
#include "MsgLoop.h" #include "WebServer.h"
#include "Manager.h" #include "Manager.h"
#include "base/HGInfo.h" #include "base/HGInfo.h"
#include "base/HGUtility.h" #include "base/HGUtility.h"
@ -8,9 +8,9 @@
#include "cJSON.h" #include "cJSON.h"
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
SockIoUser::SockIoUser(class SockIoServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn) SockIoUser::SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn)
#else #else
SockIoUser::SockIoUser(class SockIoServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn) SockIoUser::SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn)
#endif #endif
: WebUser(server, id, ip, port, sockConn) : WebUser(server, id, ip, port, sockConn)
{ {
@ -74,6 +74,46 @@ void SockIoUser::HandleEvent(const SockIoEvtParam* param)
SendResponse(param->data, param->size, HGTRUE); 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() void SockIoUser::ThreadFunc()
{ {
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SockIoUser::ThreadFunc"); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SockIoUser::ThreadFunc");
@ -106,12 +146,7 @@ void SockIoUser::ThreadFunc()
if (len <= 0) if (len <= 0)
{ {
// 这里跳出可能是服务器关闭了socketConn或者客户端关闭了socket或者网络断开 // 这里跳出可能是服务器关闭了socketConn或者客户端关闭了socket或者网络断开
WebMsg msg; PostDisConnectMsg();
msg.msgId = WEB_MSGID_DISCONNET;
msg.svr = m_server;
msg.usrId = m_id;
msg.param = NULL;
GetLoop()->Send(&msg);
break; break;
} }
else else
@ -151,12 +186,7 @@ void SockIoUser::ThreadFunc()
if (!shakeRet) if (!shakeRet)
{ {
WebMsg msg; PostDisConnectMsg();
msg.msgId = WEB_MSGID_DISCONNET;
msg.svr = m_server;
msg.usrId = m_id;
msg.param = NULL;
GetLoop()->Send(&msg);
break; break;
} }
@ -179,12 +209,7 @@ void SockIoUser::ThreadFunc()
{ {
if ((0x80 | 0x08) == headData[0]) // 断开连接 if ((0x80 | 0x08) == headData[0]) // 断开连接
{ {
WebMsg msg; PostDisConnectMsg();
msg.msgId = WEB_MSGID_DISCONNET;
msg.svr = m_server;
msg.usrId = m_id;
msg.param = NULL;
GetLoop()->Send(&msg);
break; break;
} }
else if ((0x80 | 0x09) == headData[0]) // PING帧 else if ((0x80 | 0x09) == headData[0]) // PING帧
@ -211,12 +236,7 @@ void SockIoUser::ThreadFunc()
} }
else // 帧错误,断开连接 else // 帧错误,断开连接
{ {
WebMsg msg; PostDisConnectMsg();
msg.msgId = WEB_MSGID_DISCONNET;
msg.svr = m_server;
msg.usrId = m_id;
msg.param = NULL;
GetLoop()->Send(&msg);
break; break;
} }
} }
@ -224,12 +244,7 @@ void SockIoUser::ThreadFunc()
{ {
if (0 == (headData[1] & 0x80)) // 必须经过掩码处理 if (0 == (headData[1] & 0x80)) // 必须经过掩码处理
{ {
WebMsg msg; PostDisConnectMsg();
msg.msgId = WEB_MSGID_DISCONNET;
msg.svr = m_server;
msg.usrId = m_id;
msg.param = NULL;
GetLoop()->Send(&msg);
break; break;
} }
@ -237,12 +252,7 @@ void SockIoUser::ThreadFunc()
{ {
if (0x80 != headData[1]) if (0x80 != headData[1])
{ {
WebMsg msg; PostDisConnectMsg();
msg.msgId = WEB_MSGID_DISCONNET;
msg.svr = m_server;
msg.usrId = m_id;
msg.param = NULL;
GetLoop()->Send(&msg);
break; break;
} }
} }
@ -250,12 +260,7 @@ void SockIoUser::ThreadFunc()
{ {
if (0x80 != headData[1]) if (0x80 != headData[1])
{ {
WebMsg msg; PostDisConnectMsg();
msg.msgId = WEB_MSGID_DISCONNET;
msg.svr = m_server;
msg.usrId = m_id;
msg.param = NULL;
GetLoop()->Send(&msg);
break; break;
} }
} }
@ -268,12 +273,7 @@ void SockIoUser::ThreadFunc()
if (0 == nCmdSize) if (0 == nCmdSize)
{ {
WebMsg msg; PostDisConnectMsg();
msg.msgId = WEB_MSGID_DISCONNET;
msg.svr = m_server;
msg.usrId = m_id;
msg.param = NULL;
GetLoop()->Send(&msg);
break; break;
} }
@ -303,12 +303,7 @@ void SockIoUser::ThreadFunc()
if (0 == nCmdSize) if (0 == nCmdSize)
{ {
WebMsg msg; PostDisConnectMsg();
msg.msgId = WEB_MSGID_DISCONNET;
msg.svr = m_server;
msg.usrId = m_id;
msg.param = NULL;
GetLoop()->Send(&msg);
break; break;
} }
@ -342,12 +337,7 @@ void SockIoUser::ThreadFunc()
if ((0 != nCmdSizeHigh) || (0 == nCmdSize)) if ((0 != nCmdSizeHigh) || (0 == nCmdSize))
{ {
WebMsg msg; PostDisConnectMsg();
msg.msgId = WEB_MSGID_DISCONNET;
msg.svr = m_server;
msg.usrId = m_id;
msg.param = NULL;
GetLoop()->Send(&msg);
break; break;
} }
@ -403,23 +393,7 @@ void SockIoUser::ThreadFunc()
} }
else else
{ {
SockIoCmdParam* param = new SockIoCmdParam; PostCmdMsg(&vAllData[0], vAllData.size());
param->data = new HGByte[vAllData.size()];
param->size = (HGUInt)vAllData.size();
memcpy(param->data, &vAllData[0], vAllData.size());
WebMsg msg;
msg.msgId = WEB_MSGID_COMMAND;
msg.svr = m_server;
msg.usrId = m_id;
msg.param = param;
bool b = GetLoop()->Send(&msg);
if (!b)
{
delete[] param->data;
param->size = 0;
delete param;
}
} }
bHandle = false; bHandle = false;
@ -479,24 +453,7 @@ void SockIoUser::ScanCallback(HGUInt event, void* value1, void* value2, void* pa
if (NULL != resp) if (NULL != resp)
{ {
SockIoEvtParam* param = new SockIoEvtParam; p->PostEventMsg((const HGByte*)resp, strlen(resp));
param->data = new HGByte[strlen(resp)];
param->size = (HGUInt)strlen(resp);
memcpy(param->data, resp, strlen(resp));
WebMsg msg;
msg.msgId = WEB_MSGID_EVENT;
msg.svr = p->m_server;
msg.usrId = p->m_id;
msg.param = param;
bool b = p->GetLoop()->Send(&msg);
if (!b)
{
delete[] param->data;
param->size = 0;
delete param;
}
delete[] resp; delete[] resp;
} }
} }

View File

@ -2,15 +2,17 @@
#define __SOCKIOUSER_H__ #define __SOCKIOUSER_H__
#include "WebUser.h" #include "WebUser.h"
#include "WebMsg.h" #include "Msg_1_0.h"
class WebServer;
class SockIoUser : public WebUser class SockIoUser : public WebUser
{ {
public: public:
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
SockIoUser(class SockIoServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn); SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn);
#else #else
SockIoUser(class SockIoServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn); SockIoUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn);
#endif #endif
virtual ~SockIoUser(); virtual ~SockIoUser();
@ -18,6 +20,8 @@ public:
void HandleEvent(const SockIoEvtParam* param); void HandleEvent(const SockIoEvtParam* param);
protected: protected:
void PostCmdMsg(const HGByte* data, HGUInt dataSize);
void PostEventMsg(const HGByte* data, HGUInt dataSize);
virtual void ThreadFunc(); virtual void ThreadFunc();
private: private:

View File

@ -1,11 +1,10 @@
#include "WebServer.h" #include "WebServer.h"
#include "WebUser.h" #include "WebUser.h"
#include "MsgLoop.h"
#include "base/HGInfo.h" #include "base/HGInfo.h"
WebServer::WebServer(class MsgLoop* loop, class Manager* manager) WebServer::WebServer(HGMsgPump msgPump, Manager* manager)
{ {
m_loop = loop; m_msgPump = msgPump;
m_manager = manager; m_manager = manager;
m_currUserId = 1; m_currUserId = 1;
@ -22,12 +21,12 @@ WebServer::~WebServer()
} }
class MsgLoop* WebServer::GetLoop() HGMsgPump WebServer::GetMsgPump()
{ {
return m_loop; return m_msgPump;
} }
class Manager* WebServer::GetManager() Manager* WebServer::GetManager()
{ {
return m_manager; return m_manager;
} }
@ -143,6 +142,32 @@ void WebServer::DisConnect(HGUInt usrId)
} }
} }
#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 WebServer::GetUserIndex(HGUInt id)
{ {
int nIndex = -1; int nIndex = -1;
@ -180,25 +205,6 @@ void WebServer::ThreadFunc(HGThread thread, HGPointer param)
break; break;
} }
ConnectParam* param = new ConnectParam; p->PostConnectMsg(inet_ntoa(addrClient.sin_addr), ntohs(addrClient.sin_port), socketConn);
strcpy(param->ip, inet_ntoa(addrClient.sin_addr));
param->port = ntohs(addrClient.sin_port);
param->socket = socketConn;
WebMsg msg;
msg.msgId = WEB_MSGID_CONNET;
msg.svr = p;
msg.usrId = 0;
msg.param = param;
bool b = p->m_loop->Send(&msg);
if (!b)
{
delete msg.param;
#if defined(HG_CMP_MSC)
closesocket(socketConn);
#else
close(socketConn);
#endif
}
} }
} }

View File

@ -4,17 +4,20 @@
#include "base/HGDef.h" #include "base/HGDef.h"
#include "base/HGInc.h" #include "base/HGInc.h"
#include "base/HGThread.h" #include "base/HGThread.h"
#include "WebMsg.h" #include "base/HGMsgPump.h"
#include "Msg_1_0.h"
#include <vector> #include <vector>
class Manager;
class WebServer class WebServer
{ {
public: public:
WebServer(class MsgLoop* loop, class Manager* manager); WebServer(HGMsgPump msgPump, Manager* manager);
virtual ~WebServer(); virtual ~WebServer();
class MsgLoop* GetLoop(); HGMsgPump GetMsgPump();
class Manager* GetManager(); Manager* GetManager();
bool Open(HGUShort port); bool Open(HGUShort port);
bool Close(); bool Close();
@ -23,12 +26,17 @@ public:
void DisConnect(HGUInt usrId); void DisConnect(HGUInt usrId);
protected: 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); int GetUserIndex(HGUInt id);
static void ThreadFunc(HGThread thread, HGPointer param); static void ThreadFunc(HGThread thread, HGPointer param);
protected: protected:
class MsgLoop* m_loop; HGMsgPump m_msgPump;
class Manager* m_manager; Manager* m_manager;
HGUInt m_currUserId; HGUInt m_currUserId;
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)

View File

@ -3,9 +3,9 @@
#include "base/HGInfo.h" #include "base/HGInfo.h"
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
WebUser::WebUser(class WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn) WebUser::WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn)
#else #else
WebUser::WebUser(class WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn) WebUser::WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn)
#endif #endif
{ {
m_server = server; m_server = server;
@ -48,16 +48,26 @@ HGUInt WebUser::GetId()
return m_id; return m_id;
} }
class MsgLoop* WebUser::GetLoop() Manager* WebUser::GetManager()
{
return m_server->GetLoop();
}
class Manager* WebUser::GetManager()
{ {
return m_server->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() void WebUser::ThreadFunc()
{ {
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "WebUser::ThreadFunc"); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "WebUser::ThreadFunc");

View File

@ -5,30 +5,34 @@
#include "base/HGInc.h" #include "base/HGInc.h"
#include "base/HGLock.h" #include "base/HGLock.h"
#include "base/HGThread.h" #include "base/HGThread.h"
#include "base/HGMsgPump.h"
class WebServer;
class Manager;
class WebUser class WebUser
{ {
public: public:
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
WebUser(class WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn); WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, SOCKET sockConn);
#else #else
WebUser(class WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn); WebUser(WebServer* server, HGUInt id, const char* ip, uint16_t port, int sockConn);
#endif #endif
virtual ~WebUser(); virtual ~WebUser();
void Open(); void Open();
HGUInt GetId(); HGUInt GetId();
class MsgLoop* GetLoop(); Manager* GetManager();
class Manager* GetManager();
protected: protected:
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:
class WebServer* m_server; WebServer* m_server;
HGLock m_cs; HGLock m_cs;
HGUInt m_id; HGUInt m_id;
char m_ip[16]; char m_ip[16];

View File

@ -2,13 +2,16 @@
#include "base/HGInc.h" #include "base/HGInc.h"
#include "base/HGThread.h" #include "base/HGThread.h"
#include "base/HGUtility.h" #include "base/HGUtility.h"
#include "1.0/MsgLoop.h" #include "base/HGMsgPump.h"
#include "2.0/MsgPump.h" #include "1.0/Manager.h"
#include "1.0/HttpServer.h"
#include "1.0/SockIoServer.h"
#include "1.0/MsgPumpCallback_1_0.h"
static void ThreadFunc(HGThread thread, HGPointer param) static void ThreadFunc(HGThread thread, HGPointer param)
{ {
(void)thread; (void)thread;
(void)param; HGMsgPump msgPump = (HGMsgPump)param;
HGChar cfgPath[256]; HGChar cfgPath[256];
HGBase_GetConfigPath(cfgPath, 256); HGBase_GetConfigPath(cfgPath, 256);
@ -18,8 +21,17 @@ static void ThreadFunc(HGThread thread, HGPointer param)
HGBase_GetProfileInt(cfgPath, "version", "verNum", 2, &verNum); HGBase_GetProfileInt(cfgPath, "version", "verNum", 2, &verNum);
if (1 == verNum) // 使用1.0版本接口 if (1 == verNum) // 使用1.0版本接口
{ {
MsgLoop loop; Manager manager(msgPump);
loop.Loop(); HttpServer httpServer(msgPump, &manager);
SockIoServer sockIoServer(msgPump, &manager);
httpServer.Open(18999);
sockIoServer.Open(28999);
HGBase_RunMsgPump(msgPump, HGMsgPumpCallback, NULL);
sockIoServer.Close();
httpServer.Close();
} }
else // 使用2.0版本接口 else // 使用2.0版本接口
{ {
@ -39,11 +51,17 @@ int main()
assert(0 == ret); assert(0 == ret);
#endif #endif
HGMsgPump msgPump = NULL;
HGBase_CreateMsgPump(&msgPump);
HGThread thread = NULL; HGThread thread = NULL;
HGBase_OpenThread(ThreadFunc, NULL, &thread); HGBase_OpenThread(ThreadFunc, msgPump, &thread);
HGBase_CloseThread(thread); HGBase_CloseThread(thread);
thread = NULL; thread = NULL;
HGBase_DestroyMsgPump(msgPump);
msgPump = NULL;
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
WSACleanup(); WSACleanup();
#endif #endif