1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
#ifndef _TOX_TRANSFERS_H_
#define _TOX_TRANSFERS_H_
enum FILE_TRANSFER_STATUS
{
NONE,
STARTED,
PAUSED,
BROKEN,
FAILED,
CANCELED,
FINISHED,
DESTROYED
};
struct FileTransferParam
{
PROTOFILETRANSFERSTATUS pfts;
FILE_TRANSFER_STATUS status;
FILE *hFile;
mir_cs fileLock;
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;
}
int Start(Tox *tox)
{
status = STARTED;
return tox_file_send_control(tox, friendNumber, GetDirection(), fileNumber, TOX_FILECONTROL_ACCEPT, NULL, 0);
}
int Resume(Tox *tox)
{
status = STARTED;
return tox_file_send_control(tox, friendNumber, GetDirection(), fileNumber, TOX_FILECONTROL_RESUME_BROKEN, (uint8_t*)&pfts.currentFileProgress, sizeof(pfts.currentFileProgress));
}
int Fail(Tox *tox)
{
status = FAILED;
return tox_file_send_control(tox, friendNumber, GetDirection(), fileNumber, TOX_FILECONTROL_KILL, NULL, 0);
}
int Cancel(Tox *tox)
{
status = FINISHED;
return tox_file_send_control(tox, friendNumber, GetDirection(), fileNumber, TOX_FILECONTROL_KILL, NULL, 0);
}
int Finish(Tox *tox)
{
status = FINISHED;
return tox_file_send_control(tox, friendNumber, GetDirection(), fileNumber, TOX_FILECONTROL_FINISHED, NULL, 0);
}
void Rename(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 != NULL)
{
fclose(hFile);
hFile = NULL;
}
}
};
class CTransferList
{
private:
std::map<uint8_t, FileTransferParam*> transfers;
public:
size_t Count() const
{
return transfers.size();
}
void Add(FileTransferParam *transfer)
{
if (transfers.find(transfer->fileNumber) == transfers.end())
{
transfers[transfer->fileNumber] = transfer;
}
}
FileTransferParam* Get(uint8_t fileNumber)
{
if (transfers.find(fileNumber) != transfers.end())
{
return transfers.at(fileNumber);
}
return NULL;
}
FileTransferParam* GetAt(size_t index)
{
if (index < Count())
{
std::map<uint8_t, FileTransferParam*>::iterator it = transfers.begin();
std::advance(it, index);
return it->second;
}
return NULL;
}
void Remove(uint8_t fileNumber)
{
if (transfers.find(fileNumber) != transfers.end())
{
FileTransferParam *transfer = transfers.at(fileNumber);
transfers.erase(fileNumber);
delete transfer;
}
}
void Remove(FileTransferParam *transfer)
{
if (transfers.find(transfer->fileNumber) != transfers.end())
{
transfers.erase(transfer->fileNumber);
delete transfer;
}
}
};
#endif //_TOX_TRANSFERS_H_
|