-
Notifications
You must be signed in to change notification settings - Fork 1
/
AlertTypes.cs
292 lines (251 loc) · 9.56 KB
/
AlertTypes.cs
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace Tsunami.Core
{
using AlertHandle = HandleRef;
/// <summary>
/// This is a base class for alerts that are associated with a specific
/// torrent. It contains a handle to the torrent.
/// </summary>
public class torrent_alert : Alert, IDisposable
{
#region PInvoke
[DllImport("TsunamiBridge", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr Alert_TorrentAlert_TorrentHandle_Get(AlertHandle handle);
#endregion PInvoke
public torrent_alert(IntPtr alert)
: base(alert)
{
h = new TorrentHandle(Alert_TorrentAlert_TorrentHandle_Get(handle));
}
private TorrentHandle h;
public TorrentHandle Handle
{
get { return h; }
set { h = value; }
}
}
/// <summary>
/// The peer alert is a base class for alerts that refer to a specific
/// peer. It includes all the information to identify the peer. i.e. ip and
/// peer-id.
/// </summary>
public class peer_alert : torrent_alert , IDisposable
{
#region PInvoke
[DllImport("TsunamiBridge", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr Alert_PeerAlert_Pid_Get(AlertHandle handle);
#endregion PInvoke
public peer_alert(IntPtr alert)
: base(alert)
{
pid = new Sha1Hash(Alert_PeerAlert_Pid_Get(handle));
}
// TODO ip
private Sha1Hash pid;
public Sha1Hash Pid
{
get
{
return pid;
}
set
{
pid = value;
}
}
}
/// <summary>
/// This is a base class used for alerts that are associated with a
/// specific tracker. It derives from torrent_alert since a tracker is also
/// associated with a specific torrent.
/// </summary>
public class tracker_alert : torrent_alert , IDisposable
{
#region PInvoke
[DllImport("TsunamiBridge", CallingConvention = CallingConvention.StdCall)]
public static extern void Alert_TrackerAlert_Url_Get(AlertHandle handle, StringBuilder str, int size);
#endregion PInvoke
public tracker_alert(IntPtr alert)
: base(alert)
{
sb = new StringBuilder(256);
Alert_TrackerAlert_Url_Get(handle, sb, sb.Capacity);
url = sb.ToString();
}
static private StringBuilder sb;
public string url { get; set; }
}
///<summary>
///The torrent_added_alert is posted once every time a torrent is
///successfully added. It doesn't contain any members of its own,
///ut inherits the torrent handle from its base class. It's posted
///when the status_notification bit is set in the alert_mask.
///</summary>
public class torrent_added_alert : torrent_alert, IDisposable
{
public torrent_added_alert(IntPtr alert)
: base(alert)
{ }
}
///<summary>
///The torrent_removed_alert is posted whenever a torrent is removed.
///Since the torrent handle in its baseclass will always be invalid (since
///the torrent is already removed) it has the info hash as a member, to
///identify it. It's posted when the status_notification bit is set in the
///alert_mask.
///</summary>
public class torrent_removed_alert : torrent_alert, IDisposable
{
public torrent_removed_alert(IntPtr alert)
: base(alert)
{ }
}
///<summary>
///This alert is posted when the asynchronous read operation initiated by a
///call to torrent_handle::read_piece() is completed. If the read failed,
///the torrent is paused and an error state is set and the buffer member of
///the alert is 0. If successful, buffer points to a buffer containing all
///the data of the piece. piece is the piece index that was read. size is
///the number of bytes that was read.
///</summary>
public class read_piece_alert : torrent_alert, IDisposable
{
#region PInvoke
[DllImport("TsunamiBridge", CallingConvention = CallingConvention.StdCall)]
public static extern uint Alert_ReadPieceAlert_Buffer_Size_Get(AlertHandle handle);
[DllImport("TsunamiBridge", CallingConvention = CallingConvention.StdCall)]
public static extern void Alert_ReadPieceAlert_Buffer_Get(AlertHandle handle, [Out] byte[] buff, uint size);
[DllImport("TsunamiBridge", CallingConvention = CallingConvention.StdCall)]
public static extern int Alert_ReadPieceAlert_Piece_Get(AlertHandle handle);
#endregion PInvoke
public read_piece_alert(IntPtr alert)
: base(alert)
{
size = Alert_ReadPieceAlert_Buffer_Size_Get(handle);
buffer = new byte[size];
Alert_ReadPieceAlert_Buffer_Get(handle, buffer, size);
piece = Alert_ReadPieceAlert_Piece_Get(handle);
}
public byte[] buffer { get; set; }
public int piece { get; set; }
public uint size { get; set; }
}
///<summary>
///This is posted whenever an individual file completes its download. i.e.
///All pieces overlapping this file have passed their hash check.
///</summary>
public class file_completed_alert : torrent_alert, IDisposable
{
#region PInvoke
[DllImport("TsunamiBridge", CallingConvention = CallingConvention.StdCall)]
public static extern int Alert_FileCompletedAlert_Index_Get(AlertHandle handle);
#endregion PInvoke
public file_completed_alert(IntPtr alert)
: base(alert)
{
index = Alert_FileCompletedAlert_Index_Get(handle);
}
///<summary>The index of the file that completed.</summary>
private int index;
public int Index
{
get { return index; }
set { index = value; }
}
}
///<summary>
///This is posted as a response to a torrent_handle::rename_file() call,
///if the rename operation succeeds.
///</summary>
public class file_renamed_alert : torrent_alert, IDisposable
{
#region PInvoke
[DllImport("TsunamiBridge", CallingConvention = CallingConvention.StdCall)]
public static extern void Alert_FileRenamedAlert_Name_Get(AlertHandle handle,StringBuilder sb, int size);
[DllImport("TsunamiBridge", CallingConvention = CallingConvention.StdCall)]
public static extern int Alert_FileRenamedAlert_Index_Get(AlertHandle handle);
#endregion PInvoke
public file_renamed_alert(IntPtr alert)
:base(alert)
{
sb = new StringBuilder(256);
Alert_FileRenamedAlert_Name_Get(handle, sb, sb.Capacity);
name = sb.ToString();
index = Alert_FileRenamedAlert_Index_Get(handle);
}
static private StringBuilder sb;
///<summary>The new name of the file.</summary>
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
///<summary>The index of the file that was renamed.</summary>
private int index;
public int Index
{
get { return index; }
set { index = value; }
}
}
///<summary>
///This is posted as a response to a torrent_handle::rename_file() call,
///if the rename operation failed.
///</summary>
public class file_rename_failed_alert : torrent_alert, IDisposable
{
#region PInvoke
[DllImport("TsunamiBridge", CallingConvention = CallingConvention.StdCall)]
public static extern int Alert_FileRenameFailedAlert_Index_Get(AlertHandle handle);
[DllImport("TsunamiBridge", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr Alert_FileRenameFailedAlert_Error_Get(AlertHandle handle);
#endregion PInvoke
public file_rename_failed_alert(IntPtr alert)
:base(alert)
{
index = Alert_FileRenameFailedAlert_Index_Get(handle);
error = new ErrorCode(Alert_FileRenameFailedAlert_Error_Get(handle));
}
///<summary>The index of the file that was supposed to be renamed.</summary>
private int index;
public int Index
{
get { return index; }
set { index = value; }
}
private ErrorCode error;
public ErrorCode Error
{
get { return error; }
set { error = value; }
}
}
/// <summary>
/// This alert is generated when a limit is reached that might have a
/// negative impact on upload or download rate performance.
/// </summary>
public class performance_alert : torrent_alert, IDisposable
{
#region PInvoke
[DllImport("TsunamiBridge", CallingConvention = CallingConvention.StdCall)]
public static extern uint Alert_PerformanceAlert_WarningCode_Get(AlertHandle handle);
#endregion PInvoke
public performance_alert(IntPtr alert)
:base(alert)
{
warning_code = (performance_warning_t)Alert_PerformanceAlert_WarningCode_Get(handle);
}
private performance_warning_t warning_code;
public performance_warning_t WarningCode
{
get { return warning_code; }
set { warning_code = value; }
}
}
}