Пример #1
0
	/**
	 * 处理收到的消息队列数据
	 */
	void DoReceiveMsqData(LWPR::Buffer& msg)
	{
		printf("Receiver::DoReceiveMsqData -------------------------------------------------------------\n");

		LWPR::INT32 nMsgType = 0;
		memcpy(&nMsgType, msg.Inout(), sizeof(nMsgType));
		printf("Receiver::DoReceiveMsqData Current thread id [%u] nMsgType [%d] nMsgLength [%d] [%s]\n",
		       LWPR::Thread::GetCurrentThreadId(),
		       nMsgType,
			   msg.Size(),
		       msg.Inout() + sizeof(LWPR::INT32));
	}
Пример #2
0
	/**
	 * 读写socket
	 */
	LWPR::SOCKET_RET_TYPE_E DoReceiveNormalData(LWPR::SOCKET_FD_T fd)
	{
		printf("Receiver::DoReceiveNormalData -------------------------------------------------------------\n");
		printf("Receiver::DoReceiveNormalData Current thread id [%u] fd [%d]\n", LWPR::Thread::GetCurrentThreadId(), fd);

		printf("GetListenAddr = [%s]\n", GetNetServer()->GetListenAddr().c_str());
		LWPR::Buffer buf;

		LWPR::SOCKET_RET_TYPE_E nRet = LWPR::Socket::ReadSocket(fd, buf, 5, 6);
		if(nRet == LWPR::SOCKET_RET_FAILED)
		{
			printf("Receiver::DoReceiveNormalData error\n");
		}
		else if(nRet == LWPR::SOCKET_RET_OK)
		{
			printf("Receiver::DoReceiveNormalData Current thread id [%u] fd [%d] [%s]\n",
			       LWPR::Thread::GetCurrentThreadId(),
			       fd,
			       buf.Inout());

			bool result = GetNetServer()->WriteSocketPositive(fd, "office", 6);

			printf("Receiver::WriteSocketPositive %s\n", result ? "TRUE" : "FALSE");
		}
		else if(nRet == LWPR::SOCKET_RET_TIMEOUT)
		{
			printf("Receiver::DoReceiveNormalData timeout\n");
		}

		return nRet;
	}
Пример #3
0
	void CFile::FWRITE(const LWPR::Buffer& buf)
	{
		if(fwrite(buf.Inout(), buf.Size(), 1, m_fp) != 1)
		{
			throw IOException(EXCEPTION_TRACE, "fwrite error");
		}
	}
Пример #4
0
	/**
	 * 读写socket
	 */
	LWPR::SOCKET_RET_TYPE_E DoReceiveNormalData(LWPR::SOCKET_FD_T fd)
	{
		printf("Receiver::DoReceiveNormalData -------------------------------------------------------------\n");
		printf("Receiver::DoReceiveNormalData Current thread id [%u] fd [%d]\n", LWPR::Thread::GetCurrentThreadId(), fd);

		LWPR::Buffer buf;

		LWPR::SOCKET_RET_TYPE_E nRet = LWPR::Socket::ReadSocket(fd, buf, 5, 6);
		if(nRet == LWPR::SOCKET_RET_FAILED)
		{
			printf("Receiver::DoReceiveNormalData error\n");
		}
		else if(nRet == LWPR::SOCKET_RET_OK)
		{
			printf("Receiver::DoReceiveNormalData Current thread id [%u] fd [%d] [%s]\n",
			       LWPR::Thread::GetCurrentThreadId(),
			       fd,
			       buf.Inout());

		}
		else if(nRet == LWPR::SOCKET_RET_TIMEOUT)
		{
			printf("Receiver::DoReceiveNormalData timeout\n");
		}

		return nRet;
	}
Пример #5
0
	int DoChildWork(int argc, char** argv)
	{
		for(int i = 0; i < 10; i++)
		{
			LWPR::Buffer buf;
			buf.Size(128);

			LWPR::INT32 nMsgType = LWPR::Utility::GetPid();
			memcpy(buf.Inout(), &nMsgType, sizeof(nMsgType));

			strcpy(buf.Inout() + sizeof(LWPR::INT32), argv[3]);

			LWPR::IPCID_T nMsqId = atoi(argv[2]);

			LWPR::IPCMSQ_RET_TYPE_E nRet = LWPR::IPCMSQ::SendMsg(nMsqId, buf.Inout(), buf.Size());

			printf("LWPR::IPCMSQ::SendMsg %d\n", (int)nRet);

			sleep(1);
		}

		return 0;
	}
