Exemplo n.º 1
0
	Bool HawkGateThread::RecvGateMsg(SID& iSid, OctetsStream* pOctets)
	{
		if (pOctets)
		{
			//接收SID头信息
			pOctets->Clear();
			Size_t iSize = (Size_t)pOctets->Capacity();
			if (!m_pZmq->Recv(pOctets->Begin(), iSize))
				return false;

			//SID数据校验
			HawkAssert(iSize == sizeof(iSid));
			iSid = *((SID*)pOctets->Begin());
			if (iSize != sizeof(iSid))
				return false;

			//消息状态校验
			Bool bRecvMore = m_pZmq->IsWaitRecv();
			HawkAssert(bRecvMore);
			if (!bRecvMore)
				return false;

			//接收数据体
			pOctets->Clear();
			iSize = (Size_t)pOctets->Capacity();
			if (!m_pZmq->Recv(pOctets->Begin(), iSize))
				return false;

			pOctets->Resize((UInt32)iSize);
			return true;
		}
		return false;
	}
Exemplo n.º 2
0
	Bool HawkGateProxy::RecvProtocol(GateMsgHeader& sHeader, HawkProtocol*& pProto, Int32 iTimeout)
	{
		if (m_pProxyZmq && m_pOctets)
		{
			if (m_pProxyZmq->PollEvent(HEVENT_READ, iTimeout))
			{
				//提取消息头
				m_pOctets->Clear();
				Size_t iSize = (Size_t)m_pOctets->Capacity();
				if (!m_pProxyZmq->Recv(m_pOctets->Begin(), iSize))
					return false;

				sHeader = *((GateMsgHeader*)m_pOctets->Begin());
				Bool bRecvMore = m_pProxyZmq->IsWaitRecv();			
				HawkAssert(iSize == sizeof(sHeader) && bRecvMore);
				if (iSize != sizeof(sHeader) || !bRecvMore)
					return false;

				//提取协议内容
				m_pOctets->Clear();
				iSize = (Size_t)m_pOctets->Capacity();
				if (!m_pProxyZmq->Recv(m_pOctets->Begin(), iSize))
					return false;				

				//协议解析
				m_pOctets->Resize(iSize);
				try
				{
					pProto = P_ProtocolManager->Decode(*m_pOctets);
				}
				catch (HawkException& rhsExcep)
				{
					//协议解析异常退出
					HawkFmtError("Session Decode Protocol Error, Msg: %s", rhsExcep.GetMsg().c_str());
					//释放协议
					P_ProtocolManager->ReleaseProto(pProto);
					pProto = 0;

					return false;
				}

				HawkAssert(!m_pProxyZmq->IsWaitRecv());

				return pProto != 0;
			}			
		}
		return false;
	}	
Exemplo n.º 3
0
	//会话写事件处理函数
	void hawk_GateSessionWrite(bufferevent* pEvent, void* pArgs) 
	{
		HawkGateThread::Session* pSession = (HawkGateThread::Session*)pArgs;
		HawkAssert(pSession);
		if (pSession && pSession->GThread)
			pSession->GThread->OnSessionEvent(HEVENT_WRITE, pSession);
	}
Exemplo n.º 4
0
	HsGame::HsGame() : HawkAppFrame(XID(HSO_MANAGER, HMI_APP))
	{
		HawkAssert(g_Game == 0);
		g_Game = this;

		#include "ProtocolReg.inl"
	}
Exemplo n.º 5
0
	Bool HawkSession::SendRawData(void* pData, Int32 iSize, Bool bFlush)
	{
		if (IsValid())
		{
			//输出加密
			HawkOctetsStream xOS(pData, iSize);
			if(m_pOSecurity)
				m_pOSecurity->Update(xOS);

			//输出缓冲区满, 先写出
			if (m_pOutputBuf->EmptyCap() < xOS.Size())
				FlushOutput();

			//错误检查
			HawkAssert(m_pOutputBuf->EmptyCap() >= xOS.Size());

			//添加到发送缓冲区
			m_pOutputBuf->Insert(m_pOutputBuf->End(), xOS.Begin(), xOS.End());

			//实时发送
			if (bFlush)
				FlushOutput();
		
			return true;
		}
		return false;
	}
