static int GetShm2(void **pstShm, int iShmID, int iSize, int iFlag) { char* sShm; if (!(sShm = GetShm(iShmID, iSize, iFlag & (~IPC_CREAT)))) { if (!(iFlag & IPC_CREAT)) return -1; if (!(sShm = GetShm(iShmID, iSize, iFlag))) return -1; memset(sShm, 0, iSize); } *pstShm = sShm; return 0; }
int CAppCtrl::RecvData(unsigned int uiDPKey, CBuffer& stBuff) { //找到key对应的管道 PCShmQ pstShm = GetShm(uiDPKey); if(pstShm == NULL) { SL_ERROR("shm %u fand fails!", uiDPKey); return -1; } else if(!pstShm->HasCode()) { //管道里没有数据 return 0; } m_Stat.Put(app_stat_recvpkg); stBuff.Clear(); int iCodeLen = 0; //从管道里取出一个code int iRet = pstShm->GetOneCode(stBuff.GetFreeBuf(), stBuff.GetFreeLen(), iCodeLen); if(iRet || iCodeLen < (int)sizeof(CNetHead)) { SL_ERROR("get one code but data fails (ret: %d, clen:%d)",iRet, iCodeLen); return -2; } stBuff.Append(iCodeLen); return iCodeLen; }
/*----------------------------------------------------------------------------- * 初始化hash table *-----------------------------------------------------------------------------*/ int InitHashtable(HashTable *pstHashTable,key_t key, size_t uRowNum, size_t uNodeSize, size_t auNodeNums[], size_t auMods[], int iCreateFlags, Compare compare) { int ret = 0; volatile char *pTable = NULL; if(pstHashTable == NULL || uRowNum == 0) { return -1; } /*key_t uShmKey = CACHE_HASHTABLE_BASE_KEY + key;*/ /*printf("uRowNum = %d uNodeSize = %d \n", uRowNum, uNodeSize);*/ size_t dwTableSize = HashTableEvalTableSize(uRowNum, uNodeSize, auNodeNums); DD("HashTable size = %d\n", (int)dwTableSize); if(dwTableSize == 0) { return -2; } pTable = GetShm(key, dwTableSize, 0666); if(pTable == NULL) { if(iCreateFlags == 0) { printf("shm not existd.should create! shmkey = 0x%x\n", key); return -3; } else if(iCreateFlags == 1) { if((ret = GetShm2((volatile void **)&pTable, key, dwTableSize, 0666 | IPC_CREAT)) < 0) { printf("GetShm2 error. %s\n", strerror(errno)); return -4; } /*printf("first GetShm2 ret = %d\n", ret);*/ bzero((void *)pTable,dwTableSize); } else { return -5; } } /*printf("pTable = 0x%x\n", pTable);*/ /*g_pstHashTable = (HashTable *)pTable;*/ /*pTable += sizeof(HashTable);*/ if((ret = HashTableInit(pstHashTable,(void *)pTable, uNodeSize,uRowNum, auNodeNums, auMods, iCreateFlags, compare)) < 0) { return -5; } /**ppstHashTable = g_pstHashTable;*/ return 0; }
int initHashGame() { struct stShmInfo shm_info; shm_info.key = 0x40101230; shm_info.size = sizeof (HashGame); shm_info.shm_id = -1; shm_info.shm_addr = NULL; shm_info.log[0] = 0; bool new_created; if (GetShm(&shm_info, &new_created) == NULL) { //fprintf(stderr, shm_info.log); //printf("ha\n"); return -1; } hashGame = (HashGame *) shm_info.shm_addr; return 0; }
int main() { printf("I am Client...\n"); int shm_id = GetShm(); char* mem = AtShm(shm_id); int i = 0; while(1) { sleep(1); mem[i++] = 'A'; i %= (SIZE-1); mem[i] = '\0'; } DtShm(mem); return 0; }
static int _InitRouteTable(key_t uShmKey, int iCreateFlags) { int ret = 0; size_t dwTableSize = 0; char *pTable = NULL; dwTableSize = sizeof(RouteTable) + MAX_ENTRY_NUM * sizeof(RouteEntry); if((pTable = GetShm(uShmKey, dwTableSize, 0666)) == NULL) { if(iCreateFlags == 0) { printf("shm not existd.should create! shmkey = 0x%x\n", uShmKey); return -3; } else if(iCreateFlags == 1) { if((ret = GetShm2((volatile void **)&pTable, uShmKey, dwTableSize, 0666 | IPC_CREAT)) < 0) { printf("GetShm2 error. %s\n", strerror(errno)); return -4; } printf("first GetShm2 error ret = %d but Second Ok.\n", ret); bzero((void *)pTable,dwTableSize); } else { return -5; } } if((ret = RouteTableInit(&gstRouteTable, pTable)) < 0) { return -6; } return 0; }
int main(int argc, char *argv[]) { int nSock, nSize, i, nPort; char szBuf[8096]; ASSERT(GetShm() == 0); /* 打开或创建共享内存 */ ASSERT(GetConfig() == 0); /* 读入配置信息到共享内存中 */ ASSERT(GetConfigValue("./config.ini", "LOCAL", "UDPPORT", &nPort, STRINT) == 0); /* 获取UDP服务端口号 */ ASSERT(CreateUdpSock(&nSock, nPort) == 0); /* 创建UDP套接字 */ ASSERT(InitServer() == 0); /* 进程转后台服务 */ while (1) { memset(szBuf, 0, sizeof(szBuf)); nSize = sizeof(szBuf); RecvMsgByUdp(nSock, szBuf, &nSize); /* 接收主机进程监控信息 */ PrintLog(stderr, szBuf); for (i=0; i<*pTopDesk; i++) { if (pDesk[i].nActive == 1) { SendMsgByUdp(szBuf, nSize, pDesk[i].szIp, pDesk[i].nPort); } } } }
int CAppCtrl::SendData(unsigned int uiDPKey, CNetHead& stHead, const char* pszBuf, int iBufLen) { m_Stat.Put(app_stat_sendpkg); PCShmQ pstShm = GetShm(uiDPKey); if(pstShm == NULL) { SL_ERROR("shm %u find fails!", uiDPKey); return -1; } if((stHead.m_LiveFlag & 0xFF) != 0) { SL_WARNING("nethead (handle=%u, handlereq=%u) liveflag=%d not online, but want send data", stHead.m_Handle, stHead.m_HandleSeq, stHead.m_LiveFlag); } stHead.m_iDataLength = iBufLen; int iRet = pstShm->PutOneCode((const char*)&stHead, sizeof(stHead), pszBuf, iBufLen); if(iRet) { SL_ERROR("put one code into shm fails(%d)!", iRet); } return iRet; }