Пример #6
0
bool View32Manager::AdjustStructData(const char* name,
                                     const void* indata,
                                     const STRUCT_INFO_T& remoteinfo,
                                     const bool inbigendian,
                                     LWPR::Buffer& outdata)
{
	assert(NULL != name);

	StructInfoMap::iterator it = m_Structs.find(name);
	if(m_Structs.end() != it)
	{
		STRUCT_INFO_T& localinfo = it->second;

		if(localinfo.items.size() != remoteinfo.items.size())
		{
			return false;
		}

		// 设置生成结构体的大小
		outdata.Size(localinfo.structSize);

		for(size_t i = 0; i < localinfo.items.size(); i++)
		{
			logger->debug(LTRACE, "%s %s local offset %d, remote offset %d"
			              , localinfo.structName.c_str()
			              , localinfo.items[i].cname.c_str()
			              , localinfo.items[i].offset
			              , remoteinfo.items[i].offset);

			switch(localinfo.items[i].type)
			{
			case STRUCT_DATA_TYPE_FLOAT:
			case STRUCT_DATA_TYPE_DOUBLE:
			case STRUCT_DATA_TYPE_STRING:
			case STRUCT_DATA_TYPE_CHAR:
				memcpy(outdata.Inout() + localinfo.items[i].offset,
				       (char *)indata + remoteinfo.items[i].offset,
				       remoteinfo.items[i].size * remoteinfo.items[i].count);
				break;
			case STRUCT_DATA_TYPE_SHORT:
				{
					for(LWPR::UINT32  k = 0; k < localinfo.items[i].count; k++)
					{
						short value = 0;
						memcpy(&value,
						       (char *)indata + remoteinfo.items[i].offset + k * remoteinfo.items[i].size ,
						       remoteinfo.items[i].size);

						// 字节序转化
						value = LWPR::Utility::IntegerToLocalByte16(value, inbigendian);


						memcpy((char *)outdata.Inout() + localinfo.items[i].offset + k * localinfo.items[i].size ,
						       &value,
						       localinfo.items[i].size);
					}
				}
				break;
			case STRUCT_DATA_TYPE_INT:
				{
					for(LWPR::UINT32  k = 0; k < localinfo.items[i].count; k++)
					{
						int value = 0;
						memcpy(&value,
						       (char *)indata + remoteinfo.items[i].offset + k * remoteinfo.items[i].size ,
						       remoteinfo.items[i].size);

						// 字节序转化
						value = LWPR::Utility::IntegerToLocalByte32(value, inbigendian);

						memcpy((char *)outdata.Inout() + localinfo.items[i].offset + k * localinfo.items[i].size ,
						       &value,
						       localinfo.items[i].size);
					}
				}
				break;
			case STRUCT_DATA_TYPE_LONG:
				{
					for(LWPR::UINT32  k = 0; k < localinfo.items[i].count; k++)
					{
						long value = 0;

						if(4 == remoteinfo.items[i].size)
						{
							LWPR::UINT32 value32 = 0;
							memcpy(&value32,
							       (char *)indata + remoteinfo.items[i].offset + k * remoteinfo.items[i].size ,
							       remoteinfo.items[i].size);

							// 字节序转化
							value = (long)LWPR::Utility::IntegerToLocalByte32(value32, inbigendian);
						}
						else if(8 == remoteinfo.items[i].size)
						{
							LWPR::UINT64 value64 = 0;
							memcpy(&value64,
							       (char *)indata + remoteinfo.items[i].offset + k * remoteinfo.items[i].size ,
							       remoteinfo.items[i].size);

							// 字节序转化
							value = (long)LWPR::Utility::IntegerToLocalByte64(value64, inbigendian);
						}

						memcpy((char *)outdata.Inout() + localinfo.items[i].offset + k * localinfo.items[i].size ,
						       &value,
						       localinfo.items[i].size);
					}
				}
				break;
			default:
				return false;
			}
		}

		return true;
	}

	return false;
}
Пример #7
0
	LWPR::SOCKET_RET_TYPE_E Socket::ReadSocket(SOCKET_FD_T fd, LWPR::Buffer& buf, int length)
	{
		buf.Size(length);

		return ReadSocket(fd, buf.Inout(), length, INT_MAX);
	}
