整理websdk,将消息循环独立成通用模块
This commit is contained in:
parent
9fc3f86d45
commit
77afae2c9e
|
@ -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();
|
||||
}
|
|
@ -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__ */
|
|
@ -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;
|
||||
}
|
|
@ -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__ */
|
|
@ -2,8 +2,8 @@
|
|||
#include "HttpUser.h"
|
||||
#include "base/HGInfo.h"
|
||||
|
||||
HttpServer::HttpServer(class MsgLoop* loop, class Manager* manager)
|
||||
: WebServer(loop, manager)
|
||||
HttpServer::HttpServer(HGMsgPump msgPump, class Manager* manager)
|
||||
: WebServer(msgPump, manager)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
#define __HTTPSERVER_H__
|
||||
|
||||
#include "WebServer.h"
|
||||
#include "WebMsg.h"
|
||||
#include "Msg_1_0.h"
|
||||
|
||||
class HttpServer : public WebServer
|
||||
{
|
||||
public:
|
||||
HttpServer(class MsgLoop *loop, class Manager *manager);
|
||||
HttpServer(HGMsgPump msgPump, class Manager *manager);
|
||||
virtual ~HttpServer();
|
||||
|
||||
void Connect(const ConnectParam *param);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "HttpUser.h"
|
||||
#include "MsgLoop.h"
|
||||
#include "WebServer.h"
|
||||
#include "Manager.h"
|
||||
#include "base/HGInfo.h"
|
||||
#include "cJSON.h"
|
||||
|
@ -101,9 +101,9 @@ static std::string GetJsonStringValue(cJSON* json, const std::string& key)
|
|||
}
|
||||
|
||||
#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
|
||||
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
|
||||
: 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()
|
||||
{
|
||||
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "HttpUser::ThreadFunc");
|
||||
|
@ -741,12 +767,7 @@ void HttpUser::ThreadFunc()
|
|||
if (len <= 0)
|
||||
{
|
||||
// 这里跳出,可能是服务器关闭了socketConn,或者客户端关闭了socket,或者网络断开
|
||||
WebMsg msg;
|
||||
msg.msgId = WEB_MSGID_DISCONNET;
|
||||
msg.svr = m_server;
|
||||
msg.usrId = m_id;
|
||||
msg.param = NULL;
|
||||
GetLoop()->Send(&msg);
|
||||
PostDisConnectMsg();
|
||||
break;
|
||||
}
|
||||
else
|
||||
|
@ -783,12 +804,7 @@ void HttpUser::ThreadFunc()
|
|||
headDataTailLen = 0;
|
||||
if (!headInfo.Parse(headData.c_str()))
|
||||
{
|
||||
WebMsg msg;
|
||||
msg.msgId = WEB_MSGID_DISCONNET;
|
||||
msg.svr = m_server;
|
||||
msg.usrId = m_id;
|
||||
msg.param = NULL;
|
||||
GetLoop()->Send(&msg);
|
||||
PostDisConnectMsg();
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -803,22 +819,7 @@ void HttpUser::ThreadFunc()
|
|||
}
|
||||
else
|
||||
{
|
||||
HttpCmdParam* param = new HttpCmdParam;
|
||||
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;
|
||||
}
|
||||
|
||||
PostCmdMsg(headInfo, NULL, 0);
|
||||
headInfo.Clear();
|
||||
}
|
||||
}
|
||||
|
@ -829,24 +830,7 @@ void HttpUser::ThreadFunc()
|
|||
++getDataSize;
|
||||
if (getDataSize == dataSize)
|
||||
{
|
||||
HttpCmdParam* param = new HttpCmdParam;
|
||||
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;
|
||||
}
|
||||
PostCmdMsg(headInfo, data, dataSize);
|
||||
|
||||
delete [] data;
|
||||
data = NULL;
|
||||
|
|
|
@ -2,21 +2,24 @@
|
|||
#define __HTTPUSER_H__
|
||||
|
||||
#include "WebUser.h"
|
||||
#include "WebMsg.h"
|
||||
#include "Msg_1_0.h"
|
||||
|
||||
class WebServer;
|
||||
|
||||
class HttpUser : public WebUser
|
||||
{
|
||||
public:
|
||||
#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
|
||||
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
|
||||
virtual ~HttpUser();
|
||||
|
||||
void HandleCmd(const HttpCmdParam* param);
|
||||
|
||||
protected:
|
||||
void PostCmdMsg(const HttpHead& headInfo, const HGByte *data, HGUInt dataSize);
|
||||
virtual void ThreadFunc();
|
||||
|
||||
private:
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
#include "Manager.h"
|
||||
#include "MsgLoop.h"
|
||||
#include "base/HGBuffer.h"
|
||||
#include "base/HGBase64.h"
|
||||
#include "base/HGUtility.h"
|
||||
|
@ -137,9 +136,9 @@ void DevParam::Save(const std::string& cfgPath)
|
|||
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);
|
||||
m_devName.clear();
|
||||
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->devName = sane_dev->name;
|
||||
|
||||
WebMsg msg;
|
||||
msg.msgId = WEB_MSGID_OPENDEV;
|
||||
msg.svr = NULL;
|
||||
msg.usrId = 0;
|
||||
msg.param = openDevParam;
|
||||
bool b = p->m_loop->Send(&msg);
|
||||
if (!b)
|
||||
HGMsg msg;
|
||||
msg.id = MSGID_OPEN_DEVICE;
|
||||
msg.data = openDevParam;
|
||||
if (HGBASE_ERR_OK != HGBase_PostPumpMessage(p->m_msgPump, &msg))
|
||||
{
|
||||
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->devName = sane_dev->name;
|
||||
|
||||
WebMsg msg;
|
||||
msg.msgId = WEB_MSGID_CLOSEDEV;
|
||||
msg.svr = NULL;
|
||||
msg.usrId = 0;
|
||||
msg.param = closeDevParam;
|
||||
bool b = p->m_loop->Send(&msg);
|
||||
if (!b)
|
||||
HGMsg msg;
|
||||
msg.id = MSGID_CLOSE_DEVICE;
|
||||
msg.data = closeDevParam;
|
||||
if (HGBASE_ERR_OK != HGBase_PostPumpMessage(p->m_msgPump, &msg))
|
||||
{
|
||||
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);
|
||||
HGBase_LeaveLock(p->m_lock);
|
||||
|
||||
WebMsg msg;
|
||||
msg.msgId = WEB_MSGID_SCANFINISH;
|
||||
msg.svr = NULL;
|
||||
msg.usrId = 0;
|
||||
msg.param = NULL;
|
||||
p->m_loop->Send(&msg);
|
||||
HGMsg msg;
|
||||
msg.id = MSGID_SCAN_FINISH;
|
||||
msg.data = NULL;
|
||||
HGBase_PostPumpMessage(p->m_msgPump, &msg);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -5,10 +5,10 @@
|
|||
#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 "WebMsg.h"
|
||||
#include <vector>
|
||||
#include "Msg_1_0.h"
|
||||
#include <string>
|
||||
|
||||
#define SCANEVENT_ARRIVE 1L
|
||||
|
@ -86,7 +86,7 @@ typedef void (*ScanEvent)(HGUInt event, void *value1, void *value2, void *param)
|
|||
class Manager
|
||||
{
|
||||
public:
|
||||
Manager(class MsgLoop* loop);
|
||||
Manager(HGMsgPump msgPump);
|
||||
~Manager();
|
||||
|
||||
// 打开设备
|
||||
|
@ -175,7 +175,7 @@ private:
|
|||
static int sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param);
|
||||
|
||||
private:
|
||||
class MsgLoop* m_loop;
|
||||
HGMsgPump m_msgPump;
|
||||
HGLock m_lock;
|
||||
std::string m_devName;
|
||||
SANE_Handle m_devHandle;
|
||||
|
|
|
@ -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();
|
||||
}
|
|
@ -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__ */
|
|
@ -0,0 +1,6 @@
|
|||
#include "MsgPumpCallback_1_0.h"
|
||||
|
||||
void HGMsgPumpCallback(HGMsgPump msgPump, const HGMsg* msg, HGPointer param)
|
||||
{
|
||||
// 处理
|
||||
}
|
|
@ -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__ */
|
|
@ -1,60 +1,19 @@
|
|||
#ifndef __WEBMSG_H__
|
||||
#define __WEBMSG_H__
|
||||
#ifndef __MSG_1_0_H__
|
||||
#define __MSG_1_0_H__
|
||||
|
||||
#include "base/HGDef.h"
|
||||
#include "base/HGInc.h"
|
||||
#include "HttpHead.h"
|
||||
#include <string>
|
||||
|
||||
enum WebMsgId
|
||||
{
|
||||
WEB_MSGID_QUIT = 1,
|
||||
WEB_MSGID_CONNET,
|
||||
WEB_MSGID_DISCONNET,
|
||||
WEB_MSGID_COMMAND,
|
||||
WEB_MSGID_EVENT,
|
||||
WEB_MSGID_OPENDEV,
|
||||
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;
|
||||
};
|
||||
#define MSGID_OPEN_DEVICE 1L
|
||||
#define MSGID_CLOSE_DEVICE 2L
|
||||
#define MSGID_SCAN_FINISH 3L
|
||||
#define MSGID_CONNECT 4L
|
||||
#define MSGID_DISCONNECT 5L
|
||||
#define MSGID_HTTP_COMMAND 6L
|
||||
#define MSGID_SOCKIO_COMMAND 7L
|
||||
#define MSGID_SOCKIO_EVENT 8L
|
||||
|
||||
struct OpenDevParam
|
||||
{
|
||||
|
@ -66,4 +25,47 @@ struct CloseDevParam
|
|||
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__ */
|
|
@ -2,8 +2,8 @@
|
|||
#include "SockIoUser.h"
|
||||
#include "base/HGInfo.h"
|
||||
|
||||
SockIoServer::SockIoServer(class MsgLoop* loop, class Manager* manager)
|
||||
: WebServer(loop, manager)
|
||||
SockIoServer::SockIoServer(HGMsgPump msgPump, class Manager* manager)
|
||||
: WebServer(msgPump, manager)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
#define __SOCKIOSERVER_H__
|
||||
|
||||
#include "WebServer.h"
|
||||
#include "WebMsg.h"
|
||||
#include "Msg_1_0.h"
|
||||
|
||||
class SockIoServer : public WebServer
|
||||
{
|
||||
public:
|
||||
SockIoServer(class MsgLoop *loop, class Manager *manager);
|
||||
SockIoServer(HGMsgPump msgPump, class Manager *manager);
|
||||
virtual ~SockIoServer();
|
||||
|
||||
void Connect(const ConnectParam *param);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "SockIoUser.h"
|
||||
#include "MsgLoop.h"
|
||||
#include "WebServer.h"
|
||||
#include "Manager.h"
|
||||
#include "base/HGInfo.h"
|
||||
#include "base/HGUtility.h"
|
||||
|
@ -8,9 +8,9 @@
|
|||
#include "cJSON.h"
|
||||
|
||||
#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
|
||||
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
|
||||
: WebUser(server, id, ip, port, sockConn)
|
||||
{
|
||||
|
@ -74,6 +74,46 @@ void SockIoUser::HandleEvent(const SockIoEvtParam* param)
|
|||
SendResponse(param->data, param->size, HGTRUE);
|
||||
}
|
||||
|
||||
void SockIoUser::PostCmdMsg(const HGByte* data, HGUInt dataSize)
|
||||
{
|
||||
SockIoCmdParam* param = new SockIoCmdParam;
|
||||
param->svr = m_server;
|
||||
param->usrId = m_id;
|
||||
param->data = new HGByte[dataSize];
|
||||
param->size = dataSize;
|
||||
memcpy(param->data, data, dataSize);
|
||||
|
||||
HGMsg msg;
|
||||
msg.id = MSGID_SOCKIO_COMMAND;
|
||||
msg.data = param;
|
||||
if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_server->GetMsgPump(), &msg))
|
||||
{
|
||||
delete[] param->data;
|
||||
param->size = 0;
|
||||
delete param;
|
||||
}
|
||||
}
|
||||
|
||||
void SockIoUser::PostEventMsg(const HGByte* data, HGUInt dataSize)
|
||||
{
|
||||
SockIoEvtParam* param = new SockIoEvtParam;
|
||||
param->svr = m_server;
|
||||
param->usrId = m_id;
|
||||
param->data = new HGByte[dataSize];
|
||||
param->size = dataSize;
|
||||
memcpy(param->data, data, dataSize);
|
||||
|
||||
HGMsg msg;
|
||||
msg.id = MSGID_SOCKIO_EVENT;
|
||||
msg.data = param;
|
||||
if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_server->GetMsgPump(), &msg))
|
||||
{
|
||||
delete[] param->data;
|
||||
param->size = 0;
|
||||
delete param;
|
||||
}
|
||||
}
|
||||
|
||||
void SockIoUser::ThreadFunc()
|
||||
{
|
||||
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SockIoUser::ThreadFunc");
|
||||
|
@ -106,12 +146,7 @@ void SockIoUser::ThreadFunc()
|
|||
if (len <= 0)
|
||||
{
|
||||
// 这里跳出,可能是服务器关闭了socketConn,或者客户端关闭了socket,或者网络断开
|
||||
WebMsg msg;
|
||||
msg.msgId = WEB_MSGID_DISCONNET;
|
||||
msg.svr = m_server;
|
||||
msg.usrId = m_id;
|
||||
msg.param = NULL;
|
||||
GetLoop()->Send(&msg);
|
||||
PostDisConnectMsg();
|
||||
break;
|
||||
}
|
||||
else
|
||||
|
@ -151,12 +186,7 @@ void SockIoUser::ThreadFunc()
|
|||
|
||||
if (!shakeRet)
|
||||
{
|
||||
WebMsg msg;
|
||||
msg.msgId = WEB_MSGID_DISCONNET;
|
||||
msg.svr = m_server;
|
||||
msg.usrId = m_id;
|
||||
msg.param = NULL;
|
||||
GetLoop()->Send(&msg);
|
||||
PostDisConnectMsg();
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -179,12 +209,7 @@ void SockIoUser::ThreadFunc()
|
|||
{
|
||||
if ((0x80 | 0x08) == headData[0]) // 断开连接
|
||||
{
|
||||
WebMsg msg;
|
||||
msg.msgId = WEB_MSGID_DISCONNET;
|
||||
msg.svr = m_server;
|
||||
msg.usrId = m_id;
|
||||
msg.param = NULL;
|
||||
GetLoop()->Send(&msg);
|
||||
PostDisConnectMsg();
|
||||
break;
|
||||
}
|
||||
else if ((0x80 | 0x09) == headData[0]) // PING帧
|
||||
|
@ -211,12 +236,7 @@ void SockIoUser::ThreadFunc()
|
|||
}
|
||||
else // 帧错误,断开连接
|
||||
{
|
||||
WebMsg msg;
|
||||
msg.msgId = WEB_MSGID_DISCONNET;
|
||||
msg.svr = m_server;
|
||||
msg.usrId = m_id;
|
||||
msg.param = NULL;
|
||||
GetLoop()->Send(&msg);
|
||||
PostDisConnectMsg();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -224,12 +244,7 @@ void SockIoUser::ThreadFunc()
|
|||
{
|
||||
if (0 == (headData[1] & 0x80)) // 必须经过掩码处理
|
||||
{
|
||||
WebMsg msg;
|
||||
msg.msgId = WEB_MSGID_DISCONNET;
|
||||
msg.svr = m_server;
|
||||
msg.usrId = m_id;
|
||||
msg.param = NULL;
|
||||
GetLoop()->Send(&msg);
|
||||
PostDisConnectMsg();
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -237,12 +252,7 @@ void SockIoUser::ThreadFunc()
|
|||
{
|
||||
if (0x80 != headData[1])
|
||||
{
|
||||
WebMsg msg;
|
||||
msg.msgId = WEB_MSGID_DISCONNET;
|
||||
msg.svr = m_server;
|
||||
msg.usrId = m_id;
|
||||
msg.param = NULL;
|
||||
GetLoop()->Send(&msg);
|
||||
PostDisConnectMsg();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -250,12 +260,7 @@ void SockIoUser::ThreadFunc()
|
|||
{
|
||||
if (0x80 != headData[1])
|
||||
{
|
||||
WebMsg msg;
|
||||
msg.msgId = WEB_MSGID_DISCONNET;
|
||||
msg.svr = m_server;
|
||||
msg.usrId = m_id;
|
||||
msg.param = NULL;
|
||||
GetLoop()->Send(&msg);
|
||||
PostDisConnectMsg();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -268,12 +273,7 @@ void SockIoUser::ThreadFunc()
|
|||
|
||||
if (0 == nCmdSize)
|
||||
{
|
||||
WebMsg msg;
|
||||
msg.msgId = WEB_MSGID_DISCONNET;
|
||||
msg.svr = m_server;
|
||||
msg.usrId = m_id;
|
||||
msg.param = NULL;
|
||||
GetLoop()->Send(&msg);
|
||||
PostDisConnectMsg();
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -303,12 +303,7 @@ void SockIoUser::ThreadFunc()
|
|||
|
||||
if (0 == nCmdSize)
|
||||
{
|
||||
WebMsg msg;
|
||||
msg.msgId = WEB_MSGID_DISCONNET;
|
||||
msg.svr = m_server;
|
||||
msg.usrId = m_id;
|
||||
msg.param = NULL;
|
||||
GetLoop()->Send(&msg);
|
||||
PostDisConnectMsg();
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -342,12 +337,7 @@ void SockIoUser::ThreadFunc()
|
|||
|
||||
if ((0 != nCmdSizeHigh) || (0 == nCmdSize))
|
||||
{
|
||||
WebMsg msg;
|
||||
msg.msgId = WEB_MSGID_DISCONNET;
|
||||
msg.svr = m_server;
|
||||
msg.usrId = m_id;
|
||||
msg.param = NULL;
|
||||
GetLoop()->Send(&msg);
|
||||
PostDisConnectMsg();
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -403,23 +393,7 @@ void SockIoUser::ThreadFunc()
|
|||
}
|
||||
else
|
||||
{
|
||||
SockIoCmdParam* param = new SockIoCmdParam;
|
||||
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;
|
||||
}
|
||||
PostCmdMsg(&vAllData[0], vAllData.size());
|
||||
}
|
||||
|
||||
bHandle = false;
|
||||
|
@ -479,24 +453,7 @@ void SockIoUser::ScanCallback(HGUInt event, void* value1, void* value2, void* pa
|
|||
|
||||
if (NULL != resp)
|
||||
{
|
||||
SockIoEvtParam* param = new SockIoEvtParam;
|
||||
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;
|
||||
}
|
||||
|
||||
p->PostEventMsg((const HGByte*)resp, strlen(resp));
|
||||
delete[] resp;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,15 +2,17 @@
|
|||
#define __SOCKIOUSER_H__
|
||||
|
||||
#include "WebUser.h"
|
||||
#include "WebMsg.h"
|
||||
#include "Msg_1_0.h"
|
||||
|
||||
class WebServer;
|
||||
|
||||
class SockIoUser : public WebUser
|
||||
{
|
||||
public:
|
||||
#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
|
||||
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
|
||||
virtual ~SockIoUser();
|
||||
|
||||
|
@ -18,6 +20,8 @@ public:
|
|||
void HandleEvent(const SockIoEvtParam* param);
|
||||
|
||||
protected:
|
||||
void PostCmdMsg(const HGByte* data, HGUInt dataSize);
|
||||
void PostEventMsg(const HGByte* data, HGUInt dataSize);
|
||||
virtual void ThreadFunc();
|
||||
|
||||
private:
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
#include "WebServer.h"
|
||||
#include "WebUser.h"
|
||||
#include "MsgLoop.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_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;
|
||||
}
|
||||
|
@ -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 nIndex = -1;
|
||||
|
@ -180,25 +205,6 @@ void WebServer::ThreadFunc(HGThread thread, HGPointer param)
|
|||
break;
|
||||
}
|
||||
|
||||
ConnectParam* param = new ConnectParam;
|
||||
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
|
||||
}
|
||||
p->PostConnectMsg(inet_ntoa(addrClient.sin_addr), ntohs(addrClient.sin_port), socketConn);
|
||||
}
|
||||
}
|
|
@ -4,17 +4,20 @@
|
|||
#include "base/HGDef.h"
|
||||
#include "base/HGInc.h"
|
||||
#include "base/HGThread.h"
|
||||
#include "WebMsg.h"
|
||||
#include "base/HGMsgPump.h"
|
||||
#include "Msg_1_0.h"
|
||||
#include <vector>
|
||||
|
||||
class Manager;
|
||||
|
||||
class WebServer
|
||||
{
|
||||
public:
|
||||
WebServer(class MsgLoop* loop, class Manager* manager);
|
||||
WebServer(HGMsgPump msgPump, Manager* manager);
|
||||
virtual ~WebServer();
|
||||
|
||||
class MsgLoop* GetLoop();
|
||||
class Manager* GetManager();
|
||||
HGMsgPump GetMsgPump();
|
||||
Manager* GetManager();
|
||||
|
||||
bool Open(HGUShort port);
|
||||
bool Close();
|
||||
|
@ -23,12 +26,17 @@ public:
|
|||
void DisConnect(HGUInt usrId);
|
||||
|
||||
protected:
|
||||
#if defined(HG_CMP_MSC)
|
||||
void PostConnectMsg(const char* ip, uint16_t port, SOCKET sockConn);
|
||||
#else
|
||||
void PostConnectMsg(const char* ip, uint16_t port, int sockConn);
|
||||
#endif
|
||||
int GetUserIndex(HGUInt id);
|
||||
static void ThreadFunc(HGThread thread, HGPointer param);
|
||||
|
||||
protected:
|
||||
class MsgLoop* m_loop;
|
||||
class Manager* m_manager;
|
||||
HGMsgPump m_msgPump;
|
||||
Manager* m_manager;
|
||||
|
||||
HGUInt m_currUserId;
|
||||
#if defined(HG_CMP_MSC)
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
#include "base/HGInfo.h"
|
||||
|
||||
#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
|
||||
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
|
||||
{
|
||||
m_server = server;
|
||||
|
@ -48,16 +48,26 @@ HGUInt WebUser::GetId()
|
|||
return m_id;
|
||||
}
|
||||
|
||||
class MsgLoop* WebUser::GetLoop()
|
||||
{
|
||||
return m_server->GetLoop();
|
||||
}
|
||||
|
||||
class Manager* WebUser::GetManager()
|
||||
Manager* WebUser::GetManager()
|
||||
{
|
||||
return m_server->GetManager();
|
||||
}
|
||||
|
||||
void WebUser::PostDisConnectMsg()
|
||||
{
|
||||
DisConnectParam* param = new DisConnectParam;
|
||||
param->svr = m_server;
|
||||
param->usrId = m_id;
|
||||
|
||||
HGMsg msg;
|
||||
msg.id = MSGID_DISCONNECT;
|
||||
msg.data = param;
|
||||
if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_server->GetMsgPump(), &msg))
|
||||
{
|
||||
delete param;
|
||||
}
|
||||
}
|
||||
|
||||
void WebUser::ThreadFunc()
|
||||
{
|
||||
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "WebUser::ThreadFunc");
|
||||
|
|
|
@ -5,30 +5,34 @@
|
|||
#include "base/HGInc.h"
|
||||
#include "base/HGLock.h"
|
||||
#include "base/HGThread.h"
|
||||
#include "base/HGMsgPump.h"
|
||||
|
||||
class WebServer;
|
||||
class Manager;
|
||||
|
||||
class WebUser
|
||||
{
|
||||
public:
|
||||
#if defined(HG_CMP_MSC)
|
||||
WebUser(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
|
||||
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
|
||||
virtual ~WebUser();
|
||||
|
||||
void Open();
|
||||
HGUInt GetId();
|
||||
class MsgLoop* GetLoop();
|
||||
class Manager* GetManager();
|
||||
Manager* GetManager();
|
||||
|
||||
protected:
|
||||
void PostDisConnectMsg();
|
||||
virtual void ThreadFunc();
|
||||
|
||||
private:
|
||||
static void ThreadFunc(HGThread thread, HGPointer param);
|
||||
|
||||
protected:
|
||||
class WebServer* m_server;
|
||||
WebServer* m_server;
|
||||
HGLock m_cs;
|
||||
HGUInt m_id;
|
||||
char m_ip[16];
|
||||
|
|
|
@ -2,13 +2,16 @@
|
|||
#include "base/HGInc.h"
|
||||
#include "base/HGThread.h"
|
||||
#include "base/HGUtility.h"
|
||||
#include "1.0/MsgLoop.h"
|
||||
#include "2.0/MsgPump.h"
|
||||
#include "base/HGMsgPump.h"
|
||||
#include "1.0/Manager.h"
|
||||
#include "1.0/HttpServer.h"
|
||||
#include "1.0/SockIoServer.h"
|
||||
#include "1.0/MsgPumpCallback_1_0.h"
|
||||
|
||||
static void ThreadFunc(HGThread thread, HGPointer param)
|
||||
{
|
||||
(void)thread;
|
||||
(void)param;
|
||||
HGMsgPump msgPump = (HGMsgPump)param;
|
||||
|
||||
HGChar cfgPath[256];
|
||||
HGBase_GetConfigPath(cfgPath, 256);
|
||||
|
@ -18,8 +21,17 @@ static void ThreadFunc(HGThread thread, HGPointer param)
|
|||
HGBase_GetProfileInt(cfgPath, "version", "verNum", 2, &verNum);
|
||||
if (1 == verNum) // 使用1.0版本接口
|
||||
{
|
||||
MsgLoop loop;
|
||||
loop.Loop();
|
||||
Manager manager(msgPump);
|
||||
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版本接口
|
||||
{
|
||||
|
@ -39,11 +51,17 @@ int main()
|
|||
assert(0 == ret);
|
||||
#endif
|
||||
|
||||
HGMsgPump msgPump = NULL;
|
||||
HGBase_CreateMsgPump(&msgPump);
|
||||
|
||||
HGThread thread = NULL;
|
||||
HGBase_OpenThread(ThreadFunc, NULL, &thread);
|
||||
HGBase_OpenThread(ThreadFunc, msgPump, &thread);
|
||||
HGBase_CloseThread(thread);
|
||||
thread = NULL;
|
||||
|
||||
HGBase_DestroyMsgPump(msgPump);
|
||||
msgPump = NULL;
|
||||
|
||||
#if defined(HG_CMP_MSC)
|
||||
WSACleanup();
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue