void CToxProto::OnFileRequest(Tox*, uint32_t friendNumber, uint32_t fileNumber, TOX_FILE_CONTROL control, void *arg) { CToxProto *proto = (CToxProto*)arg; MCONTACT hContact = proto->GetContact(friendNumber); if (hContact) { FileTransferParam *transfer = proto->transfers.Get(friendNumber, fileNumber); if (transfer == NULL) { proto->logger->Log(__FUNCTION__": failed to find transfer (%d)", fileNumber); return; } switch (control) { case TOX_FILE_CONTROL_PAUSE: break; case TOX_FILE_CONTROL_RESUME: break; case TOX_FILE_CONTROL_CANCEL: proto->ProtoBroadcastAck(transfer->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DENIED, (HANDLE)transfer, 0); proto->transfers.Remove(transfer); break; } } }
// getting the file data void CToxProto::OnDataReceiving(Tox*, uint32_t friendNumber, uint32_t fileNumber, uint64_t position, const uint8_t *data, size_t length, void *arg) { CToxProto *proto = (CToxProto*)arg; FileTransferParam *transfer = proto->transfers.Get(friendNumber, fileNumber); if (transfer == NULL) { proto->logger->Log(__FUNCTION__": failed to find transfer (%d) from (%d)", fileNumber, friendNumber); return; } //receiving is finished if (length == 0 || position == UINT64_MAX) { proto->OnTransferCompleted(transfer); return; } MCONTACT hContact = proto->GetContact(friendNumber); if (hContact == NULL) { proto->logger->Log(__FUNCTION__": cannot find contact by number (%d)", friendNumber); tox_file_control(proto->toxThread->tox, friendNumber, fileNumber, TOX_FILE_CONTROL_CANCEL, NULL); return; } uint64_t filePos = _ftelli64(transfer->hFile); if (filePos != position) _fseeki64(transfer->hFile, position, SEEK_SET); if (fwrite(data, sizeof(uint8_t), length, transfer->hFile) != length) { proto->logger->Log(__FUNCTION__": failed write to file (%d)", fileNumber); proto->ProtoBroadcastAck(transfer->pfts.hContact, ACKTYPE_FILE, ACKRESULT_FAILED, (HANDLE)transfer, 0); tox_file_control(proto->toxThread->tox, friendNumber, fileNumber, TOX_FILE_CONTROL_CANCEL, NULL); return; } transfer->pfts.totalProgress = transfer->pfts.currentFileProgress += length; proto->ProtoBroadcastAck(transfer->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DATA, (HANDLE)transfer, (LPARAM)&transfer->pfts); }
// incoming transfer flow void CToxProto::OnFriendFile(Tox*, uint32_t friendNumber, uint32_t fileNumber, uint32_t kind, uint64_t fileSize, const uint8_t *fileName, size_t filenameLength, void *arg) { CToxProto *proto = (CToxProto*)arg; MCONTACT hContact = proto->GetContact(friendNumber); if (hContact) { switch (kind) { case TOX_FILE_KIND_AVATAR: { proto->logger->Log(__FUNCTION__": incoming avatar (%d) from (%d)", fileNumber, friendNumber); ptrT address(proto->getTStringA(hContact, TOX_SETTINGS_ID)); TCHAR avatarName[MAX_PATH]; mir_sntprintf(avatarName, MAX_PATH, _T("%s.png"), address); AvatarTransferParam *transfer = new AvatarTransferParam(friendNumber, fileNumber, avatarName, fileSize); transfer->pfts.flags |= PFTS_RECEIVING; transfer->pfts.hContact = hContact; proto->transfers.Add(transfer); TOX_ERR_FILE_GET error; tox_file_get_file_id(proto->toxThread->tox, friendNumber, fileNumber, transfer->hash, &error); if (error != TOX_ERR_FILE_GET_OK) { proto->logger->Log(__FUNCTION__": unable to get avatar hash (%d) from (%d) cause (%d)", fileNumber, friendNumber, error); memset(transfer->hash, 0, TOX_HASH_LENGTH); } proto->OnGotFriendAvatarInfo(transfer); } break; case TOX_FILE_KIND_DATA: { proto->logger->Log(__FUNCTION__": incoming file (%d) from (%d)", fileNumber, friendNumber); ptrA rawName((char*)mir_alloc(filenameLength + 1)); memcpy(rawName, fileName, filenameLength); rawName[filenameLength] = 0; TCHAR *name = mir_utf8decodeT(rawName); FileTransferParam *transfer = new FileTransferParam(friendNumber, fileNumber, name, fileSize); transfer->pfts.flags |= PFTS_RECEIVING; transfer->pfts.hContact = hContact; proto->transfers.Add(transfer); PROTORECVFILET pre = { 0 }; pre.dwFlags = PRFF_TCHAR; pre.fileCount = 1; pre.timestamp = time(NULL); pre.descr.t = _T(""); pre.files.t = &name; pre.lParam = (LPARAM)transfer; ProtoChainRecvFile(hContact, &pre); } break; default: proto->logger->Log(__FUNCTION__": unsupported transfer (%d) from (%d) with type (%d)", fileNumber, friendNumber, kind); return; } } }