Пример #8
0
	void EventNetWorkThread::Run()
	{
		AutomaticCount autoTotalThread(m_EventNetDispatch.m_nTotalThread);

		while(IsContinue())
		{
			try
			{
				// 可读socket
				SOCKET_FD_T nReadySocket = -1;

				{
					// 空闲线程计数
					AutomaticCount autoIdleThread(m_EventNetDispatch.m_nIdleThread);

					// 加锁,获取可读的Socket(进入临界区)
					Synchronized syn(m_EventNetDispatch.m_MutexEvent);

					// 找一个可读socket,开始工作
					if(m_EventNetDispatch.m_listReadableSocket.size() > 0)
					{
						nReadySocket = m_EventNetDispatch.m_listReadableSocket.back();
						m_EventNetDispatch.m_listReadableSocket.pop_back();
					}

					// 如果没有可读socket,则select
					while(-1 == nReadySocket)
					{
						// 判断当前线程是否可以继续执行
						if(!m_EventNetDispatch.IsContinue())
						{
							return;
						}

						fd_set setReadableFd;
						memcpy(&setReadableFd, &m_EventNetDispatch.m_setActiveFd, sizeof(m_EventNetDispatch.m_setActiveFd));
						struct timeval tv = {0};
						tv.tv_sec = m_EventNetDispatch.m_EventNetOption.nHousekeepInterval;
						tv.tv_usec = 0;
						struct timeval *ptv = &tv;

						if(!m_EventNetDispatch.m_EventNetOption.bAllowDoHousekeep)
						{
							ptv = NULL;
						}

						int nCode = select(m_EventNetDispatch.m_fdMax + 1, &setReadableFd, NULL, NULL, ptv);
						// 有socket可读
						if(nCode > 0)
						{
							for(int i = 0; i <= m_EventNetDispatch.m_fdMax; i++)
							{
								if(FD_ISSET(i, &setReadableFd))
								{
									// 更新socket最后活跃时间
									m_EventNetDispatch.m_mapActiveFd[i] = time(NULL);

									// 处理新来连接
									if(i == m_EventNetDispatch.m_fdListen)
									{
										SOCKET_FD_T fd = LWPR::Socket::AcceptSocket(i);
										if(fd != -1)
										{
											// 如果新来的连接超过select支持的最大数,则CloseSocket
											if(fd >= FD_SETSIZE)
											{
												Socket::CloseSocket(fd);
											}
											else
											{
												FD_SET(fd, &m_EventNetDispatch.m_setActiveFd);
												m_EventNetDispatch.m_mapActiveFd[fd] = time(NULL);
												m_EventNetDispatch.m_fdMax = (fd > m_EventNetDispatch.m_fdMax) ? fd : m_EventNetDispatch.m_fdMax;
												if(m_EventNetDispatch.m_EventNetOption.bAllowDoRcvConnHandler)
												{
													try
													{
														m_EventNetDispatch.m_EventNetOption.pHandler->DoReceiveConnection(fd);
													}
													catch(...)
													{
													}
												}
											}
										}
									}
									// 处理自身连接,用来内部控制信息的传递
									else if(i == m_EventNetDispatch.m_fdConnSelfServer)
									{
										LWPR::Buffer buf;
										SOCKET_RET_TYPE_E res = Socket::ReadSocket(i, buf, sizeof(SOCKET_SELF_MSG_T));
										if(res == SOCKET_RET_OK)
										{
											SOCKET_SELF_MSG_T* pMsg = (SOCKET_SELF_MSG_T*)buf.Inout();

											switch(pMsg->nMsgType)
											{
											case SOCKET_SELF_MSG_ADD_FD:
												FD_SET(pMsg->nSocketFd, &m_EventNetDispatch.m_setActiveFd);
												m_EventNetDispatch.m_mapActiveFd[pMsg->nSocketFd] = time(NULL);
												break;
											case SOCKET_SELF_MSG_EXIT_THREAD:
												break;
											default:
												assert(0);
											}
										}
									}
									// 处理第一个可读socket
									else if(-1 == nReadySocket)
									{
										nReadySocket = i;
										FD_CLR(i, &m_EventNetDispatch.m_setActiveFd);
									}
									// 处理其余可读socket
									else
									{
										m_EventNetDispatch.m_listReadableSocket.push_back(i);
										FD_CLR(i, &m_EventNetDispatch.m_setActiveFd);
									}
								}
							}
						}
						// select 超时
						else if(nCode == 0)
						{
							// 检查不活跃的socket
							if(m_EventNetDispatch.m_EventNetOption.bAllowDoHousekeep)
							{
								for(int i = 0; i <= m_EventNetDispatch.m_fdMax; i++)
								{
									if(FD_ISSET(i, &m_EventNetDispatch.m_setActiveFd))
									{
										if(i == m_EventNetDispatch.m_fdListen)
										{
										}
										else if(i == m_EventNetDispatch.m_fdConnSelfServer)
										{
										}
										else
										{
											if((time(NULL) - m_EventNetDispatch.m_mapActiveFd[i]) >= m_EventNetDispatch.m_EventNetOption.nSocketExpiredTime)
											{
												if(m_EventNetDispatch.m_EventNetOption.bAllowDoCloseHandler)
												{
													try
													{
														m_EventNetDispatch.m_EventNetOption.pHandler->DoCloseExpiredSocket(i);
													}
													catch(...)
													{
													}
												}

												Socket::CloseSocket(i);
												FD_CLR(i, &m_EventNetDispatch.m_setActiveFd);
											}
										}
									}
								}
							}
						}
					} // end of while(-1 == nReadableSocket)
				} // end of Lock

				// 准备处理找到的连接
				SOCKET_RET_TYPE_E nRet = SOCKET_RET_OK;
				try
				{
					nRet = m_EventNetDispatch.m_EventNetOption.pHandler->DoReceiveNormalData(nReadySocket);
				}
				catch(...)
				{
					nRet = SOCKET_RET_FAILED;
				}

				switch(nRet)
				{
				case SOCKET_RET_FAILED:
				case SOCKET_RET_TIMEOUT:
					Socket::CloseSocket(nReadySocket);
					break;
				case SOCKET_RET_OK:
					m_EventNetDispatch.MakeSocketDispatching(nReadySocket);
					break;
				case SOCKET_RET_FREE:
					break;
				default:
					assert(0);
				}
			}
			catch(const LWPR::Exception& e)
			{
				fprintf(stderr, "%s\n", e.what());
			}
		}
	}