Cjson CHttpString::GetJsonData(){ int nLeft = str.Find("\r\n\r\n"); nLeft = nLeft + 4; Cjson json; json.LoadJson((LPSTR)(LPCTSTR)str.Mid(nLeft,str.GetLength() - nLeft)); return json; }
DWORD WINAPI ThreadRecvHandling(LPVOID lpParam){ Threadpackage package = *((Threadpackage *)lpParam); delete (Threadpackage *)lpParam; //package.pmutex->Lock(); //判断是否使用json if(package.ifUseJson == 1){ //接收的是服务端主动发来的数据,会有两种情况,一种是回复,一种是主动发送 Cjson json; json.LoadJson(package.pBuf); int MsgID = json["MsgID"].toValue().nValue; int CheckKeyClient = json["CheckKeyClient"].toValue().nValue; int CheckKeyServer = json["CheckKeyServer"].toValue().nValue; //如果是回复客户端,得到回应,不需要继续Send if(CheckKeyClient >= 0 && CheckKeyServer == -1){ Cjson jsonRsp = json; jsonRsp["MsgID"] = "delete"; jsonRsp["CheckKeyClient"] = "delete"; //把响应json和寄存json传给虚函数 package.pThis->ReceiveRspJson(jsonRsp,MsgID,package.pThis->mapCheck[CheckKeyClient]); //删除map中的寄存包裹 package.pThis->DeleteMap(CheckKeyClient); } //如果是服务端主动发送,则需要给响应 else if(CheckKeyClient == -1 && CheckKeyServer >= 0){ Cjson jsonReq = json; jsonReq["MsgID"] = "delete"; jsonReq["CheckKeyServer"] = "delete"; package.pThis->SendJsonRsp(package.pThis->ReceiveReqJson(jsonReq,MsgID),MsgID,CheckKeyServer,3); } else{ CString strError = ""; strError.Format("key值出错,CheckKeyClient = %d,CheckKeyServer = %d",CheckKeyClient,CheckKeyServer); AfxMessageBox(strError); } } else package.pThis->receive(package.pBuf,package.RecvLength); //package.pmutex->Unlock(); free(package.pBuf); return 0; }
DWORD WINAPI ThreadClientHandling(LPVOID lpParam){ Threadpackage ClientPackage = *((Threadpackage *)lpParam); delete (Threadpackage *)lpParam; //用户处理的虚函数不能上锁,如果一旦有一条线程Sleep则所有的都会被卡住 //ClientPackage.pmutex->Lock(); //不使用json模式 if(ClientPackage.ifUseJson == 0){ ClientPackage.pThis->receive(ClientPackage.pBuf,ClientPackage.nReceiveLength,ClientPackage.ppeer); } //使用json模式 else if(ClientPackage.ifUseJson == 1){ Cjson json; json.LoadJson(ClientPackage.pBuf); int MsgID = json["MsgID"].toValue().nValue; int CheckKeyClient = json["CheckKeyClient"].toValue().nValue; int CheckKeyServer = json["CheckKeyServer"].toValue().nValue; //如果是客户端主动发来的包,需要给响应 if(CheckKeyClient >= 0 && CheckKeyServer == -1){ //重置顺序,方便获得返回通路 vector<ACE_SOCK_Stream*> vecSendIPPeer; //操作vector加锁 ClientPackage.pmutex->Lock(); int i = -1; while(i++ != ClientPackage.pThis->vecIPPeer.size() - 1){ if(ClientPackage.pThis->vecIPPeer.at(i) != ClientPackage.ppeer){ vecSendIPPeer.push_back(ClientPackage.pThis->vecIPPeer.at(i)); } } vecSendIPPeer.push_back(ClientPackage.ppeer); ClientPackage.pmutex->Unlock(); Cjson jsonReq = json; jsonReq["MsgID"] = "delete"; jsonReq["CheckKeyClient"] = "delete"; Cjson jsonRsp = ClientPackage.pThis->ReceiveReqJson(jsonReq,MsgID,&vecSendIPPeer); //根据vector里的通路来发 i = -1; while(i++ != vecSendIPPeer.size() - 1){ ClientPackage.pThis->SendRspJson(jsonRsp,MsgID,CheckKeyClient,vecSendIPPeer.at(i)); } } //如果是客户端响应服务器发来的包 else if(CheckKeyClient == -1 && CheckKeyServer >= 0){ Cjson jsonRsp = json; jsonRsp["MsgID"] = "delete"; jsonRsp["CheckKeyClient"] = "delete"; //把响应json和寄存json传给虚函数 ClientPackage.pThis->ReceiveRspJson(jsonRsp,MsgID,ClientPackage.pThis->mapCheck[CheckKeyServer]); //删除map中的寄存包裹 ClientPackage.pThis->DeleteMap(CheckKeyServer); } else{ CString strError = ""; strError.Format("Key值出错,CheckKeyClient = %d,CheckKeyServer = %d",CheckKeyClient,CheckKeyServer); AfxMessageBox(strError); } } else{ CString strError = ""; strError.Format("是否使用json值出错,值为%d",ClientPackage.ifUseJson); AfxMessageBox(strError); } //ClientPackage.pmutex->Unlock(); return 0; }