Exemplo n.º 6
0
	Bool HawkLogServer::Init(const AString& sSvrAddr, const AString& sLogFile, Int32 iCacheSize, Int32 iCacheTime)
	{
		m_iCacheTime = iCacheTime;
		HawkAssert(iCacheSize > 0);
		if(!m_sSocket.Create(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) 
		{
			HawkPrint("LogServer Init Socket Error.");
			return false;
		}

		m_sSocket.SetNoDelay(true);
		m_sSocket.SetBlocking(false);

		if(!m_sSocket.Bind(SocketAddr(sSvrAddr)))
		{
			HawkPrint("LogServer Bind Socket Error.");
			return false;
		}

		m_pLogFile = new HawkDiskFile;
		if (!m_pLogFile->Open(sLogFile, HawkFile::OPEN_APPEND))
		{
			HawkPrint("LogServer Open LogFile Error.");
			return false;
		}

		if (!m_pLogCache)
			m_pLogCache = new HawkOctets(iCacheSize);
		
		return true;
	}
Exemplo n.º 7
0
	//会话错误事件处理函数
	void hawk_GateSessionError(bufferevent* pEvent, Short iWhat, void* pArgs) 
	{
		HawkGateThread::Session* pSession = (HawkGateThread::Session*)pArgs;
		HawkAssert(pSession);
		if (pSession && pSession->GThread)
			pSession->GThread->OnSessionEvent(HEVENT_EXCEPT, pSession);
	}
Exemplo n.º 8
0
	Bool HawkLogServer::Init(const AString& sSvrAddr, const HawkDBConn& sConn, Int32 iCacheSize, Int32 iCacheTime)
	{
		m_iCacheTime = iCacheTime;
		HawkAssert(iCacheSize > 0);
		if(!m_sSocket.Create(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) 
		{
			HawkPrint("LogServer Init Socket Error.");
			return false;
		}

		m_sSocket.SetNoDelay(true);
		m_sSocket.SetBlocking(false);

		if(!m_sSocket.Bind(SocketAddr(sSvrAddr)))
		{
			HawkPrint("LogServer Bind Socket Error.");
			return false;
		}

		m_pLogDB = P_DBManager->OpenDatabase("LogDB", sConn);
		if (!m_pLogDB)
		{
			HawkPrint("LogServer Open LogDB Error.");
			return false;
		}

		if (!m_pLogCache)
			m_pLogCache = new HawkOctets(iCacheSize);

		return true;
	}
Exemplo n.º 9
0
	Bool HawkGateThread::Init(UInt32 iBaseId)
	{
		HawkAssert(!m_pThread && !m_pBase && !m_pZmq);

		m_iBaseSid = iBaseId;

		//创建通用缓冲
		if (!m_pOctets)
			m_pOctets = new OctetsStream(m_pGateway->GetBufSize());

		//创建线程
		if (!m_pThread)
			m_pThread = new HawkThread(hawk_GateThreadRoutine);

		//创建事件基础对象
		if (!m_pBase)
		{
			event_config* pCfg = event_config_new();
			if (!pCfg) 
			{
				HawkPrint("Create EventConfig Failed.");
				return false;
			}
			
#ifdef PLATFORM_LINUX
			event_config_require_features(pCfg, EV_FEATURE_ET);
#endif
			event_config_set_flag(pCfg, EVENT_BASE_FLAG_NOLOCK);

			m_pBase = (void*)event_base_new_with_config(pCfg);
			event_config_free(pCfg);
			if (!m_pBase)
			{
				HawkPrint("Create EventBase Failed.");
				return false;
			}

			if (m_iBaseSid == 1)
			{
				const Char* pszMethod = event_base_get_method((event_base*)m_pBase);
				if (pszMethod && strlen(pszMethod))
				{
					HawkFmtPrint("Kernel Event Notification Mechanism: %s", pszMethod);
				}
			}			
		}

		//创建ZMQ对象
		if (!m_pZmq)
		{
			m_pZmq = P_ZmqManager->CreateZmq(HawkZmq::HZMQ_DEALER);
			m_pZmq->SetIdentity(&m_iBaseSid, sizeof(m_iBaseSid));
			m_pZmq->Connect(m_pGateway->GetThreadZmqAddr());
		}

		return true;
	}
Exemplo n.º 10
0
	//线程入口函数
	PVoid hawk_GateThreadRoutine(void* pArgs)
	{
		HawkGateThread* pThread = (HawkGateThread*)pArgs;
		HawkAssert(pThread);
		if (pThread)
			pThread->OnThreadLoop();

		return 0;
	}
Exemplo n.º 11
0
	Bool HawkAppFrame::SendProtocol(SID iSid, Protocol* pProto)
	{
		HawkAssert(iSid && pProto);

		if (!iSid || !pProto || !m_pGateProxy || !m_pGateProxy->IsConnect())
			return false;

		return m_pGateProxy->SendProtocol(iSid, pProto);
	}
Exemplo n.º 12
0
	HsRobot::HsRobot()
	{
		HawkAssert(!g_Robot || g_Robot == this);
		g_Robot = this;

		#include "ProtocolReg.inl"

		m_iCount   = 0;
		m_bRunning = false;
	}
Exemplo n.º 13
0
	Bool HawkMsgManager::Register(Int32 iMsg)
	{
		MsgRegMap::iterator it = m_mReg.find(iMsg);
		HawkAssert(it == m_mReg.end());
		if (it == m_mReg.end())
		{
			m_mReg[iMsg] = iMsg;
			return true;
		}
		return false;
	}
Exemplo n.º 14
0
	Bool HawkProtocolManager::Register(ProtoType iType, HawkProtocol* pProto)
	{		
		ProtocolMap::iterator it = m_mRegister.find(iType);
		HawkAssert(it == m_mRegister.end());
		if (pProto && it == m_mRegister.end())
		{
			pProto->AddRef();
			m_mRegister[iType] = pProto;	
			return true;
		}
		return false;
	}
Exemplo n.º 15
0
	Bool HawkSpinLock::Unlock()
	{
		HawkAssert(m_pSpinLock);

#ifdef _DEBUG
		m_bLocked = false;
		m_sFile   = "";
		m_iLine   = 0;
		m_iThread = 0;
#endif
		if (pthread_spin_unlock((pthread_spinlock_t*)m_pSpinLock) == HAWK_OK)
			return true;

		return false;
	}
Exemplo n.º 16
0
	HawkAppObj*	HsGame::AppCreateObj(const XID& sXid)
	{
		HawkAppObj* pObj = 0;

		if (sXid.Type == HSO_MANAGER)
		{
			//TODO: 创建单例对象
		}
		else if (sXid.Type == HSO_PLAYER)
		{
			pObj = new HsPlayer(sXid);
		}

		HawkAssert(pObj != 0);
		return pObj;
	}
Exemplo n.º 17
0
	Bool HawkSpinLock::TryLock(const AString& sFile,Int32 iLine)
	{
		HawkAssert(m_pSpinLock);

		if (pthread_spin_trylock((pthread_spinlock_t*)m_pSpinLock) == HAWK_OK)
		{
#ifdef _DEBUG
			m_bLocked = true;
			m_sFile   = sFile;
			m_iLine   = iLine;
			m_iThread = HawkOSOperator::GetThreadId();
#endif
			return true;
		}
		return false;
	}
Exemplo n.º 18
0
	Bool HawkGateProxy::SendRawData(const SvrMsgHeader& sHeader, const OctetsStream* pData)
	{
		if (m_iProxyState == HAWK_OK)
		{
			HawkZmq* pProxyZmq = GetThreadProxy();
			HawkAssert(pProxyZmq);

			if (pData && pProxyZmq)
			{
				if (!pProxyZmq->Send((void*)&sHeader, sizeof(sHeader), HawkZmq::HZMQ_SNDMORE))
					return false;

				if (!pProxyZmq->Send(pData->AvailableData(), pData->Size()))
					return false;

				return true;
			}
		}
		return false;
	}
Exemplo n.º 19
0
	Bool HawkGateProxy::SendProtocol(const SvrMsgHeader& sHeader, HawkProtocol* pProto)
	{
		if (m_iProxyState == HAWK_OK)
		{
			HawkZmq* pProxyZmq = GetThreadProxy();
			HawkAssert(pProxyZmq);

			if (pProto && pProxyZmq)
			{
				if (!pProxyZmq->Send((void*)&sHeader, sizeof(sHeader), HawkZmq::HZMQ_SNDMORE))
					return false;

				if (!pProxyZmq->SendProtocol(pProto))
					return false;

				return true;		
			}
		}
		return false;
	}
Exemplo n.º 20
0
	HawkQuaternion& HawkQuaternion::SetFromMatrix3(const HawkMatrix3& oRotMat)
	{
		HawkAssert(oRotMat.IsRotationMatrix());

		Float fTrace = oRotMat[0][0]+oRotMat[1][1]+oRotMat[2][2];
		Float fRoot  = 0;

		if ( fTrace > 0.0f )
		{
			fRoot = HawkMath::Sqrt(fTrace + 1.0f);
			W     = 0.5f*fRoot;
			fRoot = 0.5f/fRoot;
			X = (oRotMat[1][2]-oRotMat[2][1])*fRoot;
			Y = (oRotMat[2][0]-oRotMat[0][2])*fRoot;
			Z = (oRotMat[0][1]-oRotMat[1][0])*fRoot;
		}
		else
		{
			Int32 iNext[3] = {1, 2, 0};
			Int32 i = 0;

			if (oRotMat[1][1] > oRotMat[0][0])
				i = 1;

			if (oRotMat[2][2] > oRotMat[i][i])
				i = 2;

			Int32 j = iNext[i];
			Int32 k = iNext[j];

			fRoot = HawkMath::Sqrt(oRotMat[i][i]-oRotMat[j][j]-oRotMat[k][k] + 1.0f);
			Float* apkQuat[3] = { &X, &Y, &Z };
			*apkQuat[i] = 0.5f*fRoot;
			fRoot       = 0.5f/fRoot;
			W = (oRotMat[j][k]-oRotMat[k][j])*fRoot;
			*apkQuat[j] = (oRotMat[i][j]+oRotMat[j][i])*fRoot;
			*apkQuat[k] = (oRotMat[i][k]+oRotMat[k][i])*fRoot;
		}
		return *this;
	}
Exemplo n.º 21
0
	Bool HawkGateProxy::SendNotify(const GateNotify& sNotify, const OctetsStream* pExtData)
	{
		HawkZmq* pProxyZmq = GetThreadProxy();
		HawkAssert(pProxyZmq);

		if (pProxyZmq)
		{
			SvrMsgHeader sHeader;
			sHeader.Sid = 0;

			if (!pProxyZmq->Send((void*)&sHeader, sizeof(sHeader), HawkZmq::HZMQ_SNDMORE))
				return false;

			if (!pProxyZmq->Send((void*)&sNotify, sizeof(sNotify), pExtData ? HawkZmq::HZMQ_SNDMORE : 0))
				return false;

			if (pExtData && !pProxyZmq->Send((void*)pExtData->Begin(), pExtData->Size()))
				return false;

			return true;
		}

		return false;
	}
Exemplo n.º 22
0
	//获得边
	const HawkVector2D& HawkRect2D::GetEdge(Int32 iIdx) const
	{
		HawkAssert(iIdx>=0 && iIdx<=1);

		return Edge[iIdx];
	}
Exemplo n.º 23
0
	//设置边
	void HawkRect2D::SetEdge(Int32 iIdx,const HawkVector2D& oEdge)
	{
		HawkAssert(iIdx>=0 && iIdx<=1);

		Edge[iIdx] = oEdge;
	}
Exemplo n.º 24
0
	HsLogger::HsLogger() : HawkLogServer()
	{
		HawkAssert(!g_Logger || g_Logger == this);
		g_Logger = this;
	}
Exemplo n.º 25
0
	Bool HawkZmqManager::ProxyZmq(HawkZmq* pFrontend, HawkZmq* pBackend, Bool bBothway, Int32 iTimeout, Bool bOnce)
	{
		HawkAssert(pFrontend && pBackend);
		if (!pFrontend || !pBackend)
			return false;		

		zmq_pollitem_t items[] = 
		{
			{ pFrontend->GetHandle(), 0, 0, 0 },
			{ pBackend->GetHandle(),  0, 0, 0 }
		};
		
		items[0].events = ZMQ_POLLIN;
		if (bBothway)
			items[1].events = ZMQ_POLLIN;

		do
		{
			items[0].revents = 0;
			items[1].revents = 0;

			if (zmq_poll(items, 2, iTimeout) < 0)
				return false;

			if (items[0].revents & ZMQ_POLLIN) 
			{
				zmq_msg_t sMsg;
				if (zmq_msg_init(&sMsg) != HAWK_OK)
					return false;

				while (true) 
				{
					if (zmq_recvmsg(items[0].socket, &sMsg, 0) < 0)
					{
						zmq_msg_close(&sMsg);
						return false;
					}

					Int32  iRecvMore = 0;
					Size_t iLen = sizeof(iRecvMore);
					if (zmq_getsockopt(items[0].socket, ZMQ_RCVMORE, &iRecvMore, &iLen) < 0)
					{
						zmq_msg_close(&sMsg);
						return false;
					}

					if (zmq_sendmsg(items[1].socket, &sMsg, iRecvMore? ZMQ_SNDMORE : 0) < 0)
					{
						zmq_msg_close(&sMsg);
						return false;
					}

					if (iRecvMore == 0)
						break;
				}

				//ÊÍ·ÅÏûÏ¢
				zmq_msg_close(&sMsg);				
			}
		
			if (items[1].revents & ZMQ_POLLIN) 
			{
				zmq_msg_t sMsg;
				if (zmq_msg_init(&sMsg) != HAWK_OK)
					return false;

				while (true) 
				{
					if (zmq_recvmsg(items[1].socket, &sMsg, 0) < 0)
					{
						zmq_msg_close(&sMsg);
						return false;
					}

					Int32  iRecvMore = 0;
					Size_t iLen = sizeof(iRecvMore);
					if (zmq_getsockopt(items[1].socket, ZMQ_RCVMORE, &iRecvMore, &iLen) < 0)
					{
						zmq_msg_close(&sMsg);
						return false;
					}

					if (zmq_sendmsg(items[0].socket, &sMsg, iRecvMore? ZMQ_SNDMORE: 0) < 0)
					{
						zmq_msg_close(&sMsg);
						return false;
					}

					if (iRecvMore == 0)
						break;
				}

				zmq_msg_close(&sMsg);
			}
		}while(!bOnce);
		
		return true;
	}
Exemplo n.º 26
0
	Bool HawkAppFrame::Init(const AppCfg& sCfg)
	{
		m_sAppCfg = sCfg;
		HawkAssert(m_sAppCfg.Threads && m_sAppCfg.SvrId);
		if (HawkApp::Init(m_sAppCfg.Threads))
		{
			//日志管理器控制台打印
			P_LoggerManager->EnableConsole(m_sAppCfg.Console);

			//日志对象初始化
			if (m_sAppCfg.LogAddr.size())
			{
				if (!m_pLogProxy)
					m_pLogProxy = new HawkLogProxy;

				if (!m_pLogProxy->Init(m_sAppCfg.LogAddr, m_sAppCfg.SvrId))
				{
					HawkFmtPrint("LogProxy Init Error, Addr: %s.", m_sAppCfg.LogAddr.c_str());
					return false;
				}
				m_pLogProxy->EnableConsole(m_sAppCfg.Console);
			}			

			//内嵌网关模式
			if (sCfg.Gateway.GwCfg.size())
			{
				m_pGateway = new HawkAppGateway(this);
				if (!m_pGateway->Init())
					return false;
			}

			//网关代理器
			m_pGateProxy = new HawkAppGateProxy(this);
			if (!m_pGateProxy->Init())
				return false;

			//数据库对象初始化
			if (sCfg.DBConn.IsValid())
			{
				vector<UInt32> vThreads;
				vThreads.push_back(HawkOSOperator::GetThreadId());
				for (Int32 i=0;i<m_pThreadPool->GetThreadNum();i++)
				{
					vThreads.push_back((UInt32)m_pThreadPool->GetThreadId(i));
					HawkFmtPrint("AppThread: %d", m_pThreadPool->GetThreadId(i));
				}

				for (Size_t i=0; i<vThreads.size(); i++)
				{
					Char sDBName[DEFAULT_SIZE] = {0};
					sprintf(sDBName,"AppDB_%u", vThreads[i]);
					HawkMysql* pDBHdl = (HawkMysql*)P_DBManager->OpenDatabase(sDBName, m_sDbConn);
					if(!pDBHdl) 
						return false;

					m_mDbHdl[ vThreads[i] ] = pDBHdl; 	
				}
			}
			return true;
		}
		return false;
	}
Exemplo n.º 27
0
	Bool HawkGateThread::OnGatewayEvent()
	{
		while (m_pZmq && m_pZmq->PollEvent(HEVENT_READ, 0))
		{
			//设置非空闲
			m_bIdle = false;

			//接收网关发送的数据
			SID iSid = 0;
			if (!RecvGateMsg(iSid, m_pOctets))
				return false;

			//会话协议转发
			if (iSid)
			{
				SessionMap::iterator it = m_mSession.find(iSid);
				if (it != m_mSession.end() && it->second)
				{
					Session* pSession = it->second;

					//加密输出缓冲区
					if (OnSessionEncode(pSession, m_pOctets))
					{
						//协议加密
						ProtoType iProtoType = *((ProtoType*)m_pOctets->Begin());
						if(pSession->OSecurity)
							pSession->OSecurity->Update(*m_pOctets);

						//写进缓冲区
						if (bufferevent_write((bufferevent*)pSession->Event, m_pOctets->Begin(), m_pOctets->Size()) != HAWK_OK)
						{
							HawkFmtPrint("Event Buffer Write Error, Size: %u", m_pOctets->Size());
							OnSessionError(pSession);
						}

						//调用性能监视器
						if (m_pGateway->GetProfiler())
							m_pGateway->GetProfiler()->RegSendProto(iProtoType, m_pOctets->Size());
					}
				}
			}
			//系统消息通知
			else
			{
				HawkAssert(m_pOctets->Size() == sizeof(GateNotify));
				GateNotify* pNotify = (GateNotify*)m_pOctets->Begin();
				if (m_pOctets->Size() != sizeof(GateNotify))
					return false;
				
				//创建会话连接
				if (pNotify->Type == GateNotify::NOTIFY_SESSION_CONNECT)
				{
					if (pNotify->eConnect.Handle != INVALID_SOCKET && pNotify->eConnect.AddrLen)
					{
						SocketAddr sAddr((sockaddr*)pNotify->eConnect.Address, pNotify->eConnect.AddrLen);
						if (!StartSession(pNotify->eConnect.Handle, sAddr))
						{
							closesocket(pNotify->eConnect.Handle);
						}
					}
				}
				//关闭会话连接
				else if (pNotify->Type == GateNotify::NOTIFY_SESSION_CLOSE)
				{
					CloseSession(pNotify->eClose.Sid);
				}
				//后端服务卸载, 默认关闭会话
				else if (pNotify->Type == GateNotify::NOTIFY_SERVICE_DETACH)
				{
					while (m_mSession.size())
					{
						SID iCloseSid = m_mSession.begin()->first;
						CloseSession(iCloseSid);
					}
				}
				//退出网关服务
				else if (pNotify->Type == GateNotify::NOTIFY_SERVICE_EXIT)
				{
					//设置退出标记
					m_bRunning = false;
					HawkFmtPrint("Break EventLoop Success, ThreadId: %u", HawkOSOperator::GetThreadId());
				}
			}
		}
		return true;
	}
Exemplo n.º 28
0
Bool CExcelExporterDlg::ExportExcel(IllusionExcelFile& sExcel, const AString& sSheetName)
{	
	int iRow = sExcel.GetRowCount();
	int iCol = sExcel.GetColumnCount();
	
	//第一行: 数据类型(int, float, uchar[n])
	//第二行: 字段名
	//第三行: 字段注释
	HawkAssert(iRow >= 3 && iCol > 0);	
	if (iRow >= 3 && iCol > 0)
	{
		CString		  sVariable;
		AStringVector vTypes;
		AString sSheet = sSheetName;
		HawkAssert(sSheet.size());
		if (!sSheet.size())
			return false;

		//计算导出的数据格式
		for (int i=1;i<=iCol;i++)
		{
			if (!sExcel.GetCellString(1, i).GetLength() || !sExcel.GetCellString(2, i).GetLength())
				return false;

			AString sTypeName = sExcel.GetCellString(1, i).GetBuffer(0);
			AString sVarName  = sExcel.GetCellString(2, i).GetBuffer(0);
			AString sVarDesc  = sExcel.GetCellString(3, i).GetBuffer(0);

			vTypes.push_back(sTypeName);
			if (sTypeName != "int" && sTypeName != "float" && sTypeName.find("uchar") == AString::npos)
				return false;

			//字符数组转换-> unsigned char ***[n]
			if ( sTypeName.find("uchar") != AString::npos)
			{
				int iCap = HawkStringUtil::StringToInt<AString>(sTypeName.c_str() + strlen("uchar["));
				sVariable.Format("%s\t//%s\r\n\tunsigned char %s[%d];\r\n", CString(sVariable).GetBuffer(0), sVarDesc.c_str(), sVarName.c_str(), iCap);
			}
			else
			{
				sVariable.Format("%s\t//%s\r\n\t%s %s;\r\n", CString(sVariable).GetBuffer(0), sVarDesc.c_str(), sTypeName.c_str(), sVarName.c_str());
			}
		}

		//保存原始名字
		AString sSheetName = sSheet;
		HawkStringUtil::UpCase<AString>(sSheet);

		//格式化导出模式文件
		CString sStructFmt;
		sStructFmt.Format(STRUCT_FORMAT, sSheet.c_str(), sSheet.c_str(), sSheetName.c_str(), sVariable.GetBuffer(0));

		OutputDebugString(sStructFmt.GetBuffer(0));

		//存储模式文件
		HawkDiskFile struct_file;
		char szExportFile[PAGE_SIZE] = {0};
		sprintf(szExportFile, "Pattern/C++/%s.h", sSheetName.c_str());
		_chmod(szExportFile, _S_IREAD | _S_IWRITE);
		if (struct_file.Open(szExportFile, HawkFile::OPEN_WRITE))
		{
			struct_file.Write(sStructFmt.GetBuffer(0), sStructFmt.GetLength());
			struct_file.Close();
		}
		else
		{
			return false;
		}

		//二进制excel数据
		OctetsStream xOS;
		//记录项数目
		Int32 iCount = iRow - 3;
		xOS.Push<Int32>(iCount);

		for (int i=4; i<=iRow; i++)
		{
			for (int j=1;j<=iCol;j++)
			{
				AString sCellText = sExcel.GetCellString(i, j).GetBuffer(0);				
				if (vTypes[j-1] == "int")
				{
					if (!sCellText.size())
						sCellText = "0";

					Int32 iVal = HawkStringUtil::StringToInt<AString>(sCellText);
					xOS.Push<Int32>(iVal);
				}
				else if (vTypes[j-1] == "float")
				{
					if (!sCellText.size())
						sCellText = "0";

					Float fVal = HawkStringUtil::StringToFloat<AString>(sCellText);
					xOS.Push<Float>(fVal);
				}
				else if (vTypes[j-1].find("uchar") != AString::npos)
				{
					UString sVal = HawkStringUtil::ToUtf8(sCellText);
					int iCap = HawkStringUtil::StringToInt<AString>(vTypes[j-1].c_str() + strlen("uchar["));
					UChar* pBuf = new UChar[iCap];
					memset(pBuf, 0, iCap);
					memcpy(pBuf, sVal.c_str(), sVal.size());					
					xOS.Push(pBuf, iCap);
					HAWK_DELETE_ARRAY(pBuf);
				}
			}
		}

		//数据压缩
		UInt32 iSrcSize = xOS.Size();
		ULong lComSize  = HawkZip::GetRequiredSize(iSrcSize);
		Char* pComBuf   = new Char[lComSize];
		memset(pComBuf, 0, lComSize);
		HawkScope::DataArrayPtr scope(pComBuf);
		if (!HawkZip::Compress(pComBuf, lComSize, xOS.Begin(), iSrcSize))
			return false;

		//压缩后做位反运算
		for (ULong i=0;i<lComSize;i++)
			pComBuf[i] = (~pComBuf[i]);

		//压缩后的CRC校验
		UInt32 iCrc = HawkStringUtil::CalcHash(pComBuf, lComSize);

		HawkDiskFile bin_file;
		sprintf(szExportFile, "BinCfg/%s.bin", sSheetName.c_str());
		_chmod(szExportFile, _S_IREAD | _S_IWRITE);
		if (bin_file.Open(szExportFile, HawkFile::OPEN_WRITE))
		{
			bin_file.Write(&iSrcSize, sizeof(iSrcSize));
			bin_file.Write(&iCrc, sizeof(iCrc));
			bin_file.Write(pComBuf, lComSize, true);
			bin_file.Close();
		}
		else
		{
			return false;
		}

		return true;
	}	

	return false;
}
Exemplo n.º 29
0
	SID  HawkGateThread::GenSessionId()
	{
		HawkAssert(m_pGateway);		
		SID iSid = m_iCurSid++ * m_pGateway->GetThreadNum() + m_iBaseSid;
		return iSid;
	}
Exemplo n.º 30
0
	HsGateway::HsGateway() : HawkGateway()
	{
		HawkAssert(!g_Gateway || g_Gateway == this);
		g_Gateway   = this;
		m_iPlatform = 0;
	}