diff options
author | Alexander Lantsev <aunsane@gmail.com> | 2015-01-19 06:13:17 +0000 |
---|---|---|
committer | Alexander Lantsev <aunsane@gmail.com> | 2015-01-19 06:13:17 +0000 |
commit | 15877bd0e2177fe045e9a7097a7b836053b1a491 (patch) | |
tree | 70cd0ca7434e4370f041308979009d3a50eb353c | |
parent | a8af35bf6e3de34405ce5dbc3b035039a9ee4806 (diff) |
Tox: fixed resuming for broken files
git-svn-id: http://svn.miranda-ng.org/main/trunk@11876 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
-rw-r--r-- | protocols/Tox/Tox_12.vcxproj | 1 | ||||
-rw-r--r-- | protocols/Tox/Tox_12.vcxproj.filters | 3 | ||||
-rw-r--r-- | protocols/Tox/src/common.h | 1 | ||||
-rw-r--r-- | protocols/Tox/src/tox_contacts.cpp | 14 | ||||
-rw-r--r-- | protocols/Tox/src/tox_proto.cpp | 4 | ||||
-rw-r--r-- | protocols/Tox/src/tox_proto.h | 105 | ||||
-rw-r--r-- | protocols/Tox/src/tox_transfer.cpp | 54 |
7 files changed, 41 insertions, 141 deletions
diff --git a/protocols/Tox/Tox_12.vcxproj b/protocols/Tox/Tox_12.vcxproj index 76ca28c1f2..c1e0ab8b2e 100644 --- a/protocols/Tox/Tox_12.vcxproj +++ b/protocols/Tox/Tox_12.vcxproj @@ -202,6 +202,7 @@ <ClInclude Include="src\tox_bootstrap.h" />
<ClInclude Include="src\tox_dns.h" />
<ClInclude Include="src\tox_proto.h" />
+ <ClInclude Include="src\tox_transfer.h" />
<ClInclude Include="src\version.h" />
</ItemGroup>
<ItemGroup>
diff --git a/protocols/Tox/Tox_12.vcxproj.filters b/protocols/Tox/Tox_12.vcxproj.filters index bcfaa958c1..fee1c08363 100644 --- a/protocols/Tox/Tox_12.vcxproj.filters +++ b/protocols/Tox/Tox_12.vcxproj.filters @@ -33,6 +33,9 @@ <ClInclude Include="src\tox_dns.h">
<Filter>Header Files</Filter>
</ClInclude>
+ <ClInclude Include="src\tox_transfer.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\tox_proto.cpp">
diff --git a/protocols/Tox/src/common.h b/protocols/Tox/src/common.h index 2bdfc3a657..1250b10fd7 100644 --- a/protocols/Tox/src/common.h +++ b/protocols/Tox/src/common.h @@ -37,6 +37,7 @@ #include "version.h"
#include "resource.h"
+#include "tox_transfer.h"
#include "tox_proto.h"
extern HINSTANCE g_hInstance;
diff --git a/protocols/Tox/src/tox_contacts.cpp b/protocols/Tox/src/tox_contacts.cpp index 5cd56e099d..30c9fcc486 100644 --- a/protocols/Tox/src/tox_contacts.cpp +++ b/protocols/Tox/src/tox_contacts.cpp @@ -273,22 +273,24 @@ void CToxProto::OnConnectionStatusChanged(Tox *tox, const int friendNumber, cons tox_send_avatar_info(proto->tox, friendNumber);
proto->delSetting(hContact, "Auth");
- for (std::map<uint8_t, FileTransferParam*>::iterator it = proto->transfers.begin(); it != proto->transfers.end(); it++)
+ for (int i = 0; i < proto->transfers->Count(); i++)
{
// only for receiving
- if (it->second->friendNumber == friendNumber && it->second->GetDirection() == 1)
+ FileTransferParam *transfer = proto->transfers->At(i);
+ if (transfer->friendNumber == friendNumber && transfer->GetDirection() == 1)
{
- it->second->Broken(tox);
+ transfer->Resume(tox);
}
}
}
else
{
- for (std::map<uint8_t, FileTransferParam*>::iterator it = proto->transfers.begin(); it != proto->transfers.end(); it++)
+ for (int i = 0; i < proto->transfers->Count(); i++)
{
- if (it->second->friendNumber == friendNumber)
+ FileTransferParam *transfer = proto->transfers->At(i);
+ if (transfer->friendNumber == friendNumber)
{
- it->second->status = PAUSED;
+ transfer->status = PAUSED;
}
}
}
diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp index b5be241c0f..36dba649ea 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -46,11 +46,15 @@ CToxProto::CToxProto(const char* protoName, const TCHAR* userName) : CreateProtoService(PS_GETMYAVATART, &CToxProto::GetMyAvatar);
CreateProtoService(PS_SETMYAVATART, &CToxProto::SetMyAvatar);
+ // transfers
+ transfers = new CTransferList();
+
hToxEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
}
CToxProto::~CToxProto()
{
+ delete transfers;
mir_free(accountName);
UninitNetlib();
}
diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 92ba5ba49d..eab3eb509b 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -1,106 +1,6 @@ #ifndef _TOX_PROTO_H_
#define _TOX_PROTO_H_
-enum FILE_TRANSFER_STATUS
-{
- NONE,
- STARTED,
- PAUSED,
- FAILED,
- CANCELED,
- FINISHED,
- DESTROYED
-};
-
-struct FileTransferParam
-{
- PROTOFILETRANSFERSTATUS pfts;
- FILE_TRANSFER_STATUS status;
- FILE *hFile;
- int friendNumber;
- int fileNumber;
-
- FileTransferParam(int friendNumber, int fileNumber, const TCHAR* fileName, size_t fileSize)
- {
- status = NONE;
- hFile = NULL;
- this->friendNumber = friendNumber;
- this->fileNumber = fileNumber;
-
- pfts.cbSize = sizeof(PROTOFILETRANSFERSTATUS);
- pfts.flags = PFTS_TCHAR;
- pfts.totalFiles = 1;
- pfts.ptszFiles = (TCHAR**)mir_alloc(sizeof(TCHAR*)*(pfts.totalFiles + 1));
- pfts.ptszFiles[0] = pfts.tszCurrentFile = mir_tstrdup(fileName);
- pfts.ptszFiles[pfts.totalFiles] = NULL;
- pfts.totalBytes = pfts.currentFileSize = fileSize;
- pfts.totalProgress = pfts.currentFileProgress = 0;
- pfts.currentFileNumber = 0;
- pfts.tszWorkingDir = NULL;
- }
-
- bool OpenFile(const TCHAR *mode)
- {
- hFile = _tfopen(pfts.tszCurrentFile, mode);
- return hFile != NULL;
- }
-
- void Start(Tox *tox)
- {
- status = STARTED;
- tox_file_send_control(tox, friendNumber, GetDirection(), fileNumber, TOX_FILECONTROL_ACCEPT, NULL, 0);
- }
-
- void Broken(Tox *tox)
- {
- status = PAUSED;
- tox_file_send_control(tox, friendNumber, GetDirection(), fileNumber, TOX_FILECONTROL_RESUME_BROKEN, (uint8_t*)&pfts.currentFileProgress, sizeof(uint64_t));
- }
-
- void Fail(Tox *tox)
- {
- status = FAILED;
- tox_file_send_control(tox, friendNumber, GetDirection(), fileNumber, TOX_FILECONTROL_KILL, NULL, 0);
- }
-
- void Cancel(Tox *tox)
- {
- status = FINISHED;
- tox_file_send_control(tox, friendNumber, GetDirection(), fileNumber, TOX_FILECONTROL_KILL, NULL, 0);
- }
-
- void Finish(Tox *tox)
- {
- status = FINISHED;
- tox_file_send_control(tox, friendNumber, GetDirection(), fileNumber, TOX_FILECONTROL_FINISHED, NULL, 0);
- }
-
- void RenameName(const TCHAR* fileName)
- {
- pfts.ptszFiles[0] = replaceStrT(pfts.tszCurrentFile, fileName);
- }
-
- uint8_t GetDirection() const
- {
- return pfts.flags & PFTS_SENDING ? 0 : 1;
- }
-
- ~FileTransferParam()
- {
- status = DESTROYED;
- if (pfts.tszWorkingDir != NULL)
- {
- mir_free(pfts.tszWorkingDir);
- }
- mir_free(pfts.pszFiles[0]);
- mir_free(pfts.pszFiles);
- if (hFile)
- {
- fclose(hFile);
- }
- }
-};
-
struct CToxProto : public PROTO<CToxProto>
{
public:
@@ -170,7 +70,7 @@ private: TCHAR *accountName;
HANDLE hNetlib, hPollingThread, hToxEvent;
bool isTerminated, isConnected;
- std::map<uint8_t, FileTransferParam*> transfers;
+ CTransferList *transfers;
// tox
bool InitToxCore();
@@ -251,9 +151,6 @@ private: int __cdecl OnPreCreateMessage(WPARAM wParam, LPARAM lParam);
// transfer
- void AddToTransferList(FileTransferParam *transfer);
- void RemoveFromTransferList(FileTransferParam *transfer);
-
void __cdecl SendFileAsync(void* arg);
//static void OnFileControlCallback(Tox *tox, int32_t number, uint8_t hFile, uint64_t fileSize, uint8_t *name, uint16_t nameSize, void *arg);
diff --git a/protocols/Tox/src/tox_transfer.cpp b/protocols/Tox/src/tox_transfer.cpp index 3ff4a9a0e1..27d41e5cc7 100644 --- a/protocols/Tox/src/tox_transfer.cpp +++ b/protocols/Tox/src/tox_transfer.cpp @@ -1,22 +1,5 @@ #include "common.h"
-void CToxProto::AddToTransferList(FileTransferParam *transfer)
-{
- if (transfers.find(transfer->fileNumber) == transfers.end())
- {
- transfers[transfer->fileNumber] = transfer;
- }
-}
-
-void CToxProto::RemoveFromTransferList(FileTransferParam *transfer)
-{
- if (transfers.find(transfer->fileNumber) != transfers.end())
- {
- transfers.erase(transfer->fileNumber);
- delete transfer;
- }
-}
-
/* FILE RECEIVING */
// incoming file flow
@@ -38,7 +21,7 @@ void CToxProto::OnFriendFile(Tox *tox, int32_t friendNumber, uint8_t fileNumber, FileTransferParam *transfer = new FileTransferParam(friendNumber, fileNumber, name, fileSize);
transfer->pfts.hContact = hContact;
transfer->pfts.flags |= PFTS_RECEIVING;
- proto->AddToTransferList(transfer);
+ proto->transfers->Add(transfer);
PROTORECVFILET pre = { 0 };
pre.flags = PREF_TCHAR;
@@ -56,10 +39,6 @@ void CToxProto::OnFriendFile(Tox *tox, int32_t friendNumber, uint8_t fileNumber, // file request is allowed
HANDLE __cdecl CToxProto::FileAllow(MCONTACT hContact, HANDLE hTransfer, const PROTOCHAR *tszPath)
{
- /*std::string id = getStringA(hContact, TOX_SETTINGS_ID);
- std::vector<uint8_t> clientId = HexStringToData(id);
- uint32_t number = tox_get_friend_number(tox, clientId.data());*/
-
FileTransferParam *transfer = (FileTransferParam*)hTransfer;
transfer->pfts.tszWorkingDir = mir_tstrdup(tszPath);
@@ -74,7 +53,7 @@ HANDLE __cdecl CToxProto::FileAllow(MCONTACT hContact, HANDLE hTransfer, const P {
debugLogA("CToxProto::FileAllow: cannot to open file (%d)", transfer->fileNumber);
transfer->Fail(tox);
- RemoveFromTransferList(transfer);
+ transfers->Remove(transfer);
return NULL;
}
@@ -118,7 +97,7 @@ int __cdecl CToxProto::FileResume(HANDLE hTransfer, int *action, const PROTOCHAR else
{
transfer->Cancel(tox);
- RemoveFromTransferList(transfer);
+ transfers->Remove(transfer);
}
ProtoBroadcastAck(transfer->pfts.hContact, ACKTYPE_FILE, result ? ACKRESULT_CONNECTED : ACKRESULT_DENIED, (HANDLE)transfer, 0);
@@ -133,7 +112,12 @@ void CToxProto::OnFileData(Tox *tox, int32_t friendNumber, uint8_t fileNumber, c MCONTACT hContact = proto->FindContact(friendNumber);
if (hContact)
{
- FileTransferParam *transfer = proto->transfers.at(fileNumber);
+ FileTransferParam *transfer = proto->transfers->Get(fileNumber);
+ if (transfer = NULL)
+ {
+ tox_file_send_control(tox, friendNumber, 1, fileNumber, TOX_FILECONTROL_KILL, NULL, 0);
+ return;
+ }
if (fwrite(data, sizeof(uint8_t), size, transfer->hFile) != size)
{
@@ -188,7 +172,7 @@ HANDLE __cdecl CToxProto::SendFile(MCONTACT hContact, const PROTOCHAR *szDescrip transfer->pfts.flags |= PFTS_SENDING;
transfer->pfts.tszWorkingDir = fileDir;
transfer->hFile = hFile;
- AddToTransferList(transfer);
+ transfers->Add(transfer);
return (HANDLE)transfer;
}
@@ -253,7 +237,7 @@ int __cdecl CToxProto::FileCancel(MCONTACT hContact, HANDLE hTransfer) {
FileTransferParam *transfer = (FileTransferParam*)hTransfer;
transfer->Cancel(tox);
- RemoveFromTransferList(transfer);
+ transfers->Remove(transfer);
return 0;
}
@@ -273,7 +257,13 @@ void CToxProto::OnFileRequest(Tox *tox, int32_t friendNumber, uint8_t receive_se MCONTACT hContact = proto->FindContact(friendNumber);
if (hContact)
{
- FileTransferParam *transfer = proto->transfers.at(fileNumber);
+
+ FileTransferParam *transfer = proto->transfers->Get(fileNumber);
+ if (transfer = NULL)
+ {
+ tox_file_send_control(tox, friendNumber, receive_send, fileNumber, TOX_FILECONTROL_KILL, NULL, 0);
+ return;
+ }
switch (type)
{
@@ -301,7 +291,9 @@ void CToxProto::OnFileRequest(Tox *tox, int32_t friendNumber, uint8_t receive_se // only for sending
if (receive_send == 0)
{
- //uint64_t progress = *(uint64_t*)data;
+ uint64_t progress = *(uint64_t*)data;
+ transfer->pfts.totalProgress = transfer->pfts.currentFileProgress = progress;
+ fseek(transfer->hFile, progress, SEEK_SET);
transfer->Start(tox);
}
break;
@@ -309,7 +301,7 @@ void CToxProto::OnFileRequest(Tox *tox, int32_t friendNumber, uint8_t receive_se case TOX_FILECONTROL_KILL:
transfer->status = CANCELED;
proto->ProtoBroadcastAck(transfer->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DENIED, (HANDLE)transfer, 0);
- proto->RemoveFromTransferList(transfer);
+ proto->transfers->Remove(transfer);
break;
case TOX_FILECONTROL_FINISHED:
@@ -323,7 +315,7 @@ void CToxProto::OnFileRequest(Tox *tox, int32_t friendNumber, uint8_t receive_se proto->debugLogA("CToxProto::OnFileRequest: finished sending file (%d)", fileNumber);
}
proto->ProtoBroadcastAck(transfer->pfts.hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, (HANDLE)transfer, 0);
- proto->RemoveFromTransferList(transfer);
+ proto->transfers->Remove(transfer);
break;
}
}
|