int CFileConn::_StatesNotify(int state, const char* task_id, uint32_t user_id, CImConn* conn) { CFileConn* pConn = (CFileConn*)conn; IM::File::IMFileState msg; msg.set_state((::IM::BaseDefine::ClientFileState)state); msg.set_task_id(task_id); msg.set_user_id(user_id); CImPdu pdu; pdu.SetPBMsg(&msg); pConn->SendPdu(&pdu); log("notify to user %d state %d task %s", user_id, state, task_id); return 0; }
void FileTransferSocket::_fileState(IN std::string& body) { IM::File::IMFileState imFileState; if (!imFileState.ParseFromString(body)) { LOG__(ERR, _T("parse failed,body:%s"), util::stringToCString(body)); return; } UINT32 nfileState = imFileState.state(); std::string taskId = imFileState.task_id(); TransferFileEntity fileEntity; if (!TransferFileEntityManager::getInstance()->getFileInfoByTaskId(taskId, fileEntity)) { LOG__(ERR, _T("fileState:can't find the fileInfo ")); return; } switch (nfileState) { case IM::BaseDefine::ClientFileState::CLIENT_FILE_PEER_READY: LOG__(APP, _T("fileState--CLIENT_FILE_PEER_READY ")); break; case IM::BaseDefine::ClientFileState::CLIENT_FILE_CANCEL ://取消的了文件传输 LOG__(APP, _T("fileState--CLIENT_FILE_CANCEL ")); { if (fileEntity.pFileObject) { delete fileEntity.pFileObject; fileEntity.pFileObject = nullptr; } TransferFileEntityManager::getInstance()->updateFileInfoBysTaskID(fileEntity); module::getFileTransferModule()->asynNotifyObserver(module::KEY_FILESEVER_UPLOAD_CANCEL, fileEntity.sTaskID); } break; case IM::BaseDefine::ClientFileState::CLIENT_FILE_REFUSE://拒绝了文件 LOG__(APP, _T("fileState--CLIENT_FILE_REFUSE ")); { if (fileEntity.pFileObject) { delete fileEntity.pFileObject; fileEntity.pFileObject = nullptr; } TransferFileEntityManager::getInstance()->updateFileInfoBysTaskID(fileEntity); module::getFileTransferModule()->asynNotifyObserver(module::KEY_FILESEVER_UPLOAD_REJECT, fileEntity.sTaskID); } break; case IM::BaseDefine::ClientFileState::CLIENT_FILE_DONE: LOG__(APP, _T("fileState--CLIENT_FILE_DONE ")); if (fileEntity.pFileObject) { delete fileEntity.pFileObject; fileEntity.pFileObject = nullptr; } TransferFileEntityManager::getInstance()->updateFileInfoBysTaskID(fileEntity); module::getFileTransferModule()->asynNotifyObserver(module::KEY_FILESEVER_PROGRESSBAR_FINISHED, fileEntity.sTaskID); break; default: break; } }
void CFileConn::_HandleClientFileStates(CImPdu* pPdu) { // switch state // case ready_to_recv // if sender_on // tell sender can_send // else update state // case cancel // notify ohter node cancel // close socket // case dnoe // notify recver done // close socket if (!_IsAuth()) { return; } IM::File::IMFileState msg; CHECK_PB_PARSE_MSG(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength())); string task_id = msg.task_id(); uint32_t user_id = msg.user_id(); uint32_t file_stat = msg.state(); pthread_rwlock_wrlock(&g_file_task_map_lock); TaskMap_t::iterator iter = g_file_task_map.find((char*)task_id.c_str()); if (g_file_task_map.end() == iter) { pthread_rwlock_unlock(&g_file_task_map_lock); return; // invalid task id } transfer_task_t* t = iter->second; pthread_rwlock_unlock(&g_file_task_map_lock); t->lock(__LINE__); if (t->from_user_id != user_id && t->to_user_id != user_id) { log("invalid user_id %d for task %s", user_id, task_id.c_str()); t->unlock(__LINE__); return; } t->unlock(__LINE__); switch (file_stat) { case IM::BaseDefine::CLIENT_FILE_CANCEL: case IM::BaseDefine::CLIENT_FILE_DONE: case IM::BaseDefine::CLIENT_FILE_REFUSE: { // notify other client CFileConn* pConn = (CFileConn*)t->GetOpponentConn(user_id); pConn->SendPdu(pPdu); // release log("task %s %d by user_id %d notify %d, erased", task_id.c_str(), file_stat, user_id, t->GetOpponent(user_id)); t->self_destroy = true; break; } default: break; } return; }
void FileTransferSocket::_filePullDataRspResponse(IN std::string& body)//收 { IM::File::IMFilePullDataRsp imFilePullDataRsp; if (!imFilePullDataRsp.ParseFromString(body)) { LOG__(ERR, _T("parse failed,body:%s"), util::stringToCString(body)); return; } UInt32 nRes = imFilePullDataRsp.result_code(); if (0 != nRes) { LOG__(ERR, _T("PullDataRspResponse: error result:%d"),nRes); return; } std::string taskId = imFilePullDataRsp.task_id(); const std::string& strData = imFilePullDataRsp.file_data();//todo ?????要长度 void* pData = (void*)(strData.data()); UInt32 nBlockSize = strData.size(); UInt32 fileOffset = imFilePullDataRsp.offset(); TransferFileEntity fileEntity; if (!TransferFileEntityManager::getInstance()->getFileInfoByTaskId(taskId, fileEntity)) { LOG__(ERR, _T("can't find the fileInfo")); return; } LOG__(DEBG, _T("receive:taskId=%s,filesize=%d,name=%s,BolckSize=%d") , util::stringToCString(fileEntity.sTaskID) , fileEntity.nFileSize , fileEntity.getRealFileName() , nBlockSize); //存文件... if (!fileEntity.pFileObject->writeBlock(fileOffset, nBlockSize, pData)) { LOG__(DEBG, _T("writeBlock failed ")); return; } fileEntity.nProgress = fileOffset + nBlockSize; if (fileEntity.nProgress < fileEntity.nFileSize) { //更新进度条 TransferFileEntityManager::getInstance()->updateFileInfoBysTaskID(fileEntity);//保存当前进度 module::getFileTransferModule()->asynNotifyObserver(module::KEY_FILESEVER_UPDATA_PROGRESSBAR , fileEntity.sTaskID); //继续发file block req... int mode = fileEntity.nClientMode == IM::BaseDefine::ClientFileRole::CLIENT_OFFLINE_DOWNLOAD ? IM::BaseDefine::TransferFileType::FILE_TYPE_OFFLINE : IM::BaseDefine::TransferFileType::FILE_TYPE_ONLINE; IM::File::IMFilePullDataReq imFilePullDataReq; imFilePullDataReq.set_task_id(taskId); imFilePullDataReq.set_user_id(util::stringToInt32(fileEntity.sToID)); imFilePullDataReq.set_trans_mode(static_cast<IM::BaseDefine::TransferFileType>(mode)); imFilePullDataReq.set_offset(fileEntity.nProgress); UInt32 pullSize = fileEntity.nFileSize - fileEntity.nProgress; pullSize > nBlockSize ? imFilePullDataReq.set_data_size(nBlockSize) : imFilePullDataReq.set_data_size(pullSize); // 发包 sendPacket(IM::BaseDefine::ServiceID::SID_FILE, IM::BaseDefine::FileCmdID::CID_FILE_PULL_DATA_REQ, &imFilePullDataReq); } else//传输完成 { if (fileEntity.pFileObject) { delete fileEntity.pFileObject; fileEntity.pFileObject = nullptr; } //告知对方文件传输完成了。 IM::File::IMFileState imFileState; imFileState.set_state(IM::BaseDefine::ClientFileState::CLIENT_FILE_DONE); imFileState.set_task_id(taskId); imFileState.set_user_id(util::stringToInt32(fileEntity.sToID)); sendPacket(IM::BaseDefine::ServiceID::SID_FILE, IM::BaseDefine::FileCmdID::CID_FILE_STATE, &imFileState); TransferFileEntityManager::getInstance()->updateFileInfoBysTaskID(fileEntity); module::getFileTransferModule()->asynNotifyObserver(module::KEY_FILESEVER_PROGRESSBAR_FINISHED, fileEntity.sTaskID